Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,104 +1,161 @@
|
|
| 1 |
-
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 2 |
-
from tools import
|
| 3 |
-
from chatbot import app as app_graph
|
| 4 |
-
from langchain_core.messages import HumanMessage
|
| 5 |
-
import os
|
| 6 |
-
from fastapi.responses import StreamingResponse, FileResponse
|
| 7 |
-
from
|
| 8 |
-
|
| 9 |
-
import
|
| 10 |
-
from
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
@app.get("/")
|
| 31 |
-
def health():
|
| 32 |
-
return {'
|
| 33 |
-
|
| 34 |
-
@app.post("/upload")
|
| 35 |
-
async def upload_file(file: UploadFile = File(...)):
|
| 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 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 2 |
+
from tools import update_retriever
|
| 3 |
+
from chatbot import app as app_graph
|
| 4 |
+
from langchain_core.messages import HumanMessage
|
| 5 |
+
import os
|
| 6 |
+
from fastapi.responses import StreamingResponse, FileResponse
|
| 7 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
+
import asyncio
|
| 9 |
+
from pydantic import BaseModel
|
| 10 |
+
from utils import TTS, STT
|
| 11 |
+
|
| 12 |
+
app = FastAPI()
|
| 13 |
+
|
| 14 |
+
# Configure CORS - restrict in production
|
| 15 |
+
app.add_middleware(
|
| 16 |
+
CORSMiddleware,
|
| 17 |
+
allow_origins=["*"], # TODO: Restrict to specific origins in production
|
| 18 |
+
allow_credentials=True,
|
| 19 |
+
allow_methods=["*"],
|
| 20 |
+
allow_headers=["*"],
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
class TTSRequest(BaseModel):
|
| 24 |
+
text: str
|
| 25 |
+
|
| 26 |
+
UPLOAD_DIR = "uploads"
|
| 27 |
+
ALLOWED_EXTENSIONS = {".pdf", ".txt", ".docx", ".md"} # Add allowed file types
|
| 28 |
+
MAX_FILE_SIZE = 10 * 1024 * 1024 # 10MB limit
|
| 29 |
+
|
| 30 |
+
@app.get("/")
|
| 31 |
+
def health():
|
| 32 |
+
return {'status': 'The api is live and running'}
|
| 33 |
+
|
| 34 |
+
@app.post("/upload")
|
| 35 |
+
async def upload_file(file: UploadFile = File(...)):
|
| 36 |
+
try:
|
| 37 |
+
# Validate file extension
|
| 38 |
+
file_ext = os.path.splitext(file.filename)[1].lower()
|
| 39 |
+
if file_ext not in ALLOWED_EXTENSIONS:
|
| 40 |
+
raise HTTPException(
|
| 41 |
+
status_code=400,
|
| 42 |
+
detail=f"File type not allowed. Allowed types: {', '.join(ALLOWED_EXTENSIONS)}"
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# Create upload directory
|
| 46 |
+
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 47 |
+
|
| 48 |
+
# Read file content
|
| 49 |
+
content = await file.read()
|
| 50 |
+
|
| 51 |
+
# Validate file size
|
| 52 |
+
if len(content) > MAX_FILE_SIZE:
|
| 53 |
+
raise HTTPException(
|
| 54 |
+
status_code=400,
|
| 55 |
+
detail=f"File too large. Maximum size: {MAX_FILE_SIZE / (1024*1024)}MB"
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
# Save file
|
| 59 |
+
file_path = os.path.join(UPLOAD_DIR, file.filename)
|
| 60 |
+
with open(file_path, "wb") as f:
|
| 61 |
+
f.write(content)
|
| 62 |
+
|
| 63 |
+
# Update retriever
|
| 64 |
+
update_retriever(file_path)
|
| 65 |
+
|
| 66 |
+
return {
|
| 67 |
+
"status": "success",
|
| 68 |
+
"filename": file.filename,
|
| 69 |
+
"size": len(content)
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
except HTTPException:
|
| 73 |
+
raise
|
| 74 |
+
except Exception as e:
|
| 75 |
+
raise HTTPException(status_code=500, detail=f"Upload failed: {str(e)}")
|
| 76 |
+
|
| 77 |
+
@app.post("/chat")
|
| 78 |
+
async def chat(message: str, session_id: str = "default"):
|
| 79 |
+
if not message.strip():
|
| 80 |
+
raise HTTPException(status_code=400, detail="Message cannot be empty")
|
| 81 |
+
|
| 82 |
+
# Validate session_id (basic validation)
|
| 83 |
+
if not session_id or len(session_id) > 100:
|
| 84 |
+
raise HTTPException(status_code=400, detail="Invalid session_id")
|
| 85 |
+
|
| 86 |
+
async def event_generator():
|
| 87 |
+
try:
|
| 88 |
+
async for chunk in app_graph.astream(
|
| 89 |
+
{"messages": [HumanMessage(content=message)]},
|
| 90 |
+
config={"configurable": {"thread_id": session_id}},
|
| 91 |
+
stream_mode="messages"
|
| 92 |
+
):
|
| 93 |
+
if len(chunk) >= 1:
|
| 94 |
+
message_chunk = chunk[0] if isinstance(chunk, tuple) else chunk
|
| 95 |
+
if hasattr(message_chunk, 'content') and message_chunk.content:
|
| 96 |
+
data = str(message_chunk.content).replace("\n", "\\n")
|
| 97 |
+
yield f"data: {data}\n\n"
|
| 98 |
+
await asyncio.sleep(0.01)
|
| 99 |
+
except Exception as e:
|
| 100 |
+
yield f"data: Error: {str(e)}\n\n"
|
| 101 |
+
|
| 102 |
+
return StreamingResponse(
|
| 103 |
+
event_generator(),
|
| 104 |
+
media_type="text/event-stream",
|
| 105 |
+
headers={
|
| 106 |
+
"Cache-Control": "no-cache",
|
| 107 |
+
"Connection": "keep-alive",
|
| 108 |
+
"X-Accel-Buffering": "no",
|
| 109 |
+
},
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
@app.post("/stt")
|
| 113 |
+
async def transcribe_audio(file: UploadFile = File(...)):
|
| 114 |
+
try:
|
| 115 |
+
# Validate audio file type
|
| 116 |
+
if not file.content_type or not file.content_type.startswith("audio/"):
|
| 117 |
+
raise HTTPException(status_code=400, detail="Invalid audio file")
|
| 118 |
+
|
| 119 |
+
return await STT(file)
|
| 120 |
+
except HTTPException:
|
| 121 |
+
raise
|
| 122 |
+
except Exception as e:
|
| 123 |
+
raise HTTPException(status_code=500, detail=f"Transcription failed: {str(e)}")
|
| 124 |
+
|
| 125 |
+
@app.post("/tts")
|
| 126 |
+
async def generate_tts(request: TTSRequest):
|
| 127 |
+
try:
|
| 128 |
+
if not request.text.strip():
|
| 129 |
+
raise HTTPException(status_code=400, detail="Text is empty")
|
| 130 |
+
|
| 131 |
+
# Limit text length to prevent abuse
|
| 132 |
+
if len(request.text) > 5000:
|
| 133 |
+
raise HTTPException(status_code=400, detail="Text too long (max 5000 characters)")
|
| 134 |
+
|
| 135 |
+
audio_path = await TTS(text=request.text)
|
| 136 |
+
|
| 137 |
+
if not os.path.exists(audio_path):
|
| 138 |
+
raise HTTPException(status_code=500, detail="Audio file not created")
|
| 139 |
+
|
| 140 |
+
return FileResponse(
|
| 141 |
+
path=audio_path,
|
| 142 |
+
media_type="audio/mpeg",
|
| 143 |
+
filename="speech.mp3",
|
| 144 |
+
background=None # Consider adding cleanup after response
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
except HTTPException:
|
| 148 |
+
raise
|
| 149 |
+
except Exception as e:
|
| 150 |
+
raise HTTPException(status_code=500, detail=f"TTS failed: {str(e)}")
|
| 151 |
+
|
| 152 |
+
# Optional: Add cleanup endpoint for old files
|
| 153 |
+
@app.on_event("startup")
|
| 154 |
+
async def startup_event():
|
| 155 |
+
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 156 |
+
|
| 157 |
+
# Optional: Graceful shutdown
|
| 158 |
+
@app.on_event("shutdown")
|
| 159 |
+
async def shutdown_event():
|
| 160 |
+
# Clean up temporary files if needed
|
| 161 |
+
pass
|