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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#!/usr/bin/env python3
"""
ادغام مدل‌های HuggingFace برای تحلیل هوش مصنوعی
HuggingFace Models Integration for AI Analysis
"""

import asyncio
from typing import List, Dict, Optional, Any
from datetime import datetime
import logging

try:
    from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
    TRANSFORMERS_AVAILABLE = True
except ImportError:
    TRANSFORMERS_AVAILABLE = False
    logging.warning("⚠️ transformers not installed. AI features will be limited.")

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


class HuggingFaceAnalyzer:
    """
    تحلیل‌گر هوش مصنوعی با استفاده از مدل‌های HuggingFace
    AI Analyzer using HuggingFace models
    """

    def __init__(self):
        self.models_loaded = False
        self.sentiment_analyzer = None
        self.zero_shot_classifier = None

        if TRANSFORMERS_AVAILABLE:
            self._load_models()

    def _load_models(self):
        """بارگذاری مدل‌های HuggingFace"""
        try:
            logger.info("🤗 Loading HuggingFace models...")

            # Sentiment Analysis Model - FinBERT (specialized for financial text)
            try:
                self.sentiment_analyzer = pipeline(
                    "sentiment-analysis",
                    model="ProsusAI/finbert",
                    tokenizer="ProsusAI/finbert"
                )
                logger.info("✅ Loaded FinBERT for sentiment analysis")
            except Exception as e:
                logger.warning(f"⚠️ Could not load FinBERT: {e}")
                # Fallback to general sentiment model
                try:
                    self.sentiment_analyzer = pipeline(
                        "sentiment-analysis",
                        model="distilbert-base-uncased-finetuned-sst-2-english"
                    )
                    logger.info("✅ Loaded DistilBERT for sentiment analysis (fallback)")
                except Exception as e2:
                    logger.error(f"❌ Could not load sentiment model: {e2}")

            # Zero-shot Classification (for categorizing news/tweets)
            try:
                self.zero_shot_classifier = pipeline(
                    "zero-shot-classification",
                    model="facebook/bart-large-mnli"
                )
                logger.info("✅ Loaded BART for zero-shot classification")
            except Exception as e:
                logger.warning(f"⚠️ Could not load zero-shot classifier: {e}")

            self.models_loaded = True
            logger.info("🎉 HuggingFace models loaded successfully!")

        except Exception as e:
            logger.error(f"❌ Error loading models: {e}")
            self.models_loaded = False

    async def analyze_news_sentiment(self, news_text: str) -> Dict[str, Any]:
        """
        تحلیل احساسات یک خبر
        Analyze sentiment of a news article
        """
        if not self.models_loaded or not self.sentiment_analyzer:
            return {
                "sentiment": "neutral",
                "confidence": 0.0,
                "error": "Model not available"
            }

        try:
            # Truncate text to avoid token limit
            max_length = 512
            text = news_text[:max_length]

            # Run sentiment analysis
            result = self.sentiment_analyzer(text)[0]

            # Map FinBERT labels to standard format
            label_map = {
                "positive": "bullish",
                "negative": "bearish",
                "neutral": "neutral"
            }

            sentiment = label_map.get(result['label'].lower(), result['label'].lower())

            return {
                "sentiment": sentiment,
                "confidence": round(result['score'], 4),
                "raw_label": result['label'],
                "text_analyzed": text[:100] + "...",
                "model": "finbert",
                "timestamp": datetime.now().isoformat()
            }

        except Exception as e:
            logger.error(f"❌ Sentiment analysis error: {e}")
            return {
                "sentiment": "neutral",
                "confidence": 0.0,
                "error": str(e)
            }

    async def analyze_news_batch(self, news_list: List[Dict]) -> List[Dict]:
        """
        تحلیل دسته‌ای احساسات اخبار
        Batch sentiment analysis for news
        """
        results = []

        for news in news_list:
            text = f"{news.get('title', '')} {news.get('description', '')}"

            sentiment_result = await self.analyze_news_sentiment(text)

            results.append({
                **news,
                "ai_sentiment": sentiment_result['sentiment'],
                "ai_confidence": sentiment_result['confidence'],
                "ai_analysis": sentiment_result
            })

            # Small delay to avoid overloading
            await asyncio.sleep(0.1)

        return results

    async def categorize_news(self, news_text: str) -> Dict[str, Any]:
        """
        دسته‌بندی اخبار با zero-shot classification
        Categorize news using zero-shot classification
        """
        if not self.models_loaded or not self.zero_shot_classifier:
            return {
                "category": "general",
                "confidence": 0.0,
                "error": "Model not available"
            }

        try:
            # Define categories
            categories = [
                "price_movement",
                "regulation",
                "technology",
                "adoption",
                "security",
                "defi",
                "nft",
                "exchange",
                "mining",
                "general"
            ]

            # Truncate text
            text = news_text[:512]

            # Run classification
            result = self.zero_shot_classifier(text, categories)

            return {
                "category": result['labels'][0],
                "confidence": round(result['scores'][0], 4),
                "all_categories": [
                    {"label": label, "score": round(score, 4)}
                    for label, score in zip(result['labels'][:3], result['scores'][:3])
                ],
                "model": "bart-mnli",
                "timestamp": datetime.now().isoformat()
            }

        except Exception as e:
            logger.error(f"❌ Categorization error: {e}")
            return {
                "category": "general",
                "confidence": 0.0,
                "error": str(e)
            }

    async def calculate_aggregated_sentiment(
        self,
        news_list: List[Dict],
        symbol: Optional[str] = None
    ) -> Dict[str, Any]:
        """
        محاسبه احساسات جمعی از چندین خبر
        Calculate aggregated sentiment from multiple news items
        """
        if not news_list:
            return {
                "overall_sentiment": "neutral",
                "sentiment_score": 0.0,
                "confidence": 0.0,
                "news_count": 0
            }

        # Filter by symbol if provided
        if symbol:
            news_list = [
                n for n in news_list
                if symbol.upper() in [c.upper() for c in n.get('coins', [])]
            ]

        if not news_list:
            return {
                "overall_sentiment": "neutral",
                "sentiment_score": 0.0,
                "confidence": 0.0,
                "news_count": 0,
                "note": f"No news found for {symbol}"
            }

        # Analyze each news item
        analyzed_news = await self.analyze_news_batch(news_list[:20])  # Limit to 20

        # Calculate weighted sentiment
        bullish_count = 0
        bearish_count = 0
        neutral_count = 0
        total_confidence = 0.0

        for news in analyzed_news:
            sentiment = news.get('ai_sentiment', 'neutral')
            confidence = news.get('ai_confidence', 0.0)

            if sentiment == 'bullish':
                bullish_count += confidence
            elif sentiment == 'bearish':
                bearish_count += confidence
            else:
                neutral_count += confidence

            total_confidence += confidence

        # Calculate overall sentiment score (-100 to +100)
        if total_confidence > 0:
            sentiment_score = ((bullish_count - bearish_count) / total_confidence) * 100
        else:
            sentiment_score = 0.0

        # Determine overall classification
        if sentiment_score > 30:
            overall = "bullish"
        elif sentiment_score < -30:
            overall = "bearish"
        else:
            overall = "neutral"

        return {
            "overall_sentiment": overall,
            "sentiment_score": round(sentiment_score, 2),
            "confidence": round(total_confidence / len(analyzed_news), 2) if analyzed_news else 0.0,
            "news_count": len(analyzed_news),
            "bullish_weight": round(bullish_count, 2),
            "bearish_weight": round(bearish_count, 2),
            "neutral_weight": round(neutral_count, 2),
            "symbol": symbol,
            "timestamp": datetime.now().isoformat()
        }

    async def predict_price_direction(
        self,
        symbol: str,
        recent_news: List[Dict],
        current_price: float,
        historical_prices: List[float]
    ) -> Dict[str, Any]:
        """
        پیش‌بینی جهت قیمت بر اساس اخبار و روند قیمت
        Predict price direction based on news sentiment and price trend
        """
        # Get news sentiment
        news_sentiment = await self.calculate_aggregated_sentiment(recent_news, symbol)

        # Calculate price trend
        if len(historical_prices) >= 2:
            price_change = ((current_price - historical_prices[0]) / historical_prices[0]) * 100
        else:
            price_change = 0.0

        # Combine signals
        # News sentiment weight: 60%
        # Price momentum weight: 40%
        news_score = news_sentiment['sentiment_score'] * 0.6
        momentum_score = min(50, max(-50, price_change * 10)) * 0.4

        combined_score = news_score + momentum_score

        # Determine prediction
        if combined_score > 20:
            prediction = "bullish"
            direction = "up"
        elif combined_score < -20:
            prediction = "bearish"
            direction = "down"
        else:
            prediction = "neutral"
            direction = "sideways"

        # Calculate confidence
        confidence = min(1.0, abs(combined_score) / 100)

        return {
            "symbol": symbol,
            "prediction": prediction,
            "direction": direction,
            "confidence": round(confidence, 2),
            "combined_score": round(combined_score, 2),
            "news_sentiment_score": round(news_score / 0.6, 2),
            "price_momentum_score": round(momentum_score / 0.4, 2),
            "current_price": current_price,
            "price_change_pct": round(price_change, 2),
            "news_analyzed": news_sentiment['news_count'],
            "timestamp": datetime.now().isoformat(),
            "model": "combined_analysis"
        }


