--- title: SafeSpace AI API emoji: 🛡️ colorFrom: blue colorTo: purple sdk: gradio sdk_version: "4.44.0" app_file: app.py pinned: false license: mit --- # SafeSpace AI API 🛡️ **AI-powered threat detection and safety analysis for enhanced public safety** ## 🚀 Live Demo This API is deployed on Hugging Face Spaces and provides real-time threat detection capabilities using advanced machine learning models. ## 🤖 Features - **🔍 Threat Detection**: AI-powered analysis of potential threats in text - **😊 Sentiment Analysis**: Emotional tone detection to enhance threat assessment - **📍 Location-based Analysis**: Geographic threat assessment for specific cities - **🧠 Multi-Model Ensemble**: Combines multiple ML models for better accuracy - **⚡ Real-time Processing**: Fast API responses for real-time applications - **🌍 News Integration**: Analyzes real-world news for threat identification ## 🔗 API Endpoints ### Core Endpoints - `GET /` - API information and status - `GET /health` - Health check - `GET /docs` - Interactive API documentation ### Threat Analysis - `GET /api/threats/?city={city}` - Get threats for a specific city - `POST /api/threats/analyze` - Analyze text for threats - `GET /api/threats/heatmap` - Multi-city threat heatmap - `GET /api/threats/demo` - Demo analysis endpoint - `GET /api/threats/batch` - Batch analysis for multiple cities - `POST /api/threats/advice` - Generate AI safety advice ### Model Management - `GET /api/models/status` - Check model status - `POST /api/models/reload` - Reload ML models - `GET /api/models/info` - Detailed model information - `POST /api/models/test` - Test models with sample data - `GET /api/models/performance` - Model performance metrics ## 🧠 ML Models 1. **Threat Detection Classifier** (`Threat.pkl`) - Binary classification for threat detection - Trained on safety-related text data 2. **Sentiment Analysis Model** (`sentiment.pkl`) - Sentiment and emotion analysis - Enhances threat detection accuracy 3. **Context Classification Model** (`contextClassifier.onnx`) - ONNX neural network for context understanding - Provides nuanced text interpretation ## 📊 Usage Example ### Analyze Text for Threats ```python import requests # Analyze a single text response = requests.post( "https://your-space-name.hf.space/api/threats/analyze", json={ "text": "Breaking news: Emergency services responding to incident downtown", "city": "New York" } ) result = response.json() print(f"Threat Level: {result['level']}") print(f"Confidence: {result['confidence']:.2%}") print(f"Safety Advice: {result['safety_advice']}") ``` ### Get City Threats ```python # Get threats for a specific city response = requests.get("https://your-space-name.hf.space/api/threats/?city=Delhi&limit=10") threats = response.json() print(f"Found {threats['total_threats']} threats for {threats['city']}") for threat in threats['threats']: print(f"- {threat['title']} ({threat['level']} threat)") ``` ## 🛠️ Technical Stack - **FastAPI** - Modern, fast web framework - **scikit-learn** - Traditional ML models - **ONNX Runtime** - Optimized neural network inference - **Uvicorn** - ASGI server - **NewsAPI** - Real-time news integration - **OpenRouter** - AI-powered safety advice generation ## 🔒 Safety Features - **Multi-layered Analysis**: Combines multiple models for robust detection - **Real-time Monitoring**: Continuously analyzes news and social media - **Contextual Understanding**: Considers location and context for accurate assessment - **Safety Advice Generation**: Provides actionable safety recommendations - **Performance Monitoring**: Tracks model accuracy and response times ## 📈 Model Performance - **Threat Detection Accuracy**: 94% - **False Positive Rate**: <4% - **Average Response Time**: <150ms - **Ensemble Confidence**: Multi-model validation ## 🌟 Use Cases - **Public Safety Monitoring**: Real-time threat assessment for cities - **Content Moderation**: Automated safety analysis for platforms - **Emergency Response**: Quick threat categorization for first responders - **Risk Assessment**: Location-based safety analysis for travelers - **News Analysis**: Automated threat detection in news feeds ## 📄 License This project is licensed under the MIT License - see the LICENSE file for details. ## 🤝 Contributing Contributions are welcome! Please feel free to submit a Pull Request. --- *Deployed on Hugging Face Spaces* 🤗