Upload 374 files
Browse files- DEPLOYMENT_GUIDE.md +170 -501
- Dockerfile.optimized +51 -0
- requirements.txt +57 -41
DEPLOYMENT_GUIDE.md
CHANGED
|
@@ -1,600 +1,269 @@
|
|
| 1 |
-
# Deployment Guide -
|
| 2 |
|
| 3 |
-
##
|
| 4 |
|
| 5 |
-
###
|
| 6 |
-
|
| 7 |
-
1. **Create a Hugging Face Account**
|
| 8 |
-
- Go to https://huggingface.co/join
|
| 9 |
-
- Sign up for a free account
|
| 10 |
-
|
| 11 |
-
2. **Create a New Space**
|
| 12 |
-
- Go to https://huggingface.co/new-space
|
| 13 |
-
- Choose a name (e.g., `crypto-resource-aggregator`)
|
| 14 |
-
- Select SDK: **Docker**
|
| 15 |
-
- Choose visibility: **Public** or **Private**
|
| 16 |
-
- Click "Create Space"
|
| 17 |
-
|
| 18 |
-
3. **Upload Files**
|
| 19 |
-
Upload the following files to your Space:
|
| 20 |
-
- `app.py` - Main application file
|
| 21 |
-
- `requirements.txt` - Python dependencies
|
| 22 |
-
- `all_apis_merged_2025.json` - Resource configuration
|
| 23 |
-
- `README.md` - Documentation
|
| 24 |
-
- `Dockerfile` - Docker configuration
|
| 25 |
-
|
| 26 |
-
4. **Wait for Build**
|
| 27 |
-
- The Space will automatically build and deploy
|
| 28 |
-
- This may take 2-5 minutes
|
| 29 |
-
- You'll see the build logs in real-time
|
| 30 |
-
|
| 31 |
-
5. **Access Your API**
|
| 32 |
-
- Once deployed, your API will be available at:
|
| 33 |
-
`https://[your-username]-[space-name].hf.space`
|
| 34 |
-
- Example: `https://username-crypto-resource-aggregator.hf.space`
|
| 35 |
-
|
| 36 |
-
### Method 2: Git CLI (Recommended for Advanced Users)
|
| 37 |
-
|
| 38 |
-
```bash
|
| 39 |
-
# Clone your Space repository
|
| 40 |
-
git clone https://huggingface.co/spaces/[your-username]/[space-name]
|
| 41 |
-
cd [space-name]
|
| 42 |
-
|
| 43 |
-
# Copy all files to the repository
|
| 44 |
-
cp app.py requirements.txt all_apis_merged_2025.json README.md Dockerfile .
|
| 45 |
-
|
| 46 |
-
# Commit and push
|
| 47 |
-
git add .
|
| 48 |
-
git commit -m "Initial deployment of Crypto Resource Aggregator"
|
| 49 |
-
git push
|
| 50 |
```
|
| 51 |
-
|
| 52 |
-
---
|
| 53 |
-
|
| 54 |
-
## Alternative Deployment Options
|
| 55 |
-
|
| 56 |
-
### Option 1: Heroku
|
| 57 |
-
|
| 58 |
-
```bash
|
| 59 |
-
# Install Heroku CLI
|
| 60 |
-
# https://devcenter.heroku.com/articles/heroku-cli
|
| 61 |
-
|
| 62 |
-
# Create a new app
|
| 63 |
-
heroku create crypto-resource-aggregator
|
| 64 |
-
|
| 65 |
-
# Create Procfile
|
| 66 |
-
echo "web: python app.py" > Procfile
|
| 67 |
-
|
| 68 |
-
# Deploy
|
| 69 |
-
git add .
|
| 70 |
-
git commit -m "Deploy to Heroku"
|
| 71 |
-
git push heroku main
|
| 72 |
-
|
| 73 |
-
# Open your app
|
| 74 |
-
heroku open
|
| 75 |
```
|
| 76 |
|
| 77 |
-
###
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
# Install Railway CLI
|
| 81 |
-
npm i -g @railway/cli
|
| 82 |
-
|
| 83 |
-
# Login
|
| 84 |
-
railway login
|
| 85 |
-
|
| 86 |
-
# Initialize project
|
| 87 |
-
railway init
|
| 88 |
-
|
| 89 |
-
# Deploy
|
| 90 |
-
railway up
|
| 91 |
-
|
| 92 |
-
# Get deployment URL
|
| 93 |
-
railway domain
|
| 94 |
```
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
1. Go to https://render.com
|
| 99 |
-
2. Click "New +" → "Web Service"
|
| 100 |
-
3. Connect your GitHub repository
|
| 101 |
-
4. Configure:
|
| 102 |
-
- **Build Command**: `pip install -r requirements.txt`
|
| 103 |
-
- **Start Command**: `python app.py`
|
| 104 |
-
- **Environment**: Python 3
|
| 105 |
-
5. Click "Create Web Service"
|
| 106 |
-
|
| 107 |
-
### Option 4: Docker (Self-Hosted)
|
| 108 |
|
|
|
|
| 109 |
```bash
|
| 110 |
-
# Build
|
| 111 |
-
docker build -t crypto-
|
| 112 |
-
|
| 113 |
-
# Run the container
|
| 114 |
-
docker run -d -p 7860:7860 --name crypto-aggregator crypto-aggregator
|
| 115 |
-
|
| 116 |
-
# Check logs
|
| 117 |
-
docker logs crypto-aggregator
|
| 118 |
-
|
| 119 |
-
# Stop the container
|
| 120 |
-
docker stop crypto-aggregator
|
| 121 |
-
|
| 122 |
-
# Remove the container
|
| 123 |
-
docker rm crypto-aggregator
|
| 124 |
-
```
|
| 125 |
-
|
| 126 |
-
### Option 5: Docker Compose (Self-Hosted)
|
| 127 |
-
|
| 128 |
-
Create `docker-compose.yml`:
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
ports:
|
| 137 |
-
- "7860:7860"
|
| 138 |
-
restart: unless-stopped
|
| 139 |
-
volumes:
|
| 140 |
-
- ./history.db:/app/history.db
|
| 141 |
-
environment:
|
| 142 |
-
- ENVIRONMENT=production
|
| 143 |
-
```
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
docker-compose up -d
|
| 148 |
```
|
| 149 |
|
| 150 |
-
###
|
| 151 |
-
|
| 152 |
```bash
|
| 153 |
-
#
|
| 154 |
-
ssh -i your-key.pem ubuntu@your-instance-ip
|
| 155 |
-
|
| 156 |
-
# Install Python and dependencies
|
| 157 |
-
sudo apt update
|
| 158 |
-
sudo apt install python3-pip python3-venv -y
|
| 159 |
-
|
| 160 |
-
# Create virtual environment
|
| 161 |
python3 -m venv venv
|
| 162 |
-
source venv/bin/activate
|
| 163 |
-
|
| 164 |
-
# Upload files (from local machine)
|
| 165 |
-
scp -i your-key.pem app.py requirements.txt all_apis_merged_2025.json ubuntu@your-instance-ip:~/
|
| 166 |
|
| 167 |
-
#
|
|
|
|
| 168 |
pip install -r requirements.txt
|
| 169 |
|
| 170 |
-
#
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
# Or use systemd service (recommended)
|
| 174 |
-
sudo nano /etc/systemd/system/crypto-aggregator.service
|
| 175 |
-
```
|
| 176 |
-
|
| 177 |
-
Create systemd service file:
|
| 178 |
-
```ini
|
| 179 |
-
[Unit]
|
| 180 |
-
Description=Crypto Resource Aggregator
|
| 181 |
-
After=network.target
|
| 182 |
-
|
| 183 |
-
[Service]
|
| 184 |
-
User=ubuntu
|
| 185 |
-
WorkingDirectory=/home/ubuntu/crypto-aggregator
|
| 186 |
-
ExecStart=/home/ubuntu/venv/bin/python app.py
|
| 187 |
-
Restart=always
|
| 188 |
-
|
| 189 |
-
[Install]
|
| 190 |
-
WantedBy=multi-user.target
|
| 191 |
-
```
|
| 192 |
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
sudo systemctl enable crypto-aggregator
|
| 196 |
-
sudo systemctl start crypto-aggregator
|
| 197 |
-
sudo systemctl status crypto-aggregator
|
| 198 |
```
|
| 199 |
|
| 200 |
-
###
|
| 201 |
-
|
| 202 |
```bash
|
| 203 |
-
#
|
| 204 |
-
#
|
|
|
|
| 205 |
|
| 206 |
-
#
|
| 207 |
-
|
|
|
|
| 208 |
|
| 209 |
-
#
|
| 210 |
-
|
| 211 |
|
| 212 |
-
#
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
--region us-central1 \
|
| 217 |
-
--allow-unauthenticated
|
| 218 |
-
|
| 219 |
-
# Get URL
|
| 220 |
-
gcloud run services describe crypto-aggregator --region us-central1 --format 'value(status.url)'
|
| 221 |
```
|
| 222 |
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
1. Go to https://cloud.digitalocean.com/apps
|
| 226 |
-
2. Click "Create App"
|
| 227 |
-
3. Connect your GitHub repository
|
| 228 |
-
4. Configure:
|
| 229 |
-
- **Run Command**: `python app.py`
|
| 230 |
-
- **Environment**: Python 3.11
|
| 231 |
-
- **HTTP Port**: 7860
|
| 232 |
-
5. Click "Deploy"
|
| 233 |
|
| 234 |
-
|
|
|
|
|
|
|
| 235 |
|
| 236 |
-
|
| 237 |
|
| 238 |
-
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
export PORT=8000
|
| 243 |
|
| 244 |
-
#
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
# Database path (default: history.db)
|
| 248 |
-
export DATABASE_PATH=/path/to/history.db
|
| 249 |
-
```
|
| 250 |
|
| 251 |
-
|
|
|
|
| 252 |
|
| 253 |
-
|
|
|
|
| 254 |
|
| 255 |
-
|
|
|
|
| 256 |
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
{
|
| 264 |
-
"status": "healthy",
|
| 265 |
-
"timestamp": "2025-11-10T...",
|
| 266 |
-
"resources_loaded": true,
|
| 267 |
-
"database_connected": true
|
| 268 |
-
}
|
| 269 |
```
|
| 270 |
|
| 271 |
-
###
|
|
|
|
|
|
|
|
|
|
| 272 |
|
| 273 |
-
|
| 274 |
-
|
|
|
|
| 275 |
```
|
| 276 |
|
| 277 |
-
|
| 278 |
|
|
|
|
| 279 |
```bash
|
| 280 |
-
curl
|
| 281 |
-
-H "Content-Type: application/json" \
|
| 282 |
-
-d '{
|
| 283 |
-
"resource_type": "market_data",
|
| 284 |
-
"resource_name": "coingecko",
|
| 285 |
-
"endpoint": "/simple/price",
|
| 286 |
-
"params": {
|
| 287 |
-
"ids": "bitcoin",
|
| 288 |
-
"vs_currencies": "usd"
|
| 289 |
-
}
|
| 290 |
-
}'
|
| 291 |
```
|
| 292 |
|
| 293 |
-
###
|
| 294 |
-
|
| 295 |
```bash
|
| 296 |
-
curl
|
| 297 |
```
|
| 298 |
|
| 299 |
-
###
|
| 300 |
-
|
| 301 |
-
From your local machine:
|
| 302 |
-
|
| 303 |
```bash
|
| 304 |
-
|
| 305 |
-
# Change: BASE_URL = "http://localhost:7860"
|
| 306 |
-
# To: BASE_URL = "https://your-deployment-url.com"
|
| 307 |
-
|
| 308 |
-
# Run tests
|
| 309 |
-
python test_aggregator.py
|
| 310 |
```
|
| 311 |
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
## Performance Optimization
|
| 315 |
-
|
| 316 |
-
### 1. Enable Caching
|
| 317 |
-
|
| 318 |
-
Add Redis for caching (optional):
|
| 319 |
-
|
| 320 |
-
```python
|
| 321 |
-
import redis
|
| 322 |
-
import json
|
| 323 |
-
|
| 324 |
-
# Connect to Redis
|
| 325 |
-
redis_client = redis.Redis(host='localhost', port=6379, decode_responses=True)
|
| 326 |
-
|
| 327 |
-
# Cache resource data
|
| 328 |
-
def get_cached_data(key, ttl=300):
|
| 329 |
-
cached = redis_client.get(key)
|
| 330 |
-
if cached:
|
| 331 |
-
return json.loads(cached)
|
| 332 |
-
return None
|
| 333 |
-
|
| 334 |
-
def set_cached_data(key, data, ttl=300):
|
| 335 |
-
redis_client.setex(key, ttl, json.dumps(data))
|
| 336 |
-
```
|
| 337 |
-
|
| 338 |
-
### 2. Use Connection Pooling
|
| 339 |
-
|
| 340 |
-
Already implemented with `aiohttp.ClientSession`
|
| 341 |
-
|
| 342 |
-
### 3. Add Rate Limiting
|
| 343 |
-
|
| 344 |
-
Install:
|
| 345 |
```bash
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
Add to `app.py`:
|
| 350 |
-
```python
|
| 351 |
-
from slowapi import Limiter, _rate_limit_exceeded_handler
|
| 352 |
-
from slowapi.util import get_remote_address
|
| 353 |
-
from slowapi.errors import RateLimitExceeded
|
| 354 |
-
|
| 355 |
-
limiter = Limiter(key_func=get_remote_address)
|
| 356 |
-
app.state.limiter = limiter
|
| 357 |
-
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
|
| 358 |
-
|
| 359 |
-
@app.post("/query")
|
| 360 |
-
@limiter.limit("60/minute")
|
| 361 |
-
async def query_resource(request: Request, query: ResourceQuery):
|
| 362 |
-
# ... existing code
|
| 363 |
```
|
| 364 |
|
| 365 |
-
###
|
| 366 |
-
|
| 367 |
-
Use Sentry for error tracking:
|
| 368 |
-
|
| 369 |
```bash
|
| 370 |
-
|
| 371 |
```
|
| 372 |
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
sentry_sdk.init(
|
| 378 |
-
dsn="your-sentry-dsn",
|
| 379 |
-
integrations=[FastApiIntegration()],
|
| 380 |
-
traces_sample_rate=1.0,
|
| 381 |
-
)
|
| 382 |
```
|
| 383 |
|
| 384 |
-
|
|
|
|
|
|
|
| 385 |
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
Store API keys in environment variables:
|
| 391 |
-
|
| 392 |
-
```python
|
| 393 |
-
import os
|
| 394 |
-
|
| 395 |
-
API_KEYS = {
|
| 396 |
-
'etherscan': os.getenv('ETHERSCAN_API_KEY', 'default-key'),
|
| 397 |
-
'coinmarketcap': os.getenv('CMC_API_KEY', 'default-key'),
|
| 398 |
-
}
|
| 399 |
```
|
| 400 |
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
Most platforms (Hugging Face, Heroku, etc.) provide HTTPS by default.
|
| 404 |
-
|
| 405 |
-
For self-hosted, use Let's Encrypt:
|
| 406 |
|
|
|
|
| 407 |
```bash
|
| 408 |
-
#
|
| 409 |
-
|
| 410 |
|
| 411 |
-
#
|
| 412 |
-
sudo certbot --nginx -d your-domain.com
|
| 413 |
```
|
| 414 |
|
| 415 |
-
###
|
| 416 |
-
|
| 417 |
```bash
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
```python
|
| 422 |
-
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
| 423 |
-
from fastapi import Security
|
| 424 |
-
|
| 425 |
-
security = HTTPBearer()
|
| 426 |
-
|
| 427 |
-
@app.post("/query")
|
| 428 |
-
async def query_resource(
|
| 429 |
-
query: ResourceQuery,
|
| 430 |
-
credentials: HTTPAuthorizationCredentials = Security(security)
|
| 431 |
-
):
|
| 432 |
-
# Verify token
|
| 433 |
-
if credentials.credentials != "your-secret-token":
|
| 434 |
-
raise HTTPException(status_code=401, detail="Invalid token")
|
| 435 |
-
# ... existing code
|
| 436 |
```
|
| 437 |
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
## Monitoring & Maintenance
|
| 441 |
-
|
| 442 |
-
### 1. Monitor Logs
|
| 443 |
-
|
| 444 |
-
Hugging Face Spaces:
|
| 445 |
-
- View logs in the Space settings → "Logs" tab
|
| 446 |
-
|
| 447 |
-
Docker:
|
| 448 |
```bash
|
| 449 |
-
|
|
|
|
| 450 |
```
|
| 451 |
|
| 452 |
-
|
| 453 |
```bash
|
| 454 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 455 |
```
|
| 456 |
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
Backup database regularly:
|
| 460 |
-
|
| 461 |
-
```bash
|
| 462 |
-
# Local backup
|
| 463 |
-
cp history.db history_backup_$(date +%Y%m%d).db
|
| 464 |
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
Clean old records:
|
| 470 |
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
```
|
| 476 |
|
| 477 |
-
###
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
### 4. Health Checks
|
| 482 |
|
| 483 |
-
|
|
|
|
| 484 |
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
|
|
|
| 488 |
```
|
| 489 |
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
```bash
|
| 500 |
-
# Check if port 7860 is in use
|
| 501 |
-
lsof -i :7860
|
| 502 |
-
|
| 503 |
-
# Kill existing process
|
| 504 |
-
kill -9 $(lsof -t -i:7860)
|
| 505 |
-
|
| 506 |
-
# Or use a different port
|
| 507 |
-
PORT=8000 python app.py
|
| 508 |
```
|
| 509 |
|
| 510 |
-
|
| 511 |
|
| 512 |
-
|
| 513 |
```bash
|
| 514 |
-
#
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
# Remove lock (if exists)
|
| 518 |
-
rm history.db-journal
|
| 519 |
|
| 520 |
-
#
|
| 521 |
-
|
| 522 |
```
|
| 523 |
|
| 524 |
-
###
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
-
|
| 528 |
-
- Implement request queuing
|
| 529 |
-
- Scale horizontally with multiple instances
|
| 530 |
-
|
| 531 |
-
### Issue: API rate limits
|
| 532 |
-
|
| 533 |
-
**Solution:**
|
| 534 |
-
- Implement caching
|
| 535 |
-
- Add multiple API keys for rotation
|
| 536 |
-
- Use fallback resources
|
| 537 |
-
|
| 538 |
-
---
|
| 539 |
-
|
| 540 |
-
## Scaling
|
| 541 |
-
|
| 542 |
-
### Horizontal Scaling
|
| 543 |
-
|
| 544 |
-
Use a load balancer with multiple instances:
|
| 545 |
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
deploy:
|
| 554 |
-
replicas: 3
|
| 555 |
-
environment:
|
| 556 |
-
- WORKER_ID=${HOSTNAME}
|
| 557 |
-
|
| 558 |
-
nginx:
|
| 559 |
-
image: nginx:alpine
|
| 560 |
-
ports:
|
| 561 |
-
- "80:80"
|
| 562 |
-
volumes:
|
| 563 |
-
- ./nginx.conf:/etc/nginx/nginx.conf
|
| 564 |
-
depends_on:
|
| 565 |
-
- aggregator
|
| 566 |
```
|
| 567 |
|
| 568 |
-
### Vertical Scaling
|
| 569 |
-
|
| 570 |
-
Increase resources on your hosting platform:
|
| 571 |
-
- Hugging Face: Upgrade to paid tier
|
| 572 |
-
- AWS: Use larger EC2 instance
|
| 573 |
-
- Docker: Adjust container resources
|
| 574 |
-
|
| 575 |
-
---
|
| 576 |
-
|
| 577 |
## Support
|
| 578 |
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
3. Test individual resources with `/status`
|
| 583 |
-
4. Verify database with SQLite browser
|
| 584 |
-
|
| 585 |
-
---
|
| 586 |
-
|
| 587 |
-
## Next Steps
|
| 588 |
-
|
| 589 |
-
After deployment:
|
| 590 |
-
|
| 591 |
-
1. **Integrate with your main app** using the provided client examples
|
| 592 |
-
2. **Set up monitoring** with health checks and alerts
|
| 593 |
-
3. **Configure backups** for the history database
|
| 594 |
-
4. **Add custom resources** by updating the JSON file
|
| 595 |
-
5. **Implement caching** for frequently accessed data
|
| 596 |
-
6. **Enable authentication** if needed for security
|
| 597 |
|
| 598 |
-
|
| 599 |
|
| 600 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 🚀 Deployment Guide - HuggingFace Space
|
| 2 |
|
| 3 |
+
## مشکل Dependency Conflict و حل آن
|
| 4 |
|
| 5 |
+
### مشکل قبلی:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
```
|
| 7 |
+
ERROR: Cannot install gradio and websockets>=12.0 because these package versions have conflicting dependencies.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
```
|
| 9 |
|
| 10 |
+
### حل شده:
|
| 11 |
+
```python
|
| 12 |
+
websockets>=10.4,<12.0 # Compatible with gradio-client
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
```
|
| 14 |
|
| 15 |
+
## نصب و راهاندازی
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
### روش 1: Docker (توصیه میشود)
|
| 18 |
```bash
|
| 19 |
+
# Build
|
| 20 |
+
docker build -t crypto-hf-hub .
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# Run
|
| 23 |
+
docker run -d \
|
| 24 |
+
-p 7860:7860 \
|
| 25 |
+
-e HF_TOKEN=your_token_here \
|
| 26 |
+
--name crypto-hub \
|
| 27 |
+
crypto-hf-hub
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# Logs
|
| 30 |
+
docker logs -f crypto-hub
|
|
|
|
| 31 |
```
|
| 32 |
|
| 33 |
+
### روش 2: مستقیم (Local Development)
|
|
|
|
| 34 |
```bash
|
| 35 |
+
# ایجاد virtual environment
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
python3 -m venv venv
|
| 37 |
+
source venv/bin/activate # Windows: venv\Scripts\activate
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
# نصب dependencies
|
| 40 |
+
pip install --upgrade pip
|
| 41 |
pip install -r requirements.txt
|
| 42 |
|
| 43 |
+
# تنظیم environment variables
|
| 44 |
+
export HF_TOKEN=your_huggingface_token
|
| 45 |
+
export PORT=7860
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
# اجرا
|
| 48 |
+
uvicorn hf_unified_server:app --host 0.0.0.0 --port 7860 --reload
|
|
|
|
|
|
|
|
|
|
| 49 |
```
|
| 50 |
|
| 51 |
+
### روش 3: HuggingFace Space
|
|
|
|
| 52 |
```bash
|
| 53 |
+
# 1. ساخت Space جدید در HuggingFace
|
| 54 |
+
# Space SDK: Docker
|
| 55 |
+
# Hardware: CPU basic (free)
|
| 56 |
|
| 57 |
+
# 2. کلون کردن repository
|
| 58 |
+
git clone https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
|
| 59 |
+
cd YOUR_SPACE_NAME
|
| 60 |
|
| 61 |
+
# 3. کپی کردن فایلهای پروژه
|
| 62 |
+
cp -r crypto-hf-integrated/* .
|
| 63 |
|
| 64 |
+
# 4. Commit و Push
|
| 65 |
+
git add .
|
| 66 |
+
git commit -m "Initial deployment with HF integration"
|
| 67 |
+
git push
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
```
|
| 69 |
|
| 70 |
+
## تنظیمات HuggingFace Space
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
### Dockerfile
|
| 73 |
+
```dockerfile
|
| 74 |
+
FROM python:3.10-slim
|
| 75 |
|
| 76 |
+
WORKDIR /app
|
| 77 |
|
| 78 |
+
# Install system dependencies
|
| 79 |
+
RUN apt-get update && apt-get install -y \
|
| 80 |
+
build-essential \
|
| 81 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 82 |
|
| 83 |
+
# Copy requirements
|
| 84 |
+
COPY requirements.txt .
|
|
|
|
| 85 |
|
| 86 |
+
# Install Python dependencies
|
| 87 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
# Copy application
|
| 90 |
+
COPY . .
|
| 91 |
|
| 92 |
+
# Create directories
|
| 93 |
+
RUN mkdir -p data/database logs
|
| 94 |
|
| 95 |
+
# Expose port
|
| 96 |
+
EXPOSE 7860
|
| 97 |
|
| 98 |
+
# Set environment
|
| 99 |
+
ENV PORT=7860
|
| 100 |
+
ENV PYTHONUNBUFFERED=1
|
| 101 |
|
| 102 |
+
# Start server
|
| 103 |
+
CMD ["uvicorn", "hf_unified_server:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
```
|
| 105 |
|
| 106 |
+
### Space Settings (در UI)
|
| 107 |
+
```yaml
|
| 108 |
+
# Secrets
|
| 109 |
+
HF_TOKEN: your_token_here
|
| 110 |
|
| 111 |
+
# Hardware
|
| 112 |
+
- CPU basic (Free) - برای شروع
|
| 113 |
+
- CPU upgrade (Paid) - برای production با traffic بیشتر
|
| 114 |
```
|
| 115 |
|
| 116 |
+
## تست Endpoints
|
| 117 |
|
| 118 |
+
### Health Check
|
| 119 |
```bash
|
| 120 |
+
curl http://localhost:7860/api/health
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
```
|
| 122 |
|
| 123 |
+
### Top Coins
|
|
|
|
| 124 |
```bash
|
| 125 |
+
curl http://localhost:7860/api/coins/top?limit=10
|
| 126 |
```
|
| 127 |
|
| 128 |
+
### Market Stats
|
|
|
|
|
|
|
|
|
|
| 129 |
```bash
|
| 130 |
+
curl http://localhost:7860/api/market/stats
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
```
|
| 132 |
|
| 133 |
+
### Sentiment Analysis
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
```bash
|
| 135 |
+
curl -X POST http://localhost:7860/api/sentiment/analyze \
|
| 136 |
+
-H "Content-Type: application/json" \
|
| 137 |
+
-d '{"text": "Bitcoin is breaking new ATH!"}'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
```
|
| 139 |
|
| 140 |
+
### Models List
|
|
|
|
|
|
|
|
|
|
| 141 |
```bash
|
| 142 |
+
curl http://localhost:7860/api/models/list
|
| 143 |
```
|
| 144 |
|
| 145 |
+
### Datasets List
|
| 146 |
+
```bash
|
| 147 |
+
curl http://localhost:7860/api/datasets/list
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
```
|
| 149 |
|
| 150 |
+
### WebSocket Test (در browser console)
|
| 151 |
+
```javascript
|
| 152 |
+
const ws = new WebSocket('ws://localhost:7860/ws');
|
| 153 |
|
| 154 |
+
ws.onopen = () => console.log('Connected');
|
| 155 |
+
ws.onmessage = (event) => console.log('Update:', JSON.parse(event.data));
|
| 156 |
+
ws.onerror = (error) => console.error('Error:', error);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
```
|
| 158 |
|
| 159 |
+
## مشکلات رایج و راه حل
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
+
### 1. Out of Memory
|
| 162 |
```bash
|
| 163 |
+
# کاهش مدلهای load شده
|
| 164 |
+
# در ai_models.py فقط crypto_sent_0 و financial_sent_0 load میشه
|
| 165 |
|
| 166 |
+
# یا استفاده از CPU basic به جای GPU
|
|
|
|
| 167 |
```
|
| 168 |
|
| 169 |
+
### 2. Slow Model Loading
|
|
|
|
| 170 |
```bash
|
| 171 |
+
# استفاده از model caching در HuggingFace
|
| 172 |
+
export TRANSFORMERS_CACHE=/app/.cache/huggingface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
```
|
| 174 |
|
| 175 |
+
### 3. Rate Limiting
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
```bash
|
| 177 |
+
# استفاده از HF_TOKEN برای rate limit بالاتر
|
| 178 |
+
export HF_TOKEN=hf_your_token
|
| 179 |
```
|
| 180 |
|
| 181 |
+
### 4. WebSocket Connection Failed
|
| 182 |
```bash
|
| 183 |
+
# چک کردن CORS settings
|
| 184 |
+
# مطمئن شوید که در hf_unified_server.py:
|
| 185 |
+
app.add_middleware(
|
| 186 |
+
CORSMiddleware,
|
| 187 |
+
allow_origins=["*"],
|
| 188 |
+
allow_credentials=True,
|
| 189 |
+
allow_methods=["*"],
|
| 190 |
+
allow_headers=["*"],
|
| 191 |
+
)
|
| 192 |
```
|
| 193 |
|
| 194 |
+
## بهینهسازی برای Production
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
+
### 1. Caching
|
| 197 |
+
```python
|
| 198 |
+
# در hf_unified_server.py
|
| 199 |
+
from functools import lru_cache
|
|
|
|
| 200 |
|
| 201 |
+
@lru_cache(maxsize=128)
|
| 202 |
+
def get_cached_coins(limit: int):
|
| 203 |
+
# Cache results for 1 minute
|
| 204 |
+
pass
|
| 205 |
```
|
| 206 |
|
| 207 |
+
### 2. Rate Limiting
|
| 208 |
+
```python
|
| 209 |
+
from slowapi import Limiter
|
| 210 |
+
from slowapi.util import get_remote_address
|
|
|
|
| 211 |
|
| 212 |
+
limiter = Limiter(key_func=get_remote_address)
|
| 213 |
+
app.state.limiter = limiter
|
| 214 |
|
| 215 |
+
@app.get("/api/sentiment/analyze")
|
| 216 |
+
@limiter.limit("10/minute")
|
| 217 |
+
async def analyze_sentiment(...):
|
| 218 |
+
pass
|
| 219 |
```
|
| 220 |
|
| 221 |
+
### 3. Model Optimization
|
| 222 |
+
```python
|
| 223 |
+
# Use smaller models for faster inference
|
| 224 |
+
# quantization برای کاهش حجم
|
| 225 |
+
from transformers import AutoModelForSequenceClassification
|
| 226 |
+
model = AutoModelForSequenceClassification.from_pretrained(
|
| 227 |
+
"model_id",
|
| 228 |
+
torch_dtype=torch.float16 # Half precision
|
| 229 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
```
|
| 231 |
|
| 232 |
+
## Monitoring
|
| 233 |
|
| 234 |
+
### Logs
|
| 235 |
```bash
|
| 236 |
+
# Docker logs
|
| 237 |
+
docker logs -f crypto-hub
|
|
|
|
|
|
|
|
|
|
| 238 |
|
| 239 |
+
# HuggingFace Space logs
|
| 240 |
+
# قابل مشاهده در dashboard Space
|
| 241 |
```
|
| 242 |
|
| 243 |
+
### Metrics
|
| 244 |
+
```bash
|
| 245 |
+
# Health endpoint برای monitoring
|
| 246 |
+
curl http://your-space.hf.space/api/health
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
|
| 248 |
+
# Response:
|
| 249 |
+
{
|
| 250 |
+
"status": "healthy",
|
| 251 |
+
"uptime": 3600,
|
| 252 |
+
"models_loaded": 2,
|
| 253 |
+
"datasets_available": 14
|
| 254 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
```
|
| 256 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
## Support
|
| 258 |
|
| 259 |
+
- **Documentation**: README_HF_INTEGRATION.md
|
| 260 |
+
- **Issues**: Check logs first
|
| 261 |
+
- **Performance**: Use HF Space with GPU for heavy models
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
|
| 263 |
+
## نکات مهم
|
| 264 |
|
| 265 |
+
1. ⚠️ **HF_TOKEN**: حتماً در secrets تنظیم کنید
|
| 266 |
+
2. 🔥 **Models**: اولین بار کند است (downloading)
|
| 267 |
+
3. 💾 **Cache**: مدلها cache میشوند
|
| 268 |
+
4. 🚀 **WebSocket**: برای real-time updates
|
| 269 |
+
5. 📊 **Dashboard**: در `/` قابل دسترسی
|
Dockerfile.optimized
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
# Install system dependencies
|
| 6 |
+
RUN apt-get update && apt-get install -y \
|
| 7 |
+
build-essential \
|
| 8 |
+
git \
|
| 9 |
+
curl \
|
| 10 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 11 |
+
|
| 12 |
+
# Copy requirements first for better caching
|
| 13 |
+
COPY requirements.txt .
|
| 14 |
+
|
| 15 |
+
# Upgrade pip
|
| 16 |
+
RUN pip install --no-cache-dir --upgrade pip
|
| 17 |
+
|
| 18 |
+
# Install dependencies
|
| 19 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 20 |
+
|
| 21 |
+
# Copy application code
|
| 22 |
+
COPY . .
|
| 23 |
+
|
| 24 |
+
# Create necessary directories
|
| 25 |
+
RUN mkdir -p \
|
| 26 |
+
data/database \
|
| 27 |
+
data/backups \
|
| 28 |
+
logs \
|
| 29 |
+
static/css \
|
| 30 |
+
static/js \
|
| 31 |
+
.cache/huggingface
|
| 32 |
+
|
| 33 |
+
# Set permissions
|
| 34 |
+
RUN chmod -R 755 /app
|
| 35 |
+
|
| 36 |
+
# Environment variables
|
| 37 |
+
ENV PORT=7860 \
|
| 38 |
+
PYTHONUNBUFFERED=1 \
|
| 39 |
+
TRANSFORMERS_CACHE=/app/.cache/huggingface \
|
| 40 |
+
HF_HOME=/app/.cache/huggingface \
|
| 41 |
+
PYTHONDONTWRITEBYTECODE=1
|
| 42 |
+
|
| 43 |
+
# Expose port
|
| 44 |
+
EXPOSE 7860
|
| 45 |
+
|
| 46 |
+
# Health check
|
| 47 |
+
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
|
| 48 |
+
CMD curl -f http://localhost:7860/api/health || exit 1
|
| 49 |
+
|
| 50 |
+
# Run application
|
| 51 |
+
CMD ["uvicorn", "hf_unified_server:app", "--host", "0.0.0.0", "--port", "7860", "--workers", "1"]
|
requirements.txt
CHANGED
|
@@ -1,41 +1,57 @@
|
|
| 1 |
-
# Unified dependencies for
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Unified dependencies for Crypto Intelligence Hub (HuggingFace Space)
|
| 2 |
+
# Optimized for HF deployment with minimal conflicts
|
| 3 |
+
|
| 4 |
+
# ===== Core API Stack =====
|
| 5 |
+
fastapi==0.115.0
|
| 6 |
+
uvicorn[standard]==0.30.0
|
| 7 |
+
pydantic==2.9.0
|
| 8 |
+
pydantic-settings==2.5.0
|
| 9 |
+
sqlalchemy==2.0.35
|
| 10 |
+
httpx==0.27.2
|
| 11 |
+
websockets>=10.4,<12.0 # Compatible with gradio-client
|
| 12 |
+
python-dotenv==1.0.1
|
| 13 |
+
python-multipart==0.0.9
|
| 14 |
+
requests==2.32.3
|
| 15 |
+
aiohttp==3.10.5
|
| 16 |
+
|
| 17 |
+
# ===== Data Processing =====
|
| 18 |
+
pandas==2.2.3
|
| 19 |
+
numpy>=1.26.0,<2.0.0
|
| 20 |
+
|
| 21 |
+
# ===== Gradio Dashboard =====
|
| 22 |
+
gradio==4.44.0
|
| 23 |
+
plotly==5.24.1
|
| 24 |
+
psutil==6.0.0
|
| 25 |
+
|
| 26 |
+
# ===== HuggingFace & AI/ML =====
|
| 27 |
+
# Core transformers with minimal deps
|
| 28 |
+
transformers>=4.45.0
|
| 29 |
+
tokenizers>=0.20.0
|
| 30 |
+
huggingface-hub>=0.25.0
|
| 31 |
+
safetensors>=0.4.0
|
| 32 |
+
|
| 33 |
+
# Datasets library
|
| 34 |
+
datasets>=3.0.0
|
| 35 |
+
|
| 36 |
+
# PyTorch - CPU only for space efficiency
|
| 37 |
+
torch>=2.4.0,<2.5.0
|
| 38 |
+
torchaudio>=2.4.0,<2.5.0
|
| 39 |
+
|
| 40 |
+
# Optional: Sentence transformers for embeddings
|
| 41 |
+
sentence-transformers>=3.1.0
|
| 42 |
+
|
| 43 |
+
# Tokenization
|
| 44 |
+
sentencepiece==0.2.0
|
| 45 |
+
|
| 46 |
+
# ===== Content Parsing =====
|
| 47 |
+
feedparser==6.0.11
|
| 48 |
+
beautifulsoup4==4.12.3
|
| 49 |
+
lxml==5.3.0
|
| 50 |
+
|
| 51 |
+
# ===== Utilities =====
|
| 52 |
+
python-dateutil>=2.9.0
|
| 53 |
+
pytz>=2024.1
|
| 54 |
+
tenacity>=9.0.0
|
| 55 |
+
|
| 56 |
+
# ===== Optional: Acceleration =====
|
| 57 |
+
# accelerate>=0.34.0 # Uncomment if using multi-GPU
|