neke-leo commited on
Commit
42b9715
1 Parent(s): 8407ed4

ENH: Add new version

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +1 -0
  2. .gitignore +0 -1
  3. all_questions_audio.json +3 -0
  4. app.py +95 -29
  5. audio/0014ea63-6970-447d-9f37-d7ec600f2a18.mp3 +0 -0
  6. audio/0040266e-5e79-41dd-943d-e8b414be79b9.mp3 +0 -0
  7. audio/007de689-48fa-4ad4-924e-a32b5604a669.mp3 +0 -0
  8. audio/00dd1484-fa96-4edd-b2c0-67a36e72814b.mp3 +0 -0
  9. audio/014d0e79-346a-4184-aa0c-fde1d6cad727.mp3 +0 -0
  10. audio/01679648-46e1-4b8d-bac4-a4efc1c03f4e.mp3 +0 -0
  11. audio/01cb8d82-b08a-4a9e-b6fb-0d23d1b1d620.mp3 +0 -0
  12. audio/0235968b-0e0e-427f-a053-d77b184d1217.mp3 +0 -0
  13. audio/02869a46-696d-4545-8286-328b2bf9c38f.mp3 +0 -0
  14. audio/03481c4a-b5bd-4cfd-b2a0-ed6e257e4612.mp3 +0 -0
  15. audio/034e0813-b5a0-4b2b-ab22-beeda411b23b.mp3 +0 -0
  16. audio/0387ccf5-bc33-4b3d-bca3-b7659b9d4541.mp3 +0 -0
  17. audio/03c1a729-f086-4874-8543-8e319c8c0a38.mp3 +0 -0
  18. audio/03dc1ece-261b-4368-b86b-144738bb9ed3.mp3 +0 -0
  19. audio/0480beb7-4513-4d37-8745-c0b0916f2f11.mp3 +0 -0
  20. audio/0552041a-5470-4a4e-b758-fcc96d5d5721.mp3 +0 -0
  21. audio/060eb813-521d-4db8-8260-4d1f34b65694.mp3 +0 -0
  22. audio/06497605-8980-411a-b885-ce688aa791eb.mp3 +0 -0
  23. audio/064a1cd8-0317-4084-a7f4-cc62c501eb25.mp3 +0 -0
  24. audio/066d87df-72a7-4637-8253-1290effb8d3a.mp3 +0 -0
  25. audio/067bb319-ede8-4c58-990e-a14f4a76bfc7.mp3 +0 -0
  26. audio/068bb98d-7be4-47bc-87f5-bfa56fc6414f.mp3 +0 -0
  27. audio/06ecc371-3970-4350-8715-ac9bbf503c2f.mp3 +0 -0
  28. audio/07042220-5613-427c-8d8b-4b4586e6cb1a.mp3 +0 -0
  29. audio/074edb8e-3c8b-45f9-86fb-8b35bddfe92d.mp3 +0 -0
  30. audio/0794c21d-b363-4dcc-974d-85ec010a6fe2.mp3 +0 -0
  31. audio/07d14014-aa0a-4646-9a14-03c444e173c3.mp3 +0 -0
  32. audio/07f8546c-cf3d-4893-b904-5afb3127dc77.mp3 +0 -0
  33. audio/0841bbb2-da04-4fd3-acad-5348ec85c912.mp3 +0 -0
  34. audio/088385df-9037-4047-bda7-ebc32886aed3.mp3 +0 -0
  35. audio/08e4286a-e5a4-4de9-9221-dd777f685078.mp3 +0 -0
  36. audio/08f5081b-7d12-47d7-bf12-d41e645a4ef6.mp3 +0 -0
  37. audio/08f6c839-018a-4e7f-802c-29ac819ef0d1.mp3 +0 -0
  38. audio/09ae3b98-7857-4505-aef6-445819aa4f92.mp3 +0 -0
  39. audio/09b2d932-cba9-4192-b356-a2c27f964055.mp3 +0 -0
  40. audio/0a047afb-a55f-4d3d-8082-513fec3507cf.mp3 +0 -0
  41. audio/0a0fb2fd-ab1c-47a8-98c2-8af70f81c191.mp3 +0 -0
  42. audio/0a3972f3-17b4-4207-97d7-2e4df35777a3.mp3 +0 -0
  43. audio/0a68b75c-59b2-45a8-9fe1-2ebc8638685b.mp3 +0 -0
  44. audio/0a92c22e-1167-444b-a8dd-e9a544bed022.mp3 +0 -0
  45. audio/0a9e947f-8ae3-4eaf-b2cf-178e8f4d1f52.mp3 +0 -0
  46. audio/0ad73ebb-db96-48d4-b428-36ee00b29ae8.mp3 +0 -0
  47. audio/0aebd9f7-7cd3-4c3d-aef1-eaa05e54d6ee.mp3 +0 -0
  48. audio/0b522ac8-4e0f-4dab-9f93-7571525ffb74.mp3 +0 -0
  49. audio/0c1552e3-b333-47cc-bee7-5149c01c85e4.mp3 +0 -0
  50. audio/0c2e0437-761c-4cdc-9874-ddbd7ffe03e0.mp3 +0 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ all_questions_audio.json filter=lfs diff=lfs merge=lfs -text
