ginipick commited on
Commit
bf10941
ยท
verified ยท
1 Parent(s): 2395a55

Update ui/components.py

Browse files
Files changed (1) hide show
  1. 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
- # os.getenv("LLM_API")์—์„œ API ํ‚ค๋ฅผ ๋ถˆ๋Ÿฌ์™€์„œ ์‚ฌ์šฉ
23
  client = OpenAI(api_key=os.getenv("LLM_API"))
24
 
25
- # ๊ฐ€์‚ฌ ์ƒ์„ฑ์„ ์œ„ํ•œ OpenAI ํ˜ธ์ถœ ํ•จ์ˆ˜
26
  def openai_generate_lyrics(topic: str) -> str:
27
  """
28
- ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ '์ฃผ์ œ(topic)'๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ GPT-4.1-mini ๋ชจ๋ธ์„ ํ˜ธ์ถœํ•˜์—ฌ
29
- ํ•ด๋‹น ์–ธ์–ด(ํ•œ๊ตญ์–ด/์˜์–ด ๋“ฑ) ๊ธฐ์ค€์œผ๋กœ [verse], [chorus], [bridge] ๋“ฑ์˜
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
- try:
49
- # GPT-4.1-mini ๋ชจ๋ธ์— system / user ๋ฉ”์‹œ์ง€ ํ˜•์‹์œผ๋กœ ํ˜ธ์ถœ
50
- response = client.responses.create(
51
- model="gpt-4.1-mini",
52
- input=[
53
- {
54
- "role": "system",
55
- "content": [
56
- {
57
- "type": "input_text",
58
- "text": system_prompt
59
- }
60
- ]
61
- },
62
- {
63
- "role": "user",
64
- "content": [
65
- {
66
- "type": "input_text",
67
- "text": topic
68
- }
69
- ]
70
- }
71
- ],
72
- text={"format": {"type": "text"}},
73
- reasoning={},
74
- tools=[],
75
- temperature=1,
76
- max_output_tokens=2048,
77
- top_p=1,
78
- store=True
79
- )
80
- # response ๊ตฌ์กฐ์ƒ ๋งˆ์ง€๋ง‰ assistant ์‘๋‹ต์ด input ๋ฆฌ์ŠคํŠธ์˜ ๋งˆ์ง€๋ง‰ ์›์†Œ๋กœ ๋“ค์–ด์˜ด
81
- # role์ด assistant์ธ ํ•ญ๋ชฉ์„ ์ฐพ์•„์„œ 'text' ํ•„๋“œ๋ฅผ ์ถ”์ถœ
82
- if "input" in response and len(response["input"]) > 0:
83
- # ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ๋ฉ”์‹œ์ง€๊ฐ€ assistant์ผ ๊ฒƒ์ด๋ผ ๊ฐ€์ •
84
- last_msg = response["input"][-1]
85
- if last_msg.get("role") == "assistant":
86
- # content ๋ฆฌ์ŠคํŠธ ์ค‘ ์ฒซ ๋ฒˆ์งธ์— ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๊ฐ€์‚ฌ๊ฐ€ ๋“ค์–ด์žˆ์„ ๊ฒƒ
87
- content = last_msg.get("content", [])
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]