Upload 313 files
Browse files- .vscode/settings.json +3 -0
- DEPLOYMENT_GUIDE.md +0 -600
- Dockerfile +18 -14
- HUGGINGFACE_DEPLOYMENT.md +0 -349
- HUGGINGFACE_DIAGNOSTIC_GUIDE.md +6 -0
- README.md +1 -7
- README_DEPLOYMENT.md +258 -260
- README_HUGGINGFACE.md +151 -0
- __pycache__/config.cpython-313.pyc +0 -0
- __pycache__/database.cpython-313.pyc +0 -0
- api_server_extended.py +64 -24
- app.py +0 -0
- collectors/__pycache__/__init__.cpython-313.pyc +0 -0
- collectors/__pycache__/market_data.cpython-313.pyc +0 -0
- database/__pycache__/__init__.cpython-313.pyc +0 -0
- database/__pycache__/data_access.cpython-313.pyc +0 -0
- database/__pycache__/db_manager.cpython-313.pyc +0 -0
- database/__pycache__/models.cpython-313.pyc +0 -0
- logs/crypto_aggregator.log +0 -0
- main.py +29 -29
- requirements.txt +18 -3
- test_docker_build.bat +56 -0
- test_docker_build.sh +54 -0
- utils/__pycache__/__init__.cpython-313.pyc +0 -0
- utils/__pycache__/api_client.cpython-313.pyc +0 -0
- utils/__pycache__/logger.cpython-313.pyc +0 -0
- verify_deployment.py +0 -0
.vscode/settings.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"git.ignoreLimitWarning": true
|
| 3 |
+
}
|
DEPLOYMENT_GUIDE.md
CHANGED
|
@@ -1,600 +0,0 @@
|
|
| 1 |
-
# Deployment Guide - Crypto Resource Aggregator
|
| 2 |
-
|
| 3 |
-
## Quick Deployment to Hugging Face Spaces
|
| 4 |
-
|
| 5 |
-
### Method 1: Web Interface (Recommended for Beginners)
|
| 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 |
-
### Option 2: Railway
|
| 78 |
-
|
| 79 |
-
```bash
|
| 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 |
-
### Option 3: Render
|
| 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 the Docker image
|
| 111 |
-
docker build -t crypto-aggregator .
|
| 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 |
-
```yaml
|
| 131 |
-
version: '3.8'
|
| 132 |
-
|
| 133 |
-
services:
|
| 134 |
-
aggregator:
|
| 135 |
-
build: .
|
| 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 |
-
Run:
|
| 146 |
-
```bash
|
| 147 |
-
docker-compose up -d
|
| 148 |
-
```
|
| 149 |
-
|
| 150 |
-
### Option 6: AWS EC2
|
| 151 |
-
|
| 152 |
-
```bash
|
| 153 |
-
# Connect to your EC2 instance
|
| 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 |
-
# Install dependencies
|
| 168 |
-
pip install -r requirements.txt
|
| 169 |
-
|
| 170 |
-
# Run with nohup
|
| 171 |
-
nohup python app.py > output.log 2>&1 &
|
| 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 |
-
Enable and start:
|
| 194 |
-
```bash
|
| 195 |
-
sudo systemctl enable crypto-aggregator
|
| 196 |
-
sudo systemctl start crypto-aggregator
|
| 197 |
-
sudo systemctl status crypto-aggregator
|
| 198 |
-
```
|
| 199 |
-
|
| 200 |
-
### Option 7: Google Cloud Run
|
| 201 |
-
|
| 202 |
-
```bash
|
| 203 |
-
# Install gcloud CLI
|
| 204 |
-
# https://cloud.google.com/sdk/docs/install
|
| 205 |
-
|
| 206 |
-
# Authenticate
|
| 207 |
-
gcloud auth login
|
| 208 |
-
|
| 209 |
-
# Set project
|
| 210 |
-
gcloud config set project YOUR_PROJECT_ID
|
| 211 |
-
|
| 212 |
-
# Build and deploy
|
| 213 |
-
gcloud run deploy crypto-aggregator \
|
| 214 |
-
--source . \
|
| 215 |
-
--platform managed \
|
| 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 |
-
### Option 8: DigitalOcean App Platform
|
| 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 |
-
## Environment Variables (Optional)
|
| 237 |
-
|
| 238 |
-
You can configure the following environment variables:
|
| 239 |
-
|
| 240 |
-
```bash
|
| 241 |
-
# Port (default: 7860)
|
| 242 |
-
export PORT=8000
|
| 243 |
-
|
| 244 |
-
# Log level (default: INFO)
|
| 245 |
-
export LOG_LEVEL=DEBUG
|
| 246 |
-
|
| 247 |
-
# Database path (default: history.db)
|
| 248 |
-
export DATABASE_PATH=/path/to/history.db
|
| 249 |
-
```
|
| 250 |
-
|
| 251 |
-
---
|
| 252 |
-
|
| 253 |
-
## Post-Deployment Testing
|
| 254 |
-
|
| 255 |
-
### 1. Test Health Endpoint
|
| 256 |
-
|
| 257 |
-
```bash
|
| 258 |
-
curl https://your-deployment-url.com/health
|
| 259 |
-
```
|
| 260 |
-
|
| 261 |
-
Expected response:
|
| 262 |
-
```json
|
| 263 |
-
{
|
| 264 |
-
"status": "healthy",
|
| 265 |
-
"timestamp": "2025-11-10T...",
|
| 266 |
-
"resources_loaded": true,
|
| 267 |
-
"database_connected": true
|
| 268 |
-
}
|
| 269 |
-
```
|
| 270 |
-
|
| 271 |
-
### 2. Test Resource Listing
|
| 272 |
-
|
| 273 |
-
```bash
|
| 274 |
-
curl https://your-deployment-url.com/resources
|
| 275 |
-
```
|
| 276 |
-
|
| 277 |
-
### 3. Test Query Endpoint
|
| 278 |
-
|
| 279 |
-
```bash
|
| 280 |
-
curl -X POST https://your-deployment-url.com/query \
|
| 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 |
-
### 4. Test Status Monitoring
|
| 294 |
-
|
| 295 |
-
```bash
|
| 296 |
-
curl https://your-deployment-url.com/status
|
| 297 |
-
```
|
| 298 |
-
|
| 299 |
-
### 5. Run Full Test Suite
|
| 300 |
-
|
| 301 |
-
From your local machine:
|
| 302 |
-
|
| 303 |
-
```bash
|
| 304 |
-
# Update BASE_URL in test_aggregator.py
|
| 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 |
-
pip install slowapi
|
| 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 |
-
### 4. Add Monitoring
|
| 366 |
-
|
| 367 |
-
Use Sentry for error tracking:
|
| 368 |
-
|
| 369 |
-
```bash
|
| 370 |
-
pip install sentry-sdk
|
| 371 |
-
```
|
| 372 |
-
|
| 373 |
-
```python
|
| 374 |
-
import sentry_sdk
|
| 375 |
-
from sentry_sdk.integrations.fastapi import FastApiIntegration
|
| 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 |
-
## Security Best Practices
|
| 387 |
-
|
| 388 |
-
### 1. API Key Management
|
| 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 |
-
### 2. Enable HTTPS
|
| 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 |
-
# Install Certbot
|
| 409 |
-
sudo apt install certbot python3-certbot-nginx
|
| 410 |
-
|
| 411 |
-
# Get certificate
|
| 412 |
-
sudo certbot --nginx -d your-domain.com
|
| 413 |
-
```
|
| 414 |
-
|
| 415 |
-
### 3. Add Authentication (Optional)
|
| 416 |
-
|
| 417 |
-
```bash
|
| 418 |
-
pip install python-jose[cryptography] passlib[bcrypt]
|
| 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 |
-
docker logs -f crypto-aggregator
|
| 450 |
-
```
|
| 451 |
-
|
| 452 |
-
Systemd:
|
| 453 |
-
```bash
|
| 454 |
-
journalctl -u crypto-aggregator -f
|
| 455 |
-
```
|
| 456 |
-
|
| 457 |
-
### 2. Database Maintenance
|
| 458 |
-
|
| 459 |
-
Backup database regularly:
|
| 460 |
-
|
| 461 |
-
```bash
|
| 462 |
-
# Local backup
|
| 463 |
-
cp history.db history_backup_$(date +%Y%m%d).db
|
| 464 |
-
|
| 465 |
-
# Remote backup
|
| 466 |
-
scp user@server:/path/to/history.db ./backups/
|
| 467 |
-
```
|
| 468 |
-
|
| 469 |
-
Clean old records:
|
| 470 |
-
|
| 471 |
-
```sql
|
| 472 |
-
-- Remove records older than 30 days
|
| 473 |
-
DELETE FROM query_history WHERE timestamp < datetime('now', '-30 days');
|
| 474 |
-
DELETE FROM resource_status WHERE last_check < datetime('now', '-30 days');
|
| 475 |
-
```
|
| 476 |
-
|
| 477 |
-
### 3. Update Resources
|
| 478 |
-
|
| 479 |
-
To add new resources, update `all_apis_merged_2025.json` and redeploy.
|
| 480 |
-
|
| 481 |
-
### 4. Health Checks
|
| 482 |
-
|
| 483 |
-
Set up automated health checks:
|
| 484 |
-
|
| 485 |
-
```bash
|
| 486 |
-
# Cron job (every 5 minutes)
|
| 487 |
-
*/5 * * * * curl https://your-deployment-url.com/health || echo "API is down!"
|
| 488 |
-
```
|
| 489 |
-
|
| 490 |
-
Use UptimeRobot or similar service for monitoring.
|
| 491 |
-
|
| 492 |
-
---
|
| 493 |
-
|
| 494 |
-
## Troubleshooting
|
| 495 |
-
|
| 496 |
-
### Issue: Server won't start
|
| 497 |
-
|
| 498 |
-
**Solution:**
|
| 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 |
-
### Issue: Database locked
|
| 511 |
-
|
| 512 |
-
**Solution:**
|
| 513 |
-
```bash
|
| 514 |
-
# Stop all instances
|
| 515 |
-
pkill -f app.py
|
| 516 |
-
|
| 517 |
-
# Remove lock (if exists)
|
| 518 |
-
rm history.db-journal
|
| 519 |
-
|
| 520 |
-
# Restart
|
| 521 |
-
python app.py
|
| 522 |
-
```
|
| 523 |
-
|
| 524 |
-
### Issue: High memory usage
|
| 525 |
-
|
| 526 |
-
**Solution:**
|
| 527 |
-
- Add connection limits
|
| 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 |
-
```yaml
|
| 547 |
-
# docker-compose-scaled.yml
|
| 548 |
-
version: '3.8'
|
| 549 |
-
|
| 550 |
-
services:
|
| 551 |
-
aggregator:
|
| 552 |
-
build: .
|
| 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 |
-
For issues or questions:
|
| 580 |
-
1. Check `/health` endpoint
|
| 581 |
-
2. Review application logs
|
| 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 |
-
**Congratulations! Your Crypto Resource Aggregator is now deployed and ready to use!** 🚀
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Dockerfile
CHANGED
|
@@ -1,37 +1,41 @@
|
|
|
|
|
| 1 |
FROM python:3.11-slim
|
| 2 |
|
| 3 |
-
#
|
| 4 |
ENV PYTHONUNBUFFERED=1 \
|
| 5 |
PYTHONDONTWRITEBYTECODE=1 \
|
| 6 |
PIP_NO_CACHE_DIR=1 \
|
| 7 |
PIP_DISABLE_PIP_VERSION_CHECK=1 \
|
| 8 |
ENABLE_AUTO_DISCOVERY=false
|
| 9 |
|
| 10 |
-
#
|
| 11 |
RUN apt-get update && apt-get install -y \
|
| 12 |
gcc \
|
|
|
|
| 13 |
curl \
|
| 14 |
&& rm -rf /var/lib/apt/lists/*
|
| 15 |
|
| 16 |
-
#
|
| 17 |
WORKDIR /app
|
| 18 |
|
| 19 |
-
#
|
| 20 |
COPY requirements.txt .
|
|
|
|
|
|
|
| 21 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 22 |
|
| 23 |
-
#
|
| 24 |
COPY . .
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
RUN mkdir -p logs
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
EXPOSE 8000
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
HEALTHCHECK --interval=30s --timeout=10s --start-period=
|
| 34 |
-
CMD curl -f
|
| 35 |
|
| 36 |
-
# Run server (
|
| 37 |
-
CMD ["sh", "-c", "uvicorn
|
|
|
|
| 1 |
+
# Use Python 3.11 Slim base image
|
| 2 |
FROM python:3.11-slim
|
| 3 |
|
| 4 |
+
# Set environment variables
|
| 5 |
ENV PYTHONUNBUFFERED=1 \
|
| 6 |
PYTHONDONTWRITEBYTECODE=1 \
|
| 7 |
PIP_NO_CACHE_DIR=1 \
|
| 8 |
PIP_DISABLE_PIP_VERSION_CHECK=1 \
|
| 9 |
ENABLE_AUTO_DISCOVERY=false
|
| 10 |
|
| 11 |
+
# Install system dependencies
|
| 12 |
RUN apt-get update && apt-get install -y \
|
| 13 |
gcc \
|
| 14 |
+
g++ \
|
| 15 |
curl \
|
| 16 |
&& rm -rf /var/lib/apt/lists/*
|
| 17 |
|
| 18 |
+
# Set working directory
|
| 19 |
WORKDIR /app
|
| 20 |
|
| 21 |
+
# Copy dependency files
|
| 22 |
COPY requirements.txt .
|
| 23 |
+
|
| 24 |
+
# Install Python dependencies
|
| 25 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 26 |
|
| 27 |
+
# Copy application code
|
| 28 |
COPY . .
|
| 29 |
|
| 30 |
+
# Create necessary directories
|
| 31 |
+
RUN mkdir -p logs data data/exports data/backups
|
| 32 |
|
| 33 |
+
# Expose ports (Hugging Face uses PORT env variable, default 7860)
|
| 34 |
+
EXPOSE 7860 8000
|
| 35 |
|
| 36 |
+
# Health check (simplified to avoid requests dependency in healthcheck)
|
| 37 |
+
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
|
| 38 |
+
CMD curl -f http://localhost:${PORT:-8000}/health || exit 1
|
| 39 |
|
| 40 |
+
# Run server with uvicorn (supports Hugging Face PORT env variable)
|
| 41 |
+
CMD ["sh", "-c", "uvicorn api_server_extended:app --host 0.0.0.0 --port ${PORT:-8000}"]
|
HUGGINGFACE_DEPLOYMENT.md
CHANGED
|
@@ -1,349 +0,0 @@
|
|
| 1 |
-
# 🤗 HuggingFace Spaces Deployment Guide
|
| 2 |
-
|
| 3 |
-
This guide explains how to deploy the Crypto API Monitoring System to HuggingFace Spaces.
|
| 4 |
-
|
| 5 |
-
## Overview
|
| 6 |
-
|
| 7 |
-
The application is fully optimized for HuggingFace Spaces deployment with:
|
| 8 |
-
- **Docker-based deployment** using the standard HF Spaces port (7860)
|
| 9 |
-
- **Automatic environment detection** for frontend API calls
|
| 10 |
-
- **HuggingFace ML integration** for crypto sentiment analysis
|
| 11 |
-
- **WebSocket support** for real-time data streaming
|
| 12 |
-
- **Persistent data storage** with SQLite
|
| 13 |
-
|
| 14 |
-
## Prerequisites
|
| 15 |
-
|
| 16 |
-
1. A HuggingFace account ([sign up here](https://huggingface.co/join))
|
| 17 |
-
2. Git installed on your local machine
|
| 18 |
-
3. Basic familiarity with Docker and HuggingFace Spaces
|
| 19 |
-
|
| 20 |
-
## Deployment Steps
|
| 21 |
-
|
| 22 |
-
### 1. Create a New Space
|
| 23 |
-
|
| 24 |
-
1. Go to [HuggingFace Spaces](https://huggingface.co/spaces)
|
| 25 |
-
2. Click "Create new Space"
|
| 26 |
-
3. Configure your Space:
|
| 27 |
-
- **Name**: `Datasourceforcryptocurrency` (or your preferred name)
|
| 28 |
-
- **License**: Choose appropriate license (e.g., MIT)
|
| 29 |
-
- **SDK**: Select **Docker**
|
| 30 |
-
- **Visibility**: Public or Private (your choice)
|
| 31 |
-
4. Click "Create Space"
|
| 32 |
-
|
| 33 |
-
### 2. Clone Your Space Repository
|
| 34 |
-
|
| 35 |
-
```bash
|
| 36 |
-
# Clone your newly created space
|
| 37 |
-
git clone https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
|
| 38 |
-
cd YOUR_SPACE_NAME
|
| 39 |
-
```
|
| 40 |
-
|
| 41 |
-
### 3. Copy Application Files
|
| 42 |
-
|
| 43 |
-
Copy all files from this repository to your Space directory:
|
| 44 |
-
|
| 45 |
-
```bash
|
| 46 |
-
# Copy all files (adjust paths as needed)
|
| 47 |
-
cp -r /path/to/crypto-dt-source/* .
|
| 48 |
-
```
|
| 49 |
-
|
| 50 |
-
**Essential files for HuggingFace Spaces:**
|
| 51 |
-
- `Dockerfile` - Docker configuration optimized for HF Spaces
|
| 52 |
-
- `requirements.txt` - Python dependencies including transformers
|
| 53 |
-
- `app.py` - Main FastAPI application
|
| 54 |
-
- `config.js` - Frontend configuration with environment detection
|
| 55 |
-
- `*.html` - UI files (index.html, hf_console.html, etc.)
|
| 56 |
-
- All backend directories (`api/`, `backend/`, `monitoring/`, etc.)
|
| 57 |
-
|
| 58 |
-
### 4. Configure Environment Variables (Optional but Recommended)
|
| 59 |
-
|
| 60 |
-
In your HuggingFace Space settings, add these secrets:
|
| 61 |
-
|
| 62 |
-
**Required:**
|
| 63 |
-
- `HUGGINGFACE_TOKEN` - Your HF token for accessing models (optional if using public models)
|
| 64 |
-
|
| 65 |
-
**Optional API Keys (for enhanced data collection):**
|
| 66 |
-
- `ETHERSCAN_KEY_1` - Etherscan API key
|
| 67 |
-
- `COINMARKETCAP_KEY_1` - CoinMarketCap API key
|
| 68 |
-
- `NEWSAPI_KEY` - NewsAPI key
|
| 69 |
-
- `CRYPTOCOMPARE_KEY` - CryptoCompare API key
|
| 70 |
-
|
| 71 |
-
**HuggingFace Configuration:**
|
| 72 |
-
- `ENABLE_SENTIMENT=true` - Enable sentiment analysis
|
| 73 |
-
- `SENTIMENT_SOCIAL_MODEL=ElKulako/cryptobert` - Social sentiment model
|
| 74 |
-
- `SENTIMENT_NEWS_MODEL=kk08/CryptoBERT` - News sentiment model
|
| 75 |
-
- `HF_REGISTRY_REFRESH_SEC=21600` - Registry refresh interval (6 hours)
|
| 76 |
-
|
| 77 |
-
### 5. Push to HuggingFace
|
| 78 |
-
|
| 79 |
-
```bash
|
| 80 |
-
# Add all files
|
| 81 |
-
git add .
|
| 82 |
-
|
| 83 |
-
# Commit changes
|
| 84 |
-
git commit -m "Initial deployment of Crypto API Monitor"
|
| 85 |
-
|
| 86 |
-
# Push to HuggingFace
|
| 87 |
-
git push
|
| 88 |
-
```
|
| 89 |
-
|
| 90 |
-
### 6. Wait for Build
|
| 91 |
-
|
| 92 |
-
HuggingFace Spaces will automatically:
|
| 93 |
-
1. Build your Docker image (takes 5-10 minutes)
|
| 94 |
-
2. Download required ML models
|
| 95 |
-
3. Start the application on port 7860
|
| 96 |
-
4. Run health checks
|
| 97 |
-
|
| 98 |
-
Monitor the build logs in your Space's "Logs" tab.
|
| 99 |
-
|
| 100 |
-
### 7. Access Your Application
|
| 101 |
-
|
| 102 |
-
Once deployed, your application will be available at:
|
| 103 |
-
```
|
| 104 |
-
https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
|
| 105 |
-
```
|
| 106 |
-
|
| 107 |
-
## Features Available in HuggingFace Spaces
|
| 108 |
-
|
| 109 |
-
### 🎯 Real-Time Dashboard
|
| 110 |
-
- Access the main dashboard at the root URL
|
| 111 |
-
- Real-time WebSocket updates for all metrics
|
| 112 |
-
- Provider health monitoring
|
| 113 |
-
- System status and analytics
|
| 114 |
-
|
| 115 |
-
### 🤗 HuggingFace Console
|
| 116 |
-
- Access at `/hf_console.html`
|
| 117 |
-
- Test HF model registry
|
| 118 |
-
- Run sentiment analysis
|
| 119 |
-
- Search crypto-related models and datasets
|
| 120 |
-
|
| 121 |
-
### 📊 API Documentation
|
| 122 |
-
- Swagger UI: `/docs`
|
| 123 |
-
- ReDoc: `/redoc`
|
| 124 |
-
- API Info: `/api-info`
|
| 125 |
-
|
| 126 |
-
### 🔌 WebSocket Endpoints
|
| 127 |
-
All WebSocket endpoints are available for real-time data:
|
| 128 |
-
- `/ws` - Master WebSocket endpoint
|
| 129 |
-
- `/ws/market_data` - Market data updates
|
| 130 |
-
- `/ws/news` - News updates
|
| 131 |
-
- `/ws/sentiment` - Sentiment analysis updates
|
| 132 |
-
- `/ws/health` - Health monitoring
|
| 133 |
-
- `/ws/huggingface` - HF integration updates
|
| 134 |
-
|
| 135 |
-
## Local Development & Testing
|
| 136 |
-
|
| 137 |
-
### Using Docker Compose
|
| 138 |
-
|
| 139 |
-
```bash
|
| 140 |
-
# Build and start the application
|
| 141 |
-
docker-compose up --build
|
| 142 |
-
|
| 143 |
-
# Access at http://localhost:7860
|
| 144 |
-
```
|
| 145 |
-
|
| 146 |
-
### Using Docker Directly
|
| 147 |
-
|
| 148 |
-
```bash
|
| 149 |
-
# Build the image
|
| 150 |
-
docker build -t crypto-api-monitor .
|
| 151 |
-
|
| 152 |
-
# Run the container
|
| 153 |
-
docker run -p 7860:7860 \
|
| 154 |
-
-e HUGGINGFACE_TOKEN=your_token \
|
| 155 |
-
-e ENABLE_SENTIMENT=true \
|
| 156 |
-
-v $(pwd)/data:/app/data \
|
| 157 |
-
crypto-api-monitor
|
| 158 |
-
```
|
| 159 |
-
|
| 160 |
-
### Using Python Directly
|
| 161 |
-
|
| 162 |
-
```bash
|
| 163 |
-
# Install dependencies
|
| 164 |
-
pip install -r requirements.txt
|
| 165 |
-
|
| 166 |
-
# Set environment variables
|
| 167 |
-
export ENABLE_SENTIMENT=true
|
| 168 |
-
export HUGGINGFACE_TOKEN=your_token
|
| 169 |
-
|
| 170 |
-
# Run the application
|
| 171 |
-
python app.py
|
| 172 |
-
```
|
| 173 |
-
|
| 174 |
-
## Configuration
|
| 175 |
-
|
| 176 |
-
### Frontend Configuration (`config.js`)
|
| 177 |
-
|
| 178 |
-
The frontend automatically detects the environment:
|
| 179 |
-
- **HuggingFace Spaces**: Uses relative URLs with Space origin
|
| 180 |
-
- **Localhost**: Uses `http://localhost:7860`
|
| 181 |
-
- **Custom Deployment**: Uses current window origin
|
| 182 |
-
|
| 183 |
-
No manual configuration needed!
|
| 184 |
-
|
| 185 |
-
### Backend Configuration
|
| 186 |
-
|
| 187 |
-
Edit `.env` or set environment variables:
|
| 188 |
-
|
| 189 |
-
```bash
|
| 190 |
-
# HuggingFace
|
| 191 |
-
HUGGINGFACE_TOKEN=your_token_here
|
| 192 |
-
ENABLE_SENTIMENT=true
|
| 193 |
-
SENTIMENT_SOCIAL_MODEL=ElKulako/cryptobert
|
| 194 |
-
SENTIMENT_NEWS_MODEL=kk08/CryptoBERT
|
| 195 |
-
HF_REGISTRY_REFRESH_SEC=21600
|
| 196 |
-
HF_HTTP_TIMEOUT=8.0
|
| 197 |
-
|
| 198 |
-
# API Keys (optional)
|
| 199 |
-
ETHERSCAN_KEY_1=your_key
|
| 200 |
-
COINMARKETCAP_KEY_1=your_key
|
| 201 |
-
NEWSAPI_KEY=your_key
|
| 202 |
-
```
|
| 203 |
-
|
| 204 |
-
## Architecture
|
| 205 |
-
|
| 206 |
-
```
|
| 207 |
-
┌─────────────────────────────────────────────────┐
|
| 208 |
-
│ HuggingFace Spaces (Docker) │
|
| 209 |
-
├─────────────────────────────────────────────────┤
|
| 210 |
-
│ │
|
| 211 |
-
│ Frontend (HTML/JS) │
|
| 212 |
-
│ ├── config.js (auto-detects environment) │
|
| 213 |
-
│ ├── index.html (main dashboard) │
|
| 214 |
-
│ └── hf_console.html (HF integration UI) │
|
| 215 |
-
│ │
|
| 216 |
-
│ Backend (FastAPI) │
|
| 217 |
-
│ ├── app.py (main application) │
|
| 218 |
-
│ ├── WebSocket Manager (real-time updates) │
|
| 219 |
-
│ ├── HF Integration (sentiment analysis) │
|
| 220 |
-
│ ├── Data Collectors (200+ APIs) │
|
| 221 |
-
│ └── SQLite Database (persistent storage) │
|
| 222 |
-
│ │
|
| 223 |
-
│ ML Models (HuggingFace Transformers) │
|
| 224 |
-
│ ├── ElKulako/cryptobert │
|
| 225 |
-
│ └── kk08/CryptoBERT │
|
| 226 |
-
│ │
|
| 227 |
-
└─────────────────────────────────────────────────┘
|
| 228 |
-
```
|
| 229 |
-
|
| 230 |
-
## Troubleshooting
|
| 231 |
-
|
| 232 |
-
### Build Fails
|
| 233 |
-
|
| 234 |
-
1. Check Docker logs in HF Spaces
|
| 235 |
-
2. Verify `requirements.txt` has all dependencies
|
| 236 |
-
3. Ensure Dockerfile uses Python 3.10
|
| 237 |
-
4. Check for syntax errors in Python files
|
| 238 |
-
|
| 239 |
-
### Application Won't Start
|
| 240 |
-
|
| 241 |
-
1. Check health endpoint: `https://your-space-url/health`
|
| 242 |
-
2. Review application logs in HF Spaces
|
| 243 |
-
3. Verify port 7860 is exposed in Dockerfile
|
| 244 |
-
4. Check environment variables are set correctly
|
| 245 |
-
|
| 246 |
-
### WebSocket Connections Fail
|
| 247 |
-
|
| 248 |
-
1. Ensure your Space URL uses HTTPS
|
| 249 |
-
2. WebSockets automatically upgrade to WSS on HTTPS
|
| 250 |
-
3. Check browser console for connection errors
|
| 251 |
-
4. Verify CORS settings in `app.py`
|
| 252 |
-
|
| 253 |
-
### Sentiment Analysis Not Working
|
| 254 |
-
|
| 255 |
-
1. Set `HUGGINGFACE_TOKEN` in Space secrets
|
| 256 |
-
2. Verify models are accessible: `ElKulako/cryptobert`, `kk08/CryptoBERT`
|
| 257 |
-
3. Check HF console at `/hf_console.html`
|
| 258 |
-
4. Review logs for model download errors
|
| 259 |
-
|
| 260 |
-
### Performance Issues
|
| 261 |
-
|
| 262 |
-
1. Increase Space hardware tier (if available)
|
| 263 |
-
2. Reduce number of concurrent API monitors
|
| 264 |
-
3. Adjust `HF_REGISTRY_REFRESH_SEC` to longer interval
|
| 265 |
-
4. Consider disabling sentiment analysis if not needed
|
| 266 |
-
|
| 267 |
-
## Resource Requirements
|
| 268 |
-
|
| 269 |
-
**Minimum (Free Tier):**
|
| 270 |
-
- 2 CPU cores
|
| 271 |
-
- 2GB RAM
|
| 272 |
-
- 1GB disk space
|
| 273 |
-
|
| 274 |
-
**Recommended:**
|
| 275 |
-
- 4 CPU cores
|
| 276 |
-
- 4GB RAM
|
| 277 |
-
- 2GB disk space
|
| 278 |
-
- For better ML model performance
|
| 279 |
-
|
| 280 |
-
## Updating Your Space
|
| 281 |
-
|
| 282 |
-
```bash
|
| 283 |
-
# Pull latest changes
|
| 284 |
-
git pull
|
| 285 |
-
|
| 286 |
-
# Make your modifications
|
| 287 |
-
# ...
|
| 288 |
-
|
| 289 |
-
# Commit and push
|
| 290 |
-
git add .
|
| 291 |
-
git commit -m "Update: description of changes"
|
| 292 |
-
git push
|
| 293 |
-
```
|
| 294 |
-
|
| 295 |
-
HuggingFace will automatically rebuild and redeploy.
|
| 296 |
-
|
| 297 |
-
## Security Best Practices
|
| 298 |
-
|
| 299 |
-
1. **Use HF Secrets** for sensitive data (API keys, tokens)
|
| 300 |
-
2. **Don't commit** `.env` files with actual keys
|
| 301 |
-
3. **Review API keys** permissions (read-only when possible)
|
| 302 |
-
4. **Monitor usage** of external APIs to avoid rate limits
|
| 303 |
-
5. **Keep dependencies updated** for security patches
|
| 304 |
-
|
| 305 |
-
## Advanced Configuration
|
| 306 |
-
|
| 307 |
-
### Custom ML Models
|
| 308 |
-
|
| 309 |
-
To use custom sentiment analysis models:
|
| 310 |
-
|
| 311 |
-
```bash
|
| 312 |
-
# Set environment variables in HF Spaces
|
| 313 |
-
SENTIMENT_SOCIAL_MODEL=your-username/your-model
|
| 314 |
-
SENTIMENT_NEWS_MODEL=your-username/another-model
|
| 315 |
-
```
|
| 316 |
-
|
| 317 |
-
### Custom Port (Not Recommended for HF Spaces)
|
| 318 |
-
|
| 319 |
-
HuggingFace Spaces requires port 7860. Don't change unless deploying elsewhere.
|
| 320 |
-
|
| 321 |
-
### Multiple Workers
|
| 322 |
-
|
| 323 |
-
Edit Dockerfile CMD:
|
| 324 |
-
```dockerfile
|
| 325 |
-
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860", "--workers", "2"]
|
| 326 |
-
```
|
| 327 |
-
|
| 328 |
-
**Note**: More workers = more memory usage. Adjust based on Space tier.
|
| 329 |
-
|
| 330 |
-
## Support & Resources
|
| 331 |
-
|
| 332 |
-
- **HuggingFace Docs**: https://huggingface.co/docs/hub/spaces
|
| 333 |
-
- **FastAPI Docs**: https://fastapi.tiangolo.com/
|
| 334 |
-
- **Transformers Docs**: https://huggingface.co/docs/transformers/
|
| 335 |
-
- **Project Issues**: https://github.com/nimazasinich/crypto-dt-source/issues
|
| 336 |
-
|
| 337 |
-
## License
|
| 338 |
-
|
| 339 |
-
[Specify your license here]
|
| 340 |
-
|
| 341 |
-
## Contributing
|
| 342 |
-
|
| 343 |
-
Contributions are welcome! Please read the contributing guidelines before submitting PRs.
|
| 344 |
-
|
| 345 |
-
---
|
| 346 |
-
|
| 347 |
-
**Need help?** Open an issue or contact the maintainers.
|
| 348 |
-
|
| 349 |
-
**Enjoy your crypto monitoring dashboard on HuggingFace Spaces! 🚀**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HUGGINGFACE_DIAGNOSTIC_GUIDE.md
CHANGED
|
@@ -137,6 +137,12 @@ git pull origin main
|
|
| 137 |
rm -rf node_modules package-lock.json
|
| 138 |
npm install
|
| 139 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
# Verify critical packages
|
| 141 |
npm list typescript
|
| 142 |
npm list vite
|
|
|
|
| 137 |
rm -rf node_modules package-lock.json
|
| 138 |
npm install
|
| 139 |
|
| 140 |
+
# Install backend dependencies (FastAPI server used on HuggingFace)
|
| 141 |
+
pip install -r requirements.txt
|
| 142 |
+
|
| 143 |
+
# Verify critical backend packages
|
| 144 |
+
pip show fastapi uvicorn slowapi sqlalchemy PyJWT || true
|
| 145 |
+
|
| 146 |
# Verify critical packages
|
| 147 |
npm list typescript
|
| 148 |
npm list vite
|
README.md
CHANGED
|
@@ -1,9 +1,3 @@
|
|
| 1 |
-
---
|
| 2 |
-
sdk: docker
|
| 3 |
-
colorFrom: red
|
| 4 |
-
colorTo: red
|
| 5 |
-
sdk_version: 5.49.1
|
| 6 |
-
---
|
| 7 |
# Crypto-DT-Source
|
| 8 |
|
| 9 |
<div align="center">
|
|
@@ -492,4 +486,4 @@ This project is licensed under the **MIT License** - see the [LICENSE](LICENSE)
|
|
| 492 |
|
| 493 |
[Documentation](docs/INDEX.md) • [Quick Start](QUICK_START.md) • [فارسی](docs/persian/README_FA.md) • [Changelog](CHANGELOG.md)
|
| 494 |
|
| 495 |
-
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# Crypto-DT-Source
|
| 2 |
|
| 3 |
<div align="center">
|
|
|
|
| 486 |
|
| 487 |
[Documentation](docs/INDEX.md) • [Quick Start](QUICK_START.md) • [فارسی](docs/persian/README_FA.md) • [Changelog](CHANGELOG.md)
|
| 488 |
|
| 489 |
+
</div>
|
README_DEPLOYMENT.md
CHANGED
|
@@ -1,260 +1,258 @@
|
|
| 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 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
1.
|
| 170 |
-
2.
|
| 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 |
-
**Last Updated**: 2025-11-13
|
| 260 |
-
**Status**: ✅ PRODUCTION READY
|
|
|
|
| 1 |
+
# 🚀 Hugging Face Spaces Deployment Guide
|
| 2 |
+
|
| 3 |
+
This repository is **production-ready** for deployment on Hugging Face Spaces using Docker runtime.
|
| 4 |
+
|
| 5 |
+
## ✅ Pre-Deployment Checklist
|
| 6 |
+
|
| 7 |
+
All requirements are already configured:
|
| 8 |
+
|
| 9 |
+
- ✅ **FastAPI app**: `api_server_extended.py` defines `app = FastAPI(...)`
|
| 10 |
+
- ✅ **Health endpoint**: `/health` returns service status
|
| 11 |
+
- ✅ **Dockerfile**: Configured with correct CMD for uvicorn
|
| 12 |
+
- ✅ **Requirements**: All dependencies listed in `requirements.txt`
|
| 13 |
+
- ✅ **Port handling**: Supports `${PORT}` environment variable
|
| 14 |
+
- ✅ **Resilient startup**: Runs in degraded mode if some services fail
|
| 15 |
+
|
| 16 |
+
## 🎯 Deployment Steps
|
| 17 |
+
|
| 18 |
+
### 1. Create a New Space on Hugging Face
|
| 19 |
+
|
| 20 |
+
1. Go to https://huggingface.co/spaces
|
| 21 |
+
2. Click **"Create new Space"**
|
| 22 |
+
3. Configure:
|
| 23 |
+
- **Space name**: `crypto-monitor-api` (or your choice)
|
| 24 |
+
- **License**: Choose appropriate license
|
| 25 |
+
- **SDK**: Select **Docker**
|
| 26 |
+
- **Hardware**: CPU Basic (minimum) or CPU Upgrade (recommended)
|
| 27 |
+
- **Visibility**: Public or Private
|
| 28 |
+
|
| 29 |
+
### 2. Push Repository to Space
|
| 30 |
+
|
| 31 |
+
```bash
|
| 32 |
+
# Clone your new Space
|
| 33 |
+
git clone https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
|
| 34 |
+
cd YOUR_SPACE_NAME
|
| 35 |
+
|
| 36 |
+
# Copy all files from this repository
|
| 37 |
+
cp -r /path/to/crypto-dt-source-main/* .
|
| 38 |
+
|
| 39 |
+
# Add and commit
|
| 40 |
+
git add .
|
| 41 |
+
git commit -m "Initial deployment of Crypto Monitor API"
|
| 42 |
+
|
| 43 |
+
# Push to Hugging Face
|
| 44 |
+
git push
|
| 45 |
+
```
|
| 46 |
+
|
| 47 |
+
### 3. Configure Space Settings (Optional)
|
| 48 |
+
|
| 49 |
+
In your Space settings, you can add these environment variables:
|
| 50 |
+
|
| 51 |
+
- `PORT` - Port number (default: 7860 for HF, 8000 for local)
|
| 52 |
+
- `ENABLE_AUTO_DISCOVERY` - Enable auto-discovery service (default: false)
|
| 53 |
+
- Add any API keys as **Repository secrets** (not in code!)
|
| 54 |
+
|
| 55 |
+
### 4. Monitor Deployment
|
| 56 |
+
|
| 57 |
+
1. Go to your Space page
|
| 58 |
+
2. Check the **Logs** tab for build progress
|
| 59 |
+
3. Wait for "Running" status (usually 2-5 minutes)
|
| 60 |
+
4. Access your API at: `https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space`
|
| 61 |
+
|
| 62 |
+
## 📊 API Endpoints
|
| 63 |
+
|
| 64 |
+
Once deployed, your API will be available at:
|
| 65 |
+
|
| 66 |
+
### Core Endpoints
|
| 67 |
+
- **Root**: `https://your-space.hf.space/`
|
| 68 |
+
- **API Docs**: `https://your-space.hf.space/docs` (Interactive Swagger UI)
|
| 69 |
+
- **Health Check**: `https://your-space.hf.space/health`
|
| 70 |
+
- **Status**: `https://your-space.hf.space/api/status`
|
| 71 |
+
|
| 72 |
+
### Provider Management
|
| 73 |
+
- `GET /api/providers` - List all crypto data providers
|
| 74 |
+
- `GET /api/providers/{id}` - Get provider details
|
| 75 |
+
- `POST /api/providers/{id}/health-check` - Check provider health
|
| 76 |
+
- `GET /api/providers/category/{category}` - Filter by category
|
| 77 |
+
|
| 78 |
+
### Pool Management
|
| 79 |
+
- `GET /api/pools` - List all provider pools
|
| 80 |
+
- `POST /api/pools` - Create new pool
|
| 81 |
+
- `POST /api/pools/{id}/members` - Add provider to pool
|
| 82 |
+
- `POST /api/pools/{id}/rotate` - Rotate pool providers
|
| 83 |
+
|
| 84 |
+
### Real-time Updates
|
| 85 |
+
- `WS /ws` - WebSocket connection for live updates
|
| 86 |
+
|
| 87 |
+
### Monitoring & Diagnostics
|
| 88 |
+
- `GET /api/stats` - System statistics
|
| 89 |
+
- `GET /api/logs` - Application logs
|
| 90 |
+
- `POST /api/diagnostics/run` - Run diagnostics
|
| 91 |
+
|
| 92 |
+
See `/docs` for complete API documentation with interactive testing.
|
| 93 |
+
|
| 94 |
+
## 🧪 Local Testing
|
| 95 |
+
|
| 96 |
+
### Test with Docker (Recommended)
|
| 97 |
+
|
| 98 |
+
```bash
|
| 99 |
+
# Build the image
|
| 100 |
+
docker build -t crypto-monitor-test .
|
| 101 |
+
|
| 102 |
+
# Run the container
|
| 103 |
+
docker run -p 8000:8000 crypto-monitor-test
|
| 104 |
+
|
| 105 |
+
# Test health endpoint
|
| 106 |
+
curl http://localhost:8000/health
|
| 107 |
+
|
| 108 |
+
# Access API docs
|
| 109 |
+
open http://localhost:8000/docs
|
| 110 |
+
```
|
| 111 |
+
|
| 112 |
+
### Test with Python
|
| 113 |
+
|
| 114 |
+
```bash
|
| 115 |
+
# Install dependencies
|
| 116 |
+
pip install -r requirements.txt
|
| 117 |
+
|
| 118 |
+
# Run locally
|
| 119 |
+
python main.py
|
| 120 |
+
|
| 121 |
+
# Or with uvicorn directly
|
| 122 |
+
uvicorn api_server_extended:app --host 0.0.0.0 --port 8000 --reload
|
| 123 |
+
```
|
| 124 |
+
|
| 125 |
+
## 🔍 Troubleshooting
|
| 126 |
+
|
| 127 |
+
### Build Fails
|
| 128 |
+
|
| 129 |
+
**Check logs for specific errors:**
|
| 130 |
+
- Missing dependencies? Verify `requirements.txt`
|
| 131 |
+
- Import errors? Ensure all local modules exist
|
| 132 |
+
- System dependencies? Check Dockerfile `apt-get install` section
|
| 133 |
+
|
| 134 |
+
**Common fixes:**
|
| 135 |
+
```bash
|
| 136 |
+
# Rebuild without cache
|
| 137 |
+
docker build --no-cache -t crypto-monitor-test .
|
| 138 |
+
|
| 139 |
+
# Check for syntax errors
|
| 140 |
+
python -m py_compile api_server_extended.py
|
| 141 |
+
```
|
| 142 |
+
|
| 143 |
+
### Container Starts but Health Check Fails
|
| 144 |
+
|
| 145 |
+
**Increase startup time:**
|
| 146 |
+
Edit `Dockerfile` and increase `start-period`:
|
| 147 |
+
```dockerfile
|
| 148 |
+
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
|
| 149 |
+
CMD curl -f http://localhost:${PORT:-8000}/health || exit 1
|
| 150 |
+
```
|
| 151 |
+
|
| 152 |
+
**Check logs:**
|
| 153 |
+
```bash
|
| 154 |
+
docker logs <container_id>
|
| 155 |
+
```
|
| 156 |
+
|
| 157 |
+
### Service Runs in Degraded Mode
|
| 158 |
+
|
| 159 |
+
This is **normal** if:
|
| 160 |
+
- Some external APIs are unavailable
|
| 161 |
+
- Network connectivity is limited
|
| 162 |
+
- Optional services fail to start
|
| 163 |
+
|
| 164 |
+
The service will still work with available providers. Check `/health` endpoint for details.
|
| 165 |
+
|
| 166 |
+
### WebSocket Connection Issues
|
| 167 |
+
|
| 168 |
+
If WebSocket connections fail:
|
| 169 |
+
1. Ensure your client uses `wss://` (not `ws://`) for HTTPS spaces
|
| 170 |
+
2. Check CORS settings in `api_server_extended.py`
|
| 171 |
+
3. Verify firewall/proxy settings
|
| 172 |
+
|
| 173 |
+
## 📈 Performance Optimization
|
| 174 |
+
|
| 175 |
+
### For Better Performance
|
| 176 |
+
|
| 177 |
+
1. **Upgrade Hardware**: Use CPU Upgrade or GPU in Space settings
|
| 178 |
+
2. **Disable Auto-Discovery**: Set `ENABLE_AUTO_DISCOVERY=false` (already default)
|
| 179 |
+
3. **Reduce Provider Count**: Edit config files to monitor fewer providers
|
| 180 |
+
4. **Enable Caching**: Already enabled by default
|
| 181 |
+
|
| 182 |
+
### Resource Usage
|
| 183 |
+
|
| 184 |
+
- **Memory**: ~2-4 GB (depends on active providers)
|
| 185 |
+
- **CPU**: Low to moderate (spikes during health checks)
|
| 186 |
+
- **Storage**: ~500 MB (includes models and data)
|
| 187 |
+
|
| 188 |
+
## 🔐 Security Best Practices
|
| 189 |
+
|
| 190 |
+
1. **Never commit API keys** - Use HF Repository secrets
|
| 191 |
+
2. **Use HTTPS** - Hugging Face provides this automatically
|
| 192 |
+
3. **Rate limiting** - Already implemented via `slowapi`
|
| 193 |
+
4. **CORS** - Configured to allow all origins (adjust if needed)
|
| 194 |
+
|
| 195 |
+
## 📝 Configuration Files
|
| 196 |
+
|
| 197 |
+
### Required Files (Already Present)
|
| 198 |
+
- `Dockerfile` - Container configuration
|
| 199 |
+
- `requirements.txt` - Python dependencies
|
| 200 |
+
- `api_server_extended.py` - Main FastAPI application
|
| 201 |
+
- `.dockerignore` - Files to exclude from image
|
| 202 |
+
|
| 203 |
+
### Optional Configuration
|
| 204 |
+
- `.env.example` - Environment variable template
|
| 205 |
+
- `providers_config_*.json` - Provider configurations
|
| 206 |
+
- `crypto_resources_*.json` - Resource definitions
|
| 207 |
+
|
| 208 |
+
## 🎓 Features
|
| 209 |
+
|
| 210 |
+
This deployment includes:
|
| 211 |
+
|
| 212 |
+
✅ **200+ Crypto Data Providers** - Comprehensive coverage
|
| 213 |
+
✅ **Provider Pools** - Load balancing and failover
|
| 214 |
+
✅ **Real-time WebSocket** - Live updates
|
| 215 |
+
✅ **Health Monitoring** - Automatic health checks
|
| 216 |
+
✅ **Auto-Discovery** - Find new data sources (optional)
|
| 217 |
+
✅ **Diagnostics** - Built-in troubleshooting
|
| 218 |
+
✅ **Logging System** - Comprehensive logging
|
| 219 |
+
✅ **Resource Management** - Import/export configs
|
| 220 |
+
✅ **Rate Limiting** - Prevent abuse
|
| 221 |
+
✅ **CORS Support** - Cross-origin requests
|
| 222 |
+
✅ **API Documentation** - Interactive Swagger UI
|
| 223 |
+
|
| 224 |
+
## 🆘 Support
|
| 225 |
+
|
| 226 |
+
### Check Service Status
|
| 227 |
+
```bash
|
| 228 |
+
# Health check
|
| 229 |
+
curl https://your-space.hf.space/health
|
| 230 |
+
|
| 231 |
+
# Detailed status
|
| 232 |
+
curl https://your-space.hf.space/api/status
|
| 233 |
+
|
| 234 |
+
# Run diagnostics
|
| 235 |
+
curl -X POST https://your-space.hf.space/api/diagnostics/run
|
| 236 |
+
```
|
| 237 |
+
|
| 238 |
+
### Common Issues
|
| 239 |
+
|
| 240 |
+
1. **Space shows "Building"** - Wait 2-5 minutes for first build
|
| 241 |
+
2. **Space shows "Runtime Error"** - Check logs tab for details
|
| 242 |
+
3. **API returns 503** - Service starting up, wait 30-60 seconds
|
| 243 |
+
4. **Slow responses** - Upgrade hardware or reduce provider count
|
| 244 |
+
|
| 245 |
+
### Get Help
|
| 246 |
+
|
| 247 |
+
- Check `/api/diagnostics/run` for automatic issue detection
|
| 248 |
+
- Review Space logs for error messages
|
| 249 |
+
- Test locally with Docker to isolate issues
|
| 250 |
+
- Check Hugging Face Spaces documentation
|
| 251 |
+
|
| 252 |
+
## 📄 License
|
| 253 |
+
|
| 254 |
+
See LICENSE file for details.
|
| 255 |
+
|
| 256 |
+
---
|
| 257 |
+
|
| 258 |
+
**Ready to deploy!** Follow the steps above to get your Crypto Monitor API running on Hugging Face Spaces.
|
|
|
|
|
|
README_HUGGINGFACE.md
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Crypto Monitor Extended API - Hugging Face Spaces Deployment
|
| 2 |
+
|
| 3 |
+
This repository is configured to run as a **Docker Space** on Hugging Face.
|
| 4 |
+
|
| 5 |
+
## 🚀 Quick Deploy to Hugging Face Spaces
|
| 6 |
+
|
| 7 |
+
1. **Create a new Space** on Hugging Face
|
| 8 |
+
2. **Select Docker as the SDK**
|
| 9 |
+
3. **Clone this repository** or push it to your Space
|
| 10 |
+
4. The Space will automatically build and deploy
|
| 11 |
+
|
| 12 |
+
## 📋 Configuration
|
| 13 |
+
|
| 14 |
+
### Space Settings
|
| 15 |
+
|
| 16 |
+
- **SDK**: Docker
|
| 17 |
+
- **Hardware**: CPU Basic (or higher for better performance)
|
| 18 |
+
- **Visibility**: Public or Private (your choice)
|
| 19 |
+
|
| 20 |
+
### Environment Variables (Optional)
|
| 21 |
+
|
| 22 |
+
You can set these in your Space settings under "Repository secrets":
|
| 23 |
+
|
| 24 |
+
- `PORT` - Port number (default: 7860 for HF Spaces)
|
| 25 |
+
- `ENABLE_AUTO_DISCOVERY` - Enable auto-discovery service (default: false)
|
| 26 |
+
|
| 27 |
+
## 🔧 Technical Details
|
| 28 |
+
|
| 29 |
+
### Main Application
|
| 30 |
+
|
| 31 |
+
The FastAPI application is defined in `api_server_extended.py` and exposes:
|
| 32 |
+
|
| 33 |
+
- **Main API**: `http://your-space.hf.space/`
|
| 34 |
+
- **API Docs**: `http://your-space.hf.space/docs`
|
| 35 |
+
- **Health Check**: `http://your-space.hf.space/health`
|
| 36 |
+
- **WebSocket**: `ws://your-space.hf.space/ws`
|
| 37 |
+
|
| 38 |
+
### Features
|
| 39 |
+
|
| 40 |
+
✅ **Provider Management** - Manage 200+ crypto data providers
|
| 41 |
+
✅ **Pool Management** - Create and manage provider pools with rotation strategies
|
| 42 |
+
✅ **Real-time WebSocket** - Live updates and monitoring
|
| 43 |
+
✅ **Health Monitoring** - Automatic health checks for all providers
|
| 44 |
+
✅ **Auto-Discovery** - Discover new crypto data sources automatically
|
| 45 |
+
✅ **Diagnostics** - Built-in diagnostics and auto-repair
|
| 46 |
+
✅ **Logging** - Comprehensive logging system
|
| 47 |
+
✅ **Resource Management** - Import/export provider configurations
|
| 48 |
+
|
| 49 |
+
### API Endpoints
|
| 50 |
+
|
| 51 |
+
#### Core Endpoints
|
| 52 |
+
- `GET /` - Main dashboard
|
| 53 |
+
- `GET /health` - Health check
|
| 54 |
+
- `GET /api/status` - System status
|
| 55 |
+
- `GET /api/stats` - Statistics
|
| 56 |
+
|
| 57 |
+
#### Provider Management
|
| 58 |
+
- `GET /api/providers` - List all providers
|
| 59 |
+
- `GET /api/providers/{id}` - Get provider details
|
| 60 |
+
- `POST /api/providers/{id}/health-check` - Check provider health
|
| 61 |
+
- `GET /api/providers/category/{category}` - Get providers by category
|
| 62 |
+
|
| 63 |
+
#### Pool Management
|
| 64 |
+
- `GET /api/pools` - List all pools
|
| 65 |
+
- `POST /api/pools` - Create new pool
|
| 66 |
+
- `POST /api/pools/{id}/members` - Add member to pool
|
| 67 |
+
- `POST /api/pools/{id}/rotate` - Rotate pool
|
| 68 |
+
|
| 69 |
+
#### WebSocket
|
| 70 |
+
- `WS /ws` - WebSocket connection for real-time updates
|
| 71 |
+
|
| 72 |
+
See `/docs` for complete API documentation.
|
| 73 |
+
|
| 74 |
+
## 🏗️ Local Development
|
| 75 |
+
|
| 76 |
+
### Using Docker
|
| 77 |
+
|
| 78 |
+
```bash
|
| 79 |
+
# Build the image
|
| 80 |
+
docker build -t crypto-monitor .
|
| 81 |
+
|
| 82 |
+
# Run the container
|
| 83 |
+
docker run -p 8000:8000 crypto-monitor
|
| 84 |
+
```
|
| 85 |
+
|
| 86 |
+
### Using Python directly
|
| 87 |
+
|
| 88 |
+
```bash
|
| 89 |
+
# Install dependencies
|
| 90 |
+
pip install -r requirements.txt
|
| 91 |
+
|
| 92 |
+
# Run the server
|
| 93 |
+
python main.py
|
| 94 |
+
```
|
| 95 |
+
|
| 96 |
+
Or with uvicorn directly:
|
| 97 |
+
|
| 98 |
+
```bash
|
| 99 |
+
uvicorn api_server_extended:app --host 0.0.0.0 --port 8000 --reload
|
| 100 |
+
```
|
| 101 |
+
|
| 102 |
+
## 📊 Monitoring
|
| 103 |
+
|
| 104 |
+
Once deployed, you can monitor your Space:
|
| 105 |
+
|
| 106 |
+
1. Check the **Logs** tab in your Space
|
| 107 |
+
2. Visit `/health` endpoint for health status
|
| 108 |
+
3. Visit `/api/status` for detailed system status
|
| 109 |
+
4. Use `/docs` for interactive API documentation
|
| 110 |
+
|
| 111 |
+
## 🔍 Troubleshooting
|
| 112 |
+
|
| 113 |
+
### Space not starting?
|
| 114 |
+
|
| 115 |
+
1. Check the **Logs** tab for error messages
|
| 116 |
+
2. Verify all required files are present
|
| 117 |
+
3. Ensure `api_server_extended.py` defines `app = FastAPI(...)`
|
| 118 |
+
4. Check that all dependencies in `requirements.txt` are valid
|
| 119 |
+
|
| 120 |
+
### Health check failing?
|
| 121 |
+
|
| 122 |
+
The health check runs after 40 seconds of startup. If it fails:
|
| 123 |
+
|
| 124 |
+
1. Increase `start-period` in Dockerfile HEALTHCHECK
|
| 125 |
+
2. Check if the app is listening on the correct PORT
|
| 126 |
+
3. Verify `/health` endpoint returns 200 OK
|
| 127 |
+
|
| 128 |
+
### Performance issues?
|
| 129 |
+
|
| 130 |
+
1. Upgrade to better hardware (CPU or GPU)
|
| 131 |
+
2. Disable auto-discovery: Set `ENABLE_AUTO_DISCOVERY=false`
|
| 132 |
+
3. Reduce the number of providers being monitored
|
| 133 |
+
|
| 134 |
+
## 📝 Notes
|
| 135 |
+
|
| 136 |
+
- The application uses **no API keys** by default - all data sources are free
|
| 137 |
+
- SQLite database is used for persistence (stored in `/app/data/`)
|
| 138 |
+
- Logs are stored in `/app/logs/`
|
| 139 |
+
- The app runs in **degraded mode** if some providers are unavailable
|
| 140 |
+
- WebSocket connections are supported for real-time updates
|
| 141 |
+
|
| 142 |
+
## 🤝 Support
|
| 143 |
+
|
| 144 |
+
For issues or questions:
|
| 145 |
+
- Check the `/api/diagnostics/run` endpoint for automatic diagnostics
|
| 146 |
+
- Review logs in the Hugging Face Space logs tab
|
| 147 |
+
- Check the `/health` endpoint for system status
|
| 148 |
+
|
| 149 |
+
## 📄 License
|
| 150 |
+
|
| 151 |
+
See LICENSE file for details.
|
__pycache__/config.cpython-313.pyc
CHANGED
|
Binary files a/__pycache__/config.cpython-313.pyc and b/__pycache__/config.cpython-313.pyc differ
|
|
|
__pycache__/database.cpython-313.pyc
CHANGED
|
Binary files a/__pycache__/database.cpython-313.pyc and b/__pycache__/database.cpython-313.pyc differ
|
|
|
api_server_extended.py
CHANGED
|
@@ -183,23 +183,45 @@ class HealthCheckResponse(BaseModel):
|
|
| 183 |
async def startup_event():
|
| 184 |
"""رویداد شروع سرور"""
|
| 185 |
print("🚀 راهاندازی سرور...")
|
| 186 |
-
|
| 187 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
|
| 189 |
# ثبت لاگ شروع
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
# شروع بررسی سلامت دورهای
|
| 198 |
-
|
| 199 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
# شروع heartbeat برای WebSocket
|
| 202 |
-
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
print("✅ سرور آماده است")
|
| 205 |
|
|
@@ -208,8 +230,17 @@ async def startup_event():
|
|
| 208 |
async def shutdown_event():
|
| 209 |
"""رویداد خاموش شدن سرور"""
|
| 210 |
print("🛑 خاموشسازی سرور...")
|
| 211 |
-
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
print("✅ سرور خاموش شد")
|
| 214 |
|
| 215 |
|
|
@@ -266,17 +297,26 @@ async def test_websocket_dashboard():
|
|
| 266 |
@app.get("/health")
|
| 267 |
async def health():
|
| 268 |
"""بررسی سلامت سرور"""
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
|
| 281 |
|
| 282 |
# ===== Provider Endpoints =====
|
|
|
|
| 183 |
async def startup_event():
|
| 184 |
"""رویداد شروع سرور"""
|
| 185 |
print("🚀 راهاندازی سرور...")
|
| 186 |
+
|
| 187 |
+
try:
|
| 188 |
+
await manager.init_session()
|
| 189 |
+
except Exception as e:
|
| 190 |
+
print(f"⚠️ Warning: Could not initialize session: {e}")
|
| 191 |
+
|
| 192 |
+
try:
|
| 193 |
+
await run_startup_validation()
|
| 194 |
+
except Exception as e:
|
| 195 |
+
print(f"⚠️ Warning: Startup validation had issues: {e}")
|
| 196 |
+
print("⚠️ Continuing in degraded mode...")
|
| 197 |
|
| 198 |
# ثبت لاگ شروع
|
| 199 |
+
try:
|
| 200 |
+
log_manager.add_log(
|
| 201 |
+
LogLevel.INFO,
|
| 202 |
+
LogCategory.SYSTEM,
|
| 203 |
+
"Server started",
|
| 204 |
+
extra_data={"version": "3.0.0"}
|
| 205 |
+
)
|
| 206 |
+
except Exception as e:
|
| 207 |
+
print(f"⚠️ Warning: Could not log startup: {e}")
|
| 208 |
|
| 209 |
# شروع بررسی سلامت دورهای
|
| 210 |
+
try:
|
| 211 |
+
asyncio.create_task(periodic_health_check())
|
| 212 |
+
except Exception as e:
|
| 213 |
+
print(f"⚠️ Warning: Could not start health check task: {e}")
|
| 214 |
+
|
| 215 |
+
try:
|
| 216 |
+
await auto_discovery_service.start()
|
| 217 |
+
except Exception as e:
|
| 218 |
+
print(f"⚠️ Warning: Could not start auto-discovery: {e}")
|
| 219 |
|
| 220 |
# شروع heartbeat برای WebSocket
|
| 221 |
+
try:
|
| 222 |
+
asyncio.create_task(websocket_heartbeat())
|
| 223 |
+
except Exception as e:
|
| 224 |
+
print(f"⚠️ Warning: Could not start websocket heartbeat: {e}")
|
| 225 |
|
| 226 |
print("✅ سرور آماده است")
|
| 227 |
|
|
|
|
| 230 |
async def shutdown_event():
|
| 231 |
"""رویداد خاموش شدن سرور"""
|
| 232 |
print("🛑 خاموشسازی سرور...")
|
| 233 |
+
|
| 234 |
+
try:
|
| 235 |
+
await auto_discovery_service.stop()
|
| 236 |
+
except Exception as e:
|
| 237 |
+
print(f"⚠️ Warning during auto-discovery shutdown: {e}")
|
| 238 |
+
|
| 239 |
+
try:
|
| 240 |
+
await manager.close_session()
|
| 241 |
+
except Exception as e:
|
| 242 |
+
print(f"⚠️ Warning during session close: {e}")
|
| 243 |
+
|
| 244 |
print("✅ سرور خاموش شد")
|
| 245 |
|
| 246 |
|
|
|
|
| 297 |
@app.get("/health")
|
| 298 |
async def health():
|
| 299 |
"""بررسی سلامت سرور"""
|
| 300 |
+
try:
|
| 301 |
+
stats = manager.get_all_stats()
|
| 302 |
+
conn_stats = conn_manager.get_stats()
|
| 303 |
+
|
| 304 |
+
return {
|
| 305 |
+
"status": "healthy",
|
| 306 |
+
"timestamp": datetime.now().isoformat(),
|
| 307 |
+
"providers_count": stats['summary']['total_providers'],
|
| 308 |
+
"online_count": stats['summary']['online'],
|
| 309 |
+
"connected_clients": conn_stats['active_connections'],
|
| 310 |
+
"total_sessions": conn_stats['total_sessions']
|
| 311 |
+
}
|
| 312 |
+
except Exception as e:
|
| 313 |
+
# Return basic health status even if detailed stats fail
|
| 314 |
+
return {
|
| 315 |
+
"status": "ok",
|
| 316 |
+
"timestamp": datetime.now().isoformat(),
|
| 317 |
+
"message": "Service is running (degraded mode)",
|
| 318 |
+
"error": str(e)
|
| 319 |
+
}
|
| 320 |
|
| 321 |
|
| 322 |
# ===== Provider Endpoints =====
|
app.py
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
collectors/__pycache__/__init__.cpython-313.pyc
ADDED
|
Binary file (1.43 kB). View file
|
|
|
collectors/__pycache__/market_data.cpython-313.pyc
ADDED
|
Binary file (19 kB). View file
|
|
|
database/__pycache__/__init__.cpython-313.pyc
CHANGED
|
Binary files a/database/__pycache__/__init__.cpython-313.pyc and b/database/__pycache__/__init__.cpython-313.pyc differ
|
|
|
database/__pycache__/data_access.cpython-313.pyc
CHANGED
|
Binary files a/database/__pycache__/data_access.cpython-313.pyc and b/database/__pycache__/data_access.cpython-313.pyc differ
|
|
|
database/__pycache__/db_manager.cpython-313.pyc
CHANGED
|
Binary files a/database/__pycache__/db_manager.cpython-313.pyc and b/database/__pycache__/db_manager.cpython-313.pyc differ
|
|
|
database/__pycache__/models.cpython-313.pyc
CHANGED
|
Binary files a/database/__pycache__/models.cpython-313.pyc and b/database/__pycache__/models.cpython-313.pyc differ
|
|
|
logs/crypto_aggregator.log
ADDED
|
File without changes
|
main.py
CHANGED
|
@@ -1,30 +1,30 @@
|
|
| 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 |
-
app = _load_app_module()
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Local Development Entry Point
|
| 4 |
+
This file is for local development only and is NOT used by Hugging Face Docker runtime.
|
| 5 |
+
For production deployment, use: uvicorn api_server_extended:app
|
| 6 |
+
"""
|
| 7 |
|
| 8 |
+
if __name__ == "__main__":
|
| 9 |
+
import uvicorn
|
| 10 |
+
import os
|
| 11 |
+
|
| 12 |
+
# Get port from environment or use default
|
| 13 |
+
port = int(os.getenv("PORT", "8000"))
|
| 14 |
+
|
| 15 |
+
print(f"""
|
| 16 |
+
╔═══════════════════════════════════════════════════════════╗
|
| 17 |
+
║ 🚀 Crypto Monitor - Local Development Server ║
|
| 18 |
+
║ Port: {port} ║
|
| 19 |
+
║ Docs: http://localhost:{port}/docs ║
|
| 20 |
+
╚═══════════════════════════════════════════════════════════╝
|
| 21 |
+
""")
|
| 22 |
+
|
| 23 |
+
# Run with reload for local development
|
| 24 |
+
uvicorn.run(
|
| 25 |
+
"api_server_extended:app",
|
| 26 |
+
host="0.0.0.0",
|
| 27 |
+
port=port,
|
| 28 |
+
reload=True,
|
| 29 |
+
log_level="info"
|
| 30 |
+
)
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -11,6 +11,18 @@ numpy>=1.24.0
|
|
| 11 |
# ==================== HTTP CLIENTS ====================
|
| 12 |
requests>=2.31.0
|
| 13 |
aiohttp>=3.8.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# ==================== WEB SCRAPING & RSS ====================
|
| 16 |
beautifulsoup4>=4.12.0
|
|
@@ -27,10 +39,13 @@ huggingface-hub>=0.16.0
|
|
| 27 |
plotly>=5.14.0
|
| 28 |
kaleido>=0.2.1
|
| 29 |
|
| 30 |
-
# ====================
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
# ====================
|
| 34 |
python-dateutil>=2.8.2
|
| 35 |
|
| 36 |
# ==================== OPTIONAL: ACCELERATED INFERENCE ====================
|
|
|
|
| 11 |
# ==================== HTTP CLIENTS ====================
|
| 12 |
requests>=2.31.0
|
| 13 |
aiohttp>=3.8.0
|
| 14 |
+
httpx>=0.26.0
|
| 15 |
+
|
| 16 |
+
# ==================== WEB BACKEND ====================
|
| 17 |
+
fastapi>=0.109.0
|
| 18 |
+
uvicorn[standard]>=0.27.0
|
| 19 |
+
slowapi>=0.1.9
|
| 20 |
+
python-multipart>=0.0.6
|
| 21 |
+
websockets>=12.0
|
| 22 |
+
|
| 23 |
+
# ==================== DATA MODELS & CONFIG ====================
|
| 24 |
+
pydantic>=2.5.3
|
| 25 |
+
pydantic-settings>=2.1.0
|
| 26 |
|
| 27 |
# ==================== WEB SCRAPING & RSS ====================
|
| 28 |
beautifulsoup4>=4.12.0
|
|
|
|
| 39 |
plotly>=5.14.0
|
| 40 |
kaleido>=0.2.1
|
| 41 |
|
| 42 |
+
# ==================== DATABASE & STORAGE ====================
|
| 43 |
+
sqlalchemy>=2.0.25
|
| 44 |
+
|
| 45 |
+
# ==================== AUTHENTICATION & SECURITY ====================
|
| 46 |
+
PyJWT>=2.8.0
|
| 47 |
|
| 48 |
+
# ==================== DATE/TIME HELPERS ====================
|
| 49 |
python-dateutil>=2.8.2
|
| 50 |
|
| 51 |
# ==================== OPTIONAL: ACCELERATED INFERENCE ====================
|
test_docker_build.bat
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
@echo off
|
| 2 |
+
REM Test script to verify Docker build and run locally (Windows)
|
| 3 |
+
|
| 4 |
+
echo Building Docker image...
|
| 5 |
+
docker build -t crypto-monitor-test .
|
| 6 |
+
|
| 7 |
+
if %ERRORLEVEL% NEQ 0 (
|
| 8 |
+
echo Docker build failed!
|
| 9 |
+
exit /b 1
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
echo Docker build successful!
|
| 13 |
+
echo.
|
| 14 |
+
echo Starting container...
|
| 15 |
+
docker run -d --name crypto-monitor-test -p 8000:8000 crypto-monitor-test
|
| 16 |
+
|
| 17 |
+
if %ERRORLEVEL% NEQ 0 (
|
| 18 |
+
echo Container failed to start!
|
| 19 |
+
exit /b 1
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
echo Container started!
|
| 23 |
+
echo.
|
| 24 |
+
echo Waiting for service to be ready (40 seconds)...
|
| 25 |
+
timeout /t 40 /nobreak >nul
|
| 26 |
+
|
| 27 |
+
echo.
|
| 28 |
+
echo Testing health endpoint...
|
| 29 |
+
curl -s -o nul -w "%%{http_code}" http://localhost:8000/health > temp_status.txt
|
| 30 |
+
set /p HEALTH_RESPONSE=<temp_status.txt
|
| 31 |
+
del temp_status.txt
|
| 32 |
+
|
| 33 |
+
if "%HEALTH_RESPONSE%"=="200" (
|
| 34 |
+
echo Health check passed! (HTTP %HEALTH_RESPONSE%)
|
| 35 |
+
echo.
|
| 36 |
+
echo Service is running at:
|
| 37 |
+
echo - API: http://localhost:8000
|
| 38 |
+
echo - Docs: http://localhost:8000/docs
|
| 39 |
+
echo - Health: http://localhost:8000/health
|
| 40 |
+
echo.
|
| 41 |
+
echo Container logs:
|
| 42 |
+
docker logs crypto-monitor-test --tail 20
|
| 43 |
+
echo.
|
| 44 |
+
echo To stop the container, run:
|
| 45 |
+
echo docker stop crypto-monitor-test ^&^& docker rm crypto-monitor-test
|
| 46 |
+
) else (
|
| 47 |
+
echo Health check failed! (HTTP %HEALTH_RESPONSE%)
|
| 48 |
+
echo.
|
| 49 |
+
echo Container logs:
|
| 50 |
+
docker logs crypto-monitor-test
|
| 51 |
+
echo.
|
| 52 |
+
echo Stopping and removing container...
|
| 53 |
+
docker stop crypto-monitor-test
|
| 54 |
+
docker rm crypto-monitor-test
|
| 55 |
+
exit /b 1
|
| 56 |
+
)
|
test_docker_build.sh
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
# Test script to verify Docker build and run locally
|
| 3 |
+
|
| 4 |
+
echo "🔨 Building Docker image..."
|
| 5 |
+
docker build -t crypto-monitor-test .
|
| 6 |
+
|
| 7 |
+
if [ $? -ne 0 ]; then
|
| 8 |
+
echo "❌ Docker build failed!"
|
| 9 |
+
exit 1
|
| 10 |
+
fi
|
| 11 |
+
|
| 12 |
+
echo "✅ Docker build successful!"
|
| 13 |
+
echo ""
|
| 14 |
+
echo "🚀 Starting container..."
|
| 15 |
+
docker run -d --name crypto-monitor-test -p 8000:8000 crypto-monitor-test
|
| 16 |
+
|
| 17 |
+
if [ $? -ne 0 ]; then
|
| 18 |
+
echo "❌ Container failed to start!"
|
| 19 |
+
exit 1
|
| 20 |
+
fi
|
| 21 |
+
|
| 22 |
+
echo "✅ Container started!"
|
| 23 |
+
echo ""
|
| 24 |
+
echo "⏳ Waiting for service to be ready (40 seconds)..."
|
| 25 |
+
sleep 40
|
| 26 |
+
|
| 27 |
+
echo ""
|
| 28 |
+
echo "🔍 Testing health endpoint..."
|
| 29 |
+
HEALTH_RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:8000/health)
|
| 30 |
+
|
| 31 |
+
if [ "$HEALTH_RESPONSE" = "200" ]; then
|
| 32 |
+
echo "✅ Health check passed! (HTTP $HEALTH_RESPONSE)"
|
| 33 |
+
echo ""
|
| 34 |
+
echo "📊 Service is running at:"
|
| 35 |
+
echo " - API: http://localhost:8000"
|
| 36 |
+
echo " - Docs: http://localhost:8000/docs"
|
| 37 |
+
echo " - Health: http://localhost:8000/health"
|
| 38 |
+
echo ""
|
| 39 |
+
echo "🔍 Container logs:"
|
| 40 |
+
docker logs crypto-monitor-test --tail 20
|
| 41 |
+
echo ""
|
| 42 |
+
echo "🛑 To stop the container, run:"
|
| 43 |
+
echo " docker stop crypto-monitor-test && docker rm crypto-monitor-test"
|
| 44 |
+
else
|
| 45 |
+
echo "❌ Health check failed! (HTTP $HEALTH_RESPONSE)"
|
| 46 |
+
echo ""
|
| 47 |
+
echo "📋 Container logs:"
|
| 48 |
+
docker logs crypto-monitor-test
|
| 49 |
+
echo ""
|
| 50 |
+
echo "🛑 Stopping and removing container..."
|
| 51 |
+
docker stop crypto-monitor-test
|
| 52 |
+
docker rm crypto-monitor-test
|
| 53 |
+
exit 1
|
| 54 |
+
fi
|
utils/__pycache__/__init__.cpython-313.pyc
CHANGED
|
Binary files a/utils/__pycache__/__init__.cpython-313.pyc and b/utils/__pycache__/__init__.cpython-313.pyc differ
|
|
|
utils/__pycache__/api_client.cpython-313.pyc
ADDED
|
Binary file (10.8 kB). View file
|
|
|
utils/__pycache__/logger.cpython-313.pyc
CHANGED
|
Binary files a/utils/__pycache__/logger.cpython-313.pyc and b/utils/__pycache__/logger.cpython-313.pyc differ
|
|
|
verify_deployment.py
ADDED
|
File without changes
|