Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,4 @@
|
|
1 |
-
from
|
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 =
|
29 |
|
30 |
-
@app.
|
31 |
def serve_homepage():
|
32 |
"""Serves the HTML interface."""
|
33 |
-
return
|
34 |
|
35 |
-
@app.
|
36 |
-
|
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
|
50 |
|
51 |
-
@app.
|
52 |
-
def chat_with_pdf(
|
53 |
"""Handles user queries and returns AI-generated responses."""
|
|
|
|
|
54 |
if not documents:
|
55 |
-
return
|
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
|
80 |
|
81 |
if __name__ == "__main__":
|
82 |
-
|
|
|
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)
|