akhaliq HF staff commited on
Commit
e65a834
1 Parent(s): 9f81a69

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -7
app.py CHANGED
@@ -84,6 +84,7 @@ def generate_response_and_audio(audio_bytes: bytes, state: AppState):
84
  audio = getattr(chunk.choices[0], 'audio', [])
85
  if content:
86
  full_response += content
 
87
  if audio:
88
  audios.extend(audio)
89
 
@@ -92,7 +93,7 @@ def generate_response_and_audio(audio_bytes: bytes, state: AppState):
92
  state.conversation.append({"role": "user", "content": "Audio input"})
93
  state.conversation.append({"role": "assistant", "content": full_response})
94
 
95
- return full_response, final_audio, state
96
 
97
  except Exception as e:
98
  raise gr.Error(f"Error during audio streaming: {e}")
@@ -110,17 +111,25 @@ def response(state: AppState):
110
  )
111
  segment.export(audio_buffer, format="wav")
112
 
113
- full_response, final_audio, updated_state = generate_response_and_audio(audio_buffer.getvalue(), state)
 
 
 
 
 
 
 
 
114
 
115
  # Update the chatbot with the final conversation
116
- chatbot_output = updated_state.conversation[-2:] # Get the last two messages (user input and AI response)
117
 
118
  # Reset the audio stream for the next interaction
119
- updated_state.stream = None
120
- updated_state.pause_start = None
121
- updated_state.last_speech = 0
122
 
123
- return chatbot_output, final_audio, updated_state
124
 
125
  def set_api_key(api_key, state):
126
  if not api_key:
 
84
  audio = getattr(chunk.choices[0], 'audio', [])
85
  if content:
86
  full_response += content
87
+ yield full_response, None, state
88
  if audio:
89
  audios.extend(audio)
90
 
 
93
  state.conversation.append({"role": "user", "content": "Audio input"})
94
  state.conversation.append({"role": "assistant", "content": full_response})
95
 
96
+ yield full_response, final_audio, state
97
 
98
  except Exception as e:
99
  raise gr.Error(f"Error during audio streaming: {e}")
 
111
  )
112
  segment.export(audio_buffer, format="wav")
113
 
114
+ generator = generate_response_and_audio(audio_buffer.getvalue(), state)
115
+
116
+ # Process the generator to get the final results
117
+ final_text = ""
118
+ final_audio = None
119
+ for text, audio, updated_state in generator:
120
+ final_text = text if text else final_text
121
+ final_audio = audio if audio else final_audio
122
+ state = updated_state
123
 
124
  # Update the chatbot with the final conversation
125
+ chatbot_output = state.conversation[-2:] # Get the last two messages (user input and AI response)
126
 
127
  # Reset the audio stream for the next interaction
128
+ state.stream = None
129
+ state.pause_start = None
130
+ state.last_speech = 0
131
 
132
+ return chatbot_output, final_audio, state
133
 
134
  def set_api_key(api_key, state):
135
  if not api_key: