Update app.py
Browse files
app.py
CHANGED
@@ -3,10 +3,9 @@ from langchain_community.document_loaders import PyPDFLoader
|
|
3 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
4 |
from langchain_community.vectorstores import Chroma
|
5 |
from langchain.chains import ConversationalRetrievalChain
|
6 |
-
from
|
7 |
from langchain.chains import ConversationChain
|
8 |
from langchain.memory import ConversationBufferMemory
|
9 |
-
from langchain_community.llms import HuggingFaceEndpoint
|
10 |
|
11 |
from pathlib import Path
|
12 |
import chromadb
|
@@ -15,7 +14,7 @@ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
|
15 |
import re
|
16 |
|
17 |
# Constants
|
18 |
-
LLM_MODEL = "
|
19 |
DB_CHUNK_SIZE = 512
|
20 |
CHUNK_OVERLAP = 24
|
21 |
TEMPERATURE = 0.1
|
@@ -33,7 +32,7 @@ def load_doc(pdf_url, chunk_size, chunk_overlap):
|
|
33 |
|
34 |
# Create vector database
|
35 |
def create_db(splits, collection_name):
|
36 |
-
embedding = HuggingFaceEmbeddings(model_name=
|
37 |
new_client = chromadb.EphemeralClient()
|
38 |
vectordb = Chroma.from_documents(
|
39 |
documents=splits,
|
@@ -61,7 +60,7 @@ def initialize_llmchain(llm_model, vector_db, progress=gr.Progress()):
|
|
61 |
retriever = vector_db.as_retriever()
|
62 |
progress(0.8, desc="Defining retrieval chain...")
|
63 |
qa_chain = ConversationalRetrievalChain.from_llm(
|
64 |
-
pipe,
|
65 |
retriever=retriever,
|
66 |
chain_type="stuff",
|
67 |
memory=memory,
|
@@ -111,7 +110,7 @@ def conversation(qa_chain, message, history):
|
|
111 |
formatted_chat_history = format_chat_history(message, history)
|
112 |
response = qa_chain({"question": message, "chat_history": formatted_chat_history})
|
113 |
response_answer = response["answer"]
|
114 |
-
if
|
115 |
response_answer = response_answer.split("Helpful Answer:")[-1]
|
116 |
response_sources = response["source_documents"]
|
117 |
response_source1 = response_sources[0].page_content.strip()
|
@@ -135,7 +134,7 @@ def demo():
|
|
135 |
<h3>Ask any questions about your PDF documents</h3>""")
|
136 |
gr.Markdown(
|
137 |
"""<b>Note:</b> This AI assistant, using Langchain and open-source LLMs, performs retrieval-augmented generation (RAG) from your PDF documents. \
|
138 |
-
The user interface
|
139 |
This chatbot takes past questions into account when generating answers (via conversational memory), and includes document references for clarity purposes.<br>
|
140 |
<br><b>Warning:</b> This space uses the free CPU Basic hardware from Hugging Face. Some steps and LLM models used below (free inference endpoints) can take some time to generate a reply.
|
141 |
""")
|
|
|
3 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
4 |
from langchain_community.vectorstores import Chroma
|
5 |
from langchain.chains import ConversationalRetrievalChain
|
6 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
7 |
from langchain.chains import ConversationChain
|
8 |
from langchain.memory import ConversationBufferMemory
|
|
|
9 |
|
10 |
from pathlib import Path
|
11 |
import chromadb
|
|
|
14 |
import re
|
15 |
|
16 |
# Constants
|
17 |
+
LLM_MODEL = "t5-small" # Changed to a Seq2Seq model compatible with AutoModelForSeq2SeqLM
|
18 |
DB_CHUNK_SIZE = 512
|
19 |
CHUNK_OVERLAP = 24
|
20 |
TEMPERATURE = 0.1
|
|
|
32 |
|
33 |
# Create vector database
|
34 |
def create_db(splits, collection_name):
|
35 |
+
embedding = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
36 |
new_client = chromadb.EphemeralClient()
|
37 |
vectordb = Chroma.from_documents(
|
38 |
documents=splits,
|
|
|
60 |
retriever = vector_db.as_retriever()
|
61 |
progress(0.8, desc="Defining retrieval chain...")
|
62 |
qa_chain = ConversationalRetrievalChain.from_llm(
|
63 |
+
llm=pipe,
|
64 |
retriever=retriever,
|
65 |
chain_type="stuff",
|
66 |
memory=memory,
|
|
|
110 |
formatted_chat_history = format_chat_history(message, history)
|
111 |
response = qa_chain({"question": message, "chat_history": formatted_chat_history})
|
112 |
response_answer = response["answer"]
|
113 |
+
if "Helpful Answer:" in response_answer:
|
114 |
response_answer = response_answer.split("Helpful Answer:")[-1]
|
115 |
response_sources = response["source_documents"]
|
116 |
response_source1 = response_sources[0].page_content.strip()
|
|
|
134 |
<h3>Ask any questions about your PDF documents</h3>""")
|
135 |
gr.Markdown(
|
136 |
"""<b>Note:</b> This AI assistant, using Langchain and open-source LLMs, performs retrieval-augmented generation (RAG) from your PDF documents. \
|
137 |
+
The user interface explicitly shows multiple steps to help understand the RAG workflow.
|
138 |
This chatbot takes past questions into account when generating answers (via conversational memory), and includes document references for clarity purposes.<br>
|
139 |
<br><b>Warning:</b> This space uses the free CPU Basic hardware from Hugging Face. Some steps and LLM models used below (free inference endpoints) can take some time to generate a reply.
|
140 |
""")
|