.gitignore CHANGED
@@ -1,6 +1,5 @@
1
  # ignore old_files folder
2
  old_files
3
- audio
4
  conversations
5
  # ignore pyc files
6
  *.pyc
 
1
  # ignore old_files folder
2
  old_files
 
3
  conversations
4
  # ignore pyc files
5
  *.pyc
all_questions_audio.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8413b2ff23d94e583c395e43760627b4252f6644be54df9b3c97b2523ae3f54f
3
+ size 40038038
app.py CHANGED
@@ -3,8 +3,7 @@ import os
3
 
4
  import openai
5
 
6
- from audio_utils import text_to_speech, text_to_speech_polly
7
- from openai_prompts import contextualize_question
8
  from openai_utils import get_embedding, whisper_transcription
9
  from vector_db import LanceVectorDb, QnA
10
 
@@ -12,16 +11,14 @@ db = LanceVectorDb("qna_db")
12
  OPENAI_KEY = os.environ["OPENAI_KEY"]
13
  openai.api_key = OPENAI_KEY
14
 
15
-
16
- if len(db.table.to_pandas()) == 0:
17
  print("Empty db, trying to load qna's from json file")
18
  try:
19
- db.init_from_qna_json("all_questions.json")
20
  print("Initialized db from json file")
21
  except Exception as exception:
22
  raise Exception("Failed to initialize db from json file") from exception
23
 
24
-
25
  import os
26
 
27
 
@@ -30,7 +27,7 @@ def ensure_dir(directory):
30
  os.makedirs(directory)
31
 
32
 
33
- ensure_dir("audio")
34
 
35
  import random
36
 
@@ -41,10 +38,10 @@ def red(text):
41
  return f'\x1b[31m"{text}"\x1b[0m'
42
 
43
 
44
- def query_database(prompt: str):
45
  print("Querying database for question:", prompt)
46
  embedding = get_embedding(prompt)
47
- qnas = db.get_qna(embedding, lang="en", limit=3)
48
  print("Total_qnas:", len(qnas), [qna.score for qna in qnas])
49
  qnas = [qna for qna in qnas if qna.score < 0.49]
50
  print("Filtered_qnas:", len(qnas))
@@ -57,6 +54,7 @@ available_functions = {
57
 
58
  conversation_folder = f"conversations/{random.randint(0, 10000)}"
59
  ensure_dir(conversation_folder)
 
60
 
61
  SYSTEM_PROMPT = (
62
  "You are a question answering assistant.\n"
@@ -83,30 +81,43 @@ def bot_respond(user_query, history: list):
83
 
84
  chat_messages = history["chat_messages"]
85
 
86
- user_query = contextualize_question(user_query, chat_messages)
87
 
88
- path = os.path.join(conversation_folder, f"step_{step}_qna.json")
 
 
 
 
 
89
 
90
- qnas = query_database(user_query)
 
91
 
92
- prompt = f"The user said: ---{user_query}---\n\n"
 
 
 
 
 
 
 
 
 
 
 
 
93
 
 
 
 
 
94
  context = context_format(qnas)
95
  prompt += context
96
 
97
- qna_messages = [
98
- {
99
- "role": "system",
100
- "content": SYSTEM_PROMPT,
101
- },
102
- {
103
- "role": "user",
104
- "content": prompt,
105
- },
106
- ]
107
 
108
  completion = openai.ChatCompletion.create(
109
- model="gpt-3.5-turbo", messages=qna_messages, temperature=0
110
  )
111
 
112
  response_message = completion["choices"][0]["message"]
@@ -114,6 +125,12 @@ def bot_respond(user_query, history: list):
114
 
115
  path = os.path.join(conversation_folder, f"step_{step}_qna.json")
116
 
 
 
 
 
 
 
117
  with open(path, "w") as f:
118
  json.dump(
119
  {
@@ -124,11 +141,28 @@ def bot_respond(user_query, history: list):
124
  indent=4,
125
  )
126
 
127
- chat_messages.append({"role": "assistant", "content": bot_response})
128
-
129
  step += 1
130
 
131
- return bot_response, prompt
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
 
133
 
134
  import random
@@ -143,11 +177,29 @@ def display_history(conversation):
143
  return conversation_string
144
 
145
 
 
 
 
 
 
146
  def handle_audiofile(audio_filepath: str, history: list):
147
  user_question = whisper_transcription(audio_filepath)
148
  print("Transcription", user_question)
149
 
150
- bot_response_text, context_prompt = bot_respond(user_question, history)
 
 
 
 
 
 
 
 
 
 
 
 
 
151
 
152
  if bot_response_text:
153
  lang = detect(bot_response_text)
@@ -155,12 +207,20 @@ def handle_audiofile(audio_filepath: str, history: list):
155
  else:
156
  lang = "en"
157
 
 
 
 
 
158
  if lang not in ["en", "de"]:
159
  lang = "en"
160
 
161
- output_filepath = os.path.join("audio", f"output_{random.randint(0, 1000)}.mp3")
 
 
 
162
  text_to_speech_polly(bot_response_text, lang, output_filepath)
163
 
 
164
  context_prompt += f"<<tts language>> : {lang}\n"
165
  context_prompt += f"<<tts text>> : {bot_response_text}\n"
166
 
@@ -171,6 +231,7 @@ def handle_audiofile(audio_filepath: str, history: list):
171
  context_prompt,
172
  display_history(history["chat_messages"]),
173
  output_filepath,
 
174
  )
175
 
176
 
@@ -198,6 +259,9 @@ with gr.Blocks() as demo:
198
  )
199
  conversation_history = gr.Textbox(label="Conversation history")
200
 
 
 
 
201
  # when the audio input is stopped, run the transcribe function
202
  audio_input.stop_recording(
203
  handle_audiofile,
@@ -209,9 +273,11 @@ with gr.Blocks() as demo:
209
  context_info,
210
  conversation_history,
211
  output_audio,
 
212
  ],
213
  )
214
 
 
215
  username = os.environ["GRADIO_USERNAME"]
216
  password = os.environ["GRADIO_PASSWORD"]
217
 
 
3
 
4
  import openai
5
 
6
+ from audio_utils import text_to_speech_polly
 
7
  from openai_utils import get_embedding, whisper_transcription
8
  from vector_db import LanceVectorDb, QnA
9
 
 
11
  OPENAI_KEY = os.environ["OPENAI_KEY"]
12
  openai.api_key = OPENAI_KEY
13
 
14
+ if not db.table or len(db.table.to_pandas()) == 0:
 
15
  print("Empty db, trying to load qna's from json file")
16
  try:
17
+ db.init_from_qna_json("all_questions_audio.json")
18
  print("Initialized db from json file")
19
  except Exception as exception:
20
  raise Exception("Failed to initialize db from json file") from exception
21
 
 
22
  import os
23
 
24
 
 
27
  os.makedirs(directory)
28
 
29
 
30
+ ensure_dir("audio_temp")
31
 
32
  import random
33
 
 
38
  return f'\x1b[31m"{text}"\x1b[0m'
39
 
40
 
41
+ def query_database(prompt: str, filters: dict = {}):
42
  print("Querying database for question:", prompt)
43
  embedding = get_embedding(prompt)
44
+ qnas = db.get_qna(embedding, filters=filters, limit=3)
45
  print("Total_qnas:", len(qnas), [qna.score for qna in qnas])
46
  qnas = [qna for qna in qnas if qna.score < 0.49]
47
  print("Filtered_qnas:", len(qnas))
 
54
 
55
  conversation_folder = f"conversations/{random.randint(0, 10000)}"
56
  ensure_dir(conversation_folder)
57
+ print("Conversation", conversation_folder)
58
 
59
  SYSTEM_PROMPT = (
60
  "You are a question answering assistant.\n"
 
81
 
82
  chat_messages = history["chat_messages"]
83
 
84
+ qnas = query_database(user_query)
85
 
86
+ # Try to match an already existing question
87
+ if any(qna.score < 0.15 for qna in qnas):
88
+ min_score = min(qna.score for qna in qnas)
89
+ qna_minscore = [qna for qna in qnas if qna.score == min_score][0]
90
+ uid: str = qna_minscore.uid
91
+ mp3_path = os.path.join("audio", f"{uid}.mp3")
92
 
93
+ if not os.path.exists(mp3_path):
94
+ text_to_speech_polly(qna_minscore.answer, qna_minscore.language, mp3_path)
95
 
96
+ chat_messages.append({"role": "user", "content": user_query})
97
+
98
+ chat_messages.append({"role": "assistant", "content": qna_minscore.answer})
99
+
100
+ return {
101
+ "type": "cached_response",
102
+ "mp3_path": mp3_path,
103
+ "bot_response": qna_minscore.answer,
104
+ "prompt": "No chatbot response, cached response from database",
105
+ }
106
+
107
+ # Search only the base images
108
+ qnas = query_database(user_query, filters={"source": "base"})
109
 
110
+ # Use chatgpt to answer the question
111
+ path = os.path.join(conversation_folder, f"step_{step}_qna.json")
112
+
113
+ prompt = f"The user said: ---{user_query}---\n\n"
114
  context = context_format(qnas)
115
  prompt += context
116
 
117
+ chat_messages.append({"role": "user", "content": user_query})
 
 
 
 
 
 
 
 
 
118
 
119
  completion = openai.ChatCompletion.create(
120
+ model="gpt-4", messages=chat_messages, temperature=0
121
  )
122
 
123
  response_message = completion["choices"][0]["message"]
 
125
 
126
  path = os.path.join(conversation_folder, f"step_{step}_qna.json")
127
 
128
+ # remove the last message
129
+ chat_messages.pop(-1)
130
+
131
+ chat_messages.append({"role": "user", "content": user_query})
132
+ chat_messages.append({"role": "assistant", "content": bot_response})
133
+
134
  with open(path, "w") as f:
135
  json.dump(
136
  {
 
141
  indent=4,
142
  )
143
 
 
 
144
  step += 1
145
 
146
+ data = {
147
+ "type": "openai",
148
+ "bot_response": bot_response,
149
+ "prompt": prompt,
150
+ }
151
+
152
+ return data
153
+
154
+
155
+ def add_question(question):
156
+ if os.path.exists("runtime_questions.json"):
157
+ with open("runtime_questions.json") as f:
158
+ questions = json.load(f)
159
+ else:
160
+ questions = []
161
+
162
+ questions.append(question)
163
+
164
+ with open("runtime_questions.json", "w") as f:
165
+ json.dump(questions, f, indent=4, ensure_ascii=False)
166
 
167
 
168
  import random
 
177
  return conversation_string
178
 
179
 
180
+ if not os.path.exists("runtime_questions.json"):
181
+ with open("runtime_questions.json", "w") as f:
182
+ json.dump([], f)
183
+
184
+
185
  def handle_audiofile(audio_filepath: str, history: list):
186
  user_question = whisper_transcription(audio_filepath)
187
  print("Transcription", user_question)
188
 
189
+ res = bot_respond(user_question, history)
190
+
191
+ if res["type"] == "cached_response":
192
+ return (
193
+ user_question,
194
+ res["bot_response"],
195
+ history,
196
+ res["prompt"],
197
+ display_history(history["chat_messages"]),
198
+ res["mp3_path"],
199
+ )
200
+ else:
201
+ bot_response_text = res["bot_response"]
202
+ prompt = res["prompt"]
203
 
204
  if bot_response_text:
205
  lang = detect(bot_response_text)
 
207
  else:
208
  lang = "en"
209
 
210
+ add_question(
211
+ {"question": user_question, "answer": bot_response_text, "language": lang}
212
+ )
213
+
214
  if lang not in ["en", "de"]:
215
  lang = "en"
216
 
217
+ output_filepath = os.path.join(
218
+ "audio_temp", f"output_{random.randint(0, 1000)}.mp3"
219
+ )
220
+
221
  text_to_speech_polly(bot_response_text, lang, output_filepath)
222
 
223
+ context_prompt = prompt
224
  context_prompt += f"<<tts language>> : {lang}\n"
225
  context_prompt += f"<<tts text>> : {bot_response_text}\n"
226
 
 
231
  context_prompt,
232
  display_history(history["chat_messages"]),
233
  output_filepath,
234
+ "runtime_questions.json",
235
  )
236
 
237
 
 
259
  )
