Saif Rehman Nasir commited on
Commit
8084a16
·
1 Parent(s): 2189879

Refactor code

Browse files
Files changed (1) hide show
  1. app.py +5 -240
app.py CHANGED
@@ -53,249 +53,14 @@ def process_audio(audio_data):
53
  return recognized_text, ""
54
 
55
 
56
- # Define a function to disable the button and display a loading indicator
57
- def disable_components():
58
- # Update recognized_text content, indicating that processing is ongoing
59
- recognized_text_update = gr.update(value="Voice Recognition Running...")
60
- # Disable process_button
61
- process_button_update = gr.update(interactive=False)
62
- # Display loading animation
63
- loading_animation_update = gr.update(visible=True)
64
- return recognized_text_update, process_button_update, loading_animation_update
65
-
66
-
67
- # Define a function to enable the button and hide the loading indicator
68
- def enable_components(recognized_text):
69
- process_button_update = gr.update(interactive=True)
70
- # Hide loading animation
71
- loading_animation_update = gr.update(visible=False)
72
- return recognized_text, process_button_update, loading_animation_update
73
-
74
-
75
- # Define a function to disable the button and display a loading indicator
76
- def disable_chatbot_components():
77
- textbox = gr.update(interactive=False)
78
- submit_btn = gr.update(interactive=False)
79
- btn1 = gr.update(interactive=False)
80
- btn2 = gr.update(interactive=False)
81
- btn3 = gr.update(interactive=False)
82
- btn4 = gr.update(interactive=False)
83
- return textbox, submit_btn, btn1, btn2, btn3, btn4
84
-
85
-
86
- # Define a function to enable the button and hide the loading indicator
87
- def enable_chatbot_components():
88
- textbox = gr.update(interactive=True)
89
- submit_btn = gr.update(interactive=True)
90
- btn1 = gr.update(interactive=True)
91
- btn2 = gr.update(interactive=True)
92
- btn3 = gr.update(interactive=True)
93
- btn4 = gr.update(interactive=True)
94
- return textbox, submit_btn, btn1, btn2, btn3, btn4
95
-
96
-
97
- llama_responded = 0
98
- responded_answer = ""
99
-
100
-
101
- def respond(message, history: list[tuple[str, str]]):
102
- global llama_responded
103
- global responded_answer
104
- # Main Decision Module
105
- decision_response = ""
106
- judge_main_message = f"Here is a query: '{message}', Determine if this query is asking about one of the topics included in the list below. If it is, please directly provide only one name of the topic; otherwise, you reply 'no'. The list of topics is: [movie, music]"
107
- m_message = [{"role": "user", "content": judge_main_message}]
108
- for m in client.chat_completion(
109
- m_message,
110
- stream=True,
111
- ):
112
- token = m.choices[0].delta.content
113
- decision_response += token
114
- print(decision_response)
115
-
116
- if "movie" in decision_response:
117
- movie_client = Client("ironserengety/movies-recommender")
118
- result = movie_client.predict(
119
- message=message,
120
- system_message="You are a movie recommender named 'Exodia'. You are extremely reliable. You always mention your name in the beginning of conversation. You will provide me with answers from the given info. Give not more than 3 choices and make sure that answers are complete sentences.",
121
- max_tokens=512,
122
- temperature=0.7,
123
- top_p=0.95,
124
- api_name="/chat",
125
- )
126
- print(result)
127
- llama_responded = 1
128
- responded_answer = result
129
- return result
130
-
131
- # elif "music" in decision_response:
132
- elif "music" in decision_response:
133
- responded_answer = message
134
-
135
- return responded_answer
136
- else:
137
- # others
138
- system_message = "You are a helpful chatbot that answers questions. Give any answer within 50 words."
139
- messages = [{"role": "system", "content": system_message}]
140
-
141
- for val in history:
142
- print(val[0])
143
- if val[0] != None:
144
- if val[0]:
145
- messages.append({"role": "user", "content": val[0]})
146
- if val[1]:
147
- messages.append({"role": "assistant", "content": val[1]})
148
- messages.append({"role": "user", "content": message})
149
-
150
- response = ""
151
- print(messages)
152
-
153
- for message in client.chat_completion(
154
- messages,
155
- stream=True,
156
- ):
157
- token = message.choices[0].delta.content
158
- response += token
159
-
160
- llama_responded = 1
161
- responded_answer = response
162
- return response
163
-
164
-
165
- def update_response_display():
166
- while not llama_responded:
167
- time.sleep(1)
168
-
169
-
170
- def tts_part():
171
- global llama_responded
172
- global responded_answer
173
- result = ""
174
- if responded_answer != "":
175
- text = responded_answer
176
-
177
- client = Client("tonyassi/voice-clone")
178
- result = client.predict(text, audio=file("siri.wav"), api_name="/predict")
179
- llama_responded = 0
180
- responded_answer = ""
181
-
182
- return result
183
-
184
-
185
- def create_interface():
186
- with gr.Blocks() as demo:
187
- # Chat interface using the custom chatbot instance
188
- chatbot = gr.ChatInterface(
189
- title="Exodia AI Assistant",
190
- fill_height=True,
191
- fn=respond,
192
- submit_btn="Start Chatting",
193
- )
194
- user_start = chatbot.textbox.submit(
195
- fn=update_response_display,
196
- inputs=[],
197
- outputs=[],
198
- )
199
- user_click = chatbot.submit_btn.click(
200
- fn=update_response_display,
201
- inputs=[],
202
- outputs=[],
203
- )
204
-
205
- # Audio input section
206
- with gr.Row():
207
- audio_input = gr.Audio(
208
- sources="microphone",
209
- type="numpy", # Get audio data and sample rate
210
- label="Say Something...",
211
- )
212
- recognized_text = gr.Textbox(label="Recognized Text", interactive=False)
213
-
214
- # Process audio button
215
- process_button = gr.Button("Process Audio")
216
-
217
- # Loading animation
218
- loading_animation = gr.HTML(
219
- value='<div style="text-align: center;"><span style="font-size: 18px;">ASR Model is running...</span></div>',
220
- visible=False,
221
- )
222
-
223
- text_speaker = gr.Audio(label="Generated Audio")
224
-
225
- # Associate audio processing function and update component states on click
226
- process_button.click(
227
- fn=disable_components,
228
- inputs=[],
229
- outputs=[recognized_text, process_button, loading_animation],
230
- ).then(
231
- fn=process_audio,
232
- inputs=[audio_input],
233
- outputs=[recognized_text, chatbot.textbox],
234
- ).then(
235
- fn=enable_components,
236
- inputs=[recognized_text],
237
- outputs=[recognized_text, process_button, loading_animation],
238
- )
239
-
240
- user_start.then(
241
- fn=disable_chatbot_components,
242
- inputs=[],
243
- outputs=[
244
- chatbot.submit_btn,
245
- chatbot.textbox,
246
- process_button,
247
- chatbot.retry_btn,
248
- chatbot.undo_btn,
249
- chatbot.clear_btn,
250
- ],
251
- ).then(fn=tts_part, inputs=[], outputs=text_speaker).then(
252
- fn=enable_chatbot_components,
253
- inputs=[],
254
- outputs=[
255
- chatbot.submit_btn,
256
- chatbot.textbox,
257
- process_button,
258
- chatbot.retry_btn,
259
- chatbot.undo_btn,
260
- chatbot.clear_btn,
261
- ],
262
- )
263
-
264
- user_click.then(
265
- fn=disable_chatbot_components,
266
- inputs=[],
267
- outputs=[
268
- chatbot.submit_btn,
269
- chatbot.textbox,
270
- process_button,
271
- chatbot.retry_btn,
272
- chatbot.undo_btn,
273
- chatbot.clear_btn,
274
- ],
275
- ).then(fn=tts_part, inputs=[], outputs=text_speaker).then(
276
- fn=enable_chatbot_components,
277
- inputs=[],
278
- outputs=[
279
- chatbot.submit_btn,
280
- chatbot.textbox,
281
- process_button,
282
- chatbot.retry_btn,
283
- chatbot.undo_btn,
284
- chatbot.clear_btn,
285
- ],
286
- )
287
-
288
- return demo
289
-
290
-
291
  def master_decision(message):
