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