Spaces:
Running
on
Zero
Running
on
Zero
Update ui/components.py
Browse files- ui/components.py +68 -21
ui/components.py
CHANGED
@@ -18,9 +18,16 @@ from openai import OpenAI
|
|
18 |
|
19 |
# OpenAI ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
|
20 |
try:
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
client = None
|
|
|
24 |
|
25 |
TAG_DEFAULT = "funk, pop, soul, rock, melodic, guitar, drums, bass, keyboard, percussion, 105 BPM, energetic, upbeat, groovy, vibrant, dynamic, duet, male and female vocals"
|
26 |
LYRIC_DEFAULT = """[verse - male]
|
@@ -62,7 +69,7 @@ In this moment we take flight
|
|
62 |
|
63 |
# ํ์ฅ๋ ์ฅ๋ฅด ํ๋ฆฌ์
(๊ธฐ์กด + ๊ฐ์ ๋ ํ๊ทธ)
|
64 |
GENRE_PRESETS = {
|
65 |
-
"Modern Pop": "pop, synth, drums, guitar, 120 bpm, upbeat, catchy, vibrant,
|
66 |
"Rock": "rock, electric guitar, drums, bass, 130 bpm, energetic, rebellious, gritty, powerful vocals, raw vocals, power chords, driving rhythm",
|
67 |
"Hip Hop": "hip hop, 808 bass, hi-hats, synth, 90 bpm, bold, urban, intense, rhythmic vocals, trap beats, punchy drums",
|
68 |
"Country": "country, acoustic guitar, steel guitar, fiddle, 100 bpm, heartfelt, rustic, warm, twangy vocals, storytelling, americana",
|
@@ -109,22 +116,29 @@ LYRIC_SYSTEM_PROMPT = """๋๋ ๋
ธ๋ ๊ฐ์ฌ๋ฅผ ์์ฌํ๋ ์ ๋ฌธ๊ฐ ์ญํ
|
|
109 |
...
|
110 |
"""
|
111 |
|
112 |
-
def generate_lyrics_with_ai(prompt: str, genre: str, song_style: str
|
113 |
"""AI๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ฌ ์์ฑ"""
|
114 |
if not client:
|
|
|
|
|
|
|
|
|
|
|
115 |
return LYRIC_DEFAULT
|
116 |
|
117 |
try:
|
118 |
# ์ธ์ด ๊ฐ์ง ๋ฐ ์คํ์ผ ์ ๋ณด ์ถ๊ฐ
|
119 |
style_info = ""
|
120 |
if "๋์ฃ" in song_style:
|
121 |
-
style_info = "๋จ๋
๋์ฃ ํ์์ผ๋ก ํํธ๋ฅผ ๋๋์ด ์์ฑํด์ฃผ์ธ์."
|
122 |
elif "์๋ก (๋จ์ฑ)" in song_style:
|
123 |
style_info = "๋จ์ฑ ์๋ก ๊ฐ์๋ฅผ ์ํ ๊ฐ์ฌ๋ฅผ ์์ฑํด์ฃผ์ธ์."
|
124 |
elif "์๋ก (์ฌ์ฑ)" in song_style:
|
125 |
style_info = "์ฌ์ฑ ์๋ก ๊ฐ์๋ฅผ ์ํ ๊ฐ์ฌ๋ฅผ ์์ฑํด์ฃผ์ธ์."
|
126 |
elif "๊ทธ๋ฃน" in song_style:
|
127 |
style_info = "๊ทธ๋ฃน์ด ๋ถ๋ฅด๋ ํ์์ผ๋ก ํํธ๋ฅผ ๋๋์ด ์์ฑํด์ฃผ์ธ์."
|
|
|
|
|
128 |
|
129 |
user_prompt = f"""
|
130 |
์ฃผ์ : {prompt}
|
@@ -134,8 +148,10 @@ def generate_lyrics_with_ai(prompt: str, genre: str, song_style: str, language:
|
|
134 |
์ ์ ๋ณด๋ฅผ ๋ฐํ์ผ๋ก ๋
ธ๋ ๊ฐ์ฌ๋ฅผ ์์ฑํด์ฃผ์ธ์. ์
๋ ฅ๋ ์ธ์ด์ ๋์ผํ ์ธ์ด๋ก ์์ฑํ๊ณ , ๊ตฌ์กฐ ํ๊ทธ๋ฅผ ๋ฐ๋์ ํฌํจํด์ฃผ์ธ์.
|
135 |
"""
|
136 |
|
|
|
|
|
137 |
response = client.chat.completions.create(
|
138 |
-
model="gpt-
|
139 |
messages=[
|
140 |
{"role": "system", "content": LYRIC_SYSTEM_PROMPT},
|
141 |
{"role": "user", "content": user_prompt}
|
@@ -144,7 +160,10 @@ def generate_lyrics_with_ai(prompt: str, genre: str, song_style: str, language:
|
|
144 |
max_tokens=1000
|
145 |
)
|
146 |
|
147 |
-
|
|
|
|
|
|
|
148 |
except Exception as e:
|
149 |
print(f"AI ๊ฐ์ฌ ์์ฑ ์ค๋ฅ: {e}")
|
150 |
return LYRIC_DEFAULT
|
@@ -226,9 +245,9 @@ generation_cache = MusicGenerationCache()
|
|
226 |
|
227 |
def enhance_prompt_with_genre(base_prompt: str, genre: str, song_style: str) -> str:
|
228 |
"""์ฅ๋ฅด์ ์คํ์ผ์ ๋ฐ๋ฅธ ์ค๋งํธ ํ๋กฌํํธ ํ์ฅ"""
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
# ์ฅ๋ฅด๋ณ ์ถ๊ฐ ๊ฐ์ ํ๊ทธ
|
233 |
genre_enhancements = {
|
234 |
"Modern Pop": ["polished production", "mainstream appeal", "hook-driven"],
|
@@ -248,8 +267,6 @@ def enhance_prompt_with_genre(base_prompt: str, genre: str, song_style: str) ->
|
|
248 |
if genre in genre_enhancements:
|
249 |
additional_tags = ", ".join(genre_enhancements[genre])
|
250 |
enhanced_prompt = f"{base_prompt}, {additional_tags}"
|
251 |
-
else:
|
252 |
-
enhanced_prompt = base_prompt
|
253 |
|
254 |
# ์คํ์ผ ํ๊ทธ ์ถ๊ฐ
|
255 |
if song_style in SONG_STYLES:
|
@@ -278,10 +295,19 @@ def calculate_quality_score(audio_path: str) -> float:
|
|
278 |
except:
|
279 |
return 50.0 # ๊ธฐ๋ณธ๊ฐ
|
280 |
|
281 |
-
def update_tags_from_preset(preset_name):
|
|
|
282 |
if preset_name == "Custom":
|
283 |
-
return
|
284 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
285 |
|
286 |
def update_quality_preset(preset_name):
|
287 |
"""ํ์ง ํ๋ฆฌ์
์ ์ฉ"""
|
@@ -690,9 +716,12 @@ def create_text2music_ui(
|
|
690 |
|
691 |
# AI ์์ฌ ๋ฒํผ ์ด๋ฒคํธ
|
692 |
def generate_ai_lyrics(lyric_prompt, genre_preset, song_style):
|
693 |
-
if not lyric_prompt:
|
694 |
-
return
|
695 |
-
|
|
|
|
|
|
|
696 |
|
697 |
generate_lyrics_btn.click(
|
698 |
fn=generate_ai_lyrics,
|
@@ -752,10 +781,20 @@ def create_text2music_ui(
|
|
752 |
None, # ref_audio_input
|
753 |
)
|
754 |
|
755 |
-
#
|
|
|
|
|
|
|
756 |
genre_preset.change(
|
757 |
-
fn=
|
758 |
-
inputs=[genre_preset],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
759 |
outputs=[prompt]
|
760 |
)
|
761 |
|
@@ -1570,6 +1609,14 @@ def create_main_demo_ui(
|
|
1570 |
- **์ฅ๋ฅด ํนํ**: ์ฅ๋ฅด ํ๋ฆฌ์
์ ํ ํ "์ค๋งํธ ํฅ์" ์ฒดํฌ
|
1571 |
- **๊ฐ์ฌ ๊ตฌ์กฐ**: [verse], [chorus], [bridge] ํ๊ทธ ์ ๊ทน ํ์ฉ
|
1572 |
- **๋ค๊ตญ์ด ์ง์**: ํ๊ตญ์ด๋ก ์ฃผ์ ๋ฅผ ์
๋ ฅํ๋ฉด ํ๊ตญ์ด ๊ฐ์ฌ๊ฐ ์์ฑ๋ฉ๋๋ค
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1573 |
""")
|
1574 |
|
1575 |
with gr.Tab("๐ต Enhanced Text2Music", elem_classes="gr-tab"):
|
|
|
18 |
|
19 |
# OpenAI ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
|
20 |
try:
|
21 |
+
api_key = os.getenv("LLM_API") or os.getenv("OPENAI_API_KEY")
|
22 |
+
if api_key:
|
23 |
+
client = OpenAI(api_key=api_key)
|
24 |
+
print("OpenAI API client initialized successfully")
|
25 |
+
else:
|
26 |
+
client = None
|
27 |
+
print("Warning: No OpenAI API key found. AI lyrics generation will be disabled.")
|
28 |
+
except Exception as e:
|
29 |
client = None
|
30 |
+
print(f"Warning: Failed to initialize OpenAI client: {e}")
|
31 |
|
32 |
TAG_DEFAULT = "funk, pop, soul, rock, melodic, guitar, drums, bass, keyboard, percussion, 105 BPM, energetic, upbeat, groovy, vibrant, dynamic, duet, male and female vocals"
|
33 |
LYRIC_DEFAULT = """[verse - male]
|
|
|
69 |
|
70 |
# ํ์ฅ๋ ์ฅ๋ฅด ํ๋ฆฌ์
(๊ธฐ์กด + ๊ฐ์ ๋ ํ๊ทธ)
|
71 |
GENRE_PRESETS = {
|
72 |
+
"Modern Pop": "pop, synth, drums, guitar, 120 bpm, upbeat, catchy, vibrant, polished vocals, radio-ready, commercial, layered vocals",
|
73 |
"Rock": "rock, electric guitar, drums, bass, 130 bpm, energetic, rebellious, gritty, powerful vocals, raw vocals, power chords, driving rhythm",
|
74 |
"Hip Hop": "hip hop, 808 bass, hi-hats, synth, 90 bpm, bold, urban, intense, rhythmic vocals, trap beats, punchy drums",
|
75 |
"Country": "country, acoustic guitar, steel guitar, fiddle, 100 bpm, heartfelt, rustic, warm, twangy vocals, storytelling, americana",
|
|
|
116 |
...
|
117 |
"""
|
118 |
|
119 |
+
def generate_lyrics_with_ai(prompt: str, genre: str, song_style: str) -> str:
|
120 |
"""AI๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ฌ ์์ฑ"""
|
121 |
if not client:
|
122 |
+
print("OpenAI client not available, returning default lyrics")
|
123 |
+
return LYRIC_DEFAULT
|
124 |
+
|
125 |
+
if not prompt or prompt.strip() == "":
|
126 |
+
print("Empty prompt, returning default lyrics")
|
127 |
return LYRIC_DEFAULT
|
128 |
|
129 |
try:
|
130 |
# ์ธ์ด ๊ฐ์ง ๋ฐ ์คํ์ผ ์ ๋ณด ์ถ๊ฐ
|
131 |
style_info = ""
|
132 |
if "๋์ฃ" in song_style:
|
133 |
+
style_info = "๋จ๋
๋์ฃ ํ์์ผ๋ก ํํธ๋ฅผ ๋๋์ด ์์ฑํด์ฃผ์ธ์. [verse - male], [verse - female], [chorus - duet] ํ์์ ์ฌ์ฉํ์ธ์."
|
134 |
elif "์๋ก (๋จ์ฑ)" in song_style:
|
135 |
style_info = "๋จ์ฑ ์๋ก ๊ฐ์๋ฅผ ์ํ ๊ฐ์ฌ๋ฅผ ์์ฑํด์ฃผ์ธ์."
|
136 |
elif "์๋ก (์ฌ์ฑ)" in song_style:
|
137 |
style_info = "์ฌ์ฑ ์๋ก ๊ฐ์๋ฅผ ์ํ ๊ฐ์ฌ๋ฅผ ์์ฑํด์ฃผ์ธ์."
|
138 |
elif "๊ทธ๋ฃน" in song_style:
|
139 |
style_info = "๊ทธ๋ฃน์ด ๋ถ๋ฅด๋ ํ์์ผ๋ก ํํธ๋ฅผ ๋๋์ด ์์ฑํด์ฃผ์ธ์."
|
140 |
+
elif "์ธ์คํธ๋ฃจ๋ฉํ" in song_style:
|
141 |
+
return "[instrumental]\n\n[inst]\n\n[instrumental break]\n\n[inst]"
|
142 |
|
143 |
user_prompt = f"""
|
144 |
์ฃผ์ : {prompt}
|
|
|
148 |
์ ์ ๋ณด๋ฅผ ๋ฐํ์ผ๋ก ๋
ธ๋ ๊ฐ์ฌ๋ฅผ ์์ฑํด์ฃผ์ธ์. ์
๋ ฅ๋ ์ธ์ด์ ๋์ผํ ์ธ์ด๋ก ์์ฑํ๊ณ , ๊ตฌ์กฐ ํ๊ทธ๋ฅผ ๋ฐ๋์ ํฌํจํด์ฃผ์ธ์.
|
149 |
"""
|
150 |
|
151 |
+
print(f"Generating lyrics with prompt: {prompt}")
|
152 |
+
|
153 |
response = client.chat.completions.create(
|
154 |
+
model="gpt-4o-mini", # ๋ชจ๋ธ๋ช
์์
|
155 |
messages=[
|
156 |
{"role": "system", "content": LYRIC_SYSTEM_PROMPT},
|
157 |
{"role": "user", "content": user_prompt}
|
|
|
160 |
max_tokens=1000
|
161 |
)
|
162 |
|
163 |
+
generated_lyrics = response.choices[0].message.content
|
164 |
+
print(f"Generated lyrics successfully")
|
165 |
+
return generated_lyrics
|
166 |
+
|
167 |
except Exception as e:
|
168 |
print(f"AI ๊ฐ์ฌ ์์ฑ ์ค๋ฅ: {e}")
|
169 |
return LYRIC_DEFAULT
|
|
|
245 |
|
246 |
def enhance_prompt_with_genre(base_prompt: str, genre: str, song_style: str) -> str:
|
247 |
"""์ฅ๋ฅด์ ์คํ์ผ์ ๋ฐ๋ฅธ ์ค๋งํธ ํ๋กฌํํธ ํ์ฅ"""
|
248 |
+
enhanced_prompt = base_prompt
|
249 |
+
|
250 |
+
if genre != "Custom" and genre:
|
251 |
# ์ฅ๋ฅด๋ณ ์ถ๊ฐ ๊ฐ์ ํ๊ทธ
|
252 |
genre_enhancements = {
|
253 |
"Modern Pop": ["polished production", "mainstream appeal", "hook-driven"],
|
|
|
267 |
if genre in genre_enhancements:
|
268 |
additional_tags = ", ".join(genre_enhancements[genre])
|
269 |
enhanced_prompt = f"{base_prompt}, {additional_tags}"
|
|
|
|
|
270 |
|
271 |
# ์คํ์ผ ํ๊ทธ ์ถ๊ฐ
|
272 |
if song_style in SONG_STYLES:
|
|
|
295 |
except:
|
296 |
return 50.0 # ๊ธฐ๋ณธ๊ฐ
|
297 |
|
298 |
+
def update_tags_from_preset(preset_name, current_tags, song_style):
|
299 |
+
"""์ฅ๋ฅด ํ๋ฆฌ์
์ ํ์ ํ๊ทธ ์
๋ฐ์ดํธ"""
|
300 |
if preset_name == "Custom":
|
301 |
+
return current_tags
|
302 |
+
|
303 |
+
base_tags = GENRE_PRESETS.get(preset_name, "")
|
304 |
+
|
305 |
+
# ์คํ์ผ ํ๊ทธ ์ถ๊ฐ
|
306 |
+
if song_style in SONG_STYLES:
|
307 |
+
style_tags = SONG_STYLES[song_style]
|
308 |
+
return f"{base_tags}, {style_tags}"
|
309 |
+
|
310 |
+
return base_tags
|
311 |
|
312 |
def update_quality_preset(preset_name):
|
313 |
"""ํ์ง ํ๋ฆฌ์
์ ์ฉ"""
|
|
|
716 |
|
717 |
# AI ์์ฌ ๋ฒํผ ์ด๋ฒคํธ
|
718 |
def generate_ai_lyrics(lyric_prompt, genre_preset, song_style):
|
719 |
+
if not lyric_prompt or lyric_prompt.strip() == "":
|
720 |
+
return lyrics.value # ํ์ฌ ๊ฐ์ฌ ์ ์ง
|
721 |
+
|
722 |
+
print(f"Generating lyrics with: prompt={lyric_prompt}, genre={genre_preset}, style={song_style}")
|
723 |
+
generated = generate_lyrics_with_ai(lyric_prompt, genre_preset, song_style)
|
724 |
+
return generated
|
725 |
|
726 |
generate_lyrics_btn.click(
|
727 |
fn=generate_ai_lyrics,
|
|
|
781 |
None, # ref_audio_input
|
782 |
)
|
783 |
|
784 |
+
# ์ฅ๋ฅด ํ๋ฆฌ์
๋ณ๊ฒฝ ์ ํ๊ทธ ์
๋ฐ์ดํธ
|
785 |
+
def on_genre_change(genre_preset, current_tags, song_style):
|
786 |
+
return update_tags_from_preset(genre_preset, current_tags, song_style)
|
787 |
+
|
788 |
genre_preset.change(
|
789 |
+
fn=on_genre_change,
|
790 |
+
inputs=[genre_preset, prompt, song_style],
|
791 |
+
outputs=[prompt]
|
792 |
+
)
|
793 |
+
|
794 |
+
# ๊ณก ์คํ์ผ ๋ณ๊ฒฝ ์ ํ๊ทธ ์
๋ฐ์ดํธ
|
795 |
+
song_style.change(
|
796 |
+
fn=on_genre_change,
|
797 |
+
inputs=[genre_preset, prompt, song_style],
|
798 |
outputs=[prompt]
|
799 |
)
|
800 |
|
|
|
1609 |
- **์ฅ๋ฅด ํนํ**: ์ฅ๋ฅด ํ๋ฆฌ์
์ ํ ํ "์ค๋งํธ ํฅ์" ์ฒดํฌ
|
1610 |
- **๊ฐ์ฌ ๊ตฌ์กฐ**: [verse], [chorus], [bridge] ํ๊ทธ ์ ๊ทน ํ์ฉ
|
1611 |
- **๋ค๊ตญ์ด ์ง์**: ํ๊ตญ์ด๋ก ์ฃผ์ ๋ฅผ ์
๋ ฅํ๋ฉด ํ๊ตญ์ด ๊ฐ์ฌ๊ฐ ์์ฑ๋ฉ๋๋ค
|
1612 |
+
|
1613 |
+
### โ ๏ธ OpenAI API ์ค์
|
1614 |
+
AI ์์ฌ ๊ธฐ๋ฅ์ ์ฌ์ฉํ๋ ค๋ฉด ํ๊ฒฝ๋ณ์์ OpenAI API ํค๋ฅผ ์ค์ ํด์ผ ํฉ๋๋ค:
|
1615 |
+
```bash
|
1616 |
+
export LLM_API="your-openai-api-key"
|
1617 |
+
# ๋๋
|
1618 |
+
export OPENAI_API_KEY="your-openai-api-key"
|
1619 |
+
```
|
1620 |
""")
|
1621 |
|
1622 |
with gr.Tab("๐ต Enhanced Text2Music", elem_classes="gr-tab"):
|