292
  decision_response = ""
293
  judge_system_message = """You are helpful assistant. You will be given queries from the user and you decide on which domain the query belongs to. You have three domains : ["movies","music","others"]. If you don't know about the domain of a query, it is to be classified as "others". Please give a one word answer in smaller caps."""
294
-
295
  m_message = [
296
- {"role":"system","content":judge_system_message},
297
- {"role": "user", "content": message}
298
- ]
299
  for m in client.chat_completion(
300
  m_message,
301
  stream=True,
@@ -393,4 +158,4 @@ def chat_interface():
393
  if __name__ == "__main__":
394
  # demo = create_interface()
395
  demo = chat_interface()
396
- demo.launch()
 
53
  return recognized_text, ""
54
 
55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  def master_decision(message):
57
  decision_response = ""
58
  judge_system_message = """You are helpful assistant. You will be given queries from the user and you decide on which domain the query belongs to. You have three domains : ["movies","music","others"]. If you don't know about the domain of a query, it is to be classified as "others". Please give a one word answer in smaller caps."""
59
+
60
  m_message = [
61
+ {"role": "system", "content": judge_system_message},
62
+ {"role": "user", "content": message},
63
+ ]
64
  for m in client.chat_completion(
65
  m_message,
66
  stream=True,
 
158
  if __name__ == "__main__":
159
  # demo = create_interface()
160
  demo = chat_interface()
161
+ demo.launch(show_error=True)