|
import gradio as gr |
|
from gradio_client import Client |
|
import json |
|
import re |
|
|
|
def get_caption_from_kosmos(image_in): |
|
kosmos2_client = Client("https://ydshieh-kosmos-2.hf.space/") |
|
|
|
kosmos2_result = kosmos2_client.predict( |
|
image_in, |
|
"Detailed", |
|
fn_index=4 |
|
) |
|
|
|
print(f"KOSMOS2 RETURNS: {kosmos2_result}") |
|
|
|
with open(kosmos2_result[1], 'r') as f: |
|
data = json.load(f) |
|
|
|
reconstructed_sentence = [] |
|
for sublist in data: |
|
reconstructed_sentence.append(sublist[0]) |
|
|
|
full_sentence = ' '.join(reconstructed_sentence) |
|
|
|
|
|
|
|
pattern = r'^Describe this image in detail:\s*(.*)$' |
|
|
|
match = re.search(pattern, full_sentence) |
|
if match: |
|
description = match.group(1) |
|
print(description) |
|
else: |
|
print("Unable to locate valid description.") |
|
|
|
|
|
last_period_index = description.rfind('.') |
|
|
|
|
|
truncated_caption = description[:last_period_index + 1] |
|
|
|
|
|
print(f"\n—\nIMAGE CAPTION: {truncated_caption}") |
|
|
|
return truncated_caption |
|
|
|
def get_caption(image_in): |
|
client = Client("https://vikhyatk-moondream1.hf.space/--replicas/ggrds/") |
|
result = client.predict( |
|
image_in, |
|
"Describe precisely the image in one sentence.", |
|
api_name="/predict" |
|
) |
|
print(result) |
|
return result |
|
|
|
def get_magnet(prompt): |
|
amended_prompt = f"High quality sound effects. {prompt}" |
|
print(amended_prompt) |
|
client = Client("https://fffiloni-magnet.hf.space/--replicas/oo8sb/") |
|
result = client.predict( |
|
"facebook/audio-magnet-small", |
|
"", |
|
amended_prompt, |
|
3, |
|
0.9, |
|
10, |
|
1, |
|
20, |
|
10, |
|
10, |
|
10, |
|
"prod-stride1 (new!)", |
|
api_name="/predict_full" |
|
) |
|
print(result) |
|
return result[0]['video'] |
|
|
|
def get_audioldm(prompt): |
|
client = Client("https://haoheliu-audioldm2-text2audio-text2music.hf.space/") |
|
result = client.predict( |
|
prompt, |
|
"Low quality. Music.", |
|
10, |
|
3.5, |
|
45, |
|
3, |
|
fn_index=1 |
|
) |
|
print(result) |
|
return result |
|
|
|
def infer(image_in): |
|
caption = get_caption(image_in) |
|
magnet_result = get_magnet(caption) |
|
audioldm_result = get_audioldm(caption) |
|
return magnet_result, audioldm_result |
|
|
|
css=""" |
|
#col-container{ |
|
margin: 0 auto; |
|
max-width: 720px; |
|
} |
|
""" |
|
|
|
with gr.Blocks(css=css) 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 MAGNet and AudioLDM2 sound effects generation from image caption. |
|
</p> |
|
""") |
|
|
|
with gr.Column(): |
|
image_in = gr.Image(sources=["upload"], type="filepath", label="Image input", value="oiseau.png") |
|
submit_btn = gr.Button("Submit") |
|
with gr.Row(): |
|
magnet_o = gr.Video(label="MAGNet output") |
|
audioldm2_o = gr.Video(label="AudioLDM2 output") |
|
submit_btn.click( |
|
fn=infer, |
|
inputs=[image_in], |
|
outputs=[magnet_o, audioldm2_o] |
|
) |
|
demo.queue(max_size=10).launch(debug=True) |