260
  conversation_history = gr.Textbox(label="Conversation history")
261
 
262
+ with gr.Row():
263
+ file_output = gr.File(label="Download questions file", download=True)
264
+
265
  # when the audio input is stopped, run the transcribe function
266
  audio_input.stop_recording(
267
  handle_audiofile,
 
273
  context_info,
274
  conversation_history,
275
  output_audio,
276
+ file_output,
277
  ],
278
  )
279
 
280
+
281
  username = os.environ["GRADIO_USERNAME"]
282
  password = os.environ["GRADIO_PASSWORD"]
283
 
audio/0014ea63-6970-447d-9f37-d7ec600f2a18.mp3 ADDED
Binary file (55.8 kB). View file
 
audio/0040266e-5e79-41dd-943d-e8b414be79b9.mp3 ADDED
Binary file (29.6 kB). View file
 
audio/007de689-48fa-4ad4-924e-a32b5604a669.mp3 ADDED
Binary file (88.2 kB). View file
 
audio/00dd1484-fa96-4edd-b2c0-67a36e72814b.mp3 ADDED
Binary file (22.7 kB). View file
 
audio/014d0e79-346a-4184-aa0c-fde1d6cad727.mp3 ADDED
Binary file (38.3 kB). View file
 
audio/01679648-46e1-4b8d-bac4-a4efc1c03f4e.mp3 ADDED
Binary file (47.1 kB). View file
 
audio/01cb8d82-b08a-4a9e-b6fb-0d23d1b1d620.mp3 ADDED
Binary file (65.9 kB). View file
 
audio/0235968b-0e0e-427f-a053-d77b184d1217.mp3 ADDED
Binary file (32.2 kB). View file
 
audio/02869a46-696d-4545-8286-328b2bf9c38f.mp3 ADDED
Binary file (44.1 kB). View file
 
audio/03481c4a-b5bd-4cfd-b2a0-ed6e257e4612.mp3 ADDED
Binary file (155 kB). View file
 
audio/034e0813-b5a0-4b2b-ab22-beeda411b23b.mp3 ADDED
Binary file (43.1 kB). View file
 
audio/0387ccf5-bc33-4b3d-bca3-b7659b9d4541.mp3 ADDED
Binary file (26.1 kB). View file
 
audio/03c1a729-f086-4874-8543-8e319c8c0a38.mp3 ADDED
Binary file (103 kB). View file
 
audio/03dc1ece-261b-4368-b86b-144738bb9ed3.mp3 ADDED
Binary file (26.8 kB). View file
 
audio/0480beb7-4513-4d37-8745-c0b0916f2f11.mp3 ADDED
Binary file (44 kB). View file
 
audio/0552041a-5470-4a4e-b758-fcc96d5d5721.mp3 ADDED
Binary file (145 kB). View file
 
