Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,102 +8,64 @@ 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 |
-
# facebook/musicgen-small λͺ¨λΈ μ¬μ©
|
| 18 |
music_synthesiser = pipeline("text-to-audio", "facebook/musicgen-small")
|
| 19 |
-
print("β
μκ³‘κ° μ€λΉ μλ£!
|
| 20 |
except Exception as e:
|
| 21 |
print(f"β οΈ λͺ¨λΈ λ‘λ μ€ν¨: {e}")
|
| 22 |
music_synthesiser = None
|
| 23 |
|
| 24 |
-
# 3. μ΅μ μ Gemini λͺ¨λΈ μλ νμ
|
| 25 |
def find_working_model():
|
| 26 |
url = f"https://generativelanguage.googleapis.com/v1beta/models?key={GENAI_KEY}"
|
| 27 |
try:
|
| 28 |
response = requests.get(url)
|
| 29 |
data = response.json()
|
| 30 |
candidates = [m['name'].replace("models/", "") for m in data.get('models', []) if 'generateContent' in m.get('supportedGenerationMethods', [])]
|
| 31 |
-
preferred = ["gemini-1.5-flash", "gemini-
|
| 32 |
for p in preferred:
|
| 33 |
if p in candidates: return p
|
| 34 |
return candidates[0] if candidates else "gemini-pro"
|
| 35 |
-
except:
|
| 36 |
-
return "gemini-pro"
|
| 37 |
|
| 38 |
ACTIVE_MODEL = find_working_model()
|
| 39 |
|
| 40 |
-
#
|
| 41 |
MEMBERS = {
|
| 42 |
-
"μμ€ (Korea)": {
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
},
|
| 46 |
-
"
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
"Beatrice (Brazil)": {
|
| 51 |
-
"voice": "pt-BR-FranciscaNeural",
|
| 52 |
-
"prompt": "You are 'Beatrice', the drummer. Focus on rhythm patterns and energy."
|
| 53 |
-
},
|
| 54 |
-
"Naomi (Japan)": {
|
| 55 |
-
"voice": "ja-JP-NanamiNeural",
|
| 56 |
-
"prompt": "You are 'Naomi'. Respond in Japanese. Focus on harmony and chord progressions."
|
| 57 |
-
},
|
| 58 |
-
"Elena (Spain)": {
|
| 59 |
-
"voice": "es-ES-ElviraNeural",
|
| 60 |
-
"prompt": "You are 'Elena'. Focus on bass lines and groove."
|
| 61 |
-
},
|
| 62 |
-
"Amira (Egypt)": {
|
| 63 |
-
"voice": "ar-EG-SalmaNeural",
|
| 64 |
-
"prompt": "You are 'Amira'. Focus on keyboards and atmosphere."
|
| 65 |
-
},
|
| 66 |
-
"Liwei (China)": {
|
| 67 |
-
"voice": "zh-CN-XiaoxiaoNeural",
|
| 68 |
-
"prompt": "You are 'Liwei'. Focus on electronic sound and FX."
|
| 69 |
-
},
|
| 70 |
-
"Sophie (France)": {
|
| 71 |
-
"voice": "fr-FR-DeniseNeural",
|
| 72 |
-
"prompt": "You are 'Sophie'. Focus on emotional melodies and violin."
|
| 73 |
-
}
|
| 74 |
}
|
| 75 |
|
| 76 |
-
# 5. λ©μΈ λ‘μ§ ν¨μ
|
| 77 |
async def band_consulting(user_input, member_name, consult_category):
|
| 78 |
try:
|
| 79 |
member = MEMBERS.get(member_name, MEMBERS["μμ€ (Korea)"])
|
| 80 |
|
| 81 |
-
#
|
| 82 |
-
# μ: "πΊπΈ English (Global Random π²) / Guitar" -> "English"
|
| 83 |
requested_lang = "English" if "English" in consult_category else "Korean"
|
| 84 |
|
| 85 |
system_instruction = f"""
|
| 86 |
{member['prompt']}
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
Do not use your native language if it's different from {requested_lang}.
|
| 92 |
-
|
| 93 |
-
[Current Category: {consult_category}]
|
| 94 |
-
|
| 95 |
-
μ‘°μΈμ μ 곡ν λ€ λ°λμ λ§οΏ½οΏ½λ§μ λ€μ νμμ ν¬ν¨νμΈμ:
|
| 96 |
-
1. [TAB]: μμ
μ κ°λ¨ν μ½λ μ§νμ΄λ ν
μ€νΈ μ
보λ₯Ό 그리μΈμ.
|
| 97 |
-
2. [MUSIC]: λΆμΌμ μ΄μΈλ¦¬λ κ³ μμ§ μμ
ν둬ννΈλ₯Ό μμ΄λ‘ μμ±νμΈμ.
|
| 98 |
"""
|
| 99 |
|
| 100 |
url = f"https://generativelanguage.googleapis.com/v1beta/models/{ACTIVE_MODEL}:generateContent?key={GENAI_KEY}"
|
| 101 |
payload = {"contents": [{"parts": [{"text": f"{system_instruction}\nμ§λ¬Έ: {user_input}"}]}]}
|
| 102 |
res = requests.post(url, json=payload, headers={'Content-Type': 'application/json'})
|
| 103 |
-
|
| 104 |
-
if res.status_code != 200:
|
| 105 |
-
return "Gemini API μ°κ²°μ μ€ν¨νμ΅λλ€.", None, None, "No Tab"
|
| 106 |
-
|
| 107 |
ai_text_raw = res.json()['candidates'][0]['content']['parts'][0]['text']
|
| 108 |
|
| 109 |
tab_match = re.search(r'\[TAB\](.*?)(\[|$)', ai_text_raw, re.DOTALL)
|
|
@@ -112,42 +74,26 @@ async def band_consulting(user_input, member_name, consult_category):
|
|
| 112 |
|
| 113 |
clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL)
|
| 114 |
clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text).strip()
|
| 115 |
-
tts_text = re.sub(r'[\*\#\-\_\~]', '', clean_text)
|
| 116 |
|
| 117 |
voice_path = f"/tmp/v_{member_name.replace(' ', '_')}.mp3"
|
| 118 |
await edge_tts.Communicate(tts_text, member['voice']).save(voice_path)
|
| 119 |
|
| 120 |
music_path = None
|
| 121 |
if music_match and music_synthesiser:
|
| 122 |
-
music_p = music_match.group(1).strip() + ", high quality, studio recording,
|
| 123 |
music_output = music_synthesiser(music_p, forward_params={"max_new_tokens": 512, "guidance_scale": 4.5, "do_sample": True})
|
| 124 |
music_path = f"/tmp/m_{member_name.replace(' ', '_')}.wav"
|
| 125 |
scipy.io.wavfile.write(music_path, music_output["sampling_rate"], music_output["audio"][0].T)
|
| 126 |
|
| 127 |
return clean_text, voice_path, music_path, tab_display
|
| 128 |
-
|
| 129 |
except Exception as e:
|
| 130 |
-
|
| 131 |
-
return f"λ°΄λ μμ€ν
μ€λ₯: {str(e)}", None, None, "Error"
|
| 132 |
|
| 133 |
-
# 8. Gradio μΈν°νμ΄μ€ μ μ (APIμ©)
|
| 134 |
with gr.Blocks() as demo:
|
| 135 |
with gr.Row():
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
out_advice = gr.Textbox()
|
| 141 |
-
out_voice = gr.Audio()
|
| 142 |
-
out_music = gr.Audio()
|
| 143 |
-
out_tab = gr.Textbox()
|
| 144 |
-
|
| 145 |
-
btn = gr.Button("Predict")
|
| 146 |
-
btn.click(
|
| 147 |
-
fn=band_consulting,
|
| 148 |
-
inputs=[inp_text, inp_member, inp_cat],
|
| 149 |
-
outputs=[out_advice, out_voice, out_music, out_tab],
|
| 150 |
-
api_name="predict"
|
| 151 |
-
)
|
| 152 |
|
| 153 |
demo.launch()
|
|
|
|
| 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():
|
| 24 |
url = f"https://generativelanguage.googleapis.com/v1beta/models?key={GENAI_KEY}"
|
| 25 |
try:
|
| 26 |
response = requests.get(url)
|
| 27 |
data = response.json()
|
| 28 |
candidates = [m['name'].replace("models/", "") for m in data.get('models', []) if 'generateContent' in m.get('supportedGenerationMethods', [])]
|
| 29 |
+
preferred = ["gemini-1.5-flash", "gemini-pro"]
|
| 30 |
for p in preferred:
|
| 31 |
if p in candidates: return p
|
| 32 |
return candidates[0] if candidates else "gemini-pro"
|
| 33 |
+
except: return "gemini-pro"
|
|
|
|
| 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." },
|
| 43 |
+
"Elena (Spain)": { "voice": "es-ES-ElviraNeural", "prompt": "You are 'Elena'. Focus on groove." },
|
| 44 |
+
"Amira (Egypt)": { "voice": "ar-EG-SalmaNeural", "prompt": "You are 'Amira'. Focus on atmosphere." },
|
| 45 |
+
"Liwei (China)": { "voice": "zh-CN-XiaoxiaoNeural", "prompt": "You are 'Liwei'. Focus on sound design." },
|
| 46 |
+
"Sophie (France)": { "voice": "fr-FR-DeniseNeural", "prompt": "You are 'Sophie'. Focus on melody." }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
}
|
| 48 |
|
|
|
|
| 49 |
async def band_consulting(user_input, member_name, consult_category):
|
| 50 |
try:
|
| 51 |
member = MEMBERS.get(member_name, MEMBERS["μμ€ (Korea)"])
|
| 52 |
|
| 53 |
+
# μΈμ΄ κ°μ λ‘μ§
|
|
|
|
| 54 |
requested_lang = "English" if "English" in consult_category else "Korean"
|
| 55 |
|
| 56 |
system_instruction = f"""
|
| 57 |
{member['prompt']}
|
| 58 |
+
[CRITICAL RULE] Respond ENTIRELY in {requested_lang}.
|
| 59 |
+
Even if you are global, the user selected {requested_lang} as the consultation language.
|
| 60 |
|
| 61 |
+
μ‘°μΈ ν λ°λμ ν¬ν¨:
|
| 62 |
+
1. [TAB]: μ½λ μ§ν λλ ν
μ€νΈ μ
보.
|
| 63 |
+
2. [MUSIC]: λΆμΌμ μ΄μΈλ¦¬λ μμ΄ μμ
ν둬ννΈ.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
"""
|
| 65 |
|
| 66 |
url = f"https://generativelanguage.googleapis.com/v1beta/models/{ACTIVE_MODEL}:generateContent?key={GENAI_KEY}"
|
| 67 |
payload = {"contents": [{"parts": [{"text": f"{system_instruction}\nμ§λ¬Έ: {user_input}"}]}]}
|
| 68 |
res = requests.post(url, json=payload, headers={'Content-Type': 'application/json'})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
ai_text_raw = res.json()['candidates'][0]['content']['parts'][0]['text']
|
| 70 |
|
| 71 |
tab_match = re.search(r'\[TAB\](.*?)(\[|$)', ai_text_raw, re.DOTALL)
|
|
|
|
| 74 |
|
| 75 |
clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL)
|
| 76 |
clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text).strip()
|
| 77 |
+
tts_text = re.sub(r'[\*\#\-\_\~]', '', clean_text) # νΉμλ¬Έμ μ κ±°
|
| 78 |
|
| 79 |
voice_path = f"/tmp/v_{member_name.replace(' ', '_')}.mp3"
|
| 80 |
await edge_tts.Communicate(tts_text, member['voice']).save(voice_path)
|
| 81 |
|
| 82 |
music_path = None
|
| 83 |
if music_match and music_synthesiser:
|
| 84 |
+
music_p = music_match.group(1).strip() + ", high quality, studio recording, master sound"
|
| 85 |
music_output = music_synthesiser(music_p, forward_params={"max_new_tokens": 512, "guidance_scale": 4.5, "do_sample": True})
|
| 86 |
music_path = f"/tmp/m_{member_name.replace(' ', '_')}.wav"
|
| 87 |
scipy.io.wavfile.write(music_path, music_output["sampling_rate"], music_output["audio"][0].T)
|
| 88 |
|
| 89 |
return clean_text, voice_path, music_path, tab_display
|
|
|
|
| 90 |
except Exception as e:
|
| 91 |
+
return f"Error: {str(e)}", None, None, "Error"
|
|
|
|
| 92 |
|
|
|
|
| 93 |
with gr.Blocks() as demo:
|
| 94 |
with gr.Row():
|
| 95 |
+
i1 = gr.Textbox(); i2 = gr.Textbox(); i3 = gr.Textbox()
|
| 96 |
+
o1 = gr.Textbox(); o2 = gr.Audio(); o3 = gr.Audio(); o4 = gr.Textbox()
|
| 97 |
+
btn = gr.Button("GO"); btn.click(band_consulting, [i1, i2, i3], [o1, o2, o3, o4], api_name="predict")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
demo.launch()
|