shukdevdatta123 commited on
Commit
c1cfa7f
·
verified ·
1 Parent(s): d11d2e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +198 -58
app.py CHANGED
@@ -4,7 +4,9 @@ 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"]
@@ -35,11 +37,23 @@ SUPPORTED_LANGUAGES = [
35
  "Vietnamese", "Welsh"
36
  ]
37
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  def process_text_input(api_key, text_prompt, selected_voice):
39
  """Generate audio response from text input"""
40
  try:
41
  # Initialize OpenAI client with the provided API key
42
- client = OpenAI(api_key=api_key)
43
 
44
  completion = client.chat.completions.create(
45
  model="gpt-4o-audio-preview",
@@ -63,6 +77,10 @@ def process_text_input(api_key, text_prompt, selected_voice):
63
  text_response = completion.choices[0].message.content
64
 
65
  return text_response, temp_path
 
 
 
 
66
  except Exception as e:
67
  return f"Error: {str(e)}", None
68
 
@@ -73,7 +91,7 @@ def process_audio_input(api_key, audio_path, text_prompt, selected_voice):
73
  return "Please upload or record audio first.", None
74
 
75
  # Initialize OpenAI client with the provided API key
76
- client = OpenAI(api_key=api_key)
77
 
78
  # Read audio file and encode to base64
79
  with open(audio_path, "rb") as audio_file:
@@ -120,6 +138,10 @@ def process_audio_input(api_key, audio_path, text_prompt, selected_voice):
120
  text_response = completion.choices[0].message.content
121
 
122
  return text_response, temp_path
 
 
 
 
123
  except Exception as e:
124
  return f"Error: {str(e)}", None
125
 
@@ -129,38 +151,86 @@ def transcribe_audio(api_key, audio_path):
129
  if not audio_path:
130
  return "No audio file provided for transcription."
131
 
132
- client = OpenAI(api_key=api_key)
133
 
 
 
 
 
 
 
 
 
 
134
  with open(audio_path, "rb") as audio_file:
135
- transcription = client.audio.transcriptions.create(
136
- model="gpt-4o-transcribe",
137
- file=audio_file
138
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
139
 
140
- return transcription.text
141
  except Exception as e:
142
  return f"Transcription error: {str(e)}"
143
 
144
  def translate_audio(api_key, audio_path):
145
- """Translate audio to English using OpenAI's Whisper model"""
146
  try:
147
  if not audio_path:
148
  return "No audio file provided for translation."
 
 
 
 
 
 
 
 
 
149
 
150
- client = OpenAI(api_key=api_key)
151
 
152
- with open(audio_path, "rb") as audio_file:
153
- translation = client.audio.translations.create(
154
- model="whisper-1",
155
- file=audio_file
156
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157
 
158
- return translation.text
159
  except Exception as e:
160
  return f"Translation error: {str(e)}"
161
 
162
  def download_example_audio():
163
- """Download a random example audio file for testing"""
164
  try:
165
  # Randomly select one of the example audio URLs
166
  url = random.choice(EXAMPLE_AUDIO_URLS)
@@ -168,15 +238,27 @@ def download_example_audio():
168
  # Get the voice name from the URL for feedback
169
  voice_name = url.split('/')[-1].split('.')[0]
170
 
171
- response = requests.get(url)
172
- response.raise_for_status()
173
-
174
- # Save to a temporary file
175
- temp_path = tempfile.mktemp(suffix=".wav")
176
- with open(temp_path, "wb") as f:
177
- f.write(response.content)
178
-
179
- return temp_path, f"Loaded example voice: {voice_name}"
 
 
 
 
 
 
 
 
 
 
 
 
180
  except Exception as e:
181
  return None, f"Error loading example: {str(e)}"
182
 
@@ -185,6 +267,12 @@ def use_example_audio():
185
  audio_path, message = download_example_audio()
186
  return audio_path, message
187
 
 
 
 
 
 
 
188
  # Create Gradio Interface
189
  with gr.Blocks(title="OpenAI Audio Chat App") as app:
190
  gr.Markdown("# OpenAI Audio Chat App")
@@ -219,6 +307,9 @@ with gr.Blocks(title="OpenAI Audio Chat App") as app:
219
 
220
  # Function to process text input and then transcribe the resulting audio
221
  def text_input_with_transcription(api_key, text_prompt, voice):
 
 
 
222
  text_response, audio_path = process_text_input(api_key, text_prompt, voice)
223
 
224
  # Get transcription of the generated audio
@@ -266,10 +357,15 @@ with gr.Blocks(title="OpenAI Audio Chat App") as app:
266
 
267
  # Function to process audio input, generate response, and provide transcriptions
268
  def audio_input_with_transcription(api_key, audio_path, text_prompt, voice):
 
 
 
269
  # First transcribe the input audio
270
  input_transcription = "N/A"
271
  if audio_path:
272
  input_transcription = transcribe_audio(api_key, audio_path)
 
 
273
 
274
  # Process the audio input and get response
275
  text_response, response_audio_path = process_audio_input(api_key, audio_path, text_prompt, voice)
@@ -297,33 +393,43 @@ with gr.Blocks(title="OpenAI Audio Chat App") as app:
297
  gr.Markdown("## Listen to samples of each voice")
298
 
299
  def generate_voice_sample(api_key, voice_type):
300
- try:
301
- if not api_key:
302
- return "Please enter your OpenAI API key first.", None, "No transcription available."
303
-
304
- client = OpenAI(api_key=api_key)
305
- completion = client.chat.completions.create(
306
- model="gpt-4o-audio-preview",
307
- modalities=["text", "audio"],
308
- audio={"voice": voice_type, "format": "wav"},
309
- messages=[
310
- {
311
- "role": "user",
312
- "content": f"This is a sample of the {voice_type} voice. It has its own unique tone and character."
313
- }
314
- ]
315
- )
316
 
317
- # Save the audio to a temporary file
318
- wav_bytes = base64.b64decode(completion.choices[0].message.audio.data)
319
- temp_path = tempfile.mktemp(suffix=".wav")
320
- with open(temp_path, "wb") as f:
321
- f.write(wav_bytes)
322
-
323
- # Get transcription
324
- transcription = transcribe_audio(api_key, temp_path)
325
 
326
- return f"Sample generated with voice: {voice_type}", temp_path, transcription
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
327
  except Exception as e:
328
  return f"Error: {str(e)}", None, "No transcription available."
329
 
@@ -346,7 +452,7 @@ with gr.Blocks(title="OpenAI Audio Chat App") as app:
346
  outputs=[sample_text, sample_audio, sample_transcription]
347
  )
348
 
349
- # New tab for audio translation
350
  with gr.Tab("Audio Translation"):
351
  gr.Markdown("## Translate audio from other languages to English")
352
  gr.Markdown("Supports 50+ languages including: Arabic, Chinese, French, German, Japanese, Spanish, and many more.")
@@ -360,39 +466,72 @@ with gr.Blocks(title="OpenAI Audio Chat App") as app:
360
  )
361
 
362
  translate_btn = gr.Button("Translate to English")
 
363
 
364
  with gr.Column():
365
  translation_output = gr.Textbox(label="English Translation", lines=5)
366
  original_transcription = gr.Textbox(label="Original Transcription (if available)", lines=5)
367
 
368
  def translate_audio_input(api_key, audio_path):
369
- """Handle the translation of uploaded audio"""
 
 
 
370
  try:
371
  if not audio_path:
372
- return "Please upload or record audio first.", "No audio to transcribe."
 
 
 
373
 
374
  # Get the translation
375
  translation = translate_audio(api_key, audio_path)
376
 
 
 
 
 
 
377
  # Try to get original transcription (this might be in the original language)
378
  try:
379
  original = transcribe_audio(api_key, audio_path)
380
- except:
 
 
381
  original = "Could not transcribe original audio."
382
 
383
- return translation, original
 
 
 
 
384
  except Exception as e:
385
- return f"Translation error: {str(e)}", "Error occurred during processing."
386
 
387
  translate_btn.click(
388
  fn=translate_audio_input,
389
  inputs=[api_key, translation_audio_input],
390
- outputs=[translation_output, original_transcription]
391
  )
392
 
393
  # Show supported languages
394
  with gr.Accordion("Supported Languages", open=False):
395
  gr.Markdown(", ".join(SUPPORTED_LANGUAGES))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
396
 
397
  gr.Markdown("""
398
  ## Notes:
@@ -403,6 +542,7 @@ with gr.Blocks(title="OpenAI Audio Chat App") as app:
403
  - Each audio response is automatically transcribed for verification
404
  - The "Use Random Example Audio" button will load a random sample from OpenAI's demo voices
405
  - The translation feature supports 50+ languages, translating them to English
 
406
  """)
407
 
408
  if __name__ == "__main__":
 
4
  import requests
5
  import gradio as gr
6
  import random
7
+ import time
8
  from openai import OpenAI
9
+ from requests.exceptions import RequestException, Timeout, ConnectionError
10
 
11
  # Available voices for audio generation
12
  VOICES = ["alloy", "ash", "ballad", "coral", "echo", "fable", "onyx", "nova", "sage", "shimmer", "verse"]
 
37
  "Vietnamese", "Welsh"
38
  ]