audio/060eb813-521d-4db8-8260-4d1f34b65694.mp3 ADDED
Binary file (61.2 kB). View file
 
audio/06497605-8980-411a-b885-ce688aa791eb.mp3 ADDED
Binary file (39.8 kB). View file
 
audio/064a1cd8-0317-4084-a7f4-cc62c501eb25.mp3 ADDED
Binary file (80.8 kB). View file
 
audio/066d87df-72a7-4637-8253-1290effb8d3a.mp3 ADDED
Binary file (52.2 kB). View file
 
audio/067bb319-ede8-4c58-990e-a14f4a76bfc7.mp3 ADDED
Binary file (48.3 kB). View file
 
audio/068bb98d-7be4-47bc-87f5-bfa56fc6414f.mp3 ADDED
Binary file (28.4 kB). View file
 
audio/06ecc371-3970-4350-8715-ac9bbf503c2f.mp3 ADDED
Binary file (34.3 kB). View file
 
audio/07042220-5613-427c-8d8b-4b4586e6cb1a.mp3 ADDED
Binary file (78.1 kB). View file
 
audio/074edb8e-3c8b-45f9-86fb-8b35bddfe92d.mp3 ADDED
Binary file (31.7 kB). View file
 
audio/0794c21d-b363-4dcc-974d-85ec010a6fe2.mp3 ADDED
Binary file (30.3 kB). View file
 
audio/07d14014-aa0a-4646-9a14-03c444e173c3.mp3 ADDED
Binary file (32 kB). View file
 
audio/07f8546c-cf3d-4893-b904-5afb3127dc77.mp3 ADDED
Binary file (86.4 kB). View file
 
audio/0841bbb2-da04-4fd3-acad-5348ec85c912.mp3 ADDED
Binary file (22.9 kB). View file
 
audio/088385df-9037-4047-bda7-ebc32886aed3.mp3 ADDED
Binary file (29 kB). View file
 
audio/08e4286a-e5a4-4de9-9221-dd777f685078.mp3 ADDED
Binary file (37.1 kB). View file
 
audio/08f5081b-7d12-47d7-bf12-d41e645a4ef6.mp3 ADDED
Binary file (35 kB). View file
 
audio/08f6c839-018a-4e7f-802c-29ac819ef0d1.mp3 ADDED
Binary file (74.8 kB). View file
 
audio/09ae3b98-7857-4505-aef6-445819aa4f92.mp3 ADDED
Binary file (183 kB). View file
 
audio/09b2d932-cba9-4192-b356-a2c27f964055.mp3 ADDED
Binary file (46.7 kB). View file
 
audio/0a047afb-a55f-4d3d-8082-513fec3507cf.mp3 ADDED
Binary file (37.1 kB). View file
 
audio/0a0fb2fd-ab1c-47a8-98c2-8af70f81c191.mp3 ADDED
Binary file (131 kB). View file
 
audio/0a3972f3-17b4-4207-97d7-2e4df35777a3.mp3 ADDED
Binary file (44.4 kB). View file
 
audio/0a68b75c-59b2-45a8-9fe1-2ebc8638685b.mp3 ADDED
Binary file (77.9 kB). View file
 
audio/0a92c22e-1167-444b-a8dd-e9a544bed022.mp3 ADDED
Binary file (139 kB). View file
 
audio/0a9e947f-8ae3-4eaf-b2cf-178e8f4d1f52.mp3 ADDED
Binary file (67.3 kB). View file
 
audio/0ad73ebb-db96-48d4-b428-36ee00b29ae8.mp3 ADDED
Binary file (25.8 kB). View file
 
audio/0aebd9f7-7cd3-4c3d-aef1-eaa05e54d6ee.mp3 ADDED
Binary file (35.2 kB). View file
 
audio/0b522ac8-4e0f-4dab-9f93-7571525ffb74.mp3 ADDED
Binary file (91.9 kB). View file
 
audio/0c1552e3-b333-47cc-bee7-5149c01c85e4.mp3 ADDED
Binary file (222 kB). View file
 
audio/0c2e0437-761c-4cdc-9874-ddbd7ffe03e0.mp3 ADDED
Binary file (37.8 kB). View file