import gradio as gr import os import requests import urllib from os import path from pydub import AudioSegment img_to_text = gr.Blocks.load(name="spaces/fffiloni/CLIP-Interrogator-2") text_to_music = gr.Interface.load("spaces/fffiloni/text-2-music") def get_prompts(uploaded_image): prompt = img_to_text(uploaded_image, "ViT-L (best for Stable Diffusion 1.*)", "fast", fn_index=1)[0] music_result = get_music(prompt) print(f"""————— PROMPT: {prompt} ——————— """) return music_result, prompt def get_music(prompt): result = text_to_music(prompt, fn_index=0) print(f"""————— MUSIC prompt: {result} ——————— """) url = result save_as = "file.mp3" data = urllib.request.urlopen(url) f = open(save_as,'wb') f.write(data.read()) f.close() wave_file="file.wav" sound = AudioSegment.from_mp3(save_as) sound.export(wave_file, format="wav") return wave_file css = """ #col-container {max-width: 700px; margin-left: auto; margin-right: auto;} a {text-decoration-line: underline; font-weight: 600;} .animate-spin { animation: spin 1s linear infinite; } @keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } } """ with gr.Blocks(css=css) as demo: with gr.Column(elem_id="col-container"): gr.HTML("""

Image to Music

Sends an image in to CLIP Interrogator to generate a text prompt which is then run through Mubert text-to-music to generate music from the input image!

""") input_img = gr.Image(type="filepath", elem_id="input-img") generate = gr.Button("Generate Music from Image") music_output = gr.Audio(label="Result", type="filepath", elem_id="music-output") prompt_text = gr.Textbox(label="Prompt") generate.click(get_prompts, inputs=[input_img], outputs=[music_output, prompt_text], api_name="i2m") demo.queue(max_size=32, concurrency_count=20).launch()