sagar007 commited on
Commit
c908926
·
verified ·
1 Parent(s): f216857

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -29
app.py CHANGED
@@ -223,7 +223,7 @@ def generate_speech_with_gpu(text: str, voice_name: str = 'af', tts_model=TTS_MO
223
  print(f"Error generating speech: {str(e)}")
224
  return None
225
 
226
- def process_query(query: str, history: List[List[str]], selected_voice: str = 'af') -> Dict[str, Any]:
227
  """Process user query with streaming effect"""
228
  try:
229
  if history is None:
@@ -235,13 +235,14 @@ def process_query(query: str, history: List[List[str]], selected_voice: str = 'a
235
 
236
  current_history = history + [[query, "*Searching...*"]]
237
 
238
- yield {
239
- 'answer_output': gr.Markdown("*Searching & Thinking...*"),
240
- 'sources_output': gr.HTML(sources_html),
241
- 'search_btn': gr.Button("Searching...", interactive=False),
242
- 'chat_history_display': current_history,
243
- 'audio_output': None
244
- }
 
245
 
246
  # Generate answer
247
  prompt = format_prompt(query, web_results)
@@ -253,13 +254,13 @@ def process_query(query: str, history: List[List[str]], selected_voice: str = 'a
253
 
254
  # Generate speech from the answer (only if enabled)
255
  if TTS_ENABLED:
256
- yield {
257
- 'answer_output': gr.Markdown(final_answer),
258
- 'sources_output': gr.HTML(sources_html),
259
- 'search_btn': gr.Button("Generating audio...", interactive=False),
260
- 'chat_history_display': updated_history,
261
- 'audio_output': None
262
- }
263
  try:
264
  audio = generate_speech_with_gpu(final_answer, selected_voice)
265
  if audio is None:
@@ -271,25 +272,26 @@ def process_query(query: str, history: List[List[str]], selected_voice: str = 'a
271
  final_answer += "\n\n*TTS is disabled. Audio not available.*"
272
  audio = None
273
 
274
- yield {
275
- 'answer_output': gr.Markdown(final_answer),
276
- 'sources_output': gr.HTML(sources_html),
277
- 'search_btn': gr.Button("Search", interactive=True),
278
- 'chat_history_display': updated_history,
279
- 'audio_output': audio if audio is not None else gr.Audio(value=None)
280
- }
 
281
 
282
  except Exception as e:
283
  error_message = str(e)
284
  if "GPU quota" in error_message:
285
  error_message = "⚠️ GPU quota exceeded. Please try again later when the daily quota resets."
286
- yield {
287
- 'answer_output': gr.Markdown(f"Error: {error_message}"),
288
- 'sources_output': gr.HTML(sources_html),
289
- 'search_btn': gr.Button("Search", interactive=True),
290
- 'chat_history_display': history + [[query, f"*Error: {error_message}*"]],
291
- 'audio_output': None
292
- }
293
 
294
  # Update the CSS for better contrast and readability
295
  css = """
 
223
  print(f"Error generating speech: {str(e)}")
224
  return None
225
 
226
+ def process_query(query: str, history: List[List[str]], selected_voice: str = 'af'):
227
  """Process user query with streaming effect"""
228
  try:
229
  if history is None:
 
235
 
236
  current_history = history + [[query, "*Searching...*"]]
237
 
238
+ # Yield initial searching state
239
+ yield (
240
+ "*Searching & Thinking...*", # answer_output (Markdown)
241
+ sources_html, # sources_output (HTML)
242
+ "Searching...", # search_btn (Button)
243
+ current_history, # chat_history_display (Chatbot)
244
+ None # audio_output (Audio)
245
+ )
246
 
247
  # Generate answer
248
  prompt = format_prompt(query, web_results)
 
254
 
255
  # Generate speech from the answer (only if enabled)
256
  if TTS_ENABLED:
257
+ yield (
258
+ final_answer, # answer_output
259
+ sources_html, # sources_output
260
+ "Generating audio...", # search_btn
261
+ updated_history, # chat_history_display
262
+ None # audio_output
263
+ )
264
  try:
265
  audio = generate_speech_with_gpu(final_answer, selected_voice)
266
  if audio is None:
 
272
  final_answer += "\n\n*TTS is disabled. Audio not available.*"
273
  audio = None
274
 
275
+ # Yield final result
276
+ yield (
277
+ final_answer, # answer_output
278
+ sources_html, # sources_output
279
+ "Search", # search_btn
280
+ updated_history, # chat_history_display
281
+ audio if audio is not None else None # audio_output
282
+ )
283
 
284
  except Exception as e:
285
  error_message = str(e)
286
  if "GPU quota" in error_message:
287
  error_message = "⚠️ GPU quota exceeded. Please try again later when the daily quota resets."
288
+ yield (
289
+ f"Error: {error_message}", # answer_output
290
+ sources_html, # sources_output
291
+ "Search", # search_btn
292
+ history + [[query, f"*Error: {error_message}*"]], # chat_history_display
293
+ None # audio_output
294
+ )
295
 
296
  # Update the CSS for better contrast and readability
297
  css = """