jayant012 commited on
Commit
424ff00
1 Parent(s): 9a3bbbf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -86
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
- with open('../../openai_api_key.txt') as f:
18
- api_key = f.read()
19
- os.environ['OPENAI_API_KEY'] = api_key
20
-
21
- chat = ChatOpenAI()
22
-
23
- embedding_function = HuggingFaceEmbeddings(model_name = "BAAI/bge-large-en-v1.5",model_kwargs={'device': 'cpu'},encode_kwargs={"normalize_embeddings": True})
24
-
25
- def add_docs(path):
26
-
27
- loader = PyPDFLoader(file_path=path)
28
- docs = loader.load_and_split(text_splitter=RecursiveCharacterTextSplitter(chunk_size = 500,
29
- chunk_overlap = 100,
30
- length_function = len,
31
- is_separator_regex=False))
32
- model_vectorstore = Chroma
33
- db = model_vectorstore.from_documents(documents=docs,embedding= embedding_function, persist_directory="output/general_knowledge")
34
- return db
35
-
36
-
37
- def answer_query(message, chat_history):
38
- base_compressor = LLMChainExtractor.from_llm(chat)
39
- db = Chroma(persist_directory = "output/general_knowledge", embedding_function=embedding_function)
40
- base_retriever = db.as_retriever()
41
- mq_retriever = MultiQueryRetriever.from_llm(retriever = base_retriever, llm=chat)
42
- compression_retriever = ContextualCompressionRetriever(base_compressor=base_compressor, base_retriever=mq_retriever)
43
-
44
- matched_docs = compression_retriever.get_relevant_documents(query = message)
45
-
46
- context = ""
47
-
48
- for doc in matched_docs:
49
- page_content = doc.page_content
50
- context+=page_content
51
- context += "\n\n"
52
- template = """
53
- 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.
54
- If you can't answer the given question with the given context, you can return an emtpy string ('')
55
-
56
- Context: ```{context}```
57
- ----------------------------
58
- Question: {query}
59
- ----------------------------
60
- Answer: """
61
-
62
- human_message_prompt = HumanMessagePromptTemplate.from_template(template=template)
63
- chat_prompt = ChatPromptTemplate.from_messages([human_message_prompt])
64
- prompt = chat_prompt.format_prompt(query = message, context = context)
65
- response = chat(messages=prompt.to_messages()).content
66
-
67
- chat_history.append((message,response))
68
- return "", chat_history
69
-
70
-
71
-
72
- with gr.Blocks() as demo:
73
- gr.HTML("<h1 align = 'center'>Smart Assistant</h1>")
74
-
75
- with gr.Row():
76
-
77
- upload_files = gr.File(label = 'Upload a PDF',file_types=['.pdf'],file_count='single')
78
-
79
- chatbot = gr.Chatbot()
80
- msg = gr.Textbox(label = "Enter your question here")
81
- upload_files.upload(add_docs,upload_files)
82
- msg.submit(answer_query,[msg,chatbot],[msg,chatbot])
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()