39
 
40
+ # Max retries for API calls
41
+ MAX_RETRIES = 3
42
+ RETRY_DELAY = 2 # seconds
43
+
44
+ def create_openai_client(api_key):
45
+ """Create an OpenAI client with proper timeout settings"""
46
+ return OpenAI(
47
+ api_key=api_key,
48
+ timeout=60.0, # 60 second timeout
49
+ max_retries=3 # Allow 3 retries
50
+ )
51
+
52
  def process_text_input(api_key, text_prompt, selected_voice):
53
  """Generate audio response from text input"""
54
  try:
55
  # Initialize OpenAI client with the provided API key
56
+ client = create_openai_client(api_key)
57
 
58
  completion = client.chat.completions.create(
59
  model="gpt-4o-audio-preview",
 
77
  text_response = completion.choices[0].message.content
78
 
79
  return text_response, temp_path
80
+ except ConnectionError as e:
81
+ return f"Connection error: {str(e)}. Please check your internet connection and try again.", None
82
+ except Timeout as e:
83
+ return f"Timeout error: {str(e)}. The request took too long to complete. Please try again.", None
84
  except Exception as e:
85
  return f"Error: {str(e)}", None
86
 
 
91
  return "Please upload or record audio first.", None
92
 
93
  # Initialize OpenAI client with the provided API key
94
+ client = create_openai_client(api_key)
95
 
96
  # Read audio file and encode to base64
97
  with open(audio_path, "rb") as audio_file:
 
138
  text_response = completion.choices[0].message.content
139
 
140
  return text_response, temp_path
141
+ except ConnectionError as e:
142
+ return f"Connection error: {str(e)}. Please check your internet connection and try again.", None
143
+ except Timeout as e:
144
+ return f"Timeout error: {str(e)}. The request took too long to complete. Please try again.", None
145
  except Exception as e:
146
  return f"Error: {str(e)}", None
147
 
 
151
  if not audio_path:
152
  return "No audio file provided for transcription."
153
 
154
+ client = create_openai_client(api_key)
155
 
156
+ # Make sure the file exists and is readable
157
+ if not os.path.exists(audio_path):
158
+ return "Audio file not found or inaccessible."
159
+
160
+ # Check file size
161
+ file_size = os.path.getsize(audio_path)
162
+ if file_size == 0:
163
+ return "Audio file is empty."
164
+
165
  with open(audio_path, "rb") as audio_file:
166
+ for attempt in range(MAX_RETRIES):
167
+ try:
168
+ transcription = client.audio.transcriptions.create(
169
+ model="gpt-4o-transcribe",
170
+ file=audio_file
171
+ )
172
+ return transcription.text
173
+ except (ConnectionError, Timeout) as e:
174
+ if attempt < MAX_RETRIES - 1:
175
+ time.sleep(RETRY_DELAY)
176
+ # Reset file pointer
177
+ audio_file.seek(0)
178
+ continue
179
+ else:
180
+ return f"Transcription failed after {MAX_RETRIES} attempts: {str(e)}"
181
+ except Exception as e:
182
+ return f"Transcription error: {str(e)}"
183
 
 
184
  except Exception as e:
185
  return f"Transcription error: {str(e)}"
186
 
187
  def translate_audio(api_key, audio_path):
188
+ """Translate audio to English using OpenAI's Whisper model with improved error handling"""
189
  try:
190
  if not audio_path:
191
  return "No audio file provided for translation."
192
+
193
+ # Verify file exists and is accessible
194
+ if not os.path.exists(audio_path):
195
+ return "Audio file not found or inaccessible."
196
+
197
+ # Check file size
198
+ file_size = os.path.getsize(audio_path)
199
+ if file_size == 0:
200
+ return "Audio file is empty."
201
 
202
+ client = create_openai_client(api_key)
203
 
204
+ # Implement retry mechanism
205
+ for attempt in range(MAX_RETRIES):
206
+ try:
207
+ with open(audio_path, "rb") as audio_file:
208
+ translation = client.audio.translations.create(
209
+ model="whisper-1",
210
+ file=audio_file,
211
+ timeout=90.0 # Extended timeout for translation
212
+ )
213
+ return translation.text
214
+ except (ConnectionError, Timeout) as e:
215
+ if attempt < MAX_RETRIES - 1:
216
+ # Wait before retrying
217
+ time.sleep(RETRY_DELAY * (attempt + 1)) # Exponential backoff
218
+ continue
219
+ else:
220
+ return f"Translation failed after {MAX_RETRIES} attempts: Connection error. Please check your internet connection and try again."
221
+ except Exception as e:
222
+ # Handle other exceptions
223
+ error_message = str(e)
224
+ if "connection" in error_message.lower():
225
+ return f"Connection error: {error_message}. Please check your internet connection and try again."
226
+ else:
227
+ return f"Translation error: {error_message}"
228
 
 
229
  except Exception as e:
230
  return f"Translation error: {str(e)}"
231
 
232
  def download_example_audio():
233
+ """Download a random example audio file for testing with improved error handling"""
234
  try:
235
  # Randomly select one of the example audio URLs
236
  url = random.choice(EXAMPLE_AUDIO_URLS)
 
238
  # Get the voice name from the URL for feedback
239
  voice_name = url.split('/')[-1].split('.')[0]
240
 
241
+ # Implement retry mechanism
242
+ for attempt in range(MAX_RETRIES):
243
+ try:
244
+ response = requests.get(url, timeout=30)
245
+ response.raise_for_status()
246
+
247
+ # Save to a temporary file
248
+ temp_path = tempfile.mktemp(suffix=".wav")
249
+ with open(temp_path, "wb") as f:
250
+ f.write(response.content)
251
+
252
+ return temp_path, f"Loaded example voice: {voice_name}"
253
+ except (ConnectionError, Timeout) as e:
254
+ if attempt < MAX_RETRIES - 1:
255
+ time.sleep(RETRY_DELAY)
256
+ continue
257
+ else:
258
+ return None, f"Failed to download example after {MAX_RETRIES} attempts: {str(e)}"
259
+ except Exception as e:
260
+ return None, f"Error loading example: {str(e)}"
261
+
262
  except Exception as e:
263
  return None, f"Error loading example: {str(e)}"
264
 
 
267
  audio_path, message = download_example_audio()
268
  return audio_path, message
269
 
270
+ def check_api_key(api_key):
271
+ """Validate if the API key is provided"""
272
+ if not api_key or api_key.strip() == "":
273
+ return False
274
+ return True
275
+
276
  # Create Gradio Interface
277
  with gr.Blocks(title="OpenAI Audio Chat App") as app:
278
  gr.Markdown("# OpenAI Audio Chat App")
 
307
 
308
  # Function to process text input and then transcribe the resulting audio
309
  def text_input_with_transcription(api_key, text_prompt, voice):
310
+ if not check_api_key(api_key):
311
+ return "Please enter your OpenAI API key first.", None, "No API key provided."
312
+
313
  text_response, audio_path = process_text_input(api_key, text_prompt, voice)
314
 
315
  # Get transcription of the generated audio
 
357
 
358
  # Function to process audio input, generate response, and provide transcriptions
359
  def audio_input_with_transcription(api_key, audio_path, text_prompt, voice):
360
+ if not check_api_key(api_key):
361
+ return "Please enter your OpenAI API key first.", None, "No API key provided.", "No API key provided."
362
+
363
  # First transcribe the input audio
364
  input_transcription = "N/A"
365
  if audio_path:
366
  input_transcription = transcribe_audio(api_key, audio_path)
367
+ else:
368
+ return "Please upload or record audio first.", None, "No audio to transcribe.", "No audio provided."
369
 
370
  # Process the audio input and get response
371
  text_response, response_audio_path = process_audio_input(api_key, audio_path, text_prompt, voice)
 
393
  gr.Markdown("## Listen to samples of each voice")
394
 
395
  def generate_voice_sample(api_key, voice_type):
396
+ if not check_api_key(api_key):
397
+ return "Please enter your OpenAI API key first.", None, "No API key provided."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
398
 
399
+ try:
400
+ client = create_openai_client(api_key)
 
 
 
 
 
 
401
 
402
+ # Use retry mechanism
403
+ for attempt in range(MAX_RETRIES):
404
+ try:
405
+ completion = client.chat.completions.create(
406
+ model="gpt-4o-audio-preview",
407
+ modalities=["text", "audio"],
408
+ audio={"voice": voice_type, "format": "wav"},
409
+ messages=[
410
+ {
411
+ "role": "user",
412
+ "content": f"This is a sample of the {voice_type} voice. It has its own unique tone and character."
413
+ }
414
+ ]
415
+ )
416
+
417
+ # Save the audio to a temporary file
418
+ wav_bytes = base64.b64decode(completion.choices[0].message.audio.data)
419
+ temp_path = tempfile.mktemp(suffix=".wav")
420
+ with open(temp_path, "wb") as f:
421
+ f.write(wav_bytes)
422
+
423
+ # Get transcription
424
+ transcription = transcribe_audio(api_key, temp_path)
425
+
426
+ return f"Sample generated with voice: {voice_type}", temp_path, transcription
427
+ except (ConnectionError, Timeout) as e:
428
+ if attempt < MAX_RETRIES - 1:
429
+ time.sleep(RETRY_DELAY)
430
+ continue
431
+ else:
432
+ return f"Connection error after {MAX_RETRIES} attempts: {str(e)}. Please check your internet connection.", None, "No sample generated."
433
  except Exception as e:
434
  return f"Error: {str(e)}", None, "No transcription available."
435
 
 
452
  outputs=[sample_text, sample_audio, sample_transcription]
453
  )
