Delete app.py
Browse files
app.py
DELETED
|
@@ -1,236 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import shutil
|
| 3 |
-
from fastapi.responses import FileResponse
|
| 4 |
-
import asyncio
|
| 5 |
-
import json
|
| 6 |
-
from fastapi import FastAPI, UploadFile, File, Form, HTTPException, BackgroundTasks
|
| 7 |
-
from fastapi.responses import StreamingResponse
|
| 8 |
-
from pydantic import BaseModel
|
| 9 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 10 |
-
from utils import STT, TTS
|
| 11 |
-
from data_ingestion import Ingest_Data
|
| 12 |
-
from RAG import app as rag_app, Ragbot_State, reload_vector_store
|
| 13 |
-
|
| 14 |
-
# Initialize FastAPI
|
| 15 |
-
app = FastAPI(title="LangGraph RAG Chatbot", version="1.0")
|
| 16 |
-
|
| 17 |
-
app.add_middleware(
|
| 18 |
-
CORSMiddleware,
|
| 19 |
-
allow_origins=["*"],
|
| 20 |
-
allow_credentials=True,
|
| 21 |
-
allow_methods=["*"],
|
| 22 |
-
allow_headers=["*"],
|
| 23 |
-
)
|
| 24 |
-
|
| 25 |
-
# --- Pydantic Models ---
|
| 26 |
-
class ChatRequest(BaseModel):
|
| 27 |
-
query: str
|
| 28 |
-
thread_id: str = "default_user"
|
| 29 |
-
use_rag: bool = False
|
| 30 |
-
use_web: bool = False
|
| 31 |
-
model_name: str = "gpt"
|
| 32 |
-
|
| 33 |
-
class TTSRequest(BaseModel):
|
| 34 |
-
text: str
|
| 35 |
-
voice: str = "en-US-AriaNeural"
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
# --- Endpoints ---
|
| 39 |
-
|
| 40 |
-
@app.get("/")
|
| 41 |
-
def health_check():
|
| 42 |
-
return {"status": "running", "message": "Bot is ready"}
|
| 43 |
-
|
| 44 |
-
@app.post("/upload")
|
| 45 |
-
async def upload_document(
|
| 46 |
-
file: UploadFile = File(...),
|
| 47 |
-
background_tasks: BackgroundTasks = BackgroundTasks()
|
| 48 |
-
):
|
| 49 |
-
try:
|
| 50 |
-
temp_filename = f"temp_{file.filename}"
|
| 51 |
-
|
| 52 |
-
with open(temp_filename, "wb") as buffer:
|
| 53 |
-
shutil.copyfileobj(file.file, buffer)
|
| 54 |
-
|
| 55 |
-
def process_and_reload(path):
|
| 56 |
-
try:
|
| 57 |
-
result = Ingest_Data(path)
|
| 58 |
-
print(f"Ingestion Result: {result}")
|
| 59 |
-
reload_vector_store()
|
| 60 |
-
|
| 61 |
-
except Exception as e:
|
| 62 |
-
print(f"Error processing background task: {e}")
|
| 63 |
-
finally:
|
| 64 |
-
if os.path.exists(path):
|
| 65 |
-
os.remove(path)
|
| 66 |
-
|
| 67 |
-
background_tasks.add_task(process_and_reload, temp_filename)
|
| 68 |
-
|
| 69 |
-
return {
|
| 70 |
-
"message": "File received. Processing started in background.",
|
| 71 |
-
"filename": file.filename
|
| 72 |
-
}
|
| 73 |
-
|
| 74 |
-
except Exception as e:
|
| 75 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
# ... (keep existing imports) ...
|
| 81 |
-
|
| 82 |
-
# NEW: Streaming endpoint
|
| 83 |
-
@app.post("/chat/stream")
|
| 84 |
-
async def chat_stream_endpoint(request: ChatRequest):
|
| 85 |
-
"""
|
| 86 |
-
Streaming Chat Endpoint.
|
| 87 |
-
Streams the LLM response as Server-Sent Events (SSE).
|
| 88 |
-
"""
|
| 89 |
-
async def event_generator():
|
| 90 |
-
try:
|
| 91 |
-
config = {"configurable": {"thread_id": request.thread_id}}
|
| 92 |
-
inputs = {
|
| 93 |
-
"query": request.query,
|
| 94 |
-
"RAG": request.use_rag,
|
| 95 |
-
"web_search": request.use_web,
|
| 96 |
-
"model_name": request.model_name,
|
| 97 |
-
"context": [],
|
| 98 |
-
"metadata": [],
|
| 99 |
-
"web_context": "",
|
| 100 |
-
}
|
| 101 |
-
|
| 102 |
-
# Use astream or astream_events depending on your LangGraph version
|
| 103 |
-
async for event in rag_app.astream(inputs, config=config):
|
| 104 |
-
# Extract the content from the streaming event
|
| 105 |
-
# The structure depends on your graph, adjust as needed
|
| 106 |
-
if "response" in event:
|
| 107 |
-
messages = event["response"]
|
| 108 |
-
if messages and len(messages) > 0:
|
| 109 |
-
last_msg = messages[-1]
|
| 110 |
-
if hasattr(last_msg, 'content'):
|
| 111 |
-
chunk = {
|
| 112 |
-
"type": "content",
|
| 113 |
-
"data": last_msg.content,
|
| 114 |
-
"thread_id": request.thread_id
|
| 115 |
-
}
|
| 116 |
-
yield f"data: {json.dumps(chunk)}\n\n"
|
| 117 |
-
|
| 118 |
-
# If your graph streams token by token, handle it here
|
| 119 |
-
elif "chunk" in event:
|
| 120 |
-
chunk = {
|
| 121 |
-
"type": "token",
|
| 122 |
-
"data": event["chunk"],
|
| 123 |
-
"thread_id": request.thread_id
|
| 124 |
-
}
|
| 125 |
-
yield f"data: {json.dumps(chunk)}\n\n"
|
| 126 |
-
|
| 127 |
-
# Send completion signal
|
| 128 |
-
yield f"data: {json.dumps({'type': 'done', 'thread_id': request.thread_id})}\n\n"
|
| 129 |
-
|
| 130 |
-
except Exception as e:
|
| 131 |
-
error_data = {
|
| 132 |
-
"type": "error",
|
| 133 |
-
"error": str(e),
|
| 134 |
-
"thread_id": request.thread_id
|
| 135 |
-
}
|
| 136 |
-
yield f"data: {json.dumps(error_data)}\n\n"
|
| 137 |
-
|
| 138 |
-
return StreamingResponse(
|
| 139 |
-
event_generator(),
|
| 140 |
-
media_type="text/event-stream",
|
| 141 |
-
headers={
|
| 142 |
-
"Cache-Control": "no-cache",
|
| 143 |
-
"Connection": "keep-alive",
|
| 144 |
-
"X-Accel-Buffering": "no" # Disable nginx buffering
|
| 145 |
-
}
|
| 146 |
-
)
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
# ALTERNATIVE: If you need more granular streaming with astream_events
|
| 150 |
-
@app.post("/chat/stream/events")
|
| 151 |
-
async def chat_stream_events_endpoint(request: ChatRequest):
|
| 152 |
-
"""
|
| 153 |
-
Streaming Chat Endpoint using astream_events.
|
| 154 |
-
Provides more granular control over streaming events.
|
| 155 |
-
"""
|
| 156 |
-
async def event_generator():
|
| 157 |
-
try:
|
| 158 |
-
config = {"configurable": {"thread_id": request.thread_id}}
|
| 159 |
-
inputs = {
|
| 160 |
-
"query": request.query,
|
| 161 |
-
"RAG": request.use_rag,
|
| 162 |
-
"web_search": request.use_web,
|
| 163 |
-
"model_name": request.model_name,
|
| 164 |
-
"context": [],
|
| 165 |
-
"metadata": [],
|
| 166 |
-
"web_context": "",
|
| 167 |
-
}
|
| 168 |
-
|
| 169 |
-
# Stream events from the graph
|
| 170 |
-
async for event in rag_app.astream_events(inputs, config=config, version="v2"):
|
| 171 |
-
event_type = event.get("event")
|
| 172 |
-
|
| 173 |
-
# Handle different event types
|
| 174 |
-
if event_type == "on_chat_model_stream":
|
| 175 |
-
# This captures token-by-token streaming from the LLM
|
| 176 |
-
content = event.get("data", {}).get("chunk", {})
|
| 177 |
-
if hasattr(content, 'content') and content.content:
|
| 178 |
-
chunk = {
|
| 179 |
-
"type": "token",
|
| 180 |
-
"data": content.content,
|
| 181 |
-
"thread_id": request.thread_id
|
| 182 |
-
}
|
| 183 |
-
yield f"data: {json.dumps(chunk)}\n\n"
|
| 184 |
-
|
| 185 |
-
elif event_type == "on_chain_end":
|
| 186 |
-
# Final result
|
| 187 |
-
output = event.get("data", {}).get("output", {})
|
| 188 |
-
if "response" in output:
|
| 189 |
-
messages = output["response"]
|
| 190 |
-
if messages and len(messages) > 0:
|
| 191 |
-
last_msg = messages[-1]
|
| 192 |
-
chunk = {
|
| 193 |
-
"type": "complete",
|
| 194 |
-
"data": last_msg.content if hasattr(last_msg, 'content') else str(last_msg),
|
| 195 |
-
"thread_id": request.thread_id
|
| 196 |
-
}
|
| 197 |
-
yield f"data: {json.dumps(chunk)}\n\n"
|
| 198 |
-
|
| 199 |
-
# Send completion signal
|
| 200 |
-
yield f"data: {json.dumps({'type': 'done', 'thread_id': request.thread_id})}\n\n"
|
| 201 |
-
|
| 202 |
-
except Exception as e:
|
| 203 |
-
error_data = {
|
| 204 |
-
"type": "error",
|
| 205 |
-
"error": str(e),
|
| 206 |
-
"thread_id": request.thread_id
|
| 207 |
-
}
|
| 208 |
-
yield f"data: {json.dumps(error_data)}\n\n"
|
| 209 |
-
|
| 210 |
-
return StreamingResponse(
|
| 211 |
-
event_generator(),
|
| 212 |
-
media_type="text/event-stream",
|
| 213 |
-
headers={
|
| 214 |
-
"Cache-Control": "no-cache",
|
| 215 |
-
"Connection": "keep-alive",
|
| 216 |
-
"X-Accel-Buffering": "no"
|
| 217 |
-
}
|
| 218 |
-
)
|
| 219 |
-
|
| 220 |
-
# ---------------- STT ---------------- #
|
| 221 |
-
@app.post("/stt")
|
| 222 |
-
async def transcribe_audio(file: UploadFile = File(...)):
|
| 223 |
-
try:
|
| 224 |
-
return await STT(file)
|
| 225 |
-
except Exception as e:
|
| 226 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 227 |
-
|
| 228 |
-
# ---------------- TTS ---------------- #
|
| 229 |
-
@app.post("/tts")
|
| 230 |
-
async def text_to_speech(req: TTSRequest):
|
| 231 |
-
try:
|
| 232 |
-
audio_path = await TTS(req.text, req.voice)
|
| 233 |
-
return FileResponse(audio_path, media_type="audio/mpeg", filename="output.mp3")
|
| 234 |
-
|
| 235 |
-
except Exception as e:
|
| 236 |
-
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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