Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,9 @@ import numpy as np
|
|
7 |
from huggingface_hub import InferenceClient
|
8 |
from sentence_transformers import SentenceTransformer
|
9 |
|
|
|
|
|
|
|
10 |
# Extract text from PDF
|
11 |
def extract_text_from_pdf(pdf_path):
|
12 |
doc = fitz.open(pdf_path)
|
@@ -18,7 +21,6 @@ def extract_text_from_pdf(pdf_path):
|
|
18 |
|
19 |
# Build FAISS index
|
20 |
def build_faiss_index(documents):
|
21 |
-
model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
|
22 |
document_embeddings = model.encode(documents)
|
23 |
|
24 |
index = faiss.IndexFlatL2(document_embeddings.shape[1])
|
@@ -27,19 +29,16 @@ def build_faiss_index(documents):
|
|
27 |
faiss.write_index(index, "apexcustoms_index.faiss")
|
28 |
model.save("sentence_transformer_model")
|
29 |
|
30 |
-
return index
|
31 |
|
32 |
# Ensure that text extraction and FAISS index building is done
|
33 |
if not os.path.exists("apexcustoms_index.faiss") or not os.path.exists("sentence_transformer_model"):
|
34 |
documents = extract_text_from_pdf("apexcustoms.pdf")
|
35 |
with open("apexcustoms.json", "w") as f:
|
36 |
json.dump(documents, f)
|
37 |
-
index
|
38 |
else:
|
39 |
index = faiss.read_index("apexcustoms_index.faiss")
|
40 |
-
model = SentenceTransformer('sentence_transformer_model')
|
41 |
-
with open("apexcustoms.json", "r") as f:
|
42 |
-
documents = json.load(f)
|
43 |
|
44 |
# Hugging Face client
|
45 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
@@ -50,8 +49,12 @@ def retrieve_documents(query, k=5):
|
|
50 |
return [documents[i] for i in indices[0]]
|
51 |
|
52 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
|
53 |
relevant_docs = retrieve_documents(message)
|
54 |
-
context = "\n\n".join(relevant_docs)
|
|
|
|
|
|
|
55 |
|
56 |
messages = [{"role": "system", "content": system_message},
|
57 |
{"role": "user", "content": f"Context: {context}\n\n{message}"}]
|
|
|
7 |
from huggingface_hub import InferenceClient
|
8 |
from sentence_transformers import SentenceTransformer
|
9 |
|
10 |
+
# Initialize the SentenceTransformer model
|
11 |
+
model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
|
12 |
+
|
13 |
# Extract text from PDF
|
14 |
def extract_text_from_pdf(pdf_path):
|
15 |
doc = fitz.open(pdf_path)
|
|
|
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])
|
|
|
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")
|
|
|
49 |
return [documents[i] for i in indices[0]]
|
50 |
|
51 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
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
|
57 |
+
history = history[-5:]
|
58 |
|
59 |
messages = [{"role": "system", "content": system_message},
|
60 |
{"role": "user", "content": f"Context: {context}\n\n{message}"}]
|