Yashodeep2006 commited on
Commit
4e60bef
Β·
verified Β·
1 Parent(s): ea81ab2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -68
app.py CHANGED
@@ -1,69 +1,69 @@
1
- from fastapi.middleware.cors import CORSMiddleware
2
- from fastapi import FastAPI, HTTPException
3
- from pydantic import BaseModel, Field
4
- from fastapi.concurrency import run_in_threadpool
5
- from contextlib import asynccontextmanager
6
- from main import main
7
-
8
- # ========= Global RAG Chain =========
9
- rag_chain = None
10
-
11
- # ========= Lifespan (Startup Loader) =========
12
- @asynccontextmanager
13
- async def lifespan(app: FastAPI):
14
- global rag_chain
15
- print("πŸš€ Loading RAG model...")
16
- rag_chain = main()
17
- print("βœ… RAG model loaded successfully!")
18
- yield
19
- print("πŸ›‘ Shutting down...")
20
-
21
- # ========== Initialize FastAPI ==========
22
- app = FastAPI(
23
- title="Yashodeep's Personal Chatbot API",
24
- description="A RAG-based chatbot that answers questions about Yashodeep Hundiwale's skills, education, experience, etc.",
25
- version="1.0.0",
26
- lifespan=lifespan
27
- )
28
-
29
- # ========== Configure CORS ==========
30
- app.add_middleware(
31
- CORSMiddleware,
32
- allow_origins=["*"], # OK for now (later restrict)
33
- allow_credentials=True,
34
- allow_methods=["*"],
35
- allow_headers=["*"],
36
- )
37
-
38
- # ========== Request Schema ==========
39
- class QueryRequest(BaseModel):
40
- query: str = Field(
41
- ...,
42
- description="The user's query to the chatbot.",
43
- examples=["What is Yashodeep's educational background?"]
44
- )
45
-
46
- # ========== Routes ==========
47
- @app.get("/")
48
- def home():
49
- return {"message": "API is running πŸš€"}
50
-
51
- @app.get("/health")
52
- def health():
53
- return {"status": "ok"}
54
-
55
- @app.post("/chat")
56
- async def chat(query_request: QueryRequest):
57
- try:
58
- # Run blocking RAG in threadpool (IMPORTANT)
59
- response = await run_in_threadpool(
60
- rag_chain.invoke,
61
- {"input": query_request.query}
62
- )
63
-
64
- return {
65
- "answer": response["answer"]
66
- }
67
-
68
- except Exception as e:
69
  raise HTTPException(status_code=500, detail=str(e))
 
1
+ from fastapi.middleware.cors import CORSMiddleware
2
+ from fastapi import FastAPI, HTTPException
3
+ from pydantic import BaseModel, Field
4
+ from fastapi.concurrency import run_in_threadpool
5
+ from contextlib import asynccontextmanager
6
+ from main import main
7
+
8
+ # ========= Global RAG Chain =========
9
+ rag_chain = None
10
+
11
+ # ========= Lifespan (Startup Loader) =========
12
+ @asynccontextmanager
13
+ async def lifespan(app: FastAPI):
14
+ global rag_chain
15
+ print("πŸš€ Loading RAG model...")
16
+ rag_chain = main()
17
+ print("βœ… RAG model loaded successfully!")
18
+ yield
19
+ print("πŸ›‘ Shutting down...")
20
+
21
+ # ========== Initialize FastAPI ==========
22
+ app = FastAPI(
23
+ title="Yashodeep's Personal Chatbot API",
24
+ description="A RAG-based chatbot that answers questions about Yashodeep Hundiwale's skills, education, experience, etc.",
25
+ version="1.0.0",
26
+ lifespan=lifespan
27
+ )
28
+
29
+ # ========== Configure CORS ==========
30
+ app.add_middleware(
31
+ CORSMiddleware,
32
+ allow_origins=["*"], # OK for now (later restrict)
33
+ allow_credentials=True,
34
+ allow_methods=["*"],
35
+ allow_headers=["*"],
36
+ )
37
+
38
+ # ========== Request Schema ==========
39
+ class QueryRequest(BaseModel):
40
+ query: str = Field(
41
+ ...,
42
+ description="The user's query to the chatbot.",
43
+ examples=["What is Yashodeep's educational background?"]
44
+ )
45
+
46
+ # ========== Routes ==========
47
+ @app.get("/")
48
+ def home():
49
+ return {"message": "API is running πŸš€, Use the /chat endpoint to ask questions about me"}
50
+
51
+ @app.get("/health")
52
+ def health():
53
+ return {"status": "ok"}
54
+
55
+ @app.post("/chat")
56
+ async def chat(query_request: QueryRequest):
57
+ try:
58
+ # Run blocking RAG in threadpool (IMPORTANT)
59
+ response = await run_in_threadpool(
60
+ rag_chain.invoke,
61
+ {"input": query_request.query}
62
+ )
63
+
64
+ return {
65
+ "answer": response["answer"]
66
+ }
67
+
68
+ except Exception as e:
69
  raise HTTPException(status_code=500, detail=str(e))