rodrigomasini commited on
Commit
7cdad84
·
verified ·
1 Parent(s): 202a516

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -19
app.py CHANGED
@@ -129,21 +129,21 @@ def chatbot_conversation(audio_file_path):
129
 
130
  if not user_input:
131
  print("[ERROR] No user input extracted from transcription.")
132
- return "I could not generate the text. Please try again.", None
 
133
 
134
- # Ensure we have a system_message and history variables
135
  system_message = system_instruction
136
  history = [] # If history is meant to persist, consider storing it externally
137
  messages = []
138
 
139
- # If you had previous conversation history, you could reconstruct messages here.
140
  for val in history:
141
  if val[0]:
142
  messages.append({"role": "user", "content": val[0]})
143
  if val[1]:
144
  messages.append({"role": "assistant", "content": val[1]})
145
 
146
- # Include the current user input
147
  messages.append({"role": "user", "content": user_input})
148
  messages.insert(0, {"role": "system", "content": system_message})
149
 
@@ -151,7 +151,7 @@ def chatbot_conversation(audio_file_path):
151
  print(f"[DEBUG] Messages: {messages}")
152
 
153
  response = ""
154
- # Streaming response from the API
155
  try:
156
  for message in sync_client.chat.completions.create(
157
  model="tela-gpt4o",
@@ -163,38 +163,37 @@ def chatbot_conversation(audio_file_path):
163
  ):
164
  token = message.choices[0].delta.content
165
  response += token
166
- # Optional: print tokens as they arrive for debugging
167
- print(f"[DEBUG] Partial response token received: {token}")
168
- yield response
 
169
  except Exception as e:
170
  print(f"[ERROR] Error during streaming response: {e}")
171
- return "I could not understand you. Please try again.", None
172
-
173
- #formatted_output = format_generated_response(
174
- # {"choices": [{"message": {"content": response}}]}
175
- #)
176
 
 
177
  if response:
178
- # Append the conversation turn to history
179
  history.append([
180
  {"role": "user", "content": user_input},
181
  {"role": "assistant", "content": response}
182
  ])
183
- print("[DEBUG] Generating TTS for response.")
184
  tts_file_name = generate_speech(response)
185
  if tts_file_name:
186
  print("[DEBUG] Returning final response and TTS file.")
187
- return formatted_output, tts_file_name
 
188
  else:
189
  print("[ERROR] Failed to generate TTS.")
190
- return formatted_output, None
191
  else:
192
  print("[ERROR] No response generated.")
193
- return "I could not synthesize the audio. Please try again.", None
194
 
195
  except Exception as e:
196
  print(f"[ERROR] Exception in chatbot_conversation: {e}")
197
- return "I could not understand you. Please try again.", None
198
 
199
  gr.Interface(
200
  fn=chatbot_conversation,
 
129
 
130
  if not user_input:
131
  print("[ERROR] No user input extracted from transcription.")
132
+ yield "I could not generate the text. Please try again.", None
133
+ return
134
 
 
135
  system_message = system_instruction
136
  history = [] # If history is meant to persist, consider storing it externally
137
  messages = []
138
 
139
+ # Reconstruct history if needed (currently empty)
140
  for val in history:
141
  if val[0]:
142
  messages.append({"role": "user", "content": val[0]})
143
  if val[1]:
144
  messages.append({"role": "assistant", "content": val[1]})
145
 
146
+ # Current user input
147
  messages.append({"role": "user", "content": user_input})
148
  messages.insert(0, {"role": "system", "content": system_message})
149
 
 
151
  print(f"[DEBUG] Messages: {messages}")
152
 
153
  response = ""
154
+ # Stream partial responses
155
  try:
156
  for message in sync_client.chat.completions.create(
157
  model="tela-gpt4o",
 
163
  ):
164
  token = message.choices[0].delta.content
165
  response += token
166
+ # Yield partial text, no audio yet
167
+ # The first output is the transcription (assistant message),
168
+ # second output is audio, which we pass as None for now
169
+ yield (response, None)
170
  except Exception as e:
171
  print(f"[ERROR] Error during streaming response: {e}")
172
+ yield ("I could not understand you. Please try again.", None)
173
+ return
 
 
 
174
 
175
+ # Now that we have the full response, generate TTS
176
  if response:
 
177
  history.append([
178
  {"role": "user", "content": user_input},
179
  {"role": "assistant", "content": response}
180
  ])
181
+ print("[DEBUG] Generating TTS for full response.")
182
  tts_file_name = generate_speech(response)
183
  if tts_file_name:
184
  print("[DEBUG] Returning final response and TTS file.")
185
+ # Now yield again with final text and audio
186
+ yield (response, tts_file_name)
187
  else:
188
  print("[ERROR] Failed to generate TTS.")
189
+ yield (response, None)
190
  else:
191
  print("[ERROR] No response generated.")
192
+ yield ("I could not synthesize the audio. Please try again.", None)
193
 
194
  except Exception as e:
195
  print(f"[ERROR] Exception in chatbot_conversation: {e}")
196
+ yield ("I could not understand you. Please try again.", None)
197
 
198
  gr.Interface(
199
  fn=chatbot_conversation,