|
import gradio as gr |
|
from gradio_client import Client |
|
import json |
|
import re |
|
from moviepy.editor import VideoFileClip |
|
from moviepy.audio.AudioClip import AudioClip |
|
|
|
def extract_audio(video_in): |
|
input_video = video_in |
|
output_audio = 'audio.wav' |
|
|
|
|
|
video_clip = VideoFileClip(input_video) |
|
audio_clip = video_clip.audio |
|
|
|
|
|
audio_clip.write_audiofile(output_audio, fps=44100) |
|
print("Audio extraction complete.") |
|
|
|
return 'audio.wav' |
|
|
|
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/") |
|
result = client.predict( |
|
image_in, |
|
"Describe precisely the image in one sentence.", |
|
api_name="/answer_question" |
|
) |
|
print(result) |
|
return result |
|
|
|
def get_magnet(prompt): |
|
amended_prompt = f"{prompt}" |
|
print(amended_prompt) |
|
client = Client("https://fffiloni-magnet.hf.space/") |
|
result = client.predict( |
|
"facebook/audio-magnet-medium", |
|
"", |
|
amended_prompt, |
|
3, |
|
0.9, |
|
10, |
|
1, |
|
20, |
|
10, |
|
10, |
|
10, |
|
"prod-stride1 (new!)", |
|
api_name="/predict_full" |
|
) |
|
print(result) |
|
return result[1] |
|
|
|
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) |
|
audio_result = extract_audio(result) |
|
return audio_result |
|
|
|
def get_audiogen(prompt): |
|
client = Client("https://fffiloni-audiogen.hf.space/") |
|
result = client.predict( |
|
prompt, |
|
10, |
|
api_name="/infer" |
|
) |
|
return result |
|
|
|
def infer(image_in, chosen_model): |
|
caption = get_caption(image_in) |
|
if chosen_model == "MAGNet" : |
|
magnet_result = get_magnet(caption) |
|
return magnet_result |
|
elif chosen_model == "AudioLDM-2" : |
|
audioldm_result = get_audioldm(caption) |
|
return audioldm_result |
|
elif chosen_model == "AudioGen" : |
|
audiogen_result = get_audiogen(caption) |
|
return audiogen_result |
|
|
|
css = """ |
|
footer { |
|
visibility: hidden; |
|
} |
|
""" |
|
|
|
with gr.Blocks(css=css) as demo: |
|
with gr.Column(): |
|
image_in = gr.Image(sources=["upload"], type="filepath", label="Image input") |
|
chosen_model = gr.Radio(label="Choose a model", choices=["MAGNet", "AudioLDM-2", "AudioGen"], value="AudioLDM-2") |
|
submit_btn = gr.Button("Submit") |
|
audio_o = gr.Audio(label="Audio output") |
|
|
|
|
|
submit_btn.click( |
|
fn=infer, |
|
inputs=[image_in, chosen_model], |
|
outputs=[audio_o] |
|
) |
|
|
|
|
|
|
|
demo.queue(max_size=10).launch(debug=True) |