Update main.py
Browse files
main.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
import os
|
2 |
import zipfile
|
3 |
-
import tempfile
|
4 |
from fastapi import FastAPI, HTTPException
|
5 |
from pydantic import BaseModel
|
6 |
|
@@ -19,49 +18,14 @@ vectorstore = None
|
|
19 |
retriever = None
|
20 |
chain = None
|
21 |
|
22 |
-
|
23 |
class QueryRequest(BaseModel):
|
24 |
question: str
|
25 |
|
26 |
-
|
27 |
-
def _unpack_faiss(src_path: str) -> str:
|
28 |
-
"""
|
29 |
-
If src_path is a ZIP, unzip it into a temp dir and return the folder
|
30 |
-
containing the .faiss files; if itβs already a folder, return it.
|
31 |
-
"""
|
32 |
-
if zipfile.is_zipfile(src_path):
|
33 |
-
tmp = tempfile.TemporaryDirectory()
|
34 |
-
with zipfile.ZipFile(src_path, "r") as zf:
|
35 |
-
zf.extractall(tmp.name)
|
36 |
-
for root, _, files in os.walk(tmp.name):
|
37 |
-
if any(f.endswith(".faiss") for f in files):
|
38 |
-
return root
|
39 |
-
raise RuntimeError(f"No .faiss index found inside ZIP: {src_path}")
|
40 |
-
elif os.path.isdir(src_path):
|
41 |
-
return src_path
|
42 |
-
else:
|
43 |
-
raise RuntimeError(f"Path is neither a valid ZIP nor a directory: {src_path}")
|
44 |
-
|
45 |
-
|
46 |
-
def load_and_merge_faiss(path1: str, path2: str, embeddings: HuggingFaceEmbeddings) -> FAISS:
|
47 |
-
"""
|
48 |
-
Load two FAISS indexes (either zip files or folders), merge them,
|
49 |
-
and return the combined FAISS vectorstore.
|
50 |
-
"""
|
51 |
-
dir1 = _unpack_faiss(path1)
|
52 |
-
dir2 = _unpack_faiss(path2)
|
53 |
-
|
54 |
-
vs1 = FAISS.load_local(dir1, embeddings, allow_dangerous_deserialization=True)
|
55 |
-
vs2 = FAISS.load_local(dir2, embeddings, allow_dangerous_deserialization=True)
|
56 |
-
vs1.merge_from(vs2)
|
57 |
-
return vs1
|
58 |
-
|
59 |
-
|
60 |
@app.on_event("startup")
|
61 |
def load_components():
|
62 |
global llm, embeddings, vectorstore, retriever, chain
|
63 |
|
64 |
-
#
|
65 |
llm = ChatGroq(
|
66 |
model="meta-llama/llama-4-scout-17b-16e-instruct",
|
67 |
temperature=0,
|
@@ -74,13 +38,42 @@ def load_components():
|
|
74 |
encode_kwargs={"normalize_embeddings": True},
|
75 |
)
|
76 |
|
77 |
-
#
|
78 |
-
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
-
#
|
83 |
-
retriever = vectorstore.as_retriever(search_kwargs={"k":
|
84 |
prompt = PromptTemplate(
|
85 |
template="""
|
86 |
You are an expert assistant on Islamic knowledge.
|
@@ -105,15 +98,12 @@ Your response:
|
|
105 |
return_source_documents=False,
|
106 |
chain_type_kwargs={"prompt": prompt},
|
107 |
)
|
108 |
-
|
109 |
-
print("β
Loaded & merged both FAISS indexes, QA chain ready.")
|
110 |
-
|
111 |
|
112 |
@app.get("/")
|
113 |
def root():
|
114 |
return {"message": "Arabic Hadith Finder API is up and running!"}
|
115 |
|
116 |
-
|
117 |
@app.post("/query")
|
118 |
def query(request: QueryRequest):
|
119 |
try:
|
|
|
1 |
import os
|
2 |
import zipfile
|
|
|
3 |
from fastapi import FastAPI, HTTPException
|
4 |
from pydantic import BaseModel
|
5 |
|
|
|
18 |
retriever = None
|
19 |
chain = None
|
20 |
|
|
|
21 |
class QueryRequest(BaseModel):
|
22 |
question: str
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
@app.on_event("startup")
|
25 |
def load_components():
|
26 |
global llm, embeddings, vectorstore, retriever, chain
|
27 |
|
28 |
+
# 1) Init LLM & Embeddings
|
29 |
llm = ChatGroq(
|
30 |
model="meta-llama/llama-4-scout-17b-16e-instruct",
|
31 |
temperature=0,
|
|
|
38 |
encode_kwargs={"normalize_embeddings": True},
|
39 |
)
|
40 |
|
41 |
+
# 2) Unzip & Load both FAISS vectorstores
|
42 |
+
# β First index
|
43 |
+
zip1 = "faiss_index.zip"
|
44 |
+
dir1 = "faiss_index"
|
45 |
+
if not os.path.exists(dir1):
|
46 |
+
with zipfile.ZipFile(zip1, 'r') as z:
|
47 |
+
z.extractall(dir1)
|
48 |
+
print("β
Unzipped FAISS index 1.")
|
49 |
+
vs1 = FAISS.load_local(
|
50 |
+
dir1,
|
51 |
+
embeddings,
|
52 |
+
allow_dangerous_deserialization=True
|
53 |
+
)
|
54 |
+
print("β
FAISS index 1 loaded.")
|
55 |
+
|
56 |
+
# β Second index
|
57 |
+
zip2 = "faiss_index_extra.zip"
|
58 |
+
dir2 = "faiss_index_extra"
|
59 |
+
if not os.path.exists(dir2):
|
60 |
+
with zipfile.ZipFile(zip2, 'r') as z:
|
61 |
+
z.extractall(dir2)
|
62 |
+
print("β
Unzipped FAISS index 2.")
|
63 |
+
vs2 = FAISS.load_local(
|
64 |
+
dir2,
|
65 |
+
embeddings,
|
66 |
+
allow_dangerous_deserialization=True
|
67 |
+
)
|
68 |
+
print("β
FAISS index 2 loaded.")
|
69 |
+
|
70 |
+
# 3) Merge them
|
71 |
+
vs1.merge_from(vs2)
|
72 |
+
vectorstore = vs1
|
73 |
+
print("β
Merged FAISS indexes into a single vectorstore.")
|
74 |
|
75 |
+
# 4) Create retriever & QA chain
|
76 |
+
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
|
77 |
prompt = PromptTemplate(
|
78 |
template="""
|
79 |
You are an expert assistant on Islamic knowledge.
|
|
|
98 |
return_source_documents=False,
|
99 |
chain_type_kwargs={"prompt": prompt},
|
100 |
)
|
101 |
+
print("β
QA chain ready.")
|
|
|
|
|
102 |
|
103 |
@app.get("/")
|
104 |
def root():
|
105 |
return {"message": "Arabic Hadith Finder API is up and running!"}
|
106 |
|
|
|
107 |
@app.post("/query")
|
108 |
def query(request: QueryRequest):
|
109 |
try:
|