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"}
    ]
)