Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
[
|
| 58 |
-
[
|
| 59 |
-
1.
|
| 60 |
-
2. Put ALL
|
| 61 |
-
3. Start with a
|
| 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 |
-
|
| 89 |
-
|
|
|
|
| 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 |
|