File size: 4,644 Bytes
d6d843f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
# Hugging Face Integration - Complete

## تغییرات انجام شده

### 1. AI Models - Ensemble Sentiment (`ai_models.py`)

**Model Catalog:**
- ✅ Crypto Sentiment: ElKulako/cryptobert, kk08/CryptoBERT, burakutf/finetuned-finbert-crypto, mathugo/crypto_news_bert
- ✅ Social Sentiment: svalabs/twitter-xlm-roberta-bitcoin-sentiment, mayurjadhav/crypto-sentiment-model
- ✅ Financial Sentiment: ProsusAI/finbert, cardiffnlp/twitter-roberta-base-sentiment
- ✅ News Sentiment: mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis
- ✅ Decision Models: agarkovv/CryptoTrader-LM

**Ensemble Sentiment:**
- `ensemble_crypto_sentiment(text)` - استفاده از چند model برای sentiment analysis
- Majority voting برای تعیین label نهایی
- Confidence scoring مبتنی بر میانگین score ها

### 2. HF Registry - Dataset Catalog (`backend/services/hf_registry.py`)

**Curated Datasets:**
- **Price/OHLCV**: 7 datasets (Bitcoin, Ethereum, XRP price data)
- **News Raw**: 2 datasets (crypto news headlines)
- **News Labeled**: 5 datasets (news with sentiment/impact labels)

**Features:**
- Category-based organization
- Automatic refresh from HF Hub
- Metadata (likes, downloads, tags)

### 3. Unified Server - Complete API (`hf_unified_server.py`)

**New Endpoints:**

**Health & Status:**
- `GET /api/health` - Dashboard health check

**Market Data:**
- `GET /api/coins/top?limit=10` - Top coins by market cap
- `GET /api/coins/{symbol}` - Coin details
- `GET /api/market/stats` - Global market stats
- `GET /api/charts/price/{symbol}?timeframe=7d` - Price chart
- `POST /api/charts/analyze` - Chart analysis with AI

**News & AI:**
- `GET /api/news/latest?limit=40` - News with sentiment
- `POST /api/news/summarize` - Summarize article
- `POST /api/sentiment/analyze` - Sentiment analysis
- `POST /api/query` - Natural language query

**Datasets & Models:**
- `GET /api/datasets/list` - Available datasets
- `GET /api/datasets/sample?name=...` - Dataset sample
- `GET /api/models/list` - Available models
- `POST /api/models/test` - Test model

**Real-time:**
- `WS /ws` - WebSocket for live updates (market + news + sentiment)

### 4. Frontend Compatibility

**admin.html + static/js/**
- ✅ Tمام endpoint های مورد نیاز پیاده شده
- ✅ WebSocket support
- ✅ Sentiment از ensemble models
- ✅ Real-time updates هر 10 ثانیه

## نحوه استفاده

### Docker (HuggingFace Space)
```bash
docker build -t crypto-hf .
docker run -p 7860:7860 -e HF_TOKEN=your_token crypto-hf
```

### مستقیم
```bash
pip install -r requirements.txt
export HF_TOKEN=your_token
uvicorn hf_unified_server:app --host 0.0.0.0 --port 7860
```

### تست
```bash
# Health check
curl http://localhost:7860/api/health

# Top coins
curl http://localhost:7860/api/coins/top?limit=10

# Sentiment analysis
curl -X POST http://localhost:7860/api/sentiment/analyze \
  -H "Content-Type: application/json" \
  -d '{"text": "Bitcoin price surging to new heights!"}'

# Models list
curl http://localhost:7860/api/models/list

# Datasets list
curl http://localhost:7860/api/datasets/list
```

## Model Usage

Ensemble sentiment در action:
```python
from ai_models import ensemble_crypto_sentiment

result = ensemble_crypto_sentiment("Bitcoin breaking resistance!")
# {
#   "label": "bullish",
#   "confidence": 0.87,
#   "scores": {
#     "ElKulako/cryptobert": {"label": "bullish", "score": 0.92},
#     "kk08/CryptoBERT": {"label": "bullish", "score": 0.82}
#   },
#   "model_count": 2
# }
```

## Dependencies

requirements.txt includes:
- transformers>=4.36.0
- datasets>=2.16.0
- huggingface-hub>=0.19.0
- torch>=2.0.0

## Environment Variables

```.env
HF_TOKEN=hf_your_token_here  # برای private models
```

## چک لیست تست

- [x] `/api/health` - Status OK
- [x] `/api/coins/top` - Top 10 coins
- [x] `/api/market/stats` - Market data
- [x] `/api/news/latest` - News با sentiment
- [x] `/api/sentiment/analyze` - Ensemble working
- [x] `/api/models/list` - 10+ models listed
- [x] `/api/datasets/list` - 14+ datasets listed
- [x] `/ws` - WebSocket live updates
- [x] Dashboard UI - All tabs working

## توجه

- Models به صورت lazy-load می‌شوند (اولین استفاده)
- Ensemble sentiment از 2-3 model استفاده می‌کند برای سرعت
- Dataset sampling نیاز به authentication دارد برای بعضی datasets
- CryptoTrader-LM model بزرگ است (7B) - فقط با GPU

## Support

All endpoints from the requirements document are implemented and tested.
Frontend (admin.html) works without 404/403 errors.