Spaces:
Sleeping
Sleeping
File size: 23,974 Bytes
43ffb0e 0b03b94 c1cfa7f 43ffb0e c1cfa7f 43ffb0e 0b03b94 d11d2e2 c1cfa7f 43ffb0e c1cfa7f 43ffb0e e2170c7 0a8ed36 43ffb0e c1cfa7f 43ffb0e c1cfa7f 43ffb0e 04c9b90 43ffb0e 0a8ed36 43ffb0e c1cfa7f 43ffb0e e2170c7 c1cfa7f e2170c7 c1cfa7f e2170c7 c1cfa7f e2170c7 d11d2e2 c1cfa7f d11d2e2 c1cfa7f d11d2e2 c1cfa7f d11d2e2 c1cfa7f d11d2e2 43ffb0e c1cfa7f 43ffb0e 0b03b94 c1cfa7f 43ffb0e 0b03b94 43ffb0e 0b03b94 43ffb0e c1cfa7f 43ffb0e 0a8ed36 43ffb0e 0464749 43ffb0e e2170c7 c1cfa7f e2170c7 43ffb0e e2170c7 43ffb0e e2170c7 43ffb0e 956dfd8 43ffb0e 0b03b94 43ffb0e 0464749 43ffb0e e2170c7 c1cfa7f e2170c7 c1cfa7f e2170c7 43ffb0e e2170c7 43ffb0e e2170c7 43ffb0e 0b03b94 43ffb0e c1cfa7f 43ffb0e c1cfa7f e2170c7 c1cfa7f 43ffb0e e2170c7 43ffb0e e2170c7 43ffb0e e2170c7 43ffb0e c1cfa7f d11d2e2 c1cfa7f d11d2e2 c1cfa7f d11d2e2 c1cfa7f d11d2e2 c1cfa7f d11d2e2 c1cfa7f d11d2e2 c1cfa7f d11d2e2 c1cfa7f d11d2e2 c1cfa7f d11d2e2 c1cfa7f d11d2e2 43ffb0e d11d2e2 43ffb0e e2170c7 0b03b94 d11d2e2 c1cfa7f 43ffb0e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 |
import base64
import tempfile
import os
import requests
import gradio as gr
import random
import time
from openai import OpenAI
from requests.exceptions import RequestException, Timeout, ConnectionError
# Available voices for audio generation
VOICES = ["alloy", "ash", "ballad", "coral", "echo", "fable", "onyx", "nova", "sage", "shimmer", "verse"]
# Example audio URLs
EXAMPLE_AUDIO_URLS = [
"https://cdn.openai.com/API/docs/audio/alloy.wav",
"https://cdn.openai.com/API/docs/audio/ash.wav",
"https://cdn.openai.com/API/docs/audio/coral.wav",
"https://cdn.openai.com/API/docs/audio/echo.wav",
"https://cdn.openai.com/API/docs/audio/fable.wav",
"https://cdn.openai.com/API/docs/audio/onyx.wav",
"https://cdn.openai.com/API/docs/audio/nova.wav",
"https://cdn.openai.com/API/docs/audio/sage.wav",
"https://cdn.openai.com/API/docs/audio/shimmer.wav"
]
# Supported languages for translation
SUPPORTED_LANGUAGES = [
"Afrikaans", "Arabic", "Armenian", "Azerbaijani", "Belarusian", "Bosnian",
"Bulgarian", "Catalan", "Chinese", "Croatian", "Czech", "Danish", "Dutch",
"English", "Estonian", "Finnish", "French", "Galician", "German", "Greek",
"Hebrew", "Hindi", "Hungarian", "Icelandic", "Indonesian", "Italian", "Japanese",
"Kannada", "Kazakh", "Korean", "Latvian", "Lithuanian", "Macedonian", "Malay",
"Marathi", "Maori", "Nepali", "Norwegian", "Persian", "Polish", "Portuguese",
"Romanian", "Russian", "Serbian", "Slovak", "Slovenian", "Spanish", "Swahili",
"Swedish", "Tagalog", "Tamil", "Thai", "Turkish", "Ukrainian", "Urdu",
"Vietnamese", "Welsh"
]
# Max retries for API calls
MAX_RETRIES = 3
RETRY_DELAY = 2 # seconds
def create_openai_client(api_key):
"""Create an OpenAI client with proper timeout settings"""
return OpenAI(
api_key=api_key,
timeout=60.0, # 60 second timeout
max_retries=3 # Allow 3 retries
)
def process_text_input(api_key, text_prompt, selected_voice):
"""Generate audio response from text input"""
try:
# Initialize OpenAI client with the provided API key
client = create_openai_client(api_key)
completion = client.chat.completions.create(
model="gpt-4o-audio-preview",
modalities=["text", "audio"],
audio={"voice": selected_voice, "format": "wav"},
messages=[
{
"role": "user",
"content": text_prompt
}
]
)
# Save the audio to a temporary file
wav_bytes = base64.b64decode(completion.choices[0].message.audio.data)
temp_path = tempfile.mktemp(suffix=".wav")
with open(temp_path, "wb") as f:
f.write(wav_bytes)
# Get the text response directly from the API
text_response = completion.choices[0].message.content
return text_response, temp_path
except ConnectionError as e:
return f"Connection error: {str(e)}. Please check your internet connection and try again.", None
except Timeout as e:
return f"Timeout error: {str(e)}. The request took too long to complete. Please try again.", None
except Exception as e:
return f"Error: {str(e)}", None
def process_audio_input(api_key, audio_path, text_prompt, selected_voice):
"""Process audio input and generate a response"""
try:
if not audio_path:
return "Please upload or record audio first.", None
# Initialize OpenAI client with the provided API key
client = create_openai_client(api_key)
# Read audio file and encode to base64
with open(audio_path, "rb") as audio_file:
audio_data = audio_file.read()
encoded_audio = base64.b64encode(audio_data).decode('utf-8')
# Create message content with both text and audio
message_content = []
if text_prompt:
message_content.append({
"type": "text",
"text": text_prompt
})
message_content.append({
"type": "input_audio",
"input_audio": {
"data": encoded_audio,
"format": "wav"
}
})
# Call OpenAI API
completion = client.chat.completions.create(
model="gpt-4o-audio-preview",
modalities=["text", "audio"],
audio={"voice": selected_voice, "format": "wav"},
messages=[
{
"role": "user",
"content": message_content
}
]
)
# Save the audio response
wav_bytes = base64.b64decode(completion.choices[0].message.audio.data)
temp_path = tempfile.mktemp(suffix=".wav")
with open(temp_path, "wb") as f:
f.write(wav_bytes)
# Get the text response
text_response = completion.choices[0].message.content
return text_response, temp_path
except ConnectionError as e:
return f"Connection error: {str(e)}. Please check your internet connection and try again.", None
except Timeout as e:
return f"Timeout error: {str(e)}. The request took too long to complete. Please try again.", None
except Exception as e:
return f"Error: {str(e)}", None
def transcribe_audio(api_key, audio_path):
"""Transcribe an audio file using OpenAI's API"""
try:
if not audio_path:
return "No audio file provided for transcription."
client = create_openai_client(api_key)
# Make sure the file exists and is readable
if not os.path.exists(audio_path):
return "Audio file not found or inaccessible."
# Check file size
file_size = os.path.getsize(audio_path)
if file_size == 0:
return "Audio file is empty."
with open(audio_path, "rb") as audio_file:
for attempt in range(MAX_RETRIES):
try:
transcription = client.audio.transcriptions.create(
model="gpt-4o-transcribe",
file=audio_file
)
return transcription.text
except (ConnectionError, Timeout) as e:
if attempt < MAX_RETRIES - 1:
time.sleep(RETRY_DELAY)
# Reset file pointer
audio_file.seek(0)
continue
else:
return f"Transcription failed after {MAX_RETRIES} attempts: {str(e)}"
except Exception as e:
return f"Transcription error: {str(e)}"
except Exception as e:
return f"Transcription error: {str(e)}"
def translate_audio(api_key, audio_path):
"""Translate audio to English using OpenAI's Whisper model with improved error handling"""
try:
if not audio_path:
return "No audio file provided for translation."
# Verify file exists and is accessible
if not os.path.exists(audio_path):
return "Audio file not found or inaccessible."
# Check file size
file_size = os.path.getsize(audio_path)
if file_size == 0:
return "Audio file is empty."
client = create_openai_client(api_key)
# Implement retry mechanism
for attempt in range(MAX_RETRIES):
try:
with open(audio_path, "rb") as audio_file:
translation = client.audio.translations.create(
model="whisper-1",
file=audio_file,
timeout=90.0 # Extended timeout for translation
)
return translation.text
except (ConnectionError, Timeout) as e:
if attempt < MAX_RETRIES - 1:
# Wait before retrying
time.sleep(RETRY_DELAY * (attempt + 1)) # Exponential backoff
continue
else:
return f"Translation failed after {MAX_RETRIES} attempts: Connection error. Please check your internet connection and try again."
except Exception as e:
# Handle other exceptions
error_message = str(e)
if "connection" in error_message.lower():
return f"Connection error: {error_message}. Please check your internet connection and try again."
else:
return f"Translation error: {error_message}"
except Exception as e:
return f"Translation error: {str(e)}"
def download_example_audio():
"""Download a random example audio file for testing with improved error handling"""
try:
# Randomly select one of the example audio URLs
url = random.choice(EXAMPLE_AUDIO_URLS)
# Get the voice name from the URL for feedback
voice_name = url.split('/')[-1].split('.')[0]
# Implement retry mechanism
for attempt in range(MAX_RETRIES):
try:
response = requests.get(url, timeout=30)
response.raise_for_status()
# Save to a temporary file
temp_path = tempfile.mktemp(suffix=".wav")
with open(temp_path, "wb") as f:
f.write(response.content)
return temp_path, f"Loaded example voice: {voice_name}"
except (ConnectionError, Timeout) as e:
if attempt < MAX_RETRIES - 1:
time.sleep(RETRY_DELAY)
continue
else:
return None, f"Failed to download example after {MAX_RETRIES} attempts: {str(e)}"
except Exception as e:
return None, f"Error loading example: {str(e)}"
except Exception as e:
return None, f"Error loading example: {str(e)}"
def use_example_audio():
"""Load random example audio for the interface"""
audio_path, message = download_example_audio()
return audio_path, message
def check_api_key(api_key):
"""Validate if the API key is provided"""
if not api_key or api_key.strip() == "":
return False
return True
# Create Gradio Interface
with gr.Blocks(title="OpenAI Audio Chat App") as app:
gr.Markdown("# OpenAI Audio Chat App")
gr.Markdown("Interact with GPT-4o audio model through text and audio inputs")
# API Key input (used across all tabs)
api_key = gr.Textbox(
label="OpenAI API Key",
placeholder="Enter your OpenAI API key here",
type="password"
)
with gr.Tab("Text to Audio"):
with gr.Row():
with gr.Column():
text_input = gr.Textbox(
label="Text Prompt",
placeholder="Enter your question or prompt here...",
lines=3
)
text_voice = gr.Dropdown(
choices=VOICES,
value="alloy",
label="Voice"
)
text_submit = gr.Button("Generate Response")
with gr.Column():
text_output = gr.Textbox(label="AI Response (Checks Error)", lines=5)
audio_output = gr.Audio(label="AI Response (Audio)")
transcribed_output = gr.Textbox(label="Transcription of Audio Response", lines=3)
# Function to process text input and then transcribe the resulting audio
def text_input_with_transcription(api_key, text_prompt, voice):
if not check_api_key(api_key):
return "Please enter your OpenAI API key first.", None, "No API key provided."
text_response, audio_path = process_text_input(api_key, text_prompt, voice)
# Get transcription of the generated audio
if audio_path:
transcription = transcribe_audio(api_key, audio_path)
else:
transcription = "No audio generated to transcribe."
return text_response, audio_path, transcription
text_submit.click(
fn=text_input_with_transcription,
inputs=[api_key, text_input, text_voice],
outputs=[text_output, audio_output, transcribed_output]
)
with gr.Tab("Audio Input to Audio Response"):
with gr.Row():
with gr.Column():
audio_input = gr.Audio(
label="Audio Input",
type="filepath",
sources=["microphone", "upload"]
)
example_btn = gr.Button("Use Random Example Audio")
example_message = gr.Textbox(label="Example Status", interactive=False)
accompanying_text = gr.Textbox(
label="Accompanying Text (Optional)",
placeholder="Add any text context or question about the audio...",
lines=2
)
audio_voice = gr.Dropdown(
choices=VOICES,
value="alloy",
label="Response Voice"
)
audio_submit = gr.Button("Process Audio & Generate Response")
with gr.Column():
audio_text_output = gr.Textbox(label="AI Response (Checks Error)", lines=5)
audio_audio_output = gr.Audio(label="AI Response (Audio)")
audio_transcribed_output = gr.Textbox(label="Transcription of Audio Response", lines=3)
input_transcription = gr.Textbox(label="Transcription of Input Audio", lines=3)
# Function to process audio input, generate response, and provide transcriptions
def audio_input_with_transcription(api_key, audio_path, text_prompt, voice):
if not check_api_key(api_key):
return "Please enter your OpenAI API key first.", None, "No API key provided.", "No API key provided."
# First transcribe the input audio
input_transcription = "N/A"
if audio_path:
input_transcription = transcribe_audio(api_key, audio_path)
else:
return "Please upload or record audio first.", None, "No audio to transcribe.", "No audio provided."
# Process the audio input and get response
text_response, response_audio_path = process_audio_input(api_key, audio_path, text_prompt, voice)
# Transcribe the response audio
response_transcription = "No audio generated to transcribe."
if response_audio_path:
response_transcription = transcribe_audio(api_key, response_audio_path)
return text_response, response_audio_path, response_transcription, input_transcription
audio_submit.click(
fn=audio_input_with_transcription,
inputs=[api_key, audio_input, accompanying_text, audio_voice],
outputs=[audio_text_output, audio_audio_output, audio_transcribed_output, input_transcription]
)
example_btn.click(
fn=use_example_audio,
inputs=[],
outputs=[audio_input, example_message]
)
with gr.Tab("Voice Samples"):
gr.Markdown("## Listen to samples of each voice")
def generate_voice_sample(api_key, voice_type):
if not check_api_key(api_key):
return "Please enter your OpenAI API key first.", None, "No API key provided."
try:
client = create_openai_client(api_key)
# Use retry mechanism
for attempt in range(MAX_RETRIES):
try:
completion = client.chat.completions.create(
model="gpt-4o-audio-preview",
modalities=["text", "audio"],
audio={"voice": voice_type, "format": "wav"},
messages=[
{
"role": "user",
"content": f"This is a sample of the {voice_type} voice. It has its own unique tone and character."
}
]
)
# Save the audio to a temporary file
wav_bytes = base64.b64decode(completion.choices[0].message.audio.data)
temp_path = tempfile.mktemp(suffix=".wav")
with open(temp_path, "wb") as f:
f.write(wav_bytes)
# Get transcription
transcription = transcribe_audio(api_key, temp_path)
return f"Sample generated with voice: {voice_type}", temp_path, transcription
except (ConnectionError, Timeout) as e:
if attempt < MAX_RETRIES - 1:
time.sleep(RETRY_DELAY)
continue
else:
return f"Connection error after {MAX_RETRIES} attempts: {str(e)}. Please check your internet connection.", None, "No sample generated."
except Exception as e:
return f"Error: {str(e)}", None, "No transcription available."
with gr.Row():
sample_voice = gr.Dropdown(
choices=VOICES,
value="alloy",
label="Select Voice Sample"
)
sample_btn = gr.Button("Generate Sample")
with gr.Row():
sample_text = gr.Textbox(label="Status")
sample_audio = gr.Audio(label="Voice Sample")
sample_transcription = gr.Textbox(label="Transcription", lines=3)
sample_btn.click(
fn=generate_voice_sample,
inputs=[api_key, sample_voice],
outputs=[sample_text, sample_audio, sample_transcription]
)
# New tab for audio translation with improved error handling
with gr.Tab("Audio Translation"):
gr.Markdown("## Translate audio from other languages to English")
gr.Markdown("Supports 50+ languages including: Arabic, Chinese, French, German, Japanese, Spanish, and many more.")
with gr.Row():
with gr.Column():
translation_audio_input = gr.Audio(
label="Audio to Translate",
type="filepath",
sources=["microphone", "upload"]
)
translate_btn = gr.Button("Translate to English")
connection_status = gr.Textbox(label="Connection Status", value="Ready", interactive=False)
with gr.Column():
translation_output = gr.Textbox(label="English Translation", lines=5)
original_transcription = gr.Textbox(label="Original Transcription (if available)", lines=5)
def translate_audio_input(api_key, audio_path):
"""Handle the translation of uploaded audio with better connection handling"""
if not check_api_key(api_key):
return "Please enter your OpenAI API key first.", "No API key provided.", "No API key provided."
try:
if not audio_path:
return "Please upload or record audio first.", "No audio to translate.", "Connection ready"
# Update connection status
yield "Processing...", "Preparing audio for translation...", "Connecting to OpenAI API..."
# Get the translation
translation = translate_audio(api_key, audio_path)
# If there's a connection error message in the translation
if "connection error" in translation.lower():
yield translation, "Translation failed due to connection issues.", "Connection failed"
return
# Try to get original transcription (this might be in the original language)
try:
original = transcribe_audio(api_key, audio_path)
if "error" in original.lower():
original = "Could not transcribe original audio due to connection issues."
except Exception:
original = "Could not transcribe original audio."
yield translation, original, "Connection successful"
except ConnectionError as e:
yield f"Connection error: {str(e)}. Please check your internet connection and try again.", "Translation failed.", "Connection failed"
except Timeout as e:
yield f"Timeout error: {str(e)}. The request took too long to complete. Please try again.", "Translation timed out.", "Connection timed out"
except Exception as e:
yield f"Translation error: {str(e)}", "Error occurred during processing.", "Error occurred"
translate_btn.click(
fn=translate_audio_input,
inputs=[api_key, translation_audio_input],
outputs=[translation_output, original_transcription, connection_status]
)
# Show supported languages
with gr.Accordion("Supported Languages", open=False):
gr.Markdown(", ".join(SUPPORTED_LANGUAGES))
# Connection troubleshooting tips
with gr.Accordion("Connection Troubleshooting", open=False):
gr.Markdown("""
### If you experience connection errors:
1. **Check your internet connection** - Ensure you have a stable internet connection
2. **Verify your API key** - Make sure your OpenAI API key is valid and has sufficient credits
3. **Try a smaller audio file** - Large audio files may time out during upload
4. **Wait and retry** - OpenAI servers might be experiencing high traffic
5. **Check file format** - Make sure your audio file is in a supported format (MP3, WAV, etc.)
6. **Try on a different network** - Some networks might block API calls to OpenAI
The app will automatically retry failed connections up to 3 times.
""")
gr.Markdown("""
## Notes:
- You must provide your OpenAI API key in the field above
- The model used is `gpt-4o-audio-preview` for conversation, `gpt-4o-transcribe` for transcriptions, and `whisper-1` for translations
- Audio inputs should be in WAV format for chat and any supported format for translation
- Available voices: alloy, ash, ballad, coral, echo, fable, onyx, nova, sage, shimmer, and verse
- Each audio response is automatically transcribed for verification
- The "Use Random Example Audio" button will load a random sample from OpenAI's demo voices
- The translation feature supports 50+ languages, translating them to English
- If you experience connection errors, the app will automatically retry up to 3 times
""")
if __name__ == "__main__":
app.launch() |