Spaces:
Runtime error
Runtime error
Update ui/components.py
Browse files- ui/components.py +45 -52
ui/components.py
CHANGED
|
@@ -15,21 +15,19 @@ import hashlib
|
|
| 15 |
import numpy as np
|
| 16 |
import json
|
| 17 |
from typing import Dict, List, Tuple, Optional
|
| 18 |
-
|
|
|
|
| 19 |
# ์ถ๊ฐ๋ ๋ถ๋ถ: OpenAI ๋ชจ๋ ์ํฌํธ
|
| 20 |
from openai import OpenAI
|
| 21 |
|
| 22 |
-
#
|
| 23 |
client = OpenAI(api_key=os.getenv("LLM_API"))
|
| 24 |
|
| 25 |
-
# ๊ฐ์ฌ ์์ฑ์ ์ํ OpenAI ํธ์ถ ํจ์
|
| 26 |
def openai_generate_lyrics(topic: str) -> str:
|
| 27 |
"""
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
๊ตฌ์กฐ๋ฅผ ํฌํจํ ๋
ธ๋ ๊ฐ์ฌ๋ฅผ ์์ฑํ์ฌ ๋ฐํํฉ๋๋ค.
|
| 31 |
"""
|
| 32 |
-
# ์์คํ
ํ๋กฌํํธ(๋ช
๋ น) : ๋
ธ๋ ๊ฐ์ฌ๋ฅผ ์์ฌํ๋ ์ ๋ฌธ๊ฐ ์ญํ
|
| 33 |
system_prompt = (
|
| 34 |
"๋๋ ๋
ธ๋ ๊ฐ์ฌ๋ฅผ ์์ฌํ๋ ์ ๋ฌธ๊ฐ ์ญํ ์ด๋ค. ์ด์ฉ์๊ฐ ์
๋ ฅํ๋ ์ฃผ์ ์ ๋ฐ๋ผ "
|
| 35 |
"์ด์ ๊ด๋ จ๋ ๋
ธ๋ ๊ฐ์ฌ๋ฅผ ์์ฑํ๋ผ. ๊ฐ์ฌ์ ๊ท์น์ \"[ ]\"๋ก ๊ตฌ๋ถํ์ฌ, "
|
|
@@ -45,51 +43,46 @@ def openai_generate_lyrics(topic: str) -> str:
|
|
| 45 |
"skies\n\n[verse]\nKeyboard dances on the keys\nMelodies on evening breeze\nCatch "
|
| 46 |
"the tune and hold it tight\nIn this moment we take flight\n\"\"\""
|
| 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 |
-
if len(content) > 0 and content[0].get("type") == "output_text":
|
| 89 |
-
return content[0].get("text", "")
|
| 90 |
-
return "๊ฐ์ฌ ์์ฑ ์คํจ: ์๋ต ํ์์ ํ์
ํ ์ ์์ต๋๋ค."
|
| 91 |
-
except Exception as e:
|
| 92 |
-
return f"๊ฐ์ฌ ์์ฑ ์คํจ: {str(e)}"
|
| 93 |
|
| 94 |
TAG_DEFAULT = "funk, pop, soul, rock, melodic, guitar, drums, bass, keyboard, percussion, 105 BPM, energetic, upbeat, groovy, vibrant, dynamic"
|
| 95 |
LYRIC_DEFAULT = """[verse]
|
|
|
|
| 15 |
import numpy as np
|
| 16 |
import json
|
| 17 |
from typing import Dict, List, Tuple, Optional
|
| 18 |
+
import time
|
| 19 |
+
import requests
|
| 20 |
# ์ถ๊ฐ๋ ๋ถ๋ถ: OpenAI ๋ชจ๋ ์ํฌํธ
|
| 21 |
from openai import OpenAI
|
| 22 |
|
| 23 |
+
# ๊ฐ์ฌ ์์ฑ์ ์ํ OpenAI ํธ์ถ ํจ์
|
| 24 |
client = OpenAI(api_key=os.getenv("LLM_API"))
|
| 25 |
|
|
|
|
| 26 |
def openai_generate_lyrics(topic: str) -> str:
|
| 27 |
"""
|
| 28 |
+
์ฃผ์ (topic)๋ฅผ ๋ฐ์ GPT-4.1-mini๋ก [verse]/[chorus]/[bridge] ํ์์
|
| 29 |
+
๊ฐ์ฌ๋ฅผ ์์ฑํด ๋ฐํํ๋ค. ์ต๋ 3ํ ์ฌ์๋ํ๋ค.
|
|
|
|
| 30 |
"""
|
|
|
|
| 31 |
system_prompt = (
|
| 32 |
"๋๋ ๋
ธ๋ ๊ฐ์ฌ๋ฅผ ์์ฌํ๋ ์ ๋ฌธ๊ฐ ์ญํ ์ด๋ค. ์ด์ฉ์๊ฐ ์
๋ ฅํ๋ ์ฃผ์ ์ ๋ฐ๋ผ "
|
| 33 |
"์ด์ ๊ด๋ จ๋ ๋
ธ๋ ๊ฐ์ฌ๋ฅผ ์์ฑํ๋ผ. ๊ฐ์ฌ์ ๊ท์น์ \"[ ]\"๋ก ๊ตฌ๋ถํ์ฌ, "
|
|
|
|
| 43 |
"skies\n\n[verse]\nKeyboard dances on the keys\nMelodies on evening breeze\nCatch "
|
| 44 |
"the tune and hold it tight\nIn this moment we take flight\n\"\"\""
|
| 45 |
)
|
| 46 |
+
|
| 47 |
+
retries = 3
|
| 48 |
+
for attempt in range(retries):
|
| 49 |
+
try:
|
| 50 |
+
response = client.responses.create(
|
| 51 |
+
model="gpt-4.1-mini",
|
| 52 |
+
input=[
|
| 53 |
+
{
|
| 54 |
+
"role": "system",
|
| 55 |
+
"content": [{"type": "input_text", "text": system_prompt}]
|
| 56 |
+
},
|
| 57 |
+
{
|
| 58 |
+
"role": "user",
|
| 59 |
+
"content": [{"type": "input_text", "text": topic}]
|
| 60 |
+
}
|
| 61 |
+
],
|
| 62 |
+
text={"format": {"type": "text"}},
|
| 63 |
+
reasoning={},
|
| 64 |
+
tools=[],
|
| 65 |
+
temperature=1,
|
| 66 |
+
max_output_tokens=2048,
|
| 67 |
+
top_p=1,
|
| 68 |
+
store=True
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
# assistant ๋ฉ์์ง์์ ๊ฐ์ฌ ํ
์คํธ ์ถ์ถ
|
| 72 |
+
msgs = response.get("input", [])
|
| 73 |
+
if msgs and msgs[-1].get("role") == "assistant":
|
| 74 |
+
contents = msgs[-1].get("content", [])
|
| 75 |
+
if contents and contents[0].get("type") == "output_text":
|
| 76 |
+
return contents[0]["text"]
|
| 77 |
+
|
| 78 |
+
# ํ์์ด ์์๊ณผ ๋ค๋ฅด๋ฉด ์์ธ ๋ฐ์์์ผ ์ฌ์๋
|
| 79 |
+
raise ValueError("Unexpected response structure")
|
| 80 |
+
|
| 81 |
+
except (requests.exceptions.RequestException, Exception) as e:
|
| 82 |
+
print(f"Attempt {attempt + 1}/{retries} failed: {e}")
|
| 83 |
+
time.sleep(2)
|
| 84 |
+
|
| 85 |
+
return "๊ฐ์ฌ ์์ฑ ์คํจ: ์ฌ์๋ ๋ถ๊ฐ"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
TAG_DEFAULT = "funk, pop, soul, rock, melodic, guitar, drums, bass, keyboard, percussion, 105 BPM, energetic, upbeat, groovy, vibrant, dynamic"
|
| 88 |
LYRIC_DEFAULT = """[verse]
|