Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,30 +1,11 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
-
from langchain_community.document_loaders import PyPDFLoader
|
4 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
5 |
from langchain_community.vectorstores.faiss import FAISS
|
6 |
-
from sentence_transformers import SentenceTransformer
|
7 |
from langchain.chains import RetrievalQA
|
8 |
from langchain_community.llms import HuggingFaceHub
|
9 |
-
from langchain.docstore.document import Document
|
10 |
|
11 |
-
# Load the
|
12 |
-
|
13 |
-
data = loader.load()
|
14 |
-
|
15 |
-
# Split the document into chunks
|
16 |
-
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=20)
|
17 |
-
texts = text_splitter.split_documents(data)
|
18 |
-
|
19 |
-
# Create a list of document objects from the texts
|
20 |
-
documents = [Document(page_content=doc.page_content) for doc in texts]
|
21 |
-
|
22 |
-
# Create a vector store
|
23 |
-
embeddings = SentenceTransformer("sentence-transformers/all-mpnet-base-v2")
|
24 |
-
texts = [doc.page_content for doc in documents] # Get the text content from the documents
|
25 |
-
embeddings = embeddings.encode(texts) # Get the embeddings for the texts
|
26 |
-
|
27 |
-
vector_store = FAISS.from_documents(documents, embeddings)
|
28 |
|
29 |
# Initialize the HuggingFaceHub LLM
|
30 |
llm = HuggingFaceHub(repo_id="HuggingFaceH4/zephyr-7b-beta", model_kwargs={"temperature": None, "top_p": None})
|
@@ -47,7 +28,7 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
47 |
|
48 |
messages.append({"role": "user", "content": message})
|
49 |
|
50 |
-
result = qa({"input_documents":
|
51 |
response = result["result"]
|
52 |
|
53 |
history.append((message, response))
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
3 |
from langchain_community.vectorstores.faiss import FAISS
|
|
|
4 |
from langchain.chains import RetrievalQA
|
5 |
from langchain_community.llms import HuggingFaceHub
|
|
|
6 |
|
7 |
+
# Load the vector store from the saved index files
|
8 |
+
vector_store = FAISS.load_local("db.index", embeddings=None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
# Initialize the HuggingFaceHub LLM
|
11 |
llm = HuggingFaceHub(repo_id="HuggingFaceH4/zephyr-7b-beta", model_kwargs={"temperature": None, "top_p": None})
|
|
|
28 |
|
29 |
messages.append({"role": "user", "content": message})
|
30 |
|
31 |
+
result = qa({"input_documents": vector_store.texts, "question": message})
|
32 |
response = result["result"]
|
33 |
|
34 |
history.append((message, response))
|