Spaces:
Runtime error
Runtime error
File size: 4,352 Bytes
d5b9769 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | import gradio as gr
import torch
import numpy as np
from transformers import AutoProcessor, MusicgenForConditionalGeneration
# λͺ¨λΈ λ‘λ (첫 μ€ν μ λ€μ΄λ‘λ λ° λ‘λ© μκ°μ΄ μμλ©λλ€.)
print("λͺ¨λΈμ λ‘λνλ μ€μ
λλ€... (facebook/musicgen-melody)")
device = "cuda" if torch.cuda.is_available() else "cpu"
model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-melody").to(device)
processor = AutoProcessor.from_pretrained("facebook/musicgen-melody")
print("λͺ¨λΈ λ‘λ© μλ£!")
def generate_music(text_prompt, audio_melody):
"""
ν
μ€νΈ ν둬ννΈμ μ νμ μΈ λ©λ‘λ μ€λμ€λ₯Ό λ°μ μμ
μ μμ±νλ ν¨μ.
"""
if not text_prompt:
return None, "Please enter a text description."
try:
# μ
λ ₯ μ μ²λ¦¬
# audio_melodyκ° Noneμ΄λ©΄ ν
μ€νΈ κΈ°λ°μΌλ‘λ§ μμ±ν©λλ€.
inputs = processor(
text=[text_prompt],
audio=audio_melody,
sampling_rate=processor.sampling_rate,
padding=True,
return_tensors="pt",
)
# λͺ¨λΈ μμ± (GPU μ¬μ© κ°λ₯ μ GPUλ‘ μ΄λ)
inputs = {k: v.to(device) for k, v in inputs.items()}
audio_values = model.generate(**inputs, max_new_tokens=256)
# κ²°κ³Όλ₯Ό numpy λ°°μ΄λ‘ λ³ν (Gradio μ€λμ€ μΆλ ₯μ μν΄)
sampling_rate = model.config.audio_encoder.sampling_rate
audio_data = audio_values[0].cpu().numpy()
return (sampling_rate, audio_data), "μμ±μ΄ μλ£λμμ΅λλ€!"
except Exception as e:
return None, f"μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}"
# Gradio 6 μ ν리μΌμ΄μ
μ μ
with gr.Blocks() as demo:
# ν€λ μΉμ
gr.Markdown(
"""
# π΅ AI μμ
μμ±κΈ° (MusicGen Melody)
ν
μ€νΈ μ€λͺ
μ μ
λ ₯νκ±°λ μμ λ§μ λ©λ‘λλ₯Ό μ
λ‘λνμ¬ AIκ° μμ
μ λ§λ€μ΄λ립λλ€.
"""
)
# νμ μꡬμ¬ν: Built with anycoder λ§ν¬
gr.HTML(
'<div style="text-align: center; margin-bottom: 20px;">'
'<a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" style="color: #f97316; font-weight: bold; text-decoration: none;">Built with anycoder</a>'
'</div>'
)
with gr.Row():
with gr.Column(scale=1):
# μ
λ ₯ μΉμ
text_input = gr.Textbox(
label="π μμ
μ€λͺ
(Prompt)",
placeholder="μ: A relaxing jazz song with piano and saxophone, 80s pop song with heavy synth",
lines=3,
max_lines=5
)
melody_input = gr.Audio(
label="πΌ μ°Έκ³ λ©λ‘λ (μ νμ¬ν)",
sources=["upload", "microphone"],
type="numpy",
info="νΉμ λ©λ‘λλ₯Ό κΈ°λ°μΌλ‘ μμ±νκ³ μΆλ€λ©΄ μ€λμ€λ₯Ό μ
λ‘λνμΈμ."
)
generate_btn = gr.Button("πΆ μμ
μμ±νκΈ°", variant="primary", size="lg")
with gr.Column(scale=1):
# μΆλ ₯ μΉμ
status_output = gr.Textbox(label="μν", interactive=False)
audio_output = gr.Audio(label="π§ μμ±λ μμ
", type="numpy")
# μμ μΉμ
gr.Examples(
examples=[
["A happy pop song with synth sounds", None],
["Sad violin melody in a rainy day", None],
["Epic orchestral trailer music with drums", None],
["Lo-fi hip hop beats for studying", None],
],
inputs=[text_input, melody_input],
)
# μ΄λ²€νΈ 리μ€λ μ°κ²°
generate_btn.click(
fn=generate_music,
inputs=[text_input, melody_input],
outputs=[audio_output, status_output],
api_visibility="public"
)
# Gradio 6 λ°μΉ λ©μλ (ν
λ§ λ° κΈ°ν μ€μ μ¬κΈ°μ μ μ©)
demo.launch(
theme=gr.themes.Soft(
primary_hue="orange",
secondary_hue="red",
neutral_hue="slate",
text_size="lg",
spacing_size="lg",
radius_size="md"
),
footer_links=[
{"label": "Hugging Face Model", "url": "https://huggingface.co/facebook/musicgen-melody"},
{"label": "Gradio", "url": "https://gradio.app"}
]
) |