454
 
455
+ # New tab for audio translation with improved error handling
456
  with gr.Tab("Audio Translation"):
457
  gr.Markdown("## Translate audio from other languages to English")
458
  gr.Markdown("Supports 50+ languages including: Arabic, Chinese, French, German, Japanese, Spanish, and many more.")
 
466
  )
467
 
468
  translate_btn = gr.Button("Translate to English")
469
+ connection_status = gr.Textbox(label="Connection Status", value="Ready", interactive=False)
470
 
471
  with gr.Column():
472
  translation_output = gr.Textbox(label="English Translation", lines=5)
473
  original_transcription = gr.Textbox(label="Original Transcription (if available)", lines=5)
474
 
475
  def translate_audio_input(api_key, audio_path):
476
+ """Handle the translation of uploaded audio with better connection handling"""
477
+ if not check_api_key(api_key):
478
+ return "Please enter your OpenAI API key first.", "No API key provided.", "No API key provided."
479
+
480
  try:
481
  if not audio_path:
482
+ return "Please upload or record audio first.", "No audio to translate.", "Connection ready"
483
+
484
+ # Update connection status
485
+ yield "Processing...", "Preparing audio for translation...", "Connecting to OpenAI API..."
486
 
487
  # Get the translation
488
  translation = translate_audio(api_key, audio_path)
