--- title: Qwen2.5-0.5B Chat emoji: 🤖 colorFrom: blue colorTo: purple sdk: gradio sdk_version: 6.14.0 app_file: app.py pinned: false license: apache-2.0 suggested_hardware: t4-medium suggested_storage: large --- # 🚀 Qwen2.5-0.5B Production Deployment A production-ready deployment of the Qwen2.5-0.5B-Instruct model with comprehensive safety layers, observability, and enterprise features. ## ✨ Features ### 🛡️ **Safety & Security** - **Input Content Filtering**: Blocks harmful, biased, or inappropriate inputs - **Output Safety Checking**: Prevents generation of dangerous content - **PII Detection**: Identifies and protects sensitive information - **Bias Monitoring**: Detects and mitigates biased responses - **Jailbreak Protection**: Resists attempts to bypass safety measures ### 📊 **Observability & Monitoring** - **Real-time Metrics**: Request count, latency, success rates - **Safety Event Logging**: Comprehensive security event tracking - **Performance Analytics**: Token throughput, response times - **Resource Monitoring**: Memory usage, processing efficiency - **SQLite Database**: Persistent metrics storage ### 🧠 **Memory & Intelligence** - **Conversation Memory**: Maintains context across interactions - **Session Management**: Individual user conversation tracking - **Smart Context**: Intelligent conversation history management - **Memory Optimization**: Efficient storage and retrieval ### 🔧 **Tool Integration** - **Calculator**: Mathematical computations - **Time/Date**: Current time and date queries - **Weather**: Weather information (demo integration) - **Web Search**: Search capabilities (demo integration) - **Extensible Framework**: Easy tool addition ## 🎯 Performance Specifications ### **Model Details** - **Model**: Qwen2.5-0.5B-Instruct - **Parameters**: 500 million - **Context Length**: 32,768 tokens - **Memory Footprint**: ~1GB (FP16) - **Language Support**: Multilingual ### **Performance Metrics** - **GPU Inference**: ~80 tokens/second - **CPU Inference**: ~15 tokens/second - **Average Response**: 1.6s (GPU), 8.7s (CPU) - **Concurrent Users**: 50+ (GPU), 10+ (CPU) - **Daily Capacity**: 100,000+ requests (GPU) ## 💰 Cost Analysis | Deployment Type | Hourly Cost | Daily Cost | Monthly Cost | Requests/Hour | |-----------------|-------------|------------|--------------|---------------| | **HF Spaces GPU** | $0.60 | $14.40 | $432 | 2,215 | | **HF Spaces CPU** | Free | Free | Free | 415 | | **Modal GPU** | $0.40 | $9.60 | $288 | 2,215 | | **RunPod GPU** | $0.30 | $7.20 | $216 | 2,215 | | **Replicate** | $0.002/prediction | Pay-per-use | ~$1.66 | Variable | ## 🚀 Quick Start ### **Option 1: Hugging Face Spaces (Recommended)** 1. **Fork this Space**: ```bash git clone https://huggingface.co/spaces/your-username/qwen-production cd qwen-production ``` 2. **Enable GPU** (Optional but recommended): - Go to Space settings - Enable "T4 Medium" GPU - Upgrade to persistent storage 3. **Deploy**: - Push changes to trigger rebuild - Access via Spaces URL ### **Option 2: Modal Deployment** 1. **Install Modal**: ```bash pip install modal modal token new ``` 2. **Create Modal App**: ```python # modal_deploy.py import modal app = modal.App("qwen-production") image = modal.Image.debian_slim().pip_install([ "torch", "transformers", "gradio", "accelerate" ]) @app.function(image=image, gpu="T4", timeout=300) def inference(prompt: str) -> str: # Your deployment code here pass ``` 3. **Deploy**: ```bash modal deploy modal_deploy.py ``` ### **Option 3: Docker Deployment** 1. **Build Image**: ```bash docker build -t qwen-production . ``` 2. **Run Container**: ```bash docker run -p 7860:7860 \ -v $(pwd)/data:/app/data \ -e HUGGING_FACE_HUB_TOKEN=your_token \ qwen-production ``` 3. **Access**: ``` http://localhost:7860 ``` ## 🔧 Configuration ### **Environment Variables** ```bash # Model Configuration MODEL_NAME=Qwen/Qwen2.5-0.5B-Instruct MAX_LENGTH=256 TEMPERATURE=0.7 # Safety Configuration ENABLE_SAFETY_FILTERS=true PII_DETECTION=true BIAS_MONITORING=true # Observability METRICS_DB_PATH=/app/data/metrics.db LOG_LEVEL=INFO # Performance BATCH_SIZE=1 USE_CACHE=true OPTIMIZE_FOR_LATENCY=true ``` ### **Hardware Recommendations** | Use Case | CPU | RAM | GPU | Storage | |----------|-----|-----|-----|---------| | **Demo/Dev** | 2 cores | 8GB | Optional | 20GB | | **Production** | 4+ cores | 16GB+ | T4/V100 | 50GB+ | | **High Traffic** | 8+ cores | 32GB+ | A10/A100 | 100GB+ | ## 📊 Monitoring Dashboard The deployment includes a real-time monitoring dashboard showing: - **Request Metrics**: Volume, latency, success rates - **Safety Events**: Content filtering, PII detection - **Resource Usage**: Memory, CPU, GPU utilization - **User Analytics**: Session tracking, conversation patterns ## 🛡️ Safety Framework ### **Input Safety Layers** 1. **Content Classification**: Harmful, illegal, inappropriate 2. **Bias Detection**: Gender, racial, religious bias 3. **PII Scanning**: SSN, credit cards, emails 4. **Jailbreak Detection**: Prompt injection attempts ### **Output Safety Layers** 1. **Harmful Content Filtering**: Dangerous instructions 2. **Bias Mitigation**: Stereotype prevention 3. **Factual Accuracy**: Hallucination detection 4. **Appropriateness Check**: Context-aware filtering ## 🔗 API Endpoints ### **Chat Interface** ``` POST /api/chat { "message": "Hello!", "session_id": "optional-session-id", "temperature": 0.7 } ``` ### **Metrics Dashboard** ``` GET /api/metrics ``` ### **Health Check** ``` GET /health ``` ## 📈 Scaling Considerations ### **Horizontal Scaling** - Load balancers for multiple instances - Session affinity for conversation memory - Shared database for metrics aggregation ### **Vertical Scaling** - GPU upgrades for higher throughput - Memory increases for longer contexts - CPU optimization for better performance ### **Auto-scaling Triggers** - Request queue length > 10 - Average response time > 5s - Error rate > 5% - CPU/Memory utilization > 80% ## 🔒 Security Best Practices 1. **Authentication**: API keys, OAuth integration 2. **Rate Limiting**: Per-user request limits 3. **Content Security**: CSP headers, XSS protection 4. **Data Privacy**: Conversation encryption, PII handling 5. **Audit Logging**: Comprehensive request logging ## 📚 Documentation - [API Reference](./docs/api.md) - [Deployment Guide](./docs/deployment.md) - [Safety Documentation](./docs/safety.md) - [Monitoring Guide](./docs/monitoring.md) - [Cost Optimization](./docs/cost-optimization.md) ## 🤝 Contributing 1. Fork the repository 2. Create feature branch 3. Add comprehensive tests 4. Update documentation 5. Submit pull request ## 📄 License Apache 2.0 License - see [LICENSE](LICENSE) for details. ## 🆘 Support - **Issues**: Create GitHub issue with detailed description - **Discussions**: Use GitHub Discussions for questions - **Enterprise**: Contact for enterprise support options --- **Built with ❤️ using Qwen2.5, Transformers, and Gradio**