Core23 commited on
Commit
74bb2e1
1 Parent(s): 3cd8c03

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -9
app.py CHANGED
@@ -118,18 +118,26 @@ def generate_response(transcribed_text):
118
  )
119
  return response['choices'][0]['message']['content']
120
 
 
 
 
 
 
 
 
 
121
 
122
  def inference(text):
123
- response = openai.Audio.create(
124
  model="tts-1-hd",
125
  voice="alloy",
126
  text=text
127
  )
128
- if 'url' in response:
129
- audio_url = response['url']
130
  return audio_url
131
  else:
132
- raise Exception("An error occurred: OpenAI did not return an 'url' for the audio file.")
133
 
134
  def process_audio_and_respond(audio):
135
  text = transcribe(audio)
@@ -137,18 +145,20 @@ def process_audio_and_respond(audio):
137
  audio_url = inference(response_text)
138
  response = requests.get(audio_url)
139
 
140
- # Create a temporary file
141
  with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
142
  temp_file.write(response.content)
143
- temp_filepath = temp_file.name # We need the file path for Gradio to play the audio
144
 
145
- return response_text, temp_filepath # Return the audio file path
146
 
147
  demo = gr.Interface(
148
  process_audio_and_respond,
149
  gr.inputs.Audio(source="microphone", type="filepath", label="Bonyeza kitufe cha kurekodi na uliza swali lako"),
150
- [gr.outputs.Textbox(label="Jibu (kwa njia ya maandishi)"),
151
- gr.outputs.Audio(type="filepath", label="Jibu kwa njia ya sauti (Bofya kusikiliza Jibu)")],
 
 
152
  description="Uliza Swali kuhusu haki and sheria za udongo",
153
  theme="compact",
154
  layout="vertical",
 
118
  )
119
  return response['choices'][0]['message']['content']
120
 
121
+ # Accessing the chat completion message content; modify based on actual object structure
122
+ # Assuming 'response.choices()' method returns a list of choices
123
+ choices = response.choices()
124
+ if choices:
125
+ # Assuming each choice has a 'message' method that returns a message object with 'content' attribute
126
+ return choices[0].message().content
127
+ else:
128
+ raise ValueError('No response choices found.')
129
 
130
  def inference(text):
131
+ response = client.audio.create(
132
  model="tts-1-hd",
133
  voice="alloy",
134
  text=text
135
  )
136
+ if hasattr(response, 'url'):
137
+ audio_url = response.url
138
  return audio_url
139
  else:
140
+ raise Exception("An error occurred: OpenAI did not return a valid response for the audio file.")
141
 
142
  def process_audio_and_respond(audio):
143
  text = transcribe(audio)
 
145
  audio_url = inference(response_text)
146
  response = requests.get(audio_url)
147
 
148
+ # Create temporary audio file
149
  with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
150
  temp_file.write(response.content)
151
+ temp_filepath = temp_file.name
152
 
153
+ return response_text, temp_filepath
154
 
155
  demo = gr.Interface(
156
  process_audio_and_respond,
157
  gr.inputs.Audio(source="microphone", type="filepath", label="Bonyeza kitufe cha kurekodi na uliza swali lako"),
158
+ [
159
+ gr.outputs.Textbox(label="Jibu (kwa njia ya maandishi)"),
160
+ gr.outputs.Audio(type="filepath", label="Jibu kwa njia ya sauti (Bofya kusikiliza Jibu)")
161
+ ],
162
  description="Uliza Swali kuhusu haki and sheria za udongo",
163
  theme="compact",
164
  layout="vertical",