benjamin5607 commited on
Commit
5473ad3
Β·
verified Β·
1 Parent(s): 100bf14

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -21
app.py CHANGED
@@ -8,16 +8,12 @@ import scipy.io.wavfile
8
  import numpy as np
9
  from transformers import pipeline
10
 
11
- # 1. API ν‚€ μ„€μ •
12
  GENAI_KEY = os.getenv("GEMINI_KEY")
13
 
14
- # 2. [μž‘κ³‘κ°€] MusicGen λ‘œλ“œ
15
- print("⏳ μž‘κ³‘κ°€ μ†Œμ§‘ 쀑...")
16
  try:
 
17
  music_synthesiser = pipeline("text-to-audio", "facebook/musicgen-small")
18
- print("βœ… μž‘κ³‘κ°€ μ€€λΉ„ μ™„λ£Œ!")
19
  except Exception as e:
20
- print(f"⚠️ λͺ¨λΈ λ‘œλ“œ μ‹€νŒ¨: {e}")
21
  music_synthesiser = None
22
 
23
  def find_working_model():
@@ -34,9 +30,8 @@ def find_working_model():
34
 
35
  ACTIVE_MODEL = find_working_model()
36
 
37
- # 8인 멀버 데이터
38
  MEMBERS = {
39
- "μ„œμœ€ (Korea)": { "voice": "ko-KR-SunHiNeural", "prompt": "당신은 보컬 'μ„œμœ€'μž…λ‹ˆλ‹€. μ‹œλ‹ˆμ»¬ν•˜κ³  μŒμ•…μ— μ§„μ‹¬μž…λ‹ˆλ‹€." },
40
  "Chloe (USA)": { "voice": "en-US-AriaNeural", "prompt": "You are 'Chloe', the lead guitarist. Cool rockstar vibe." },
41
  "Beatrice (Brazil)": { "voice": "pt-BR-FranciscaNeural", "prompt": "You are 'Beatrice', the drummer. Focus on rhythm." },
42
  "Naomi (Japan)": { "voice": "ja-JP-NanamiNeural", "prompt": "You are 'Naomi'. Focus on harmony." },
@@ -49,24 +44,22 @@ MEMBERS = {
49
  async def band_consulting(user_input, member_name, consult_category):
50
  try:
51
  member = MEMBERS.get(member_name, MEMBERS["μ„œμœ€ (Korea)"])
 
52
  requested_lang = "English" if "English" in consult_category else "Korean"
53
 
54
- # λ‹΅λ³€ 5쀄 μš”μ•½ 및 상세 μ„€λͺ… 악보 이동 μ§€μ‹œ
55
  system_instruction = f"""
56
  {member['prompt']}
57
- [CRITICAL: LANGUAGE] You MUST speak 100% in {requested_lang}. NEVER use other languages.
58
- [CRITICAL: STYLE]
59
- 1. Your verbal advice (main text) must be VERY CONCISE (under 5 lines).
60
- 2. Put ALL detailed musical explanations, scales, and tips inside the [TAB] section.
61
- 3. Start with a cool rockstar greeting.
62
 
63
- μ‘°μ–Έ ν›„ λ°˜λ“œμ‹œ 포함:
64
- 1. [TAB]: 상세 μ„€λͺ… + μ½”λ“œ μ§„ν–‰/ν…μŠ€νŠΈ 악보.
65
- 2. [MUSIC]: μ˜μ–΄ μŒμ•… ν”„λ‘¬ν”„νŠΈ.
66
  """
67
 
68
- # ... (Gemini 호좜 및 데이터 μΆ”μΆœ 둜직 동일)
69
-
70
  url = f"https://generativelanguage.googleapis.com/v1beta/models/{ACTIVE_MODEL}:generateContent?key={GENAI_KEY}"
71
  payload = {"contents": [{"parts": [{"text": f"{system_instruction}\n질문: {user_input}"}]}]}
72
  res = requests.post(url, json=payload, headers={'Content-Type': 'application/json'})
@@ -78,15 +71,16 @@ async def band_consulting(user_input, member_name, consult_category):
78
 
79
  clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL)
80
  clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text).strip()
81
- tts_text = re.sub(r'[\*\#\-\_\~]', '', clean_text) # 특수문자 제거
82
 
83
  voice_path = f"/tmp/v_{member_name.replace(' ', '_')}.mp3"
84
  await edge_tts.Communicate(tts_text, member['voice']).save(voice_path)
85
 
86
  music_path = None
