Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,50 +7,41 @@ import numpy as np
|
|
7 |
from huggingface_hub import InferenceClient
|
8 |
from sentence_transformers import SentenceTransformer
|
9 |
|
10 |
-
|
11 |
-
|
12 |
|
13 |
# Extract text from PDF
|
14 |
def extract_text_from_pdf(pdf_path):
|
15 |
doc = fitz.open(pdf_path)
|
16 |
-
text = ""
|
17 |
-
for page_num in range(doc.page_count):
|
18 |
-
page = doc.load_page(page_num)
|
19 |
-
text += page.get_text()
|
20 |
-
return text.split("\n\n")
|
21 |
|
22 |
# Build FAISS index
|
23 |
def build_faiss_index(documents):
|
|
|
24 |
document_embeddings = model.encode(documents)
|
25 |
|
26 |
index = faiss.IndexFlatL2(document_embeddings.shape[1])
|
27 |
-
index.add(document_embeddings)
|
28 |
-
|
29 |
faiss.write_index(index, "apexcustoms_index.faiss")
|
30 |
model.save("sentence_transformer_model")
|
31 |
|
32 |
-
return index
|
33 |
|
34 |
# Ensure that text extraction and FAISS index building is done
|
35 |
if not os.path.exists("apexcustoms_index.faiss") or not os.path.exists("sentence_transformer_model"):
|
36 |
documents = extract_text_from_pdf("apexcustoms.pdf")
|
37 |
with open("apexcustoms.json", "w") as f:
|
38 |
json.dump(documents, f)
|
39 |
-
index = build_faiss_index(documents)
|
40 |
else:
|
41 |
index = faiss.read_index("apexcustoms_index.faiss")
|
|
|
|
|
|
|
42 |
|
43 |
# Hugging Face client
|
44 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
45 |
-
|
46 |
-
def retrieve_documents(query, k=5):
|
47 |
-
query_embedding = model.encode([query])
|
48 |
-
distances, indices = index.search(query_embedding, k)
|
49 |
return [documents[i] for i in indices[0]]
|
50 |
|
51 |
-
|
52 |
-
# Retrieve relevant documents
|
53 |
-
relevant_docs = retrieve_documents(message)
|
54 |
context = "\n\n".join(relevant_docs[:3]) # Limit context to top 3 documents
|
55 |
|
56 |
# Limit history to the last 5 exchanges to reduce payload size
|
|
|
7 |
from huggingface_hub import InferenceClient
|
8 |
from sentence_transformers import SentenceTransformer
|
9 |
|
10 |
+
|
11 |
+
|
12 |
|
13 |
# Extract text from PDF
|
14 |
def extract_text_from_pdf(pdf_path):
|
15 |
doc = fitz.open(pdf_path)
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
# Build FAISS index
|
18 |
def build_faiss_index(documents):
|
19 |
+
model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
|
20 |
document_embeddings = model.encode(documents)
|
21 |
|
22 |
index = faiss.IndexFlatL2(document_embeddings.shape[1])
|
|
|
|
|
23 |
faiss.write_index(index, "apexcustoms_index.faiss")
|
24 |
model.save("sentence_transformer_model")
|
25 |
|
26 |
+
return index, model
|
27 |
|
28 |
# Ensure that text extraction and FAISS index building is done
|
29 |
if not os.path.exists("apexcustoms_index.faiss") or not os.path.exists("sentence_transformer_model"):
|
30 |
documents = extract_text_from_pdf("apexcustoms.pdf")
|
31 |
with open("apexcustoms.json", "w") as f:
|
32 |
json.dump(documents, f)
|
33 |
+
index, model = build_faiss_index(documents)
|
34 |
else:
|
35 |
index = faiss.read_index("apexcustoms_index.faiss")
|
36 |
+
model = SentenceTransformer('sentence_transformer_model')
|
37 |
+
with open("apexcustoms.json", "r") as f:
|
38 |
+
documents = json.load(f)
|
39 |
|
40 |
# Hugging Face client
|
41 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
|
|
|
|
|
|
|
|
42 |
return [documents[i] for i in indices[0]]
|
43 |
|
44 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
|
|
|
45 |
context = "\n\n".join(relevant_docs[:3]) # Limit context to top 3 documents
|
46 |
|
47 |
# Limit history to the last 5 exchanges to reduce payload size
|