489
 
490
+ # If there's a connection error message in the translation
491
+ if "connection error" in translation.lower():
492
+ yield translation, "Translation failed due to connection issues.", "Connection failed"
493
+ return
494
+
495
  # Try to get original transcription (this might be in the original language)
496
  try:
497
  original = transcribe_audio(api_key, audio_path)
498
+ if "error" in original.lower():
499
+ original = "Could not transcribe original audio due to connection issues."
500
+ except Exception:
501
  original = "Could not transcribe original audio."
502
 
503
+ yield translation, original, "Connection successful"
504
+ except ConnectionError as e:
505
+ yield f"Connection error: {str(e)}. Please check your internet connection and try again.", "Translation failed.", "Connection failed"
506
+ except Timeout as e:
507
+ yield f"Timeout error: {str(e)}. The request took too long to complete. Please try again.", "Translation timed out.", "Connection timed out"
508
  except Exception as e:
509
+ yield f"Translation error: {str(e)}", "Error occurred during processing.", "Error occurred"
510
 
511
  translate_btn.click(
512
  fn=translate_audio_input,
513
  inputs=[api_key, translation_audio_input],
514
+ outputs=[translation_output, original_transcription, connection_status]
515
  )
516
 
517
  # Show supported languages
518
  with gr.Accordion("Supported Languages", open=False):
