π€ HuggingFace Spaces Deployment Guide
This guide explains how to deploy the Crypto API Monitoring System to HuggingFace Spaces.
Overview
The application is fully optimized for HuggingFace Spaces deployment with:
- Docker-based deployment using the standard HF Spaces port (7860)
- Automatic environment detection for frontend API calls
- HuggingFace ML integration for crypto sentiment analysis
- WebSocket support for real-time data streaming
- Persistent data storage with SQLite
Prerequisites
- A HuggingFace account (sign up here)
- Git installed on your local machine
- Basic familiarity with Docker and HuggingFace Spaces
Deployment Steps
1. Create a New Space
- Go to HuggingFace Spaces
- Click "Create new Space"
- Configure your Space:
- Name:
Datasourceforcryptocurrency(or your preferred name) - License: Choose appropriate license (e.g., MIT)
- SDK: Select Docker
- Visibility: Public or Private (your choice)
- Name:
- Click "Create Space"
2. Clone Your Space Repository
# Clone your newly created space
git clone https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
cd YOUR_SPACE_NAME
3. Copy Application Files
Copy all files from this repository to your Space directory:
# Copy all files (adjust paths as needed)
cp -r /path/to/crypto-dt-source/* .
Essential files for HuggingFace Spaces:
Dockerfile- Docker configuration optimized for HF Spacesrequirements.txt- Python dependencies including transformersapp.py- Main FastAPI applicationconfig.js- Frontend configuration with environment detection*.html- UI files (index.html, hf_console.html, etc.)- All backend directories (
api/,backend/,monitoring/, etc.)
4. Configure Environment Variables (Optional but Recommended)
In your HuggingFace Space settings, add these secrets:
Required:
HUGGINGFACE_TOKEN- Your HF token for accessing models (optional if using public models)
Optional API Keys (for enhanced data collection):
ETHERSCAN_KEY_1- Etherscan API keyCOINMARKETCAP_KEY_1- CoinMarketCap API keyNEWSAPI_KEY- NewsAPI keyCRYPTOCOMPARE_KEY- CryptoCompare API key
HuggingFace Configuration:
ENABLE_SENTIMENT=true- Enable sentiment analysisSENTIMENT_SOCIAL_MODEL=ElKulako/cryptobert- Social sentiment modelSENTIMENT_NEWS_MODEL=kk08/CryptoBERT- News sentiment modelHF_REGISTRY_REFRESH_SEC=21600- Registry refresh interval (6 hours)
5. Push to HuggingFace
# Add all files
git add .
# Commit changes
git commit -m "Initial deployment of Crypto API Monitor"
# Push to HuggingFace
git push
6. Wait for Build
HuggingFace Spaces will automatically:
- Build your Docker image (takes 5-10 minutes)
- Download required ML models
- Start the application on port 7860
- Run health checks
Monitor the build logs in your Space's "Logs" tab.
7. Access Your Application
Once deployed, your application will be available at:
https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
Features Available in HuggingFace Spaces
π― Real-Time Dashboard
- Access the main dashboard at the root URL
- Real-time WebSocket updates for all metrics
- Provider health monitoring
- System status and analytics
π€ HuggingFace Console
- Access at
/hf_console.html - Test HF model registry
- Run sentiment analysis
- Search crypto-related models and datasets
π API Documentation
- Swagger UI:
/docs - ReDoc:
/redoc - API Info:
/api-info
π WebSocket Endpoints
All WebSocket endpoints are available for real-time data:
/ws- Master WebSocket endpoint/ws/market_data- Market data updates/ws/news- News updates/ws/sentiment- Sentiment analysis updates/ws/health- Health monitoring/ws/huggingface- HF integration updates
Local Development & Testing
Using Docker Compose
# Build and start the application
docker-compose up --build
# Access at http://localhost:7860
Using Docker Directly
# Build the image
docker build -t crypto-api-monitor .
# Run the container
docker run -p 7860:7860 \
-e HUGGINGFACE_TOKEN=your_token \
-e ENABLE_SENTIMENT=true \
-v $(pwd)/data:/app/data \
crypto-api-monitor
Using Python Directly
# Install dependencies
pip install -r requirements.txt
# Set environment variables
export ENABLE_SENTIMENT=true
export HUGGINGFACE_TOKEN=your_token
# Run the application
python app.py
Configuration
Frontend Configuration (config.js)
The frontend automatically detects the environment:
- HuggingFace Spaces: Uses relative URLs with Space origin
- Localhost: Uses
http://localhost:7860 - Custom Deployment: Uses current window origin
No manual configuration needed!
Backend Configuration
Edit .env or set environment variables:
# HuggingFace
HUGGINGFACE_TOKEN=your_token_here
ENABLE_SENTIMENT=true
SENTIMENT_SOCIAL_MODEL=ElKulako/cryptobert
SENTIMENT_NEWS_MODEL=kk08/CryptoBERT
HF_REGISTRY_REFRESH_SEC=21600
HF_HTTP_TIMEOUT=8.0
# API Keys (optional)
ETHERSCAN_KEY_1=your_key
COINMARKETCAP_KEY_1=your_key
NEWSAPI_KEY=your_key
Architecture
βββββββββββββββββββββββββββββββββββββββββββββββββββ
β HuggingFace Spaces (Docker) β
βββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Frontend (HTML/JS) β
β βββ config.js (auto-detects environment) β
β βββ index.html (main dashboard) β
β βββ hf_console.html (HF integration UI) β
β β
β Backend (FastAPI) β
β βββ app.py (main application) β
β βββ WebSocket Manager (real-time updates) β
β βββ HF Integration (sentiment analysis) β
β βββ Data Collectors (200+ APIs) β
β βββ SQLite Database (persistent storage) β
β β
β ML Models (HuggingFace Transformers) β
β βββ ElKulako/cryptobert β
β βββ kk08/CryptoBERT β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββ
Troubleshooting
Build Fails
- Check Docker logs in HF Spaces
- Verify
requirements.txthas all dependencies - Ensure Dockerfile uses Python 3.10
- Check for syntax errors in Python files
Application Won't Start
- Check health endpoint:
https://your-space-url/health - Review application logs in HF Spaces
- Verify port 7860 is exposed in Dockerfile
- Check environment variables are set correctly
WebSocket Connections Fail
- Ensure your Space URL uses HTTPS
- WebSockets automatically upgrade to WSS on HTTPS
- Check browser console for connection errors
- Verify CORS settings in
app.py
Sentiment Analysis Not Working
- Set
HUGGINGFACE_TOKENin Space secrets - Verify models are accessible:
ElKulako/cryptobert,kk08/CryptoBERT - Check HF console at
/hf_console.html - Review logs for model download errors
Performance Issues
- Increase Space hardware tier (if available)
- Reduce number of concurrent API monitors
- Adjust
HF_REGISTRY_REFRESH_SECto longer interval - Consider disabling sentiment analysis if not needed
Resource Requirements
Minimum (Free Tier):
- 2 CPU cores
- 2GB RAM
- 1GB disk space
Recommended:
- 4 CPU cores
- 4GB RAM
- 2GB disk space
- For better ML model performance
Updating Your Space
# Pull latest changes
git pull
# Make your modifications
# ...
# Commit and push
git add .
git commit -m "Update: description of changes"
git push
HuggingFace will automatically rebuild and redeploy.
Security Best Practices
- Use HF Secrets for sensitive data (API keys, tokens)
- Don't commit
.envfiles with actual keys - Review API keys permissions (read-only when possible)
- Monitor usage of external APIs to avoid rate limits
- Keep dependencies updated for security patches
Advanced Configuration
Custom ML Models
To use custom sentiment analysis models:
# Set environment variables in HF Spaces
SENTIMENT_SOCIAL_MODEL=your-username/your-model
SENTIMENT_NEWS_MODEL=your-username/another-model
Custom Port (Not Recommended for HF Spaces)
HuggingFace Spaces requires port 7860. Don't change unless deploying elsewhere.
Multiple Workers
Edit Dockerfile CMD:
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860", "--workers", "2"]
Note: More workers = more memory usage. Adjust based on Space tier.
Support & Resources
- HuggingFace Docs: https://huggingface.co/docs/hub/spaces
- FastAPI Docs: https://fastapi.tiangolo.com/
- Transformers Docs: https://huggingface.co/docs/transformers/
- Project Issues: https://github.com/nimazasinich/crypto-dt-source/issues
License
[Specify your license here]
Contributing
Contributions are welcome! Please read the contributing guidelines before submitting PRs.
Need help? Open an issue or contact the maintainers.
Enjoy your crypto monitoring dashboard on HuggingFace Spaces! π