class SimpleHuggingFaceAnalyzer:
    """
    نسخه ساده برای زمانی که transformers نصب نیست
    Simplified version when transformers is not available
    Uses simple keyword-based sentiment
    """

    async def analyze_news_sentiment(self, news_text: str) -> Dict[str, Any]:
        """Simple keyword-based sentiment"""
        text_lower = news_text.lower()

        # Bullish keywords
        bullish_keywords = [
            'bullish', 'surge', 'rally', 'gain', 'rise', 'soar',
            'adoption', 'breakthrough', 'positive', 'growth', 'boom'
        ]

        # Bearish keywords
        bearish_keywords = [
            'bearish', 'crash', 'plunge', 'drop', 'fall', 'decline',
            'regulation', 'ban', 'hack', 'scam', 'negative', 'crisis'
        ]

        bullish_count = sum(1 for word in bullish_keywords if word in text_lower)
        bearish_count = sum(1 for word in bearish_keywords if word in text_lower)

        if bullish_count > bearish_count:
            sentiment = "bullish"
            confidence = min(0.8, bullish_count * 0.2)
        elif bearish_count > bullish_count:
            sentiment = "bearish"
            confidence = min(0.8, bearish_count * 0.2)
        else:
            sentiment = "neutral"
            confidence = 0.5

        return {
            "sentiment": sentiment,
            "confidence": confidence,
            "method": "keyword_based",
            "timestamp": datetime.now().isoformat()
        }


