junaid17 commited on
Commit
5099d0e
·
verified ·
1 Parent(s): ff1a601

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -109
app.py DELETED
@@ -1,109 +0,0 @@
1
- from fastapi import FastAPI, UploadFile, File, HTTPException
2
- from tools import create_rag_tool, 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 langchain_core.messages import AIMessage
8
- from fastapi.middleware.cors import CORSMiddleware
9
- import asyncio
10
- from pydantic import BaseModel
11
- from utils import TTS, STT
12
-
13
-
14
- app = FastAPI()
15
-
16
- app.add_middleware(
17
- CORSMiddleware,
18
- allow_origins=["*"],
19
- allow_credentials=True,
20
- allow_methods=["*"],
21
- allow_headers=["*"],
22
- )
23
-
24
- class TTSRequest(BaseModel):
25
- text: str
26
-
27
- class ChatRequest(BaseModel):
28
- message: str
29
- session_id: str = "default"
30
-
31
-
32
- UPLOAD_DIR = "uploads"
33
-
34
- @app.get("/")
35
- def health():
36
- return {'Status' : 'The api is live and running'}
37
-
38
- @app.post("/upload")
39
- async def upload_file(file: UploadFile = File(...)):
40
- os.makedirs(UPLOAD_DIR, exist_ok=True)
41
-
42
- file_path = os.path.join(UPLOAD_DIR, file.filename)
43
-
44
- with open(file_path, "wb") as f:
45
- f.write(await file.read())
46
-
47
- update_retriever(file_path)
48
-
49
- return {
50
- "status": "success",
51
- "filename": file.filename
52
- }
53
-
54
-
55
- @app.post("/chat")
56
- async def chat(request: ChatRequest):
57
- message = request.message
58
- session_id = request.session_id
59
- async def event_generator():
60
- async for chunk in app_graph.astream(
61
- {"messages": [HumanMessage(content=message)]},
62
- config={"configurable": {"thread_id": session_id}},
63
- stream_mode="messages"
64
- ):
65
- if len(chunk) >= 1:
66
- message_chunk = chunk[0] if isinstance(chunk, tuple) else chunk
67
- if hasattr(message_chunk, 'content') and message_chunk.content:
68
- data = str(message_chunk.content).replace("\n", "\\n")
69
- yield f"data: {data}\n\n"
70
- await asyncio.sleep(0.01)
71
-
72
- return StreamingResponse(
73
- event_generator(),
74
- media_type="text/event-stream",
75
- headers={
76
- "Cache-Control": "no-cache",
77
- "Connection": "keep-alive",
78
- "X-Accel-Buffering": "no",
79
- },
80
- )
81
- # ---------------- STT ---------------- #
82
-
83
- @app.post("/stt")
84
- async def transcribe_audio(file: UploadFile = File(...)):
85
- try:
86
- return await STT(file)
87
- except Exception as e:
88
- raise HTTPException(status_code=500, detail=str(e))
89
-
90
-
91
- @app.post("/tts")
92
- async def generate_tts(request: TTSRequest):
93
- try:
94
- if not request.text.strip():
95
- raise HTTPException(status_code=400, detail="Text is empty")
96
-
97
- audio_path = await TTS(text=request.text)
98
-
99
- if not os.path.exists(audio_path):
100
- raise HTTPException(status_code=500, detail="Audio file not created")
101
-
102
- return FileResponse(
103
- path=audio_path,
104
- media_type="audio/mpeg",
105
- filename="speech.mp3"
106
- )
107
-
108
- except Exception as e:
109
- raise HTTPException(status_code=500, detail=str(e))