Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,9 +13,10 @@ from dotenv import load_dotenv
|
|
| 13 |
from flask import jsonify
|
| 14 |
|
| 15 |
load_dotenv()
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
| 19 |
|
| 20 |
# FAISS index setup
|
| 21 |
DIM = 768 # Adjust based on the embedding model
|
|
@@ -81,8 +82,12 @@ def retrieve_document(query):
|
|
| 81 |
query_embedding = embedding_model.encode([query]).astype(np.float32)
|
| 82 |
|
| 83 |
# Search for the closest document in FAISS index
|
|
|
|
| 84 |
_, closest_idx = index.search(query_embedding, 1)
|
| 85 |
|
|
|
|
|
|
|
|
|
|
| 86 |
# Check if a relevant document was found
|
| 87 |
if closest_idx[0][0] == -1 or str(closest_idx[0][0]) not in metadata:
|
| 88 |
print("No relevant document found")
|
|
@@ -91,6 +96,8 @@ def retrieve_document(query):
|
|
| 91 |
# Retrieve the document file path
|
| 92 |
filename = metadata[str(closest_idx[0][0])]
|
| 93 |
|
|
|
|
|
|
|
| 94 |
# Read and return the document content
|
| 95 |
with open(filename, "r", encoding="utf-8") as f:
|
| 96 |
return f.read()
|
|
|
|
| 13 |
from flask import jsonify
|
| 14 |
|
| 15 |
load_dotenv()
|
| 16 |
+
API_URL = "https://e4e5-196-96-202-255.ngrok-free.app"
|
| 17 |
+
API_URL_FILES = f"{API_URL}/file"
|
| 18 |
+
API_URL_EMBEDDINGS = f"{API_URL}/embeddings"
|
| 19 |
+
API_URL_METADATA = f"{API_URL}/metadata"
|
| 20 |
|
| 21 |
# FAISS index setup
|
| 22 |
DIM = 768 # Adjust based on the embedding model
|
|
|
|
| 82 |
query_embedding = embedding_model.encode([query]).astype(np.float32)
|
| 83 |
|
| 84 |
# Search for the closest document in FAISS index
|
| 85 |
+
index = faiss.read_index(embeddings_file)
|
| 86 |
_, closest_idx = index.search(query_embedding, 1)
|
| 87 |
|
| 88 |
+
with open(metadata_file, "r") as f:
|
| 89 |
+
metadata = [json.loads(line) for line in f]
|
| 90 |
+
|
| 91 |
# Check if a relevant document was found
|
| 92 |
if closest_idx[0][0] == -1 or str(closest_idx[0][0]) not in metadata:
|
| 93 |
print("No relevant document found")
|
|
|
|
| 96 |
# Retrieve the document file path
|
| 97 |
filename = metadata[str(closest_idx[0][0])]
|
| 98 |
|
| 99 |
+
pdf_file = requests.get(API_URL_FILES, filename)
|
| 100 |
+
|
| 101 |
# Read and return the document content
|
| 102 |
with open(filename, "r", encoding="utf-8") as f:
|
| 103 |
return f.read()
|