jeremygracey-ai commited on
Commit
4cccc3c
·
verified ·
1 Parent(s): e292cae

Initial upload of main.py

Browse files
Files changed (1) hide show
  1. main.py +44 -0
main.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from pydantic import BaseModel
3
+ import uvicorn
4
+ from app_logic import load_llm_model, initialize_vector_db, get_rag_response
5
+
6
+ # Define the input data model
7
+ class QueryRequest(BaseModel):
8
+ question: str
9
+
10
+ # Initialize FastAPI app
11
+ app = FastAPI(title="Medical RAG API")
12
+
13
+ # Configuration paths
14
+ MODEL_PATH = "/root/.cache/huggingface/hub/models--TheBloke--Mistral-7B-Instruct-v0.1-GGUF/snapshots/731a9fc8f06f5f5e2db8a0cf9d256197eb6e05d1/mistral-7b-instruct-v0.1.Q4_K_M.gguf"
15
+ CHROMA_DIR = "./chroma_db"
16
+
17
+ # Global variables for the model and retriever
18
+ llm = None
19
+ retriever = None
20
+
21
+ @app.on_event("startup")
22
+ async def startup_event():
23
+ global llm, retriever
24
+ try:
25
+ print("Loading LLM and Vector Database...")
26
+ llm = load_llm_model(MODEL_PATH)
27
+ retriever = initialize_vector_db(CHROMA_DIR)
28
+ print("Startup complete.")
29
+ except Exception as e:
30
+ print(f"Error during startup: {e}")
31
+
32
+ @app.post("/query")
33
+ async def query_rag(request: QueryRequest):
34
+ if llm is None or retriever is None:
35
+ raise HTTPException(status_code=503, detail="Model not initialized")
36
+
37
+ try:
38
+ response = get_rag_response(request.question, llm, retriever)
39
+ return {"question": request.question, "answer": response}
40
+ except Exception as e:
41
+ raise HTTPException(status_code=500, detail=str(e))
42
+
43
+ if __name__ == "__main__":
44
+ uvicorn.run(app, host="0.0.0.0", port=8000)