Spaces:
Paused
Paused
Download app.py from Nymbo/Image2SFX-comparison: direct link, hf CLI and curl.
- Browser
- Download file 3.81 kB
-
https://huggingface.co/spaces/Nymbo/Image2SFX-comparison/resolve/main/app.py
- Command line
-
hf download hf://spaces/Nymbo/Image2SFX-comparison/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Nymbo/Image2SFX-comparison/resolve/main/app.py
3.81 kB
| import random | |
| import re | |
| import gradio as gr | |
| from gradio_client import Client, handle_file | |
| MAX_SEED = 2**31 - 1 | |
| def _client(space, request): | |
| # Forward the visitor's ZeroGPU identity so upstream GPU Spaces bill their quota, not this Space's IP | |
| headers = {} | |
| if request is not None: | |
| ip_token = request.headers.get("x-ip-token") | |
| if ip_token: | |
| headers["X-IP-Token"] = ip_token | |
| return Client(space, headers=headers, verbose=False) | |
| def get_caption(image_in, request=None): | |
| try: | |
| client = _client("fffiloni/Kosmos-2-API", request) | |
| result = client.predict( | |
| image_input=handle_file(image_in), | |
| text_input="Detailed", | |
| api_name="/generate_predictions", | |
| ) | |
| except Exception as e: | |
| raise gr.Error(f"Kosmos-2 caption Space is not ready, please try again in a few minutes ({e})") | |
| sentence = "".join(item["token"] for item in result[1]) | |
| sentence = re.sub(r"^\s*Describe this image in detail:\s*", "", sentence).strip() | |
| print(f"IMAGE CAPTION: {sentence}") | |
| return sentence | |
| def get_audioldm(prompt, request=None): | |
| try: | |
| client = _client("fffiloni/audioldm2-text2audio-text2music-API", request) | |
| return client.predict( | |
| prompt, | |
| "Low quality. Music.", | |
| 10, | |
| 3.5, | |
| random.randint(0, MAX_SEED), | |
| 3, | |
| api_name="/text2audio", | |
| ) | |
| except Exception as e: | |
| raise gr.Error(f"AudioLDM-2 Space API is not ready, please try again in a few minutes ({e})") | |
| def get_audiogen(prompt, request=None): | |
| try: | |
| client = _client("fffiloni/audiogen", request) | |
| return client.predict(prompt, 10, api_name="/infer") | |
| except Exception as e: | |
| raise gr.Error(f"AudioGen Space API is not ready, please try again in a few minutes ({e})") | |
| def get_ezaudio(prompt, request=None): | |
| try: | |
| client = _client("OpenSound/EzAudio", request) | |
| return client.predict(prompt, 10, 5, 0.75, 50, 1, 0, True, api_name="/generate_audio") | |
| except Exception as e: | |
| raise gr.Error(f"EzAudio Space API is not ready, please try again in a few minutes ({e})") | |
| def infer(image_in, chosen_model, request: gr.Request): | |
| if not image_in: | |
| raise gr.Error("Please provide an image.") | |
| caption = get_caption(image_in, request) | |
| if chosen_model == "AudioLDM-2": | |
| return caption, get_audioldm(caption, request) | |
| elif chosen_model == "AudioGen": | |
| return caption, get_audiogen(caption, request) | |
| elif chosen_model == "EzAudio": | |
| return caption, get_ezaudio(caption, request) | |
| raise gr.Error(f"Unknown model: {chosen_model}") | |
| css = """ | |
| #col-container{ | |
| margin: 0 auto; | |
| max-width: 800px; | |
| } | |
| """ | |
| with gr.Blocks() as demo: | |
| with gr.Column(elem_id="col-container"): | |
| gr.HTML(""" | |
| <h2 style="text-align: center;"> | |
| Image to SFX | |
| </h2> | |
| <p style="text-align: center;"> | |
| Compare AudioLDM2, AudioGen and EzAudio sound effects generation from image caption. | |
| </p> | |
| """) | |
| with gr.Column(): | |
| image_in = gr.Image(sources=["upload"], type="filepath", label="Image input", value="oiseau.png") | |
| with gr.Row(): | |
| chosen_model = gr.Radio(label="Choose a model", choices=["AudioLDM-2", "AudioGen", "EzAudio"], value="AudioLDM-2") | |
| submit_btn = gr.Button("Submit") | |
| with gr.Column(): | |
| caption_o = gr.Textbox(label="Image caption") | |
| audio_o = gr.Audio(label="Audio output") | |
| submit_btn.click( | |
| fn=infer, | |
| inputs=[image_in, chosen_model], | |
| outputs=[caption_o, audio_o], | |
| concurrency_limit=4, | |
| api_name="infer", | |
| ) | |
| demo.queue(max_size=10).launch(css=css) | |