mdevora commited on
Commit
8c5d1ba
·
1 Parent(s): 8a1cbe0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -8
app.py CHANGED
@@ -4,6 +4,7 @@ import gradio as gr
4
  import openai
5
  from langchain.chains import RetrievalQA
6
  from langchain.document_loaders.directory import DirectoryLoader
 
7
  from langchain.text_splitter import CharacterTextSplitter
8
  import textwrap
9
  from langchain.embeddings import OpenAIEmbeddings
@@ -12,6 +13,7 @@ from langchain_community.document_loaders import UnstructuredURLLoader
12
  from langchain_core.callbacks import StreamingStdOutCallbackHandler
13
  from langchain.prompts import PromptTemplate
14
  from langchain.chat_models import ChatOpenAI
 
15
 
16
 
17
  os.environ["HUGGINGFACEHUB_API_TOKEN"] = os.getenv('HUGGINGFACEHUB_API_TOKEN')
@@ -121,17 +123,20 @@ llm = ChatOpenAI(streaming=True, callbacks=[StreamingStdOutCallbackHandler()], m
121
  prompt_template = """I will ask about tools, techniques and discoveries about the exoplanets field. You are going to
122
  receive a context with information about related works, papers and tools for the PhD of Martín Dévora Pajares,
123
  also appearing as M. Dévora-Pajares.
124
- You can only answer things based on the context and if you find that the question can't be answered from the context,
125
- just respond that you don't know.
126
  Context: {context}
127
- Question: {question}"""
 
128
 
129
  PROMPT = PromptTemplate(
130
- template=prompt_template, input_variables=["context", "question"]
131
  )
132
  chain_type_kwargs = {"prompt": PROMPT}
 
133
  qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=vector_store.as_retriever(),
134
- chain_type_kwargs=chain_type_kwargs, return_source_documents=True)
 
135
  # while True:
136
  # query = input("Question: ")
137
  # #docs = vector_store.similarity_search(query)
@@ -145,10 +150,10 @@ def chat(message, history):
145
  # print(f"Answer: {response['result']}")
146
  # print(f"Sources: {response['source_documents']}")
147
 
148
- gr.ChatInterface(chat, chatbot=gr.Chatbot(),
149
  textbox=gr.Textbox(placeholder="Ask me about Voutique!", container=False, scale=7),
150
- title="PlanetHunters tools and works information Chatbot!",
151
- description="Ask anything about SHERLOCK, MATRIX, WATSON and our history of papers!",
152
  theme="soft",
153
  examples=["What is SHERLOCK?", "What is MATRIX?", "What are the foundational papers of SHERLOCK?"],
154
  cache_examples=True,
 
4
  import openai
5
  from langchain.chains import RetrievalQA
6
  from langchain.document_loaders.directory import DirectoryLoader
7
+ from langchain.memory import ConversationBufferWindowMemory
8
  from langchain.text_splitter import CharacterTextSplitter
9
  import textwrap
10
  from langchain.embeddings import OpenAIEmbeddings
 
13
  from langchain_core.callbacks import StreamingStdOutCallbackHandler
14
  from langchain.prompts import PromptTemplate
15
  from langchain.chat_models import ChatOpenAI
16
+ from pdfminer.high_level import extract_text
17
 
18
 
19
  os.environ["HUGGINGFACEHUB_API_TOKEN"] = os.getenv('HUGGINGFACEHUB_API_TOKEN')
 
123
  prompt_template = """I will ask about tools, techniques and discoveries about the exoplanets field. You are going to
124
  receive a context with information about related works, papers and tools for the PhD of Martín Dévora Pajares,
125
  also appearing as M. Dévora-Pajares.
126
+ You can only answer things based on the context and will receive a memory section to remember previous interactions
127
+ with the user.
128
  Context: {context}
129
+ Question: {question}
130
+ Memory: {memory}"""
131
 
132
  PROMPT = PromptTemplate(
133
+ template=prompt_template, input_variables=["context", "question", "memory"]
134
  )
135
  chain_type_kwargs = {"prompt": PROMPT}
136
+ memory = ConversationBufferWindowMemory(k=4, memory_key="memory", input_key="human_input")
137
  qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=vector_store.as_retriever(),
138
+ chain_type_kwargs=chain_type_kwargs, return_source_documents=True,
139
+ memory=memory)
140
  # while True:
141
  # query = input("Question: ")
142
  # #docs = vector_store.similarity_search(query)
 
150
  # print(f"Answer: {response['result']}")
151
  # print(f"Sources: {response['source_documents']}")
152
 
153
+ gr.ChatInterface(chat, chatbot=gr.Chatbot(height=300),
154
  textbox=gr.Textbox(placeholder="Ask me about Voutique!", container=False, scale=7),
155
+ title="Ask",
156
+ description="Ask",
157
  theme="soft",
158
  examples=["What is SHERLOCK?", "What is MATRIX?", "What are the foundational papers of SHERLOCK?"],
159
  cache_examples=True,