File size: 3,001 Bytes
56dc677
 
 
 
 
 
4a13628
56dc677
 
c7fc3b6
56dc677
 
 
 
 
 
 
a8c8142
56dc677
 
 
918acab
56dc677
 
918acab
56dc677
 
 
 
 
 
 
4a13628
56dc677
 
 
 
 
 
d4b6133
4a13628
56dc677
 
4a13628
56dc677
674469e
544d113
56dc677
544d113
56dc677
 
 
 
 
 
4a13628
56dc677
 
4a13628
56dc677
 
 
 
 
4a13628
56dc677
 
 
 
4a13628
56dc677
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from fastapi import APIRouter, HTTPException, UploadFile, File, Form
from fastapi.responses import JSONResponse
import uuid
import base64
from models.audio import UserMessage, ChatbotResponse, MessageType
from services.chatbot_service import ChatbotService

router = APIRouter()
chatbot_service = ChatbotService()

@router.post("/chat/message", response_model=ChatbotResponse)
async def send_chat_message(
    session_id: str = Form(...),
    message_type: str = Form(...),
    message: str = Form(None),
    audio_file: UploadFile = File(None)
):
    try:
        # Validate input
        if not message and not audio_file:
            raise HTTPException(status_code=400, detail="Either message or audio file must be provided")
        
        if message_type == "audio" and not audio_file:
            raise HTTPException(status_code=400, detail="Audio file required for audio messages")
        
        # Process audio file if provided
        content = ""
        if audio_file:
            audio_data = await audio_file.read()
            content = base64.b64encode(audio_data).decode('utf-8')
        else:
            content = message
        
        # Create user message
        user_message = UserMessage(
            message_id=str(uuid.uuid4()),
            content=content,
            message_type=MessageType(message_type),
            session_id=session_id
        )
        
        # Process through chatbot service
        response = await chatbot_service.process_user_message(user_message)
        
        return response
        
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error processing message: {str(e)}")

@router.post("/chat/audio")
async def send_audio_message(
    session_id: str = Form(...),
    audio_file: UploadFile = File(...)
):
    """Endpoint specifically for audio messages"""
    try:
        audio_data = await audio_file.read()
        audio_base64 = base64.b64encode(audio_data).decode('utf-8')
        
        user_message = UserMessage(
            message_id=str(uuid.uuid4()),
            content=audio_base64,
            message_type=MessageType.AUDIO,
            session_id=session_id
        )
        
        response = await chatbot_service.process_user_message(user_message)
        return response
        
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error processing audio: {str(e)}")

@router.get("/session/{session_id}/history")
async def get_session_history(session_id: str):
    """Get conversation history for a session"""
    history = chatbot_service.get_session_history(session_id)
    if not history:
        raise HTTPException(status_code=404, detail="Session not found")
    return history

@router.post("/session/new")
async def create_new_session():
    """Create a new chat session"""
    session_id = str(uuid.uuid4())
    chatbot_service._get_or_create_session(session_id)
    return {"session_id": session_id, "message": "New session created"}