Spaces:
Runtime error
Runtime error
File size: 1,609 Bytes
36fc420 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
import gradio as gr
from langchain.vectorstores import FAISS
from langchain_community.embeddings import GPT4AllEmbeddings
# Define the path to the saved FAISS index
FAISS_INDEX_PATH = "data/faiss_index.index"
# Initialize the embedding model
embedding_model = GPT4AllEmbeddings()
# Load the FAISS vector store with deserialization enabled
vector_store = FAISS.load_local(
FAISS_INDEX_PATH,
embedding_model,
allow_dangerous_deserialization=True # Ensure security if possible
)
print(f"FAISS vector store loaded from {FAISS_INDEX_PATH}")
# Initialize the retriever
retriever = vector_store.as_retriever(search_kwargs={"k": 5})
print("Retriever initialized successfully.")
def search_documents(query):
"""
Takes a user query and returns the top 5 relevant documents.
"""
results = retriever.get_relevant_documents(query)
if not results:
return "No relevant documents found."
# Format the results
formatted_results = ""
for idx, doc in enumerate(results, 1):
formatted_results += f"**Document {idx}:**\n{doc.page_content}\n\n"
return formatted_results
# Define the Gradio interface
iface = gr.Interface(
fn=search_documents,
inputs=gr.Textbox(lines=2, placeholder="Enter your query here...", label="Query"),
outputs=gr.Markdown(label="Search Results"),
title="FAISS Vector Store Search",
description="Enter a query to search through the FAISS vector store and retrieve relevant documents."
)
# Launch the Gradio app
if __name__ == "__main__":
iface.launch()
|