nkjoy commited on
Commit
d5b9769
Β·
verified Β·
1 Parent(s): 16fa115

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. app.py +118 -0
  2. 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