Spaces:
Runtime error
Runtime error
File size: 5,546 Bytes
63f345f 041bd28 698d4cd ccdc62f 041bd28 213e5d3 041bd28 0def226 213e5d3 698d4cd 213e5d3 041bd28 9c06b1a 0def226 041bd28 0def226 041bd28 9c06b1a 0def226 041bd28 0def226 041bd28 0def226 2f15cbe 0def226 041bd28 0def226 ccdc62f 63f345f ccdc62f 041bd28 63f345f 041bd28 81b2481 0def226 041bd28 81b2481 0def226 698d4cd 63f345f 24da5c3 63f345f 041bd28 18fbeec 698d4cd 041bd28 4d9e689 041bd28 698d4cd 4d9e689 0def226 172038e 041bd28 18fbeec 041bd28 172038e 698d4cd 63f345f a4f881b e18ae6e 041bd28 63f345f 041bd28 63f345f 041bd28 18fbeec 041bd28 18fbeec 041bd28 63f345f 041bd28 0def226 041bd28 2f15cbe 041bd28 698d4cd 8a09658 041bd28 e18ae6e 63f345f 041bd28 a4f881b 63f345f |
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 |
import spaces
import os
import tempfile
import gradio as gr
from dotenv import load_dotenv
import torch
from scipy.io.wavfile import write
from diffusers import DiffusionPipeline
from transformers import pipeline
from pathlib import Path
from PIL import Image # <-- ADDED THIS IMPORT
import io # <-- ADDED THIS IMPORT
load_dotenv()
hf_token = os.getenv("HF_TKN")
device_id = 0 if torch.cuda.is_available() else -1
# Correctly initialize the modern, reliable captioning pipeline
captioning_pipeline = pipeline(
"image-to-text",
model="Salesforce/blip-image-captioning-large",
device=device_id
)
# Initialize the audio pipeline
pipe = DiffusionPipeline.from_pretrained(
"cvssp/audioldm2",
use_auth_token=hf_token
)
# === THIS IS THE CORRECTED FUNCTION ===
@spaces.GPU(duration=120)
def analyze_image_with_free_model(image_file_bytes):
try:
# No more temp files!
# Open the image data directly from memory using Pillow
image = Image.open(io.BytesIO(image_file_bytes))
# Pass the Pillow Image object directly to the pipeline. This is the robust method.
results = captioning_pipeline(image)
if not results or not isinstance(results, list):
return "Error: Could not generate caption.", True
caption = results[0].get("generated_text", "").strip()
if not caption:
return "No caption was generated.", True
return caption, False
except Exception as e:
print(f"ERROR in analyze_image_with_free_model: {e}") # Print error to logs
return f"Error analyzing image: {e}", True
@spaces.GPU(duration=120)
def get_audioldm_from_caption(caption):
try:
pipe.to("cuda")
audio_output = pipe(
prompt=caption,
num_inference_steps=50,
guidance_scale=7.5
)
pipe.to("cpu")
audio = audio_output.audios[0]
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_wav:
write(temp_wav.name, 16000, audio)
return temp_wav.name
except Exception as e:
print(f"Error generating audio from caption: {e}")
return None
# --- Gradio UI (No changes needed here) ---
css = """
#col-container{
margin: 0 auto;
max-width: 800px;
}
"""
with gr.Blocks(css=css) as demo:
with gr.Column(elem_id="col-container"):
gr.HTML("""
<h1 style="text-align: center;">🎶 Generate Sound Effects from Image</h1>
<p style="text-align: center;">
âš¡ Powered by <a href="https://bilsimaging.com" target="_blank">Bilsimaging</a>
</p>
""")
gr.Markdown("""
Welcome to this unique sound effect generator! This tool allows you to upload an image and generate a
descriptive caption and a corresponding sound effect, all using free, open-source models on Hugging Face.
**💡 How it works:**
1. **Upload an image**: Choose an image that you'd like to analyze.
2. **Generate Description**: Click on 'Generate Description' to get a textual description of your uploaded image.
3. **Generate Sound Effect**: Based on the image description, click on 'Generate Sound Effect' to create a
sound effect that matches the image context.
Enjoy the journey from visual to auditory sensation with just a few clicks!
""")
image_upload = gr.File(label="Upload Image", type="binary")
generate_description_button = gr.Button("Generate Description")
caption_display = gr.Textbox(label="Image Description", interactive=False)
generate_sound_button = gr.Button("Generate Sound Effect")
audio_output = gr.Audio(label="Generated Sound Effect")
gr.Markdown("""
## 👥 How You Can Contribute
We welcome contributions and suggestions for improvements. Your feedback is invaluable
to the continuous enhancement of this application.
For support, questions, or to contribute, please contact us at
[[email protected]](mailto:[email protected]).
Support our work and get involved by donating through
[Ko-fi](https://ko-fi.com/bilsimaging). - Bilel Aroua
""")
gr.Markdown("""
## 📢 Stay Connected
This app is a testament to the creative possibilities that emerge when technology meets art.
Enjoy exploring the auditory landscape of your images!
""")
# --- Gradio event handlers (I've updated the function called here) ---
def update_caption(image_file_bytes):
# We pass the bytes from the uploader directly to our corrected function
description, _ = analyze_image_with_free_model(image_file_bytes)
return description
def generate_sound(description):
if not description or description.startswith("Error"):
return None
audio_path = get_audioldm_from_caption(description)
return audio_path
generate_description_button.click(
fn=update_caption,
inputs=image_upload,
outputs=caption_display
)
generate_sound_button.click(
fn=generate_sound,
inputs=caption_display,
outputs=audio_output
)
gr.HTML('<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FBils%2FGenerate-Sound-Effects-from-Image"><img src="https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FBils%2FGenerate-Sound-Effects-from-Image&countColor=%23263759" /></a>')
html = gr.HTML()
demo.launch(debug=True, share=True) |