shukdevdatta123 commited on
Commit
39187a7
·
verified ·
1 Parent(s): 0464749

Update v2.txt

Browse files
Files changed (1) hide show
  1. v2.txt +32 -11
v2.txt CHANGED
@@ -3,11 +3,25 @@ import tempfile
3
  import os
4
  import requests
5
  import gradio as gr
 
6
  from openai import OpenAI
7
 
8
  # Available voices for audio generation
9
  VOICES = ["alloy", "ash", "ballad", "coral", "echo", "fable", "onyx", "nova", "sage", "shimmer", "verse"]
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  def process_text_input(api_key, text_prompt, selected_voice):
12
  """Generate audio response from text input"""
13
  try:
@@ -115,9 +129,14 @@ def transcribe_audio(api_key, audio_path):
115
  return f"Transcription error: {str(e)}"
116
 
117
  def download_example_audio():
118
- """Download an example audio file for testing"""
119
  try:
120
- url = "https://cdn.openai.com/API/docs/audio/alloy.wav"
 
 
 
 
 
121
  response = requests.get(url)
122
  response.raise_for_status()
123
 
@@ -126,14 +145,14 @@ def download_example_audio():
126
  with open(temp_path, "wb") as f:
127
  f.write(response.content)
128
 
129
- return temp_path
130
  except Exception as e:
131
- return None
132
 
133
  def use_example_audio():
134
- """Load example audio for the interface"""
135
- audio_path = download_example_audio()
136
- return audio_path
137
 
138
  # Create Gradio Interface
139
  with gr.Blocks(title="OpenAI Audio Chat App") as app:
@@ -163,7 +182,7 @@ with gr.Blocks(title="OpenAI Audio Chat App") as app:
163
  text_submit = gr.Button("Generate Response")
164
 
165
  with gr.Column():
166
- text_output = gr.Textbox(label="AI Response (Text)", lines=5)
167
  audio_output = gr.Audio(label="AI Response (Audio)")
168
  transcribed_output = gr.Textbox(label="Transcription of Audio Response", lines=3)
169
 
@@ -193,7 +212,8 @@ with gr.Blocks(title="OpenAI Audio Chat App") as app:
193
  type="filepath",
194
  sources=["microphone", "upload"]
195
  )
196
- example_btn = gr.Button("Use Example Audio")
 
197
 
