# ๐Ÿ›ก๏ธ Police Bot AI Integration Guide This guide explains how to integrate the AI wellness assistant with your website and enhance it with additional local LLM capabilities. ## ๐Ÿ—๏ธ System Architecture ``` Website Frontend (React/HTML/CSS) โ†“ HTTP/WebSocket Flask Web API (web_api.py) โ†“ Police Bot Runtime (police_runtime.py) โ†“ โ”œโ”€โ”€ LLaMA3 via Ollama (Primary Reasoning) โ”œโ”€โ”€ F5-TTS (Voice Synthesis) โ””โ”€โ”€ [Third Local LLM] (Enhanced Capabilities) ``` ## ๐Ÿš€ Quick Start ### 1. Setup the AI Runtime ```bash # Run the setup script python setup.py # Start Ollama with the police-bot model ollama run police-bot # Start the web API python web_api.py ``` ### 2. Test the System ```bash # Test the command-line interface python police_runtime.py # Test the web API curl -X POST http://localhost:5000/api/chat \ -H "Content-Type: application/json" \ -d '{"message": "I am feeling stressed today"}' ``` ## ๐ŸŒ Website Integration ### Option 1: Simple HTML Integration Add this to your website: ```html Police Wellness Assistant
``` ### Option 2: React Integration ```jsx import React, { useState, useEffect } from 'react'; import { PoliceBotClient } from './police-bot-client.js'; function PoliceBotChat() { const [messages, setMessages] = useState([]); const [input, setInput] = useState(''); const [isProcessing, setIsProcessing] = useState(false); const [wellnessTips, setWellnessTips] = useState([]); const client = new PoliceBotClient('http://localhost:5000'); const sendMessage = async () => { if (!input.trim() || isProcessing) return; const userMessage = { text: input, sender: 'user', timestamp: Date.now() }; setMessages(prev => [...prev, userMessage]); setInput(''); setIsProcessing(true); try { await client.sendMessage( input, (response) => { const botMessage = { text: response.text, sender: 'bot', timestamp: Date.now() }; setMessages(prev => [...prev, botMessage]); setWellnessTips(response.wellness_tips || []); }, (error) => { const errorMessage = { text: `Error: ${error}`, sender: 'bot', timestamp: Date.now() }; setMessages(prev => [...prev, errorMessage]); } ); } finally { setIsProcessing(false); } }; return (

๐Ÿ›ก๏ธ Police Wellness Assistant

{messages.map((msg, index) => (
{msg.text}
))}
setInput(e.target.value)} onKeyPress={(e) => e.key === 'Enter' && sendMessage()} placeholder="Type your message..." disabled={isProcessing} />
{wellnessTips.length > 0 && (

Wellness Tips

)}
); } export default PoliceBotChat; ``` ## ๐Ÿค– Adding a Third Local LLM You can enhance the system with additional local LLMs for specialized tasks: ### Option 1: Emotion Analysis LLM ```python # Add to police_runtime.py def get_emotion_llm_response(self, text: str) -> str: """Get emotion analysis from a dedicated LLM""" try: # You can use a smaller, specialized model for emotion analysis data = { "model": "emotion-analyzer", # Different Ollama model "prompt": f"Analyze the emotional state of this text: '{text}'. Respond with only: stressed, positive, negative, or neutral.", "stream": False, "options": { "temperature": 0.1, # Lower temperature for consistent analysis "max_tokens": 10 } } response = requests.post(self.ollama_url, json=data, timeout=10) response.raise_for_status() result = response.json() emotion = result.get("response", "").strip().lower() # Validate emotion valid_emotions = ["stressed", "positive", "negative", "neutral"] return emotion if emotion in valid_emotions else "neutral" except Exception as e: logger.error(f"Error in emotion analysis: {e}") return "neutral" ``` ### Option 2: Wellness Recommendation LLM ```python def get_wellness_llm_response(self, emotional_state: str, context: str = "") -> list: """Get personalized wellness recommendations from a specialized LLM""" try: prompt = f""" As a wellness expert for police officers, provide 3-4 specific, actionable wellness tips for someone who is feeling {emotional_state}. Context: {context} Focus on: - Quick, practical interventions (2-5 minutes) - Stress management techniques - Physical wellness (hydration, movement) - Mental wellness (mindfulness, perspective) Respond with only the tips, one per line, no numbering. """ data = { "model": "wellness-expert", # Specialized wellness model "prompt": prompt, "stream": False, "options": { "temperature": 0.7, "max_tokens": 200 } } response = requests.post(self.ollama_url, json=data, timeout=15) response.raise_for_status() result = response.json() tips_text = result.get("response", "").strip() # Parse tips into list tips = [tip.strip() for tip in tips_text.split('\n') if tip.strip()] return tips[:4] # Limit to 4 tips except Exception as e: logger.error(f"Error in wellness recommendations: {e}") return self.generate_wellness_tips(emotional_state) # Fallback ``` ### Option 3: Multi-Model Setup Create a model manager to handle multiple LLMs: ```python class ModelManager: def __init__(self): self.models = { "primary": "police-bot", # Main conversation "emotion": "emotion-analyzer", # Emotion analysis "wellness": "wellness-expert", # Wellness recommendations "kannada": "kannada-assistant" # Kannada language support } def get_response(self, model_type: str, prompt: str, **kwargs) -> str: """Get response from specified model""" model_name = self.models.get(model_type, "police-bot") data = { "model": model_name, "prompt": prompt, "stream": False, **kwargs } response = requests.post("http://localhost:11434/api/generate", json=data) return response.json().get("response", "").strip() ``` ## ๐Ÿ”ง Configuration ### Environment Variables Create a `.env` file: ```env # Ollama Configuration OLLAMA_URL=http://localhost:11434 OLLAMA_MODEL=police-bot # F5-TTS Configuration F5TTS_CKPT=C:\Users\Samarth Kadam\Voice-train\F5-TTS\ckpts\my_speak\model_last.pt F5TTS_SCRIPT=C:\Users\Samarth Kadam\Voice-train\F5-TTS\src\f5_tts\infer\infer_cli.py # Web API Configuration WEB_PORT=5000 WEB_HOST=0.0.0.0 # Security API_KEY=your-secret-api-key ``` ### CORS Configuration For production deployment, configure CORS properly: ```python # In web_api.py from flask_cors import CORS app = Flask(__name__) CORS(app, origins=[ "http://localhost:3000", # React dev server "http://your-website.com", # Production website "https://your-website.com" ]) ``` ## ๐Ÿš€ Production Deployment ### Using Gunicorn ```bash # Install gunicorn pip install gunicorn # Start production server gunicorn -w 4 -b 0.0.0.0:5000 web_api:app ``` ### Using Docker ```dockerfile FROM python:3.11-slim WORKDIR /app COPY requirements.txt . RUN pip install -r requirements.txt COPY . . EXPOSE 5000 CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:5000", "web_api:app"] ``` ### Systemd Service (Linux) ```ini # /etc/systemd/system/police-bot.service [Unit] Description=Police Bot AI Runtime After=network.target [Service] Type=simple User=police-bot WorkingDirectory=/opt/police-bot-runtime Environment=PATH=/opt/police-bot-runtime/venv/bin ExecStart=/opt/police-bot-runtime/venv/bin/python web_api.py Restart=always [Install] WantedBy=multi-user.target ``` ## ๐Ÿ”’ Security Considerations 1. **API Authentication**: Add API key validation 2. **Rate Limiting**: Implement request rate limiting 3. **Input Validation**: Sanitize all user inputs 4. **HTTPS**: Use SSL/TLS in production 5. **Firewall**: Restrict access to necessary ports only ## ๐Ÿ“Š Monitoring and Logging ```python # Add to web_api.py import logging from logging.handlers import RotatingFileHandler # Configure logging if not app.debug: file_handler = RotatingFileHandler('logs/police_bot.log', maxBytes=10240, backupCount=10) file_handler.setFormatter(logging.Formatter( '%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]' )) file_handler.setLevel(logging.INFO) app.logger.addHandler(file_handler) app.logger.setLevel(logging.INFO) app.logger.info('Police Bot startup') ``` ## ๐Ÿงช Testing ### API Testing ```bash # Test chat endpoint curl -X POST http://localhost:5000/api/chat \ -H "Content-Type: application/json" \ -d '{"message": "I am feeling stressed today"}' # Test health endpoint curl http://localhost:5000/health # Test status endpoint curl http://localhost:5000/api/status ``` ### Load Testing ```python import requests import time import threading def test_load(): for i in range(10): response = requests.post('http://localhost:5000/api/chat', json={'message': f'Test message {i}'}) print(f'Request {i}: {response.status_code}') # Run multiple threads threads = [threading.Thread(target=test_load) for _ in range(5)] for thread in threads: thread.start() for thread in threads: thread.join() ``` ## ๐ŸŽฏ Next Steps 1. **Voice Avatar**: Add a visual avatar that speaks 2. **Multi-language**: Implement Kannada language support 3. **Session Management**: Add user session tracking 4. **Analytics**: Track usage patterns and wellness trends 5. **Mobile App**: Create a mobile companion app 6. **Integration**: Connect with existing police systems ## ๐Ÿ“ž Support For technical support or questions: - Check the logs in the `logs/` directory - Verify all services are running: `python setup.py` - Test individual components: `python police_runtime.py` - Check API health: `curl http://localhost:5000/health` --- **Remember**: This system is designed to support the mental wellness of police personnel. Always prioritize their privacy and well-being in any modifications or deployments.