87
  if music_match and music_synthesiser:
88
- music_p = music_match.group(1).strip() + ", high quality, studio recording, master sound"
89
- music_output = music_synthesiser(music_p, forward_params={"max_new_tokens": 512, "guidance_scale": 4.5, "do_sample": True})
 
90
  music_path = f"/tmp/m_{member_name.replace(' ', '_')}.wav"
91
  scipy.io.wavfile.write(music_path, music_output["sampling_rate"], music_output["audio"][0].T)
92
 
 
8
  import numpy as np
9
  from transformers import pipeline
10
 
 
11
  GENAI_KEY = os.getenv("GEMINI_KEY")
12
 
 
 
13
  try:
14
+ # 음질 보정 λͺ¨λΈ λ‘œλ“œ
15
  music_synthesiser = pipeline("text-to-audio", "facebook/musicgen-small")
 
16
  except Exception as e:
 
17
  music_synthesiser = None
18
 
19
  def find_working_model():
 
30
 
31
  ACTIVE_MODEL = find_working_model()
32
 
 
33
  MEMBERS = {
34
+ "μ„œμœ€ (Korea)": { "voice": "ko-KR-SunHiNeural", "prompt": "당신은 보컬 'μ„œμœ€'μž…λ‹ˆλ‹€. μ‹œλ‹ˆμ»¬ν•˜μ§€λ§Œ μŒμ•…μ— μ§„μ‹¬μž…λ‹ˆλ‹€." },
35
  "Chloe (USA)": { "voice": "en-US-AriaNeural", "prompt": "You are 'Chloe', the lead guitarist. Cool rockstar vibe." },
36
  "Beatrice (Brazil)": { "voice": "pt-BR-FranciscaNeural", "prompt": "You are 'Beatrice', the drummer. Focus on rhythm." },
37
  "Naomi (Japan)": { "voice": "ja-JP-NanamiNeural", "prompt": "You are 'Naomi'. Focus on harmony." },
 
44
  async def band_consulting(user_input, member_name, consult_category):
45
  try:
46
  member = MEMBERS.get(member_name, MEMBERS["μ„œμœ€ (Korea)"])
47
+ # λ¦¬λ”λ‹˜ μš”μ²­: μ˜μ–΄ 상담 μ‹œ 무쑰건 μ˜μ–΄λ§Œ μ‚¬μš©
48
  requested_lang = "English" if "English" in consult_category else "Korean"
49
 
 
50
  system_instruction = f"""
51
  {member['prompt']}
52
+ [LANGUAGE RULE] Respond 100% in {requested_lang}. NEVER use other languages.
53
+ [RESPONSE STYLE]
54
+ 1. Keep your verbal response (main text) VERY CONCISE, under 5 short lines.
55
+ 2. Put ALL technical details, scales, and long musical advice inside the [TAB] section.
56
+ 3. Start with a charismatic rockstar greeting.
57
 
58
+ After advice, you MUST include:
59
+ 1. [TAB]: Detailed musical explanation + Chord/Tab progression.
60
+ 2. [MUSIC]: English music prompt (high quality, studio recording).
61
  """
62
 
 
 
63
  url = f"https://generativelanguage.googleapis.com/v1beta/models/{ACTIVE_MODEL}:generateContent?key={GENAI_KEY}"
64
  payload = {"contents": [{"parts": [{"text": f"{system_instruction}\n질문: {user_input}"}]}]}
65
  res = requests.post(url, json=payload, headers={'Content-Type': 'application/json'})
 
71
 
72
  clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL)
73
  clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text).strip()
74
+ tts_text = re.sub(r'[\*\#\-\_\~]', '', clean_text)
75
 
76
  voice_path = f"/tmp/v_{member_name.replace(' ', '_')}.mp3"
77
  await edge_tts.Communicate(tts_text, member['voice']).save(voice_path)
78
 
79
  music_path = None
80
  if music_match and music_synthesiser:
81
+ p = music_match.group(1).strip() + ", high quality, studio recording"
82
+ # λ¦¬λ”λ‹˜ μš”μ²­: 길이 μ•½ 10~12초(512 tokens)
83
+ music_output = music_synthesiser(p, forward_params={"max_new_tokens": 512, "guidance_scale": 4.5, "do_sample": True})
84
  music_path = f"/tmp/m_{member_name.replace(' ', '_')}.wav"
85
  scipy.io.wavfile.write(music_path, music_output["sampling_rate"], music_output["audio"][0].T)
86