Spaces:
Runtime error
Runtime error
Commit
·
3845902
1
Parent(s):
66ac5f2
Update app.py with better error handling and audio selection
Browse files
app.py
CHANGED
|
@@ -7,11 +7,51 @@ print(f"Python version: {sys.version}")
|
|
| 7 |
print(f"Working directory: {os.getcwd()}")
|
| 8 |
print(f"Directory contents: {os.listdir('.')}")
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# Create necessary directories
|
| 11 |
os.makedirs("assets", exist_ok=True)
|
| 12 |
os.makedirs("ckpts", exist_ok=True)
|
| 13 |
os.makedirs("outputs", exist_ok=True)
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# Import required packages
|
| 16 |
import gradio as gr
|
| 17 |
|
|
@@ -26,7 +66,7 @@ with gr.Blocks(title="SonicDiffusion") as demo:
|
|
| 26 |
gr.Markdown("# SonicDiffusion - Audio-to-Image Generation")
|
| 27 |
gr.Markdown("Generate images conditioned on audio inputs using Stable Diffusion")
|
| 28 |
|
| 29 |
-
status_output = gr.Textbox(label="Status", value="System initialized.
|
| 30 |
|
| 31 |
with gr.Tab("1. Setup"):
|
| 32 |
with gr.Row():
|
|
@@ -54,7 +94,7 @@ with gr.Blocks(title="SonicDiffusion") as demo:
|
|
| 54 |
outputs=status_output
|
| 55 |
)
|
| 56 |
|
| 57 |
-
download_assets_btn = gr.Button("Download Required Assets")
|
| 58 |
download_assets_btn.click(fn=controller.download_assets, outputs=status_output)
|
| 59 |
|
| 60 |
with gr.Tab("2. Generate"):
|
|
@@ -66,7 +106,7 @@ with gr.Blocks(title="SonicDiffusion") as demo:
|
|
| 66 |
value="Landscape Model"
|
| 67 |
)
|
| 68 |
|
| 69 |
-
load_model_btn = gr.Button("Load Selected Model")
|
| 70 |
load_model_btn.click(
|
| 71 |
fn=controller.load_model,
|
| 72 |
inputs=[model_dropdown],
|
|
@@ -79,10 +119,27 @@ with gr.Blocks(title="SonicDiffusion") as demo:
|
|
| 79 |
value="a high quality photograph of a fantasy landscape"
|
| 80 |
)
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
audio_input = gr.Audio(
|
| 83 |
-
label="Upload Audio",
|
| 84 |
type="filepath",
|
| 85 |
-
sources=["upload"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
)
|
| 87 |
|
| 88 |
with gr.Row():
|
|
@@ -102,42 +159,26 @@ with gr.Blocks(title="SonicDiffusion") as demo:
|
|
| 102 |
step=5
|
| 103 |
)
|
| 104 |
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
with gr.Column():
|
| 108 |
-
output_image = gr.Image(label="Generated Image")
|
|
|
|
| 109 |
|
| 110 |
generate_btn.click(
|
| 111 |
-
fn=
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
| 114 |
)
|
| 115 |
-
|
| 116 |
-
with gr.Row():
|
| 117 |
-
gr.Markdown("### Example Audio Files")
|
| 118 |
-
gr.Markdown("""
|
| 119 |
-
Example audio files will be downloaded to the 'assets' folder when you click 'Download Required Assets'.
|
| 120 |
-
- fire_crackling.wav - Natural fire sounds
|
| 121 |
-
- plastic_bag.wav - Crinkling plastic sounds
|
| 122 |
-
|
| 123 |
-
Use these with prompts like "a serene landscape with mountains" or "a mysterious forest at night".
|
| 124 |
-
""")
|
| 125 |
|
| 126 |
if __name__ == "__main__":
|
| 127 |
-
# Attempt to download example audio files if they don't exist
|
| 128 |
-
if not os.path.exists("assets/fire_crackling.wav") or not os.path.exists("assets/plastic_bag.wav"):
|
| 129 |
-
try:
|
| 130 |
-
from download_assets import download_gdrive_file
|
| 131 |
-
|
| 132 |
-
assets = {
|
| 133 |
-
"assets/fire_crackling.wav": "1vOAZcbkpo_hre2g26n--lUXdwbTQp22k",
|
| 134 |
-
"assets/plastic_bag.wav": "15igeDor7a47a-oluSCfO6GeUvFVl2ttb"
|
| 135 |
-
}
|
| 136 |
-
|
| 137 |
-
for path, file_id in assets.items():
|
| 138 |
-
if not os.path.exists(path):
|
| 139 |
-
download_gdrive_file(file_id, path)
|
| 140 |
-
except Exception as e:
|
| 141 |
-
print(f"Error downloading example audio files: {e}")
|
| 142 |
-
|
| 143 |
demo.launch()
|
|
|
|
| 7 |
print(f"Working directory: {os.getcwd()}")
|
| 8 |
print(f"Directory contents: {os.listdir('.')}")
|
| 9 |
|
| 10 |
+
# Import key packages and print versions
|
| 11 |
+
packages = {
|
| 12 |
+
"torch": None,
|
| 13 |
+
"transformers": None,
|
| 14 |
+
"diffusers": None,
|
| 15 |
+
"accelerate": None,
|
| 16 |
+
"einops": None,
|
| 17 |
+
"omegaconf": None,
|
| 18 |
+
"librosa": None
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
for package in packages.keys():
|
| 22 |
+
try:
|
| 23 |
+
module = __import__(package)
|
| 24 |
+
try:
|
| 25 |
+
packages[package] = module.__version__
|
| 26 |
+
print(f"{package}: {module.__version__}")
|
| 27 |
+
except AttributeError:
|
| 28 |
+
packages[package] = "Installed (version unknown)"
|
| 29 |
+
print(f"{package}: Installed (version unknown)")
|
| 30 |
+
except ImportError as e:
|
| 31 |
+
print(f"{package}: Not installed")
|
| 32 |
+
packages[package] = "Not installed"
|
| 33 |
+
|
| 34 |
# Create necessary directories
|
| 35 |
os.makedirs("assets", exist_ok=True)
|
| 36 |
os.makedirs("ckpts", exist_ok=True)
|
| 37 |
os.makedirs("outputs", exist_ok=True)
|
| 38 |
|
| 39 |
+
# Download example audio files if they don't exist
|
| 40 |
+
if not os.path.exists("assets/fire_crackling.wav") or not os.path.exists("assets/plastic_bag.wav"):
|
| 41 |
+
try:
|
| 42 |
+
from download_assets import download_gdrive_file
|
| 43 |
+
|
| 44 |
+
assets = {
|
| 45 |
+
"assets/fire_crackling.wav": "1vOAZcbkpo_hre2g26n--lUXdwbTQp22k",
|
| 46 |
+
"assets/plastic_bag.wav": "15igeDor7a47a-oluSCfO6GeUvFVl2ttb"
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
for path, file_id in assets.items():
|
| 50 |
+
if not os.path.exists(path):
|
| 51 |
+
download_gdrive_file(file_id, path)
|
| 52 |
+
except Exception as e:
|
| 53 |
+
print(f"Error downloading example audio files: {e}")
|
| 54 |
+
|
| 55 |
# Import required packages
|
| 56 |
import gradio as gr
|
| 57 |
|
|
|
|
| 66 |
gr.Markdown("# SonicDiffusion - Audio-to-Image Generation")
|
| 67 |
gr.Markdown("Generate images conditioned on audio inputs using Stable Diffusion")
|
| 68 |
|
| 69 |
+
status_output = gr.Textbox(label="Status", value="System initialized. Start by checking dependencies and downloading assets.", lines=8)
|
| 70 |
|
| 71 |
with gr.Tab("1. Setup"):
|
| 72 |
with gr.Row():
|
|
|
|
| 94 |
outputs=status_output
|
| 95 |
)
|
| 96 |
|
| 97 |
+
download_assets_btn = gr.Button("Download Required Assets", variant="primary")
|
| 98 |
download_assets_btn.click(fn=controller.download_assets, outputs=status_output)
|
| 99 |
|
| 100 |
with gr.Tab("2. Generate"):
|
|
|
|
| 106 |
value="Landscape Model"
|
| 107 |
)
|
| 108 |
|
| 109 |
+
load_model_btn = gr.Button("Load Selected Model", variant="primary")
|
| 110 |
load_model_btn.click(
|
| 111 |
fn=controller.load_model,
|
| 112 |
inputs=[model_dropdown],
|
|
|
|
| 119 |
value="a high quality photograph of a fantasy landscape"
|
| 120 |
)
|
| 121 |
|
| 122 |
+
# Use the preloaded example audio files
|
| 123 |
+
audio_dropdown = gr.Dropdown(
|
| 124 |
+
label="Select Example Audio",
|
| 125 |
+
choices=["assets/fire_crackling.wav", "assets/plastic_bag.wav", "Upload my own audio"],
|
| 126 |
+
value="assets/fire_crackling.wav"
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
audio_input = gr.Audio(
|
| 130 |
+
label="Or Upload Your Own Audio",
|
| 131 |
type="filepath",
|
| 132 |
+
sources=["upload", "microphone"],
|
| 133 |
+
visible=False
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
def toggle_audio_upload(choice):
|
| 137 |
+
return gr.update(visible=(choice == "Upload my own audio"))
|
| 138 |
+
|
| 139 |
+
audio_dropdown.change(
|
| 140 |
+
fn=toggle_audio_upload,
|
| 141 |
+
inputs=[audio_dropdown],
|
| 142 |
+
outputs=[audio_input]
|
| 143 |
)
|
| 144 |
|
| 145 |
with gr.Row():
|
|
|
|
| 159 |
step=5
|
| 160 |
)
|
| 161 |
|
| 162 |
+
def prepare_audio_path(dropdown_choice, uploaded_audio):
|
| 163 |
+
if dropdown_choice == "Upload my own audio":
|
| 164 |
+
return uploaded_audio
|
| 165 |
+
else:
|
| 166 |
+
return dropdown_choice
|
| 167 |
+
|
| 168 |
+
generate_btn = gr.Button("Generate Image", variant="primary")
|
| 169 |
|
| 170 |
with gr.Column():
|
| 171 |
+
output_image = gr.Image(label="Generated Image", type="pil")
|
| 172 |
+
output_status = gr.Textbox(label="Generation Status", value="")
|
| 173 |
|
| 174 |
generate_btn.click(
|
| 175 |
+
fn=lambda p, d, u, c, s: (
|
| 176 |
+
controller.generate(p, prepare_audio_path(d, u), c, s),
|
| 177 |
+
"Generation completed"
|
| 178 |
+
),
|
| 179 |
+
inputs=[prompt_input, audio_dropdown, audio_input, cfg_scale, steps],
|
| 180 |
+
outputs=[output_image, output_status]
|
| 181 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
demo.launch()
|