userlele commited on
Commit
636abee
·
verified ·
1 Parent(s): 8bf5d56

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +20 -12
main.py CHANGED
@@ -1,17 +1,34 @@
1
- from fastapi import FastAPI, UploadFile, File, Form
2
  from fastapi.responses import JSONResponse
3
  import tempfile, shutil, os
4
 
5
  from llm import load_and_process_pdf, create_vectorstore, create_rag_chain, get_response
6
 
7
- app = FastAPI(title="PDF Q&A Chatbot", description="Ask questions about a PDF file using RAG.", version="1.0")
 
 
 
 
8
 
9
- # Global in-memory state for simplicity
10
  global_state = {
11
  "vectorstore": None,
12
  "rag_chain": None
13
  }
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  @app.post("/upload/")
16
  async def upload_pdf(file: UploadFile = File(...)):
17
  """Endpoint to upload and process a PDF file."""
@@ -31,12 +48,3 @@ async def upload_pdf(file: UploadFile = File(...)):
31
  return {"message": "PDF processed successfully", "filename": file.filename}
32
  finally:
33
  os.unlink(tmp_file_path)
34
-
35
- @app.post("/ask/")
36
- async def ask_question(question: str = Form(...)):
37
- """Ask a question about the previously uploaded PDF."""
38
- if not global_state["vectorstore"] or not global_state["rag_chain"]:
39
- return JSONResponse(status_code=400, content={"error": "Please upload and process a PDF first."})
40
-
41
- answer = get_response(global_state["rag_chain"], global_state["vectorstore"], question)
42
- return {"question": question, "answer": answer}
 
1
+ from fastapi import FastAPI, UploadFile, File, Form, Request
2
  from fastapi.responses import JSONResponse
3
  import tempfile, shutil, os
4
 
5
  from llm import load_and_process_pdf, create_vectorstore, create_rag_chain, get_response
6
 
7
+ app = FastAPI(
8
+ title="PDF Q&A Chatbot",
9
+ description="Ask questions about a PDF file using RAG.",
10
+ version="1.0"
11
+ )
12
 
13
+ # Global in-memory state
14
  global_state = {
15
  "vectorstore": None,
16
  "rag_chain": None
17
  }
18
 
19
+ @app.get("/")
20
+ def home():
21
+ return {"message": "Welcome to the PDF Q&A Chatbot by Bitfumes"}
22
+
23
+ @app.get("/ask")
24
+ def ask(prompt: str):
25
+ """Ask a question using the uploaded and processed PDF."""
26
+ if not global_state["vectorstore"] or not global_state["rag_chain"]:
27
+ return JSONResponse(status_code=400, content={"error": "Please upload and process a PDF first."})
28
+
29
+ answer = get_response(global_state["rag_chain"], global_state["vectorstore"], prompt)
30
+ return {"question": prompt, "answer": answer}
31
+
32
  @app.post("/upload/")
33
  async def upload_pdf(file: UploadFile = File(...)):
34
  """Endpoint to upload and process a PDF file."""
 
48
  return {"message": "PDF processed successfully", "filename": file.filename}
49
  finally:
50
  os.unlink(tmp_file_path)