Spaces:
Runtime error
Runtime error
Upload src\api\chat_router.py with huggingface_hub
Browse files- src//api//chat_router.py +87 -0
src//api//chat_router.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, HTTPException, Depends, BackgroundTasks, File, UploadFile
|
| 2 |
+
from typing import Optional, Dict, Any
|
| 3 |
+
import uuid
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
import os
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
|
| 8 |
+
from models.message import Message, MessageCreate, MessageRead
|
| 9 |
+
from models.conversation import Conversation
|
| 10 |
+
from services.translation_service import translation_service, Language
|
| 11 |
+
from services.voice_processing_service import voice_processing_service
|
| 12 |
+
from services.chat_service import chat_service
|
| 13 |
+
from tools.mcp_server import execute_mcp_tool
|
| 14 |
+
from middleware.auth import get_optional_user, get_current_user
|
| 15 |
+
from models.user import User
|
| 16 |
+
|
| 17 |
+
router = APIRouter(prefix="/api", tags=["chat"])
|
| 18 |
+
|
| 19 |
+
# Audio endpoint for serving synthesized voice files
|
| 20 |
+
@router.get("/audio/{filename}")
|
| 21 |
+
async def get_audio_file(filename: str):
|
| 22 |
+
"""Serve audio files generated by voice synthesis"""
|
| 23 |
+
import tempfile
|
| 24 |
+
temp_dir = Path(tempfile.gettempdir())
|
| 25 |
+
file_path = temp_dir / filename
|
| 26 |
+
|
| 27 |
+
if not file_path.exists():
|
| 28 |
+
raise HTTPException(status_code=404, detail="Audio file not found")
|
| 29 |
+
|
| 30 |
+
from fastapi.responses import FileResponse
|
| 31 |
+
return FileResponse(
|
| 32 |
+
path=file_path,
|
| 33 |
+
media_type="audio/mpeg",
|
| 34 |
+
filename=filename
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
@router.post("/chat", response_model=Dict[str, Any])
|
| 39 |
+
async def process_chat_message(
|
| 40 |
+
message: str,
|
| 41 |
+
conversation_id: Optional[str] = None,
|
| 42 |
+
message_type: str = "text",
|
| 43 |
+
language: str = "en",
|
| 44 |
+
user_preferences: Optional[Dict[str, Any]] = None,
|
| 45 |
+
current_user: User = Depends(get_current_user)
|
| 46 |
+
):
|
| 47 |
+
"""
|
| 48 |
+
Process chat message and return AI response
|
| 49 |
+
Handles both text and voice input, processes through the AI system, and returns appropriate response
|
| 50 |
+
"""
|
| 51 |
+
try:
|
| 52 |
+
# Validate inputs
|
| 53 |
+
if not message.strip():
|
| 54 |
+
raise HTTPException(status_code=400, detail="Message cannot be empty")
|
| 55 |
+
|
| 56 |
+
# Process the message using the chat service
|
| 57 |
+
response_obj = await chat_service.process_user_message(
|
| 58 |
+
message=message,
|
| 59 |
+
conversation_id=conversation_id,
|
| 60 |
+
message_type=message_type,
|
| 61 |
+
language=language,
|
| 62 |
+
user_preferences=user_preferences,
|
| 63 |
+
user_id=current_user.id if current_user else None
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
return response_obj
|
| 67 |
+
|
| 68 |
+
except Exception as e:
|
| 69 |
+
raise HTTPException(status_code=500, detail=f"Error processing chat message: {str(e)}")
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
# Additional endpoints for conversation management
|
| 73 |
+
@router.get("/conversations", response_model=Dict[str, Any])
|
| 74 |
+
async def list_conversations():
|
| 75 |
+
"""List user's conversations"""
|
| 76 |
+
# In a real implementation, this would fetch from database
|
| 77 |
+
return {"conversations": []}
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
@router.get("/conversations/{conversation_id}", response_model=Dict[str, Any])
|
| 81 |
+
async def get_conversation_history(conversation_id: str):
|
| 82 |
+
"""Get conversation history"""
|
| 83 |
+
# In a real implementation, this would fetch from database
|
| 84 |
+
return {
|
| 85 |
+
"conversation": {"id": conversation_id, "title": "Sample Conversation"},
|
| 86 |
+
"messages": []
|
| 87 |
+
}
|