wendru18 commited on
Commit
edf97c1
·
1 Parent(s): a75d4e9

removed globals

Browse files
Files changed (1) hide show
  1. app.py +19 -32
app.py CHANGED
@@ -11,11 +11,6 @@ import praw
11
  import os
12
  import re
13
 
14
- reddit = None
15
- bot = None
16
- chat_history = []
17
- kb = "Bot has no knowledge yet! Please enter an initial query to educate the bot."
18
-
19
  embs = TensorflowHubEmbeddings(model_url="https://tfhub.dev/google/universal-sentence-encoder/4")
20
 
21
  def set_openai_key(key):
@@ -27,8 +22,6 @@ def set_openai_key(key):
27
 
28
  def set_reddit_keys(client_id, client_secret, user_agent):
29
 
30
- global reddit
31
-
32
  # If any of the keys are empty, use the environment variables
33
  if [client_id, client_secret, user_agent] == ["", "", ""]:
34
  client_id = os.environ.get("REDDIT_CLIENT_ID")
@@ -37,6 +30,8 @@ def set_reddit_keys(client_id, client_secret, user_agent):
37
 
38
  reddit = praw.Reddit(client_id=client_id, client_secret=client_secret, user_agent=user_agent)
39
 
 
 
40
  def generate_topics(query, model="gpt-3.5-turbo"):
41
 
42
  current_date = datetime.datetime.now().strftime("%Y-%m-%d")
@@ -65,9 +60,7 @@ def generate_topics(query, model="gpt-3.5-turbo"):
65
 
66
  return topics
67
 
68
- def get_relevant_comments(topics):
69
-
70
- global reddit
71
 
72
  comments = []
73
 
@@ -117,39 +110,32 @@ def construct_bot(retriever):
117
  bot = ConversationalRetrievalChain.from_llm(OpenAI(openai_api_key=openai.api_key, temperature=0), retriever, return_source_documents=True, max_tokens_limit=2000)
118
  return bot
119
 
120
- def get_response(query, chat_history):
 
 
121
  response = bot({"question": query, "chat_history": chat_history})
122
  return response
123
 
124
  def restart():
125
-
126
- global chat_history
127
- global bot
128
-
129
- chat_history = []
130
- bot = None
131
 
132
  print("Chat history and bot knowledge has been cleared!")
133
 
134
- return None, "", "Bot has no knowledge yet! Please enter an initial query to educate the bot."
135
-
136
- def main(query, openAI_key, reddit_client_id, reddit_client_secret, reddit_user_agent):
137
 
138
- global chat_history
139
- global bot
140
- global kb
141
 
142
  set_openai_key(openAI_key)
143
- set_reddit_keys(reddit_client_id, reddit_client_secret, reddit_user_agent)
144
 
145
  if chat_history == []:
146
 
 
 
147
  print("Bot knowledge has not been initialised yet! Generating topics...")
148
  topics = generate_topics(query)
149
  kb = "Bot now has knowledge of the following topics: [" + "".join([f"{i+1}. {topic} " for i, topic in enumerate(topics)]) + "]"
150
 
151
  print("Fetching relevant comments...")
152
- comments = get_relevant_comments(topics)
153
 
154
  print("Embedding relevant comments...")
155
  retriever = construct_retriever(comments)
