Spaces:
Runtime error
Runtime error
Improves audio generation robustness
Browse filesAdds input validation and sanitization to audio generation functions to prevent errors caused by invalid or unexpected inputs.
Handles potential errors gracefully with fallback mechanisms, including a simplified audio tone as a last resort, enhancing the application's stability and user experience.
Also, adds limits to the random seed input to prevent errors.
app.py
CHANGED
|
@@ -6,12 +6,26 @@ def generate_audio_from_prompt(prompt, duration, seed):
|
|
| 6 |
"""
|
| 7 |
Generate audio using simple synthesis based on prompt characteristics
|
| 8 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
sample_rate = 44100
|
| 10 |
duration_samples = int(duration * sample_rate)
|
| 11 |
|
| 12 |
-
# Set seed for reproducibility
|
| 13 |
if seed is not None:
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# Extract features from prompt to influence audio
|
| 17 |
prompt_lower = prompt.lower()
|
|
@@ -77,6 +91,15 @@ def create_audio_generation_interface():
|
|
| 77 |
Generate audio based on text prompt using intelligent synthesis
|
| 78 |
"""
|
| 79 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
print(f"Generating audio for prompt: '{prompt}', duration: {duration}s, seed: {seed}")
|
| 81 |
|
| 82 |
# Use our intelligent synthesis function
|
|
@@ -86,13 +109,24 @@ def create_audio_generation_interface():
|
|
| 86 |
|
| 87 |
except Exception as e:
|
| 88 |
print(f"Error generating audio: {e}")
|
| 89 |
-
# Ultimate fallback
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
# Create the Gradio interface
|
| 98 |
with gr.Blocks(title="Stable Audio Open", theme=gr.themes.Soft()) as interface:
|
|
@@ -123,7 +157,9 @@ def create_audio_generation_interface():
|
|
| 123 |
seed_input = gr.Number(
|
| 124 |
label="Random Seed (optional)",
|
| 125 |
value=None,
|
| 126 |
-
precision=0
|
|
|
|
|
|
|
| 127 |
)
|
| 128 |
|
| 129 |
generate_btn = gr.Button("🎵 Generate Audio", variant="primary")
|
|
|
|
| 6 |
"""
|
| 7 |
Generate audio using simple synthesis based on prompt characteristics
|
| 8 |
"""
|
| 9 |
+
# Input validation and sanitization
|
| 10 |
+
if prompt is None:
|
| 11 |
+
prompt = "gentle melody"
|
| 12 |
+
if not isinstance(prompt, str):
|
| 13 |
+
prompt = str(prompt)
|
| 14 |
+
if duration is None or not isinstance(duration, (int, float)) or duration <= 0:
|
| 15 |
+
duration = 10.0 # Default duration
|
| 16 |
+
duration = min(max(duration, 1.0), 30.0) # Clamp to reasonable range
|
| 17 |
+
|
| 18 |
sample_rate = 44100
|
| 19 |
duration_samples = int(duration * sample_rate)
|
| 20 |
|
| 21 |
+
# Set seed for reproducibility - handle None case explicitly
|
| 22 |
if seed is not None:
|
| 23 |
+
try:
|
| 24 |
+
seed_int = int(seed)
|
| 25 |
+
np.random.seed(seed_int)
|
| 26 |
+
except (ValueError, TypeError):
|
| 27 |
+
# If seed can't be converted to int, use system entropy
|
| 28 |
+
pass
|
| 29 |
|
| 30 |
# Extract features from prompt to influence audio
|
| 31 |
prompt_lower = prompt.lower()
|
|
|
|
| 91 |
Generate audio based on text prompt using intelligent synthesis
|
| 92 |
"""
|
| 93 |
try:
|
| 94 |
+
# Input validation for main function
|
| 95 |
+
if prompt is None:
|
| 96 |
+
prompt = "gentle melody"
|
| 97 |
+
if not isinstance(prompt, str):
|
| 98 |
+
prompt = str(prompt)
|
| 99 |
+
if duration is None or not isinstance(duration, (int, float)):
|
| 100 |
+
duration = 10.0
|
| 101 |
+
duration = float(max(1.0, min(30.0, duration))) # Ensure valid range
|
| 102 |
+
|
| 103 |
print(f"Generating audio for prompt: '{prompt}', duration: {duration}s, seed: {seed}")
|
| 104 |
|
| 105 |
# Use our intelligent synthesis function
|
|
|
|
| 109 |
|
| 110 |
except Exception as e:
|
| 111 |
print(f"Error generating audio: {e}")
|
| 112 |
+
# Ultimate fallback with safety checks
|
| 113 |
+
try:
|
| 114 |
+
safe_duration = float(max(1.0, min(30.0, duration if isinstance(duration, (int, float)) else 10.0)))
|
| 115 |
+
sample_rate = 44100
|
| 116 |
+
duration_samples = int(safe_duration * sample_rate)
|
| 117 |
+
t = np.linspace(0, safe_duration, duration_samples, endpoint=False)
|
| 118 |
+
audio = 0.3 * np.sin(2 * np.pi * 440 * t) # Simple A4 tone
|
| 119 |
+
|
| 120 |
+
return (sample_rate, audio), f"Error: {str(e)}. Using simple fallback."
|
| 121 |
+
except Exception as fallback_error:
|
| 122 |
+
print(f"Fallback also failed: {fallback_error}")
|
| 123 |
+
# Absolute minimum fallback
|
| 124 |
+
sample_rate = 44100
|
| 125 |
+
duration_samples = 441000 # 10 seconds
|
| 126 |
+
t = np.linspace(0, 10.0, duration_samples, endpoint=False)
|
| 127 |
+
audio = 0.3 * np.sin(2 * np.pi * 440 * t)
|
| 128 |
+
|
| 129 |
+
return (sample_rate, audio), "Critical error occurred. Using emergency fallback."
|
| 130 |
|
| 131 |
# Create the Gradio interface
|
| 132 |
with gr.Blocks(title="Stable Audio Open", theme=gr.themes.Soft()) as interface:
|
|
|
|
| 157 |
seed_input = gr.Number(
|
| 158 |
label="Random Seed (optional)",
|
| 159 |
value=None,
|
| 160 |
+
precision=0,
|
| 161 |
+
minimum=0,
|
| 162 |
+
maximum=999999 # Reasonable upper limit
|
| 163 |
)
|
| 164 |
|
| 165 |
generate_btn = gr.Button("🎵 Generate Audio", variant="primary")
|