Krishnavamshithumma commited on
Commit
81a0c9e
Β·
verified Β·
1 Parent(s): 1f0a91c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -7
app.py CHANGED
@@ -21,6 +21,10 @@ def transcribe_audio_and_chat(audio_filepath, history, api_key):
21
  if not api_key:
22
  raise gr.Error("❌ Please enter your OpenAI API key.")
23
 
 
 
 
 
24
  if audio_filepath is None:
25
  raise gr.Error("No audio received. Please speak into the microphone.")
26
 
@@ -37,10 +41,12 @@ def transcribe_audio_and_chat(audio_filepath, history, api_key):
37
  except sr.UnknownValueError:
38
  # If speech is unintelligible, add an assistant message to history
39
  history.append({"role": "assistant", "content": "Sorry, I could not understand the audio. Please try again."})
 
40
  return history, None
41
  except sr.RequestError as e:
42
  # If API request fails, add an assistant message to history
43
  history.append({"role": "assistant", "content": f"Could not request results from Google Speech Recognition service; {e}"})
 
44
  return history, None
45
 
46
  finally:
@@ -70,14 +76,15 @@ def transcribe_audio_and_chat(audio_filepath, history, api_key):
70
  history.append({"role": "user", "content": user_input})
71
  history.append({"role": "assistant", "content": bot_reply})
72
 
73
- # Return the updated history for the chatbot component, and clear the audio input.
74
- return history, None
 
75
 
76
  except Exception as e:
77
  print(f"An unexpected error occurred: {e}") # Log the error
78
  raise gr.Error(f"❌ An unexpected error occurred: {str(e)}")
79
 
80
- # --- Gradio UI setup ---
81
  with gr.Blocks(title="Voice Bot: Krishnavamshi Thumma") as demo:
82
  gr.Markdown("## πŸŽ™οΈ Krishnavamshi Thumma - Voice Assistant")
83
 
@@ -125,9 +132,8 @@ with gr.Blocks(title="Voice Bot: Krishnavamshi Thumma") as demo:
125
  api_key = gr.Textbox(label="πŸ” OpenAI API Key", type="password", elem_id="apiKeyInput")
126
  key_status = gr.HTML("<div id='keyStatus'></div>")
127
 
128
- # Crucially, set type="messages" here to match OpenAI's expected format
129
  chatbot = gr.Chatbot(elem_id="chatBox", type="messages", height=400)
130
- state = gr.State([]) # Now `state` will directly hold OpenAI-compatible messages
131
 
132
  audio_input = gr.Audio(
133
  sources=["microphone"],
@@ -142,7 +148,7 @@ with gr.Blocks(title="Voice Bot: Krishnavamshi Thumma") as demo:
142
  audio_input.change(
143
  transcribe_audio_and_chat,
144
  inputs=[audio_input, state, api_key],
145
- outputs=[chatbot, state]
146
  )
147
 
148
  gr.HTML("""
@@ -162,7 +168,7 @@ with gr.Blocks(title="Voice Bot: Krishnavamshi Thumma") as demo:
162
  </script>
163
  """)
164
 
165
- # When clearing, ensure state is reset to an empty list, matching the 'messages' format
166
  clear_btn.click(lambda: ([], []), None, [chatbot, state])
167
 
168
  demo.launch()
 
21
  if not api_key:
22
  raise gr.Error("❌ Please enter your OpenAI API key.")
23
 
24
+ # Always ensure history is a list, even if it somehow became None
25
+ if history is None:
26
+ history = []
27
+
28
  if audio_filepath is None:
29
  raise gr.Error("No audio received. Please speak into the microphone.")
30
 
 
41
  except sr.UnknownValueError:
42
  # If speech is unintelligible, add an assistant message to history
43
  history.append({"role": "assistant", "content": "Sorry, I could not understand the audio. Please try again."})
44
+ # Return history for chatbot, and None for audio input
45
  return history, None
46
  except sr.RequestError as e:
47
  # If API request fails, add an assistant message to history
48
  history.append({"role": "assistant", "content": f"Could not request results from Google Speech Recognition service; {e}"})
49
+ # Return history for chatbot, and None for audio input
50
  return history, None
51
 
52
  finally:
 
76
  history.append({"role": "user", "content": user_input})
77
  history.append({"role": "assistant", "content": bot_reply})
78
 
79
+ # Return the updated history for the chatbot component (state),
80
+ # and None for the audio input to clear it.
81
+ return history, None # CORRECT: Return history for 'state' output, None for audio input
82
 
83
  except Exception as e:
84
  print(f"An unexpected error occurred: {e}") # Log the error
85
  raise gr.Error(f"❌ An unexpected error occurred: {str(e)}")
86
 
87
+ # --- Gradio UI setup (no changes needed here) ---
88
  with gr.Blocks(title="Voice Bot: Krishnavamshi Thumma") as demo:
89
  gr.Markdown("## πŸŽ™οΈ Krishnavamshi Thumma - Voice Assistant")
90
 
 
132
  api_key = gr.Textbox(label="πŸ” OpenAI API Key", type="password", elem_id="apiKeyInput")
133
  key_status = gr.HTML("<div id='keyStatus'></div>")
134
 
 
135
  chatbot = gr.Chatbot(elem_id="chatBox", type="messages", height=400)
136
+ state = gr.State([]) # `state` will hold OpenAI-compatible messages
137
 
138
  audio_input = gr.Audio(
139
  sources=["microphone"],
 
148
  audio_input.change(
149
  transcribe_audio_and_chat,
150
  inputs=[audio_input, state, api_key],
151
+ outputs=[chatbot, state] # Ensure chatbot and state are updated
152
  )
153
 
154
  gr.HTML("""
 
168
  </script>
169
  """)
170
 
171
+ # When clearing, ensure state is reset to an empty list
172
  clear_btn.click(lambda: ([], []), None, [chatbot, state])
173
 
174
  demo.launch()