Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,91 +8,145 @@ import scipy.io.wavfile
|
|
| 8 |
import numpy as np
|
| 9 |
from transformers import pipeline
|
| 10 |
|
| 11 |
-
# API ν€ μ€μ
|
| 12 |
GENAI_KEY = os.getenv("GEMINI_KEY")
|
| 13 |
|
| 14 |
-
#
|
| 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:
|
|
|
|
| 34 |
|
| 35 |
ACTIVE_MODEL = find_working_model()
|
| 36 |
|
| 37 |
-
# λ©€λ²
|
| 38 |
MEMBERS = {
|
| 39 |
-
"μμ€ (Korea)": {
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
}
|
| 43 |
-
|
|
|
|
| 44 |
async def band_consulting(user_input, member_name, consult_category):
|
| 45 |
try:
|
| 46 |
-
# λ©€λ²
|
| 47 |
member = MEMBERS.get(member_name, MEMBERS["μμ€ (Korea)"])
|
| 48 |
|
| 49 |
system_instruction = f"""
|
| 50 |
{member['prompt']}
|
| 51 |
[μλ΄ λΆμΌ: {consult_category}]
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
| 55 |
"""
|
| 56 |
|
|
|
|
| 57 |
url = f"https://generativelanguage.googleapis.com/v1beta/models/{ACTIVE_MODEL}:generateContent?key={GENAI_KEY}"
|
| 58 |
payload = {"contents": [{"parts": [{"text": f"{system_instruction}\nμ§λ¬Έ: {user_input}"}]}]}
|
| 59 |
-
res = requests.post(url, json=payload)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
ai_text_raw = res.json()['candidates'][0]['content']['parts'][0]['text']
|
| 61 |
|
| 62 |
-
|
|
|
|
| 63 |
music_match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE)
|
| 64 |
|
| 65 |
-
tab_display = tab_match.group(1).strip() if tab_match else "No Score"
|
| 66 |
|
| 67 |
-
#
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
tts_text = re.sub(r'[\*\#\-\_\~]', '',
|
| 71 |
|
| 72 |
-
#
|
| 73 |
-
voice_path = f"/tmp/v_{member_name}.mp3"
|
| 74 |
await edge_tts.Communicate(tts_text, member['voice']).save(voice_path)
|
| 75 |
|
| 76 |
-
#
|
| 77 |
music_path = None
|
| 78 |
if music_match and music_synthesiser:
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
| 83 |
|
| 84 |
-
return
|
| 85 |
|
| 86 |
except Exception as e:
|
| 87 |
-
|
|
|
|
| 88 |
|
| 89 |
-
# Gradio μΈν°νμ΄μ€
|
| 90 |
with gr.Blocks() as demo:
|
| 91 |
with gr.Row():
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
btn = gr.Button("Predict")
|
| 96 |
-
btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
demo.launch()
|
|
|
|
| 8 |
import numpy as np
|
| 9 |
from transformers import pipeline
|
| 10 |
|
| 11 |
+
# 1. API ν€ μ€μ (Hugging Face Settings -> Variablesμ λ±λ‘ νμ)
|
| 12 |
GENAI_KEY = os.getenv("GEMINI_KEY")
|
| 13 |
|
| 14 |
+
# 2. [μ곑κ°] MusicGen λͺ¨λΈ λ‘λ (μμ§ λ³΄μ μ€μ )
|
| 15 |
+
print("β³ μ곑κ°(MusicGen)λ₯Ό μμ§ μ€μ
λλ€... (1~2λΆ μμ)")
|
| 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-1.5-flash-latest", "gemini-pro"]
|
| 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 |
+
# 4. λ°΄λ λ©€λ² λ°μ΄ν° (μ΄λ¦ μ€ν μλ²½ μμ : "μμ€ (Korea)")
|
| 41 |
MEMBERS = {
|
| 42 |
+
"μμ€ (Korea)": {
|
| 43 |
+
"voice": "ko-KR-SunHiNeural",
|
| 44 |
+
"prompt": "λΉμ μ λ‘λ°΄λ 보컬 'μμ€'μ
λλ€. νκ΅μ΄λ‘ λλ΅νμΈμ. μ±κ²©: μλ컬, μΈ€λ°λ , μμ
μ μ§μ¬. μ λ¬Έμ μΈ μμ
μ‘°μΈμ μ 곡νμΈμ."
|
| 45 |
+
},
|
| 46 |
+
"Chloe (USA)": {
|
| 47 |
+
"voice": "en-US-AriaNeural",
|
| 48 |
+
"prompt": "You are 'Chloe', the lead guitarist. Respond in English. Focus on guitar tone and riffs."
|
| 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 |
+
# λ©€λ² μ°ΎκΈ° (κΈ°λ³Έκ°: μμ€)
|
| 80 |
member = MEMBERS.get(member_name, MEMBERS["μμ€ (Korea)"])
|
| 81 |
|
| 82 |
system_instruction = f"""
|
| 83 |
{member['prompt']}
|
| 84 |
[μλ΄ λΆμΌ: {consult_category}]
|
| 85 |
+
|
| 86 |
+
μ‘°μΈμ μ 곡ν λ€ λ°λμ λ§μ§λ§μ λ€μ νμμ ν¬ν¨νμΈμ:
|
| 87 |
+
1. [TAB]: μμ
μ κ°λ¨ν μ½λ μ§νμ΄λ ν
μ€νΈ μ
보λ₯Ό 그리μΈμ.
|
| 88 |
+
2. [MUSIC]: λΆμΌμ μ΄μΈλ¦¬λ κ³ μμ§ μμ
ν둬ννΈλ₯Ό μμ΄λ‘ μμ±νμΈμ.
|
| 89 |
+
(μ: [MUSIC: melodic rock guitar, high quality, studio recording, 120bpm])
|
| 90 |
"""
|
| 91 |
|
| 92 |
+
# Gemini API νΈμΆ
|
| 93 |
url = f"https://generativelanguage.googleapis.com/v1beta/models/{ACTIVE_MODEL}:generateContent?key={GENAI_KEY}"
|
| 94 |
payload = {"contents": [{"parts": [{"text": f"{system_instruction}\nμ§λ¬Έ: {user_input}"}]}]}
|
| 95 |
+
res = requests.post(url, json=payload, headers={'Content-Type': 'application/json'})
|
| 96 |
+
|
| 97 |
+
if res.status_code != 200:
|
| 98 |
+
return "Gemini API μ°κ²°μ μ€ν¨νμ΅λλ€.", None, None, "No Tab"
|
| 99 |
+
|
| 100 |
ai_text_raw = res.json()['candidates'][0]['content']['parts'][0]['text']
|
| 101 |
|
| 102 |
+
# μ
보([TAB]) λ° μμ
λͺ
λ Ή([MUSIC]) μΆμΆ
|
| 103 |
+
tab_match = re.search(r'\[TAB\](.*?)(\[|$)', ai_text_raw, re.DOTALL)
|
| 104 |
music_match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE)
|
| 105 |
|
| 106 |
+
tab_display = tab_match.group(1).strip() if tab_match else "No Score Available"
|
| 107 |
|
| 108 |
+
# ν
μ€νΈ μ μ λ° μμ± νν°λ§ (νΉμλ¬Έμ μ κ±°νμ¬ μμ€μ΄κ° μ½μ§ μκ² ν¨)
|
| 109 |
+
clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL)
|
| 110 |
+
clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text).strip()
|
| 111 |
+
tts_text = re.sub(r'[\*\#\-\_\~]', '', clean_text)
|
| 112 |
|
| 113 |
+
# 6. λͺ©μ리(TTS) μμ±
|
| 114 |
+
voice_path = f"/tmp/v_{member_name.replace(' ', '_')}.mp3"
|
| 115 |
await edge_tts.Communicate(tts_text, member['voice']).save(voice_path)
|
| 116 |
|
| 117 |
+
# 7. μμ
(MusicGen) μμ± (μμ§ λ³΄μ νΈλ¦)
|
| 118 |
music_path = None
|
| 119 |
if music_match and music_synthesiser:
|
| 120 |
+
music_p = music_match.group(1).strip() + ", high quality, studio recording, clear mix"
|
| 121 |
+
# guidance_scale=4.5λ‘ λμ¬ μμ§ λ° ν둬ννΈ μ νλ ν₯μ
|
| 122 |
+
music_output = music_synthesiser(music_p, forward_params={"max_new_tokens": 448, "guidance_scale": 4.5, "do_sample": True})
|
| 123 |
+
music_path = f"/tmp/m_{member_name.replace(' ', '_')}.wav"
|
| 124 |
+
scipy.io.wavfile.write(music_path, music_output["sampling_rate"], music_output["audio"][0].T)
|
| 125 |
|
| 126 |
+
return clean_text, voice_path, music_path, tab_display
|
| 127 |
|
| 128 |
except Exception as e:
|
| 129 |
+
print(f"β Error in band_consulting: {e}")
|
| 130 |
+
return f"λ°΄λ μμ€ν
μ€λ₯: {str(e)}", None, None, "Error"
|
| 131 |
|
| 132 |
+
# 8. Gradio μΈν°νμ΄μ€ μ μ (APIμ©)
|
| 133 |
with gr.Blocks() as demo:
|
| 134 |
with gr.Row():
|
| 135 |
+
inp_text = gr.Textbox()
|
| 136 |
+
inp_member = gr.Textbox()
|
| 137 |
+
inp_cat = gr.Textbox()
|
| 138 |
+
|
| 139 |
+
out_advice = gr.Textbox()
|
| 140 |
+
out_voice = gr.Audio()
|
| 141 |
+
out_music = gr.Audio()
|
| 142 |
+
out_tab = gr.Textbox()
|
| 143 |
+
|
| 144 |
btn = gr.Button("Predict")
|
| 145 |
+
btn.click(
|
| 146 |
+
fn=band_consulting,
|
| 147 |
+
inputs=[inp_text, inp_member, inp_cat],
|
| 148 |
+
outputs=[out_advice, out_voice, out_music, out_tab],
|
| 149 |
+
api_name="predict"
|
| 150 |
+
)
|
| 151 |
|
| 152 |
demo.launch()
|