198
  accompanying_text = gr.Textbox(
199
  label="Accompanying Text (Optional)",
@@ -208,7 +228,7 @@ with gr.Blocks(title="OpenAI Audio Chat App") as app:
208
  audio_submit = gr.Button("Process Audio & Generate Response")
209
 
210
  with gr.Column():
211
- audio_text_output = gr.Textbox(label="AI Response (Text)", lines=5)
212
  audio_audio_output = gr.Audio(label="AI Response (Audio)")
213
  audio_transcribed_output = gr.Textbox(label="Transcription of Audio Response", lines=3)
214
  input_transcription = gr.Textbox(label="Transcription of Input Audio", lines=3)
@@ -239,7 +259,7 @@ with gr.Blocks(title="OpenAI Audio Chat App") as app:
239
  example_btn.click(
240
  fn=use_example_audio,
241
  inputs=[],
242
- outputs=[audio_input]
243
  )
244
 
245
  with gr.Tab("Voice Samples"):
@@ -302,6 +322,7 @@ with gr.Blocks(title="OpenAI Audio Chat App") as app:
302
  - Audio inputs should be in WAV format
303
  - Available voices: alloy, ash, ballad, coral, echo, fable, onyx, nova, sage, shimmer, and verse
304
  - Each audio response is automatically transcribed for verification
 
305
  """)
306
 
307
  if __name__ == "__main__":
 
3
  import os
4
  import requests
5
  import gradio as gr
6
+ import random
7
  from openai import OpenAI
8
 
9
  # Available voices for audio generation
10
  VOICES = ["alloy", "ash", "ballad", "coral", "echo", "fable", "onyx", "nova", "sage", "shimmer", "verse"]
11
 
12
+ # Example audio URLs
13
+ EXAMPLE_AUDIO_URLS = [
14
+ "https://cdn.openai.com/API/docs/audio/alloy.wav",
15
+ "https://cdn.openai.com/API/docs/audio/ash.wav",
16
+ "https://cdn.openai.com/API/docs/audio/coral.wav",
17
+ "https://cdn.openai.com/API/docs/audio/echo.wav",
18
+ "https://cdn.openai.com/API/docs/audio/fable.wav",
19
+ "https://cdn.openai.com/API/docs/audio/onyx.wav",
20
+ "https://cdn.openai.com/API/docs/audio/nova.wav",
21
+ "https://cdn.openai.com/API/docs/audio/sage.wav",
22
+ "https://cdn.openai.com/API/docs/audio/shimmer.wav"
23
+ ]
24
+
25
  def process_text_input(api_key, text_prompt, selected_voice):
26
  """Generate audio response from text input"""
27
  try:
 
129
  return f"Transcription error: {str(e)}"
130
 
131
  def download_example_audio():
132
+ """Download a random example audio file for testing"""
133
  try:
134
+ # Randomly select one of the example audio URLs
135
+ url = random.choice(EXAMPLE_AUDIO_URLS)
136
+
137
+ # Get the voice name from the URL for feedback
138
+ voice_name = url.split('/')[-1].split('.')[0]
139
+
140
  response = requests.get(url)
141
  response.raise_for_status()
142
 
 
145
  with open(temp_path, "wb") as f:
146
  f.write(response.content)
147
 
148
+ return temp_path, f"Loaded example voice: {voice_name}"
149
  except Exception as e:
150
+ return None, f"Error loading example: {str(e)}"
151
 
152
  def use_example_audio():
153
+ """Load random example audio for the interface"""
154
+ audio_path, message = download_example_audio()
155
+ return audio_path, message
156
 
157
  # Create Gradio Interface
158
  with gr.Blocks(title="OpenAI Audio Chat App") as app:
 
182
  text_submit = gr.Button("Generate Response")
183
 
184
  with gr.Column():
185
+ text_output = gr.Textbox(label="AI Response (Checks Error)", lines=5)
186
  audio_output = gr.Audio(label="AI Response (Audio)")
187
  transcribed_output = gr.Textbox(label="Transcription of Audio Response", lines=3)
188
 
 
212
  type="filepath",
213
  sources=["microphone", "upload"]
214
  )
215
+ example_btn = gr.Button("Use Random Example Audio")
216
+ example_message = gr.Textbox(label="Example Status", interactive=False)
217
 
218
  accompanying_text = gr.Textbox(
219
  label="Accompanying Text (Optional)",
 
228
  audio_submit = gr.Button("Process Audio & Generate Response")
229
 
230
  with gr.Column():
231
+ audio_text_output = gr.Textbox(label="AI Response (Checks Error)", lines=5)
232
  audio_audio_output = gr.Audio(label="AI Response (Audio)")
233
  audio_transcribed_output = gr.Textbox(label="Transcription of Audio Response", lines=3)
234
  input_transcription = gr.Textbox(label="Transcription of Input Audio", lines=3)
 
259
  example_btn.click(
260
  fn=use_example_audio,
261
  inputs=[],
262
+ outputs=[audio_input, example_message]
263
  )
264
 
265
  with gr.Tab("Voice Samples"):
 
322
  - Audio inputs should be in WAV format
323
  - Available voices: alloy, ash, ballad, coral, echo, fable, onyx, nova, sage, shimmer, and verse
324
  - Each audio response is automatically transcribed for verification
325
+ - The "Use Random Example Audio" button will load a random sample from OpenAI's demo voices
326
  """)
327
 
328
  if __name__ == "__main__":