Spaces:
Running
Running
| from fastapi import APIRouter, HTTPException | |
| from fastapi.responses import StreamingResponse, JSONResponse | |
| import logging | |
| from models.schemas import ChatRequest, ErrorResponse | |
| from services.text_service import text_service | |
| logger = logging.getLogger("text-router") | |
| router = APIRouter(prefix="/v1/text", tags=["Text Generation"]) | |
| async def create_chat_completion(request: ChatRequest): | |
| """ | |
| Create a chat completion using the text model | |
| Supports: | |
| - Standard completions | |
| - Streaming responses | |
| - JSON extraction mode | |
| """ | |
| if not text_service.is_ready(): | |
| raise HTTPException(status_code=503, detail="Text model not ready") | |
| try: | |
| messages = [msg.model_dump() for msg in request.messages] | |
| result = await text_service.generate_completion( | |
| messages=messages, | |
| temperature=request.temperature, | |
| max_tokens=request.max_tokens, | |
| stream=request.stream, | |
| return_json=request.returnJson | |
| ) | |
| if request.stream: | |
| return StreamingResponse(result, media_type="text/event-stream") | |
| return JSONResponse(content=result) | |
| except ValueError as e: | |
| raise HTTPException(status_code=400, detail=str(e)) | |
| except Exception as e: | |
| logger.error(f"Chat completion error: {e}") | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def text_health(): | |
| """Check text model health status""" | |
| return { | |
| "status": "healthy" if text_service.is_ready() else "initializing", | |
| "model_ready": text_service.is_ready() | |
| } |