Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,86 +1,83 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import os
|
3 |
-
from langchain_community.document_loaders import PyPDFLoader
|
4 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
5 |
-
from langchain.embeddings import HuggingFaceEmbeddings
|
6 |
-
from langchain.chat_models import ChatOpenAI
|
7 |
-
|
8 |
-
from langchain.retrievers.document_compressors import LLMChainExtractor
|
9 |
-
from langchain.retrievers.multi_query import MultiQueryRetriever
|
10 |
-
from langchain.retrievers import ContextualCompressionRetriever
|
11 |
-
from langchain.prompts.chat import ChatPromptTemplate, HumanMessagePromptTemplate
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
from langchain.vectorstores import Chroma
|
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 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
with gr.
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
if __name__ == "__main__":
|
86 |
-
demo.launch(share = True)
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
from langchain_community.document_loaders import PyPDFLoader
|
4 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
5 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
6 |
+
from langchain.chat_models import ChatOpenAI
|
7 |
+
|
8 |
+
from langchain.retrievers.document_compressors import LLMChainExtractor
|
9 |
+
from langchain.retrievers.multi_query import MultiQueryRetriever
|
10 |
+
from langchain.retrievers import ContextualCompressionRetriever
|
11 |
+
from langchain.prompts.chat import ChatPromptTemplate, HumanMessagePromptTemplate
|
12 |
+
|
13 |
+
|
14 |
+
|
15 |
+
from langchain.vectorstores import Chroma
|
16 |
+
|
17 |
+
|
18 |
+
chat = ChatOpenAI()
|
19 |
+
|
20 |
+
embedding_function = HuggingFaceEmbeddings(model_name = "BAAI/bge-large-en-v1.5",model_kwargs={'device': 'cpu'},encode_kwargs={"normalize_embeddings": True})
|
21 |
+
|
22 |
+
def add_docs(path):
|
23 |
+
|
24 |
+
loader = PyPDFLoader(file_path=path)
|
25 |
+
docs = loader.load_and_split(text_splitter=RecursiveCharacterTextSplitter(chunk_size = 500,
|
26 |
+
chunk_overlap = 100,
|
27 |
+
length_function = len,
|
28 |
+
is_separator_regex=False))
|
29 |
+
model_vectorstore = Chroma
|
30 |
+
db = model_vectorstore.from_documents(documents=docs,embedding= embedding_function, persist_directory="output/general_knowledge")
|
31 |
+
return db
|
32 |
+
|
33 |
+
|
34 |
+
def answer_query(message, chat_history):
|
35 |
+
base_compressor = LLMChainExtractor.from_llm(chat)
|
36 |
+
db = Chroma(persist_directory = "output/general_knowledge", embedding_function=embedding_function)
|
37 |
+
base_retriever = db.as_retriever()
|
38 |
+
mq_retriever = MultiQueryRetriever.from_llm(retriever = base_retriever, llm=chat)
|
39 |
+
compression_retriever = ContextualCompressionRetriever(base_compressor=base_compressor, base_retriever=mq_retriever)
|
40 |
+
|
41 |
+
matched_docs = compression_retriever.get_relevant_documents(query = message)
|
42 |
+
|
43 |
+
context = ""
|
44 |
+
|
45 |
+
for doc in matched_docs:
|
46 |
+
page_content = doc.page_content
|
47 |
+
context+=page_content
|
48 |
+
context += "\n\n"
|
49 |
+
template = """
|
50 |
+
Answer the following question only by using the context given below in the triple backticks, do not use any other information to answer the question.
|
51 |
+
If you can't answer the given question with the given context, you can return an emtpy string ('')
|
52 |
+
|
53 |
+
Context: ```{context}```
|
54 |
+
----------------------------
|
55 |
+
Question: {query}
|
56 |
+
----------------------------
|
57 |
+
Answer: """
|
58 |
+
|
59 |
+
human_message_prompt = HumanMessagePromptTemplate.from_template(template=template)
|
60 |
+
chat_prompt = ChatPromptTemplate.from_messages([human_message_prompt])
|
61 |
+
prompt = chat_prompt.format_prompt(query = message, context = context)
|
62 |
+
response = chat(messages=prompt.to_messages()).content
|
63 |
+
|
64 |
+
chat_history.append((message,response))
|
65 |
+
return "", chat_history
|
66 |
+
|
67 |
+
|
68 |
+
|
69 |
+
with gr.Blocks() as demo:
|
70 |
+
gr.HTML("<h1 align = 'center'>Smart Assistant</h1>")
|
71 |
+
|
72 |
+
with gr.Row():
|
73 |
+
|
74 |
+
upload_files = gr.File(label = 'Upload a PDF',file_types=['.pdf'],file_count='single')
|
75 |
+
|
76 |
+
chatbot = gr.Chatbot()
|
77 |
+
msg = gr.Textbox(label = "Enter your question here")
|
78 |
+
upload_files.upload(add_docs,upload_files)
|
79 |
+
msg.submit(answer_query,[msg,chatbot],[msg,chatbot])
|
80 |
+
|
81 |
+
|
82 |
+
if __name__ == "__main__":
|
83 |
+
demo.launch()
|
|
|
|
|
|