519
  gr.Markdown(", ".join(SUPPORTED_LANGUAGES))
520
+
521
+ # Connection troubleshooting tips
522
+ with gr.Accordion("Connection Troubleshooting", open=False):
523
+ gr.Markdown("""
524
+ ### If you experience connection errors:
525
+
526
+ 1. **Check your internet connection** - Ensure you have a stable internet connection
527
+ 2. **Verify your API key** - Make sure your OpenAI API key is valid and has sufficient credits
528
+ 3. **Try a smaller audio file** - Large audio files may time out during upload
529
+ 4. **Wait and retry** - OpenAI servers might be experiencing high traffic
530
+ 5. **Check file format** - Make sure your audio file is in a supported format (MP3, WAV, etc.)
531
+ 6. **Try on a different network** - Some networks might block API calls to OpenAI
532
+
533
+ The app will automatically retry failed connections up to 3 times.
534
+ """)
535
 
536
  gr.Markdown("""
537
  ## Notes:
 
542
  - Each audio response is automatically transcribed for verification
543
  - The "Use Random Example Audio" button will load a random sample from OpenAI's demo voices
544
  - The translation feature supports 50+ languages, translating them to English
545
+ - If you experience connection errors, the app will automatically retry up to 3 times
546
  """)
547
 
548
  if __name__ == "__main__":