|
|
|
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 |
|
import google.generativeai as genai |
|
from pathlib import Path |
|
|
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
genai.configure(api_key=os.getenv("API_KEY")) |
|
|
|
|
|
hf_token = os.getenv("HF_TKN") |
|
|
|
def analyze_image_with_gemini(image_file): |
|
""" |
|
Analyzes an uploaded image with Gemini and generates a descriptive caption. |
|
""" |
|
try: |
|
|
|
temp_image_path = tempfile.NamedTemporaryFile(delete=False, suffix=".jpg").name |
|
with open(temp_image_path, "wb") as temp_file: |
|
temp_file.write(image_file) |
|
|
|
|
|
image_parts = [{"mime_type": "image/jpeg", "data": Path(temp_image_path).read_bytes()}] |
|
prompt_parts = ["Describe precisely the image in one sentence.\n", image_parts[0], "\n"] |
|
generation_config = {"temperature": 0.05, "top_p": 1, "top_k": 26, "max_output_tokens": 4096} |
|
safety_settings = [{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}, |
|
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}, |
|
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}, |
|
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}] |
|
model = genai.GenerativeModel(model_name="gemini-1.0-pro-vision-latest", |
|
generation_config=generation_config, |
|
safety_settings=safety_settings) |
|
response = model.generate_content(prompt_parts) |
|
return response.text.strip(), False |
|
except Exception as e: |
|
print(f"Error analyzing image with Gemini: {e}") |
|
return "Error analyzing image with Gemini", True |
|
|
|
def get_audioldm_from_caption(caption): |
|
""" |
|
Generates sound from a caption using the AudioLDM-2 model. |
|
""" |
|
|
|
pipe = DiffusionPipeline.from_pretrained("cvssp/audioldm2", use_auth_token=hf_token) |
|
pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu") |
|
|
|
|
|
audio_output = pipe(prompt=caption, num_inference_steps=50, guidance_scale=7.5) |
|
audio = audio_output.audios[0] |
|
|
|
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav") |
|
write(temp_file.name, 16000, audio) |
|
|
|
return temp_file.name |
|
|
|
|
|
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" _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. Whether you're exploring the sound of nature, urban environments, or anything in between, this app brings your images to auditory life. |
|
|
|
**π‘ How it works:** |
|
1. **Upload an image**: Choose an image that you'd like to analyze. |
|
2. **Generate Description**: Click on 'Tap to Generate Description from the image' 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! |
|
|
|
For Example Demos sound effects generated , check out our [YouTube channel](https://www.youtube.com/playlist?list=PLwEbW4bdYBSC8exiJ9PfzufGND_14f--C) |
|
""") |
|
|
|
|
|
image_upload = gr.File(label="Upload Image", type="binary") |
|
generate_description_button = gr.Button("Tap to Generate a Description from your image") |
|
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! |
|
""") |
|
|
|
def update_caption(image_file): |
|
description, _ = analyze_image_with_gemini(image_file) |
|
return description |
|
|
|
|
|
def generate_sound(description): |
|
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 |
|
) |
|
|
|
|
|
|
|
|
|
demo.launch(debug=True, share=True) |