Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
iface.launch()
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
from huggingface_hub import InferenceClient
|
| 8 |
+
|
| 9 |
+
def _grab_best_device(use_gpu=True):
|
| 10 |
+
if torch.cuda.device_count() > 0 and use_gpu:
|
| 11 |
+
device = "cuda"
|
| 12 |
+
else:
|
| 13 |
+
device = "cpu"
|
| 14 |
+
return device
|
| 15 |
+
|
| 16 |
+
device = _grab_best_device()
|
| 17 |
+
|
| 18 |
+
title = """# MusiGen Prompt Upsampling"""
|
| 19 |
+
|
| 20 |
+
vibes = pipeline("text-to-audio",
|
| 21 |
+
"facebook/musicgen-stereo-medium",
|
| 22 |
+
torch_dtype=torch.float16,
|
| 23 |
+
device="cuda")
|
| 24 |
+
|
| 25 |
+
client = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta",)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
# Inference
|
| 29 |
+
def generate_audio(text,):
|
| 30 |
+
prompt = f"Take the next sentence and enrich it with details. Keep it compact. {text}"
|
| 31 |
+
output = client.text_generation(prompt, max_new_tokens=100)
|
| 32 |
+
out = vibes(output)
|
| 33 |
+
audio = out["audio"][0]
|
| 34 |
+
|
| 35 |
+
return audio
|
| 36 |
+
|
| 37 |
+
css = """
|
| 38 |
+
#container{
|
| 39 |
+
margin: 0 auto;
|
| 40 |
+
max-width: 80rem;
|
| 41 |
+
}
|
| 42 |
+
#intro{
|
| 43 |
+
max-width: 100%;
|
| 44 |
+
text-align: center;
|
| 45 |
+
margin: 0 auto;
|
| 46 |
+
}
|
| 47 |
+
"""
|
| 48 |
+
|
| 49 |
+
# Gradio blocks demo
|
| 50 |
+
with gr.Blocks(css=css) as demo_blocks:
|
| 51 |
+
gr.Markdown(title, elem_id="intro")
|
| 52 |
+
|
| 53 |
+
with gr.Row():
|
| 54 |
+
with gr.Column():
|
| 55 |
+
inp_text = gr.Textbox(label="Input Prompt", info="What would you like MusicGen to synthesise?")
|
| 56 |
+
btn = gr.Button("Generate Music!πΆ")
|
| 57 |
+
|
| 58 |
+
with gr.Column():
|
| 59 |
+
gr.Audio(type="numpy", autoplay=False, label=f"Generated Music", show_label=True,)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
with gr.Accordion("Run MusicGen with Transformers π€", open=False):
|
| 63 |
+
gr.Markdown(
|
| 64 |
+
"""
|
| 65 |
+
import torch
|
| 66 |
+
import soundfile as sf
|
| 67 |
+
from transformers import pipeline
|
| 68 |
+
|
| 69 |
+
synthesiser = pipeline("text-to-audio",
|
| 70 |
+
"facebook/musicgen-stereo-medium",
|
| 71 |
+
device="cuda:0",
|
| 72 |
+
torch_dtype=torch.float16)
|
| 73 |
+
|
| 74 |
+
music = synthesiser("lo-fi music with a soothing melody", forward_params={"max_new_tokens": 256})
|
| 75 |
+
|
| 76 |
+
sf.write("musicgen_out.wav", music["audio"][0].T, music["sampling_rate"])
|
| 77 |
+
|
| 78 |
+
"""
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
btn.click(generate_audio, [inp_text, language], outputs)
|
| 82 |
+
|
| 83 |
|
| 84 |
+
demo_blocks.queue().launch()
|
|
|