# Factory function
def get_analyzer() -> Any:
    """Get appropriate analyzer based on availability"""
    if TRANSFORMERS_AVAILABLE:
        return HuggingFaceAnalyzer()
    else:
        logger.warning("⚠️ Using simple analyzer (transformers not available)")
        return SimpleHuggingFaceAnalyzer()


async def main():
    """Test HuggingFace models"""
    print("\n" + "="*70)
    print("🤗 Testing HuggingFace AI Models")
    print("="*70)

    analyzer = get_analyzer()

    # Test sentiment analysis
    test_news = [
        "Bitcoin surges past $50,000 as institutional adoption accelerates",
        "SEC delays decision on crypto ETF, causing market uncertainty",
        "Ethereum network upgrade successfully completed without issues"
    ]

    print("\n📊 Testing Sentiment Analysis:")
    for i, news in enumerate(test_news, 1):
        result = await analyzer.analyze_news_sentiment(news)
        print(f"\n{i}. {news[:60]}...")
        print(f"   Sentiment: {result['sentiment']}")
        print(f"   Confidence: {result['confidence']:.2%}")

    # Test if advanced features available
    if isinstance(analyzer, HuggingFaceAnalyzer) and analyzer.models_loaded:
        print("\n\n🎯 Testing News Categorization:")
        categorization = await analyzer.categorize_news(test_news[0])
        print(f"   Category: {categorization['category']}")
        print(f"   Confidence: {categorization['confidence']:.2%}")

        print("\n\n📈 Testing Aggregated Sentiment:")
        mock_news = [
            {"title": news, "description": "", "coins": ["BTC"]}
            for news in test_news
        ]
        agg_sentiment = await analyzer.calculate_aggregated_sentiment(mock_news, "BTC")
        print(f"   Overall: {agg_sentiment['overall_sentiment']}")
        print(f"   Score: {agg_sentiment['sentiment_score']}/100")
        print(f"   Confidence: {agg_sentiment['confidence']:.2%}")


if __name__ == "__main__":
    asyncio.run(main())