@@ -159,7 +145,7 @@ def main(query, openAI_key, reddit_client_id, reddit_client_secret, reddit_user_
159
 
160
  print("Bot has been constructed and is ready to use!")
161
 
162
- response = get_response(query, chat_history)
163
 
164
  answer, source_documents = response["answer"], response["source_documents"]
165
 
@@ -167,7 +153,7 @@ def main(query, openAI_key, reddit_client_id, reddit_client_secret, reddit_user_
167
 
168
  chat_history.append((query, answer))
169
 
170
- return "", kb, chat_history, source_urls
171
 
172
  # Testing only!
173
 
@@ -175,7 +161,7 @@ title = "Ask Reddit GPT 📜"
175
 
176
 
177
  with gr.Blocks() as demo:
178
-
179
  with gr.Group():
180
  gr.Markdown(f'<center><h1>{title}</h1></center>')
181
  gr.Markdown(f"Ask Reddit GPT allow you to ask about and chat with information found on Reddit. The tool uses the Reddit API to build a database of knowledge (stored in a Chroma database) and LangChain to query it. For each response, a list of potential sources are sent back. The first query you sent will take a while as it will need to build a knowledge base based on the topics concerning such query. Subsequent queries on the same topic will be much faster. If however, you would like to ask a question concerning other topics, you will need to clear out the knowledge base. To do this, click the 'Restart knowledge base' button below.")
@@ -204,8 +190,9 @@ with gr.Blocks() as demo:
204
 
205
  with gr.Group():
206
 
207
- kb = gr.Markdown(kb)
208
- chat_bot = gr.Chatbot()
 
209
 
210
  query = gr.Textbox()
211
  submit = gr.Button("Submit")
@@ -216,7 +203,7 @@ with gr.Blocks() as demo:
216
 
217
  sources = gr.Markdown()
218
 
219
- submit.click(main, [query, openAI_key, reddit_client_id, reddit_client_secret, reddit_user_agent], [query, kb, chat_bot, sources])
220
- clear.click(restart, None, [chat_bot, sources, kb], queue=False)
221
 
222
  demo.launch()
 
11
  import os
12
  import re
13
 
 
 
 
 
 
14
  embs = TensorflowHubEmbeddings(model_url="https://tfhub.dev/google/universal-sentence-encoder/4")
15
 
16
  def set_openai_key(key):
 
22
 
23
  def set_reddit_keys(client_id, client_secret, user_agent):
24
 
 
 
25
  # If any of the keys are empty, use the environment variables
26
  if [client_id, client_secret, user_agent] == ["", "", ""]:
27
  client_id = os.environ.get("REDDIT_CLIENT_ID")
 
30
 
31
  reddit = praw.Reddit(client_id=client_id, client_secret=client_secret, user_agent=user_agent)
32
 
33
+ return reddit
34
+
35
  def generate_topics(query, model="gpt-3.5-turbo"):
36
 
37
  current_date = datetime.datetime.now().strftime("%Y-%m-%d")
 
60
 
61
  return topics
62
 
63
+ def get_relevant_comments(reddit, topics):
 
 
64
 
65
  comments = []
66
 
 
110
  bot = ConversationalRetrievalChain.from_llm(OpenAI(openai_api_key=openai.api_key, temperature=0), retriever, return_source_documents=True, max_tokens_limit=2000)
111
  return bot
112
 
113
+ def get_response(bot, query, chat_history):
114
+ # Convert chat_history to a list of tuples
115
+ chat_history = [tuple(chat) for chat in chat_history]
116
  response = bot({"question": query, "chat_history": chat_history})
117
  return response
118
 
119
  def restart():
 
 
 
 
 
 
120
 
121
  print("Chat history and bot knowledge has been cleared!")
122
 
123
+ return [], "", gr.State(), "Bot has no knowledge yet! Please enter an initial query to educate the bot."
 
 
124
 
125
+ def main(query, openAI_key, reddit_client_id, reddit_client_secret, reddit_user_agent, chat_history, bot, kb):
 
 
126
 
127
  set_openai_key(openAI_key)
 
128
 
129
  if chat_history == []:
130
 
131
+ reddit = set_reddit_keys(reddit_client_id, reddit_client_secret, reddit_user_agent)
132
+
133
  print("Bot knowledge has not been initialised yet! Generating topics...")
134
  topics = generate_topics(query)
135
  kb = "Bot now has knowledge of the following topics: [" + "".join([f"{i+1}. {topic} " for i, topic in enumerate(topics)]) + "]"
136
 
137
  print("Fetching relevant comments...")
138
+ comments = get_relevant_comments(reddit, topics)
139
 
140
  print("Embedding relevant comments...")
141
  retriever = construct_retriever(comments)
 
145
 
146
  print("Bot has been constructed and is ready to use!")
147
 
148
+ response = get_response(bot, query, chat_history)
149
 
150
  answer, source_documents = response["answer"], response["source_documents"]
151
 
 
153
 
154
  chat_history.append((query, answer))
155
 
156
+ return "", kb, chat_history, source_urls, bot
157
 
158
  # Testing only!
159
 
 
161
 
162
 
163
  with gr.Blocks() as demo:
164
+
165
  with gr.Group():
166
  gr.Markdown(f'<center><h1>{title}</h1></center>')
167
  gr.Markdown(f"Ask Reddit GPT allow you to ask about and chat with information found on Reddit. The tool uses the Reddit API to build a database of knowledge (stored in a Chroma database) and LangChain to query it. For each response, a list of potential sources are sent back. The first query you sent will take a while as it will need to build a knowledge base based on the topics concerning such query. Subsequent queries on the same topic will be much faster. If however, you would like to ask a question concerning other topics, you will need to clear out the knowledge base. To do this, click the 'Restart knowledge base' button below.")
 
190
 
191
  with gr.Group():
192
 
193
+ kb = gr.Markdown("Bot has no knowledge yet! Please enter an initial query to educate the bot.")
194
+ chat_history = gr.Chatbot()
195
+ bot = gr.State()
196
 
197
  query = gr.Textbox()
198
  submit = gr.Button("Submit")
 
203
 
204
  sources = gr.Markdown()
205
 
206
+ submit.click(main, [query, openAI_key, reddit_client_id, reddit_client_secret, reddit_user_agent, chat_history, bot, kb], [query, kb, chat_history, sources, bot])
207
+ clear.click(restart, None, [chat_history, sources, bot, kb], queue=False)
208
 
209
  demo.launch()