ryanrwatkins commited on
Commit
d9be7b0
1 Parent(s): b77d45c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -116
app.py CHANGED
@@ -12,42 +12,25 @@ from PyPDF2 import PdfReader
12
  from PyPDF2 import PdfWriter
13
  from langchain.embeddings.openai import OpenAIEmbeddings
14
  from langchain.text_splitter import CharacterTextSplitter
15
- #from langchain.vectorstores import ElasticVectorSearch, Pinecone, Weaviate, FAISS
16
  from langchain.chains.question_answering import load_qa_chain
17
  from langchain.llms import OpenAI
18
-
19
  from langchain.embeddings.openai import OpenAIEmbeddings
20
- #from langchain.vectorstores import Chroma
21
- #from langchain.text_splitter import TokenTextSplitter
22
- #from langchain.llms import OpenAI
23
  from langchain import OpenAI
24
  from langchain.chat_models import ChatOpenAI
25
- #from langchain.chains import ChatVectorDBChain
26
- #from langchain.chains import RetrievalQA
27
  from langchain.document_loaders import PyPDFLoader
28
  from langchain.chains.question_answering import load_qa_chain
29
 
30
- # Use Chroma in Colab to create vector embeddings, I then saved them to HuggingFace so now I have to set it use them here.
31
- #from chromadb.config import Settings
32
- #client = chromadb.Client(Settings(
33
- ## chroma_db_impl="duckdb+parquet",
34
- # persist_directory="./embeddings" # Optional, defaults to .chromadb/ in the current directory
35
- #))
36
-
37
 
38
  openai.api_key = os.environ['openai_key']
39
  os.environ["OPENAI_API_KEY"] = os.environ['openai_key']
40
 
41
 
42
-
 
43
 
44
  def get_empty_state():
45
  return {"total_tokens": 0, "messages": []}
46
 
47
-
48
- #Initial prompt template, others added below from TXT file
49
- prompt_templates = {"All Needs Experts": "Respond as if you are combiation of all needs assessment experts."}
50
- actor_description = {"All Needs Experts": "A combiation of all needs assessment experts."}
51
 
52
  def download_prompt_templates():
53
  url = "https://huggingface.co/spaces/ryanrwatkins/needs/raw/main/gurus.txt"
@@ -62,8 +45,6 @@ def download_prompt_templates():
62
  description = row[2].strip('"')
63
  prompt_templates[act] = prompt
64
  actor_description[act] = description
65
-
66
-
67
 
68
  except requests.exceptions.RequestException as e:
69
  print(f"An error occurred while downloading prompt templates: {e}")
@@ -73,64 +54,22 @@ def download_prompt_templates():
73
  choices = choices[:1] + sorted(choices[1:])
74
  return gr.update(value=choices[0], choices=choices)
75
 
 
 
76
  def on_prompt_template_change(prompt_template):
77
  if not isinstance(prompt_template, str): return
78
  return prompt_templates[prompt_template]
79
 
 
 
80
  def on_prompt_template_change_description(prompt_template):
81
  if not isinstance(prompt_template, str): return
82
  return actor_description[prompt_template]
83
 
84
 
85
 
86
-
87
  def submit_message(prompt, prompt_template, temperature, max_tokens, context_length, state):
88
-
89
-
90
-
91
- # load in all the files
92
- #path = './files'
93
- #pdf_files = glob.glob(os.path.join(path, "*.pdf"))
94
- #pdf_files = glob.glob(os.path.join(path, "*.pdf"))
95
-
96
- #for file in pdf_files:
97
- # loader = PyPDFLoader(file)
98
- # pages = loader.load_and_split()
99
- # text_splitter = TokenTextSplitter(chunk_size=1000, chunk_overlap=0)
100
- # split_pages = text_splitter.split_documents(pages)
101
-
102
- #persist_directory = "./embeddings"
103
- #embeddings = OpenAIEmbeddings()
104
- #vectordb = Chroma.from_documents(split_pages, embeddings, persist_directory=persist_directory)
105
- #vectordb.persist()
106
-
107
- #path = './files'
108
- #pdf_files = glob.glob(os.path.join(path, "*.pdf"))
109
 
