Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -41,9 +41,8 @@ retriever = AutoModelForSeq2SeqLM.from_pretrained(retriever_model_name)
|
|
41 |
retriever_tokenizer = AutoTokenizer.from_pretrained(retriever_model_name)
|
42 |
|
43 |
# Initialize FAISS index using LangChain
|
44 |
-
embedding_dimension = embedding_model.get_sentence_embedding_dimension()
|
45 |
hf_embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
|
46 |
-
faiss_index = FAISS(embedding_function=hf_embeddings
|
47 |
|
48 |
# Load or create FAISS index
|
49 |
index_path = "faiss_index.pkl"
|
@@ -74,7 +73,7 @@ def upload_files(files):
|
|
74 |
|
75 |
# Encode sentences and add to FAISS index
|
76 |
embeddings = embedding_model.encode(sentences)
|
77 |
-
faiss_index.add_texts(sentences
|
78 |
|
79 |
# Save the updated index
|
80 |
with open(index_path, "wb") as f:
|
@@ -96,10 +95,8 @@ def process_and_query(state, files, question):
|
|
96 |
question_embedding = embedding_model.encode([question])
|
97 |
|
98 |
# Search the FAISS index for similar passages
|
99 |
-
|
100 |
-
|
101 |
-
# Get the retrieved passages from the document text
|
102 |
-
retrieved_passages = [state["processed_text"].split("\n")[i] for i in retrieved_ids.flatten()]
|
103 |
|
104 |
# Use generator model to generate response based on question and retrieved passages
|
105 |
combined_input = question + " ".join(retrieved_passages)
|
@@ -132,3 +129,5 @@ with gr.Blocks() as demo:
|
|
132 |
query_button.click(fn=process_and_query, inputs=[query], outputs=query_output)
|
133 |
|
134 |
demo.launch()
|
|
|
|
|
|
41 |
retriever_tokenizer = AutoTokenizer.from_pretrained(retriever_model_name)
|
42 |
|
43 |
# Initialize FAISS index using LangChain
|
|
|
44 |
hf_embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
|
45 |
+
faiss_index = FAISS(embedding_function=hf_embeddings)
|
46 |
|
47 |
# Load or create FAISS index
|
48 |
index_path = "faiss_index.pkl"
|
|
|
73 |
|
74 |
# Encode sentences and add to FAISS index
|
75 |
embeddings = embedding_model.encode(sentences)
|
76 |
+
faiss_index.add_texts(sentences)
|
77 |
|
78 |
# Save the updated index
|
79 |
with open(index_path, "wb") as f:
|
|
|
95 |
question_embedding = embedding_model.encode([question])
|
96 |
|
97 |
# Search the FAISS index for similar passages
|
98 |
+
retrieved_results = faiss_index.similarity_search(question, k=5) # Retrieve top 5 passages
|
99 |
+
retrieved_passages = [result['text'] for result in retrieved_results]
|
|
|
|
|
100 |
|
101 |
# Use generator model to generate response based on question and retrieved passages
|
102 |
combined_input = question + " ".join(retrieved_passages)
|
|
|
129 |
query_button.click(fn=process_and_query, inputs=[query], outputs=query_output)
|
130 |
|
131 |
demo.launch()
|
132 |
+
|
133 |
+
|