Spaces:
Running
Running
fix faiss_index error
Browse files- app.py +5 -4
- requirements.txt +1 -2
app.py
CHANGED
|
@@ -22,6 +22,7 @@ from sentence_transformers import SentenceTransformer
|
|
| 22 |
import torch
|
| 23 |
from langchain_community.llms.ollama import Ollama
|
| 24 |
from langchain.prompts import ChatPromptTemplate
|
|
|
|
| 25 |
|
| 26 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 27 |
|
|
@@ -143,10 +144,10 @@ def get_faiss_semantic_index():
|
|
| 143 |
|
| 144 |
# Load embeddings from the pickle file
|
| 145 |
with open(file_path, "rb") as f:
|
| 146 |
-
|
| 147 |
|
| 148 |
print("Embeddings loaded successfully.")
|
| 149 |
-
return
|
| 150 |
|
| 151 |
# def promt_engineer(text):
|
| 152 |
PROMPT_TEMPLATE = """
|
|
@@ -184,7 +185,7 @@ Answer the question based on the above context: {question}
|
|
| 184 |
|
| 185 |
def chat_actions():
|
| 186 |
|
| 187 |
-
|
| 188 |
|
| 189 |
st.session_state["chat_history"].append(
|
| 190 |
{"role": "user", "content": st.session_state["chat_input"]},
|
|
@@ -192,7 +193,7 @@ def chat_actions():
|
|
| 192 |
|
| 193 |
# query_embedding = model.encode(st.session_state["chat_input"])
|
| 194 |
query = st.session_state["chat_input"]
|
| 195 |
-
docs =
|
| 196 |
for doc in docs:
|
| 197 |
print("\n")
|
| 198 |
print(str(doc.metadata["page"]+1) + ":", doc.page_content)
|
|
|
|
| 22 |
import torch
|
| 23 |
from langchain_community.llms.ollama import Ollama
|
| 24 |
from langchain.prompts import ChatPromptTemplate
|
| 25 |
+
from langchain_community.vectorstores import FAISS
|
| 26 |
|
| 27 |
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 28 |
|
|
|
|
| 144 |
|
| 145 |
# Load embeddings from the pickle file
|
| 146 |
with open(file_path, "rb") as f:
|
| 147 |
+
faiss_index = pickle.load(f)
|
| 148 |
|
| 149 |
print("Embeddings loaded successfully.")
|
| 150 |
+
return faiss_index
|
| 151 |
|
| 152 |
# def promt_engineer(text):
|
| 153 |
PROMPT_TEMPLATE = """
|
|
|
|
| 185 |
|
| 186 |
def chat_actions():
|
| 187 |
|
| 188 |
+
faiss_index = get_faiss_semantic_index()
|
| 189 |
|
| 190 |
st.session_state["chat_history"].append(
|
| 191 |
{"role": "user", "content": st.session_state["chat_input"]},
|
|
|
|
| 193 |
|
| 194 |
# query_embedding = model.encode(st.session_state["chat_input"])
|
| 195 |
query = st.session_state["chat_input"]
|
| 196 |
+
docs = faiss_index.similarity_search(query, k=2)
|
| 197 |
for doc in docs:
|
| 198 |
print("\n")
|
| 199 |
print(str(doc.metadata["page"]+1) + ":", doc.page_content)
|
requirements.txt
CHANGED
|
@@ -12,5 +12,4 @@ openai
|
|
| 12 |
PyMuPDF # instead of fitz
|
| 13 |
nltk
|
| 14 |
langchain_community
|
| 15 |
-
langchain
|
| 16 |
-
faiss
|
|
|
|
| 12 |
PyMuPDF # instead of fitz
|
| 13 |
nltk
|
| 14 |
langchain_community
|
| 15 |
+
langchain
|
|
|