110
- #merger = PdfWriter()
111
-
112
- # add all file in the list to the merger object
113
- #for pdf in pdf_files:
114
- # merger.append(pdf)
115
- #merger.write("merged-pdf.pdf")
116
- #merger.close()
117
-
118
- #reader = PdfReader("merged-pdf.pdf")
119
- #raw_text = ''
120
- #for i, page in enumerate(reader.pages):
121
- # text = page.extract_text()
122
- # if text:
123
- # raw_text += text
124
- #text_splitter = CharacterTextSplitter(
125
- # separator = "\n",
126
- # chunk_size = 1000,
127
- # chunk_overlap = 200,
128
- # length_function = len,
129
- #)
130
- #texts = text_splitter.split_text(raw_text)
131
- #len(texts)
132
- #embeddings = OpenAIEmbeddings()
133
-
134
 
135
  history = state['messages']
136
 
@@ -147,50 +86,21 @@ def submit_message(prompt, prompt_template, temperature, max_tokens, context_len
147
 
148
 
149
  try:
150
- #completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=system_prompt + history[-context_length*2:] + [prompt_msg], temperature=temperature, max_tokens=max_tokens)
151
-
152
- # completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=system_prompt + history[-context_length*2:] + [prompt_msg], temperature=temperature, max_tokens=max_tokens)
153
-
154
- #completion_chain = load_qa_chain(ChatOpenAI(temperature=temperature, max_tokens=max_tokens, model_name="gpt-3.5-turbo"), chain_type="stuff" )
155
- #completion = RetrievalQA(combine_documents_chain=completion_chain, retriever=vectordb.as_retriever())
156
- #query = str(system_prompt + history[-context_length*2:] + [prompt_msg])
157
- #completion = completion.run(query)
158
- # from https://blog.devgenius.io/chat-with-document-s-using-openai-chatgpt-api-and-text-embedding-6a0ce3dc8bc8
159
- #completion_chain = load_qa_chain(ChatOpenAI(temperature=temperature, max_tokens=max_tokens, model_name="gpt-3.5-turbo"), chain_type="stuff" )
160
- #completion = RetrievalQA(combine_documents_chain=completion_chain, retriever=vectordb.as_retriever(), return_source_documents=False)
161
- #completion = RetrievalQA.from_chain_type(llm=ChatOpenAI(temperature=temperature, max_tokens=max_tokens, model_name="gpt-3.5-turbo"), chain_type="stuff", retriever=vectordb.as_retriever(), return_source_documents=True)
162
- #query = str(system_prompt + history[-context_length*2:] + [prompt_msg])
163
- #completion = completion({"query": query})
164
- #completion = completion.run(query)
165
-
166
- # completion = completion({"question": query, "chat_history": history[-context_length*2:]})
167
-
168
- #with open("foo.pkl", 'rb') as f:
169
- # new_docsearch = pickle.load(f)
170
-
171
- #docsearch = FAISS.from_texts(texts, embeddings)
172
  with open("embeddings.pkl", 'rb') as f:
173
  new_docsearch = pickle.load(f)
174
 
175
- #query = str(system_prompt + history[-context_length*2:] + [prompt_msg])
176
  query = str(system_prompt + history + [prompt_msg])
 
177
  docs = new_docsearch.similarity_search(query)
178
- #print(docs[0].page_content)
179
 
180
  chain = load_qa_chain(ChatOpenAI(temperature=temperature, max_tokens=max_tokens, model_name="gpt-3.5-turbo"), chain_type="stuff")
181
  completion = chain.run(input_documents=docs, question=query)
182
  completion = { "content": completion }
183
 
184
- # VectorDBQA.from_chain_type(llm=OpenAI(), chain_type="stuff", vectorstore=docsearch, return_source_documents=True)
185
- # https://colab.research.google.com/drive/1dzdNDZyofRB0f2KIB4gHXmIza7ehMX30?usp=sharing#scrollTo=b-ejDn_JfpWW
186
-
187
  get_empty_state()
188
  state.append(completion.copy())
189
-
190
- #history.append(prompt_msg.copy())
191
- #history.append(completion.copy())
192
- #history.append(completion.choices[0].message.to_dict())
193
- #history.append(completion["result"].choices[0].message.to_dict())
194
 
195
  state['total_tokens'] += completion['usage']['total_tokens']
196
 
@@ -207,15 +117,17 @@ def submit_message(prompt, prompt_template, temperature, max_tokens, context_len
207
 
208
 
209
  chat_messages = [(prompt_msg['content'], completion['content'])]
210
- #chat_messages = [(history[i]['content'], history[i+1]['content']) for i in range(0, len(history)-1, 2)]
211
- #chat_messages = [(history[-2]['content'], history[-1]['content'])]
212
 
213
  return '', chat_messages, total_tokens_used_msg, state
214
 
 
 
 
215
  def clear_conversation():
216
  return gr.update(value=None, visible=True), None, "", get_empty_state()
217
 
218
 
 
219
  css = """
220
  #col-container {max-width: 80%; margin-left: auto; margin-right: auto;}
221
  #chatbox {min-height: 400px;}
@@ -232,41 +144,33 @@ with gr.Blocks(css=css) as demo:
232
 
233
 
234
  with gr.Column(elem_id="col-container"):
235
- #with open("embeddings.pkl", 'rb') as f:
236
- # new_docsearch = pickle.load(f)
237
-
238
- #query = str("performance")
239
- #docs = new_docsearch.similarity_search(query)
240
 
241
- gr.Markdown("""## Ask questions of experts on needs assessments, get responses from a *needs assessment* version of ChatGPT.
242
  Ask questions of all of them, or pick your expert below.""" ,
243
  elem_id="header")
244
 
245
-
246
-
247
 
248
  with gr.Row():
249
  with gr.Column():
250
  chatbot = gr.Chatbot(elem_id="chatbox")
251
- #input_message = gr.Textbox(show_label=False, placeholder=docs, visible=True).style(container=False)
252
- input_message = gr.Textbox(show_label=False, placeholder="Enter your needs assessment question and press enter", visible=True).style(container=False)
253
 
254
  btn_submit = gr.Button("Submit")
255
  total_tokens_str = gr.Markdown(elem_id="total_tokens_str")
256
  btn_clear_conversation = gr.Button("Start New Conversation")
257
  with gr.Column():
258
- prompt_template = gr.Dropdown(label="Choose a expert:", choices=list(prompt_templates.keys()))
259
  prompt_template_preview = gr.Markdown(elem_id="prompt_template_preview")
260
  with gr.Accordion("Advanced parameters", open=False):
261
- temperature = gr.Slider(minimum=0, maximum=2.0, value=0.7, step=0.1, label="Flexibility", info="Higher = more creative/chaotic, Lower = just the guru")
262
- max_tokens = gr.Slider(minimum=100, maximum=400, value=200, step=1, label="Max tokens per response")
263
- context_length = gr.Slider(minimum=1, maximum=5, value=2, step=1, label="Context length", info="Number of previous questions you have asked. Be careful with high values, it can blow up the token budget quickly.")
264
 
265
 
266
  btn_submit.click(submit_message, [ input_message, prompt_template, temperature, max_tokens, context_length, state], [input_message, chatbot, total_tokens_str, state])
267
  input_message.submit(submit_message, [ input_message, prompt_template, temperature, max_tokens, context_length, state], [input_message, chatbot, total_tokens_str, state])
268
  btn_clear_conversation.click(clear_conversation, [], [input_message, chatbot, total_tokens_str, state])
269
- #prompt_template.change(on_prompt_template_change, inputs=[prompt_template], outputs=[prompt_template_preview])
270
  prompt_template.change(on_prompt_template_change_description, inputs=[prompt_template], outputs=[prompt_template_preview])
271
 
272
 
 
12
  from PyPDF2 import PdfWriter
13
  from langchain.embeddings.openai import OpenAIEmbeddings
14
  from langchain.text_splitter import CharacterTextSplitter
 
15
  from langchain.chains.question_answering import load_qa_chain
16
  from langchain.llms import OpenAI
 
17
  from langchain.embeddings.openai import OpenAIEmbeddings
 
 
 
18
  from langchain import OpenAI
19
  from langchain.chat_models import ChatOpenAI
 
 
20
  from langchain.document_loaders import PyPDFLoader
21
  from langchain.chains.question_answering import load_qa_chain
22
 
 
 
 
 
 
 
 
23
 
24
  openai.api_key = os.environ['openai_key']
25
  os.environ["OPENAI_API_KEY"] = os.environ['openai_key']
26
 
27
 
28
+ prompt_templates = {"All Needs Experts": "Respond as if you are combiation of all needs assessment experts."}
29
+ actor_description = {"All Needs Experts": "A combiation of all needs assessment experts."}
30
 
31
  def get_empty_state():
32
  return {"total_tokens": 0, "messages": []}
33
 
 
 
 
 
34
 
35
  def download_prompt_templates():
36
  url = "https://huggingface.co/spaces/ryanrwatkins/needs/raw/main/gurus.txt"
 
45
  description = row[2].strip('"')
46
  prompt_templates[act] = prompt
47
  actor_description[act] = description
 
 
48
 
49
  except requests.exceptions.RequestException as e:
50
  print(f"An error occurred while downloading prompt templates: {e}")
 
54
  choices = choices[:1] + sorted(choices[1:])
55
  return gr.update(value=choices[0], choices=choices)
56
 
57
+
58
+
59
  def on_prompt_template_change(prompt_template):
60
  if not isinstance(prompt_template, str): return
61
  return prompt_templates[prompt_template]
62
 
63
+
64
+
65
  def on_prompt_template_change_description(prompt_template):
66
  if not isinstance(prompt_template, str): return
67
  return actor_description[prompt_template]
68
 
69
 
70
 
 
71
  def submit_message(prompt, prompt_template, temperature, max_tokens, context_length, state):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
  history = state['messages']
75
 
 
86
 
87
 
88
  try:
89
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  with open("embeddings.pkl", 'rb') as f:
91
  new_docsearch = pickle.load(f)
92
 
93
+
94
  query = str(system_prompt + history + [prompt_msg])
95
+
96
  docs = new_docsearch.similarity_search(query)
 
97
 
98
  chain = load_qa_chain(ChatOpenAI(temperature=temperature, max_tokens=max_tokens, model_name="gpt-3.5-turbo"), chain_type="stuff")
99
  completion = chain.run(input_documents=docs, question=query)
100
  completion = { "content": completion }
101
 
 
 
 
102
  get_empty_state()
103
  state.append(completion.copy())
 
 
 
 
 
104
 
105
  state['total_tokens'] += completion['usage']['total_tokens']
106
 
 
117
 
118
 
119
  chat_messages = [(prompt_msg['content'], completion['content'])]
 
 
120
 
121
  return '', chat_messages, total_tokens_used_msg, state
122
 
123
+
124
+
125
+
126
  def clear_conversation():
127
  return gr.update(value=None, visible=True), None, "", get_empty_state()
128
 
129
 
130
+
131
  css = """
132
  #col-container {max-width: 80%; margin-left: auto; margin-right: auto;}
133
  #chatbox {min-height: 400px;}
 
144
 
145
 
146
  with gr.Column(elem_id="col-container"):
147
+
 
 
 
 
148
 
149
+ gr.Markdown("""## Ask questions of *needs assessment* experts,\get responses from a *needs assessment* version of ChatGPT.
150
  Ask questions of all of them, or pick your expert below.""" ,
151
  elem_id="header")
152
 
 
 
153
 
154
  with gr.Row():
155
  with gr.Column():
156
  chatbot = gr.Chatbot(elem_id="chatbox")
157
+ input_message = gr.Textbox(show_label=False, placeholder="Enter your needs assessment question", visible=True).style(container=False)
 
158
 
159
  btn_submit = gr.Button("Submit")
160
  total_tokens_str = gr.Markdown(elem_id="total_tokens_str")
161
  btn_clear_conversation = gr.Button("Start New Conversation")
162
  with gr.Column():
163
+ prompt_template = gr.Dropdown(label="Choose an Expert:", choices=list(prompt_templates.keys()))
164
  prompt_template_preview = gr.Markdown(elem_id="prompt_template_preview")
165
  with gr.Accordion("Advanced parameters", open=False):
166
+ temperature = gr.Slider(minimum=0, maximum=2.0, value=0.7, step=0.1, label="Flexibility", info="Higher = More AI, Lower = More Expert")
167
+ max_tokens = gr.Slider(minimum=100, maximum=400, value=200, step=1, label="Length of Response.")
168
+ context_length = gr.Slider(minimum=1, maximum=5, value=2, step=1, label="Context Length", info="Number of previous questions you have asked.")
169
 
170
 
171
  btn_submit.click(submit_message, [ input_message, prompt_template, temperature, max_tokens, context_length, state], [input_message, chatbot, total_tokens_str, state])
172
  input_message.submit(submit_message, [ input_message, prompt_template, temperature, max_tokens, context_length, state], [input_message, chatbot, total_tokens_str, state])
173
  btn_clear_conversation.click(clear_conversation, [], [input_message, chatbot, total_tokens_str, state])
 
174
  prompt_template.change(on_prompt_template_change_description, inputs=[prompt_template], outputs=[prompt_template_preview])
175
 
176