junaid17 commited on
Commit
5d5440b
·
verified ·
1 Parent(s): 1d8d8b5

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -82
app.py DELETED
@@ -1,82 +0,0 @@
1
- from fastapi import FastAPI, UploadFile, File
2
- from fastapi.middleware.cors import CORSMiddleware
3
- from fastapi.responses import StreamingResponse, FileResponse
4
- from langchain_core.messages import HumanMessage
5
- from chatbot import app as app_graph
6
- from tools import update_retriever
7
- from utils import STT, TTS
8
- import os
9
- import asyncio
10
-
11
- app = FastAPI()
12
-
13
- # ---------------- CORS ---------------- #
14
- app.add_middleware(
15
- CORSMiddleware,
16
- allow_origins=["*"],
17
- allow_credentials=True,
18
- allow_methods=["*"],
19
- allow_headers=["*"],
20
- )
21
-
22
- UPLOAD_DIR = "uploads"
23
- os.makedirs(UPLOAD_DIR, exist_ok=True)
24
-
25
-
26
- @app.get("/")
27
- def health():
28
- return {"status": "API is running"}
29
-
30
-
31
- # =============================
32
- # Upload PDF
33
- # =============================
34
- @app.post("/upload")
35
- async def upload_file(file: UploadFile = File(...)):
36
- file_path = os.path.join(UPLOAD_DIR, file.filename)
37
-
38
- with open(file_path, "wb") as f:
39
- f.write(await file.read())
40
-
41
- update_retriever(file_path)
42
-
43
- return {
44
- "status": "success",
45
- "filename": file.filename
46
- }
47
-
48
-
49
- # =============================
50
- # Chat Endpoint
51
- # =============================
52
- @app.post("/chat")
53
- async def chat(message: str, session_id: str = "default"):
54
-
55
- async def event_stream():
56
- async for chunk in app_graph.astream(
57
- {"messages": [HumanMessage(content=message)]},
58
- config={"configurable": {"thread_id": session_id}},
59
- stream_mode="messages",
60
- ):
61
- msg = chunk[0] if isinstance(chunk, tuple) else chunk
62
- if hasattr(msg, "content") and msg.content:
63
- yield msg.content + "\n"
64
-
65
- return StreamingResponse(event_stream(), media_type="text/plain")
66
-
67
-
68
- # =============================
69
- # STT
70
- # =============================
71
- @app.post("/stt")
72
- async def stt(file: UploadFile = File(...)):
73
- return await STT(file)
74
-
75
-
76
- # =============================
77
- # TTS
78
- # =============================
79
- @app.post("/tts")
80
- async def tts(text: str):
81
- audio_path = await TTS(text)
82
- return FileResponse(audio_path)