NanobotzAI commited on
Commit
d7b350d
·
verified ·
1 Parent(s): a94b857

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -14
app.py CHANGED
@@ -1,6 +1,4 @@
1
- from fastapi import FastAPI, Query
2
- from fastapi.responses import FileResponse, JSONResponse
3
- import uvicorn
4
  import fitz # PyMuPDF for PDF text extraction
5
  import faiss # FAISS for vector search
6
  import numpy as np
@@ -25,17 +23,22 @@ index = faiss.IndexFlatL2(vector_dim) # FAISS index
25
 
26
  documents = [] # Store extracted text
27
 
28
- app = FastAPI()
29
 
30
- @app.get("/")
31
  def serve_homepage():
32
  """Serves the HTML interface."""
33
- return FileResponse("index.html")
34
 
35
- @app.post("/upload_pdf/")
36
- async def upload_pdf(file_path: str):
37
  """Handles PDF file processing."""
38
  global documents
 
 
 
 
 
39
 
40
  # Extract text from PDF
41
  doc = fitz.open(file_path)
@@ -46,13 +49,15 @@ async def upload_pdf(file_path: str):
46
  embeddings = embed_model.encode(text_chunks)
47
  index.add(np.array(embeddings, dtype=np.float32))
48
 
49
- return JSONResponse({"message": "PDF uploaded and indexed successfully!"})
50
 
51
- @app.get("/chat/")
52
- def chat_with_pdf(msg: str = Query(..., title="User Message")):
53
  """Handles user queries and returns AI-generated responses."""
 
 
54
  if not documents:
55
- return JSONResponse({"response": "Please upload a PDF first."})
56
 
57
  # Retrieve relevant context
58
  query_embedding = embed_model.encode([msg])
@@ -76,7 +81,7 @@ def chat_with_pdf(msg: str = Query(..., title="User Message")):
76
  token = chunk.choices[0].delta.content or ""
77
  response_text += token
78
 
79
- return JSONResponse({"response": response_text})
80
 
81
  if __name__ == "__main__":
82
- uvicorn.run(app, host="0.0.0.0", port=8000)
 
1
+ from flask import Flask, request, jsonify, send_from_directory
 
 
2
  import fitz # PyMuPDF for PDF text extraction
3
  import faiss # FAISS for vector search
4
  import numpy as np
 
23
 
24
  documents = [] # Store extracted text
25
 
26
+ app = Flask(__name__)
27
 
28
+ @app.route("/")
29
  def serve_homepage():
30
  """Serves the HTML interface."""
31
+ return send_from_directory(os.getcwd(), 'index.html')
32
 
33
+ @app.route("/upload_pdf/", methods=["POST"])
34
+ def upload_pdf():
35
  """Handles PDF file processing."""
36
  global documents
37
+ file = request.files['file']
38
+
39
+ # Save the uploaded file temporarily
40
+ file_path = os.path.join(os.getcwd(), file.filename)
41
+ file.save(file_path)
42
 
43
  # Extract text from PDF
44
  doc = fitz.open(file_path)
 
49
  embeddings = embed_model.encode(text_chunks)
50
  index.add(np.array(embeddings, dtype=np.float32))
51
 
52
+ return jsonify({"message": "PDF uploaded and indexed successfully!"})
53
 
54
+ @app.route("/chat/", methods=["GET"])
55
+ def chat_with_pdf():
56
  """Handles user queries and returns AI-generated responses."""
57
+ msg = request.args.get("msg")
58
+
59
  if not documents:
60
+ return jsonify({"response": "Please upload a PDF first."})
61
 
62
  # Retrieve relevant context
63
  query_embedding = embed_model.encode([msg])
 
81
  token = chunk.choices[0].delta.content or ""
82
  response_text += token
83
 
84
+ return jsonify({"response": response_text})
85
 
86
  if __name__ == "__main__":
87
+ app.run(host="0.0.0.0", port=8000)