Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,119 +8,93 @@ import scipy.io.wavfile
|
|
| 8 |
import numpy as np
|
| 9 |
from transformers import pipeline
|
| 10 |
|
| 11 |
-
#
|
| 12 |
GENAI_KEY = os.getenv("GEMINI_KEY")
|
| 13 |
|
| 14 |
-
#
|
| 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"β οΈ
|
| 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 |
-
if response.status_code != 200: return "gemini-pro"
|
| 30 |
data = response.json()
|
| 31 |
candidates = [m['name'].replace("models/", "") for m in data.get('models', []) if 'generateContent' in m.get('supportedGenerationMethods', [])]
|
| 32 |
-
preferred = ["gemini-1.5-flash", "gemini-
|
| 33 |
for p in preferred:
|
| 34 |
if p in candidates: return p
|
| 35 |
return candidates[0] if candidates else "gemini-pro"
|
| 36 |
-
except:
|
| 37 |
-
return "gemini-pro"
|
| 38 |
|
| 39 |
ACTIVE_MODEL = find_working_model()
|
| 40 |
|
| 41 |
-
#
|
| 42 |
MEMBERS = {
|
| 43 |
-
"μμ€ (Korea)": { "voice": "ko-KR-SunHiNeural", "prompt": "λΉμ μ
|
| 44 |
-
"Chloe (USA)": { "voice": "en-US-AriaNeural", "prompt": "You are 'Chloe', the
|
| 45 |
-
"Beatrice (Brazil)": { "voice": "pt-BR-FranciscaNeural", "prompt": "You are 'Beatrice', the drummer.
|
| 46 |
-
"Naomi (Japan)": { "voice": "ja-JP-NanamiNeural", "prompt": "You are 'Naomi'. Speak in Japanese. Focus on melody and chords." },
|
| 47 |
-
"Elena (Spain)": { "voice": "es-ES-ElviraNeural", "prompt": "You are 'Elena'. Speak in Spanish/English. Focus on bass lines." },
|
| 48 |
-
"Amira (Egypt)": { "voice": "ar-EG-SalmaNeural", "prompt": "You are 'Amira'. Speak in Arabic/English. Focus on atmosphere." },
|
| 49 |
-
"Liwei (China)": { "voice": "zh-CN-XiaoxiaoNeural", "prompt": "You are 'Liwei'. Speak in Chinese/English. Focus on FX and sound design." },
|
| 50 |
-
"Sophie (France)": { "voice": "fr-FR-DeniseNeural", "prompt": "You are 'Sophie'. Speak in French/English. Focus on emotional melody." }
|
| 51 |
}
|
| 52 |
|
| 53 |
-
async def band_consulting(user_input, member_name):
|
| 54 |
try:
|
| 55 |
-
|
| 56 |
-
return "Error: Unknown Member", None, None
|
| 57 |
-
|
| 58 |
-
member = MEMBERS[member_name]
|
| 59 |
|
| 60 |
-
#
|
| 61 |
system_instruction = f"""
|
| 62 |
{member['prompt']}
|
|
|
|
| 63 |
|
| 64 |
-
|
| 65 |
-
1.
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
Example: "Let's go with a heavy beat! [MUSIC: heavy metal drum beat, 140bpm, aggressive]"
|
| 71 |
-
If no music needed, do NOT write [MUSIC:...].
|
| 72 |
"""
|
| 73 |
|
| 74 |
-
# Gemini νΈμΆ
|
| 75 |
url = f"https://generativelanguage.googleapis.com/v1beta/models/{ACTIVE_MODEL}:generateContent?key={GENAI_KEY}"
|
| 76 |
-
payload = {"contents": [{"parts": [{"text": f"{system_instruction}\
|
| 77 |
-
res = requests.post(url, json=payload
|
| 78 |
-
|
| 79 |
-
if res.status_code != 200:
|
| 80 |
-
return f"API Error: {res.text}", None, None
|
| 81 |
-
|
| 82 |
ai_text_raw = res.json()['candidates'][0]['content']['parts'][0]['text']
|
| 83 |
|
| 84 |
-
# μμ
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE)
|
| 88 |
|
| 89 |
-
if
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
print(f"π΅ μ곑 μμ²: {music_prompt}")
|
| 93 |
|
| 94 |
-
#
|
| 95 |
-
voice_path = f"/tmp/{member_name}
|
| 96 |
await edge_tts.Communicate(clean_text, member['voice']).save(voice_path)
|
| 97 |
|
| 98 |
-
#
|
| 99 |
music_path = None
|
| 100 |
-
if
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
except Exception as e:
|
| 107 |
-
print(f"β μ곑 μ€ν¨: {e}")
|
| 108 |
|
| 109 |
-
return clean_text, voice_path, music_path
|
| 110 |
|
| 111 |
except Exception as e:
|
| 112 |
-
return f"
|
| 113 |
|
| 114 |
-
# Gradio
|
| 115 |
with gr.Blocks() as demo:
|
| 116 |
with gr.Row():
|
| 117 |
-
inp = gr.Textbox(
|
| 118 |
-
mem = gr.Textbox(
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
btn = gr.Button("GO")
|
| 124 |
-
btn.click(band_consulting, [inp, mem], [out_text, out_voice, out_music], api_name="predict")
|
| 125 |
|
| 126 |
demo.launch()
|
|
|
|
| 8 |
import numpy as np
|
| 9 |
from transformers import pipeline
|
| 10 |
|
| 11 |
+
# API ν€ μ€μ
|
| 12 |
GENAI_KEY = os.getenv("GEMINI_KEY")
|
| 13 |
|
| 14 |
+
# 1. [μ곑κ°] 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 |
+
# λ©€λ² νλ₯΄μλ (μ
보 μμ± λ₯λ ₯ μΆκ°)
|
| 38 |
MEMBERS = {
|
| 39 |
+
"μμ€ (Korea)": { "voice": "ko-KR-SunHiNeural", "prompt": "λΉμ μ 보컬 'μμ€'μ
λλ€. μλ컬ν λ§ν¬. μλ΄ λΆμΌμ λ§μΆ° μμ
μ μ‘°μΈκ³Ό μ½λ μ§νμ μλ €μ£ΌμΈμ." },
|
| 40 |
+
"Chloe (USA)": { "voice": "en-US-AriaNeural", "prompt": "You are 'Chloe', the guitarist. Speak in English. Focus on guitar tabs and tones." },
|
| 41 |
+
"Beatrice (Brazil)": { "voice": "pt-BR-FranciscaNeural", "prompt": "You are 'Beatrice', the drummer. Focus on rhythm patterns and groovy beats." }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
}
|
| 43 |
|
| 44 |
+
async def band_consulting(user_input, member_name, consult_category):
|
| 45 |
try:
|
| 46 |
+
member = MEMBERS.get(member_name, MEMBERS["μyun (Korea)"])
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
# μμ€ν
ν둬ννΈ: μ
보μ μμ
μμ±μ ꡬ체ν
|
| 49 |
system_instruction = f"""
|
| 50 |
{member['prompt']}
|
| 51 |
+
[μλ΄ λΆμΌ: {consult_category}]
|
| 52 |
|
| 53 |
+
λΉμ μ μ λ¬Έκ°λ‘μ μ‘°μΈμ νκ³ , λ§μ§λ§μ λ κ°μ§λ₯Ό λ°λμ ν¬ν¨νμΈμ:
|
| 54 |
+
1. [TAB]: μμ
μ κ°λ¨ν μ
보λ μ½λ μ§νμ ν
μ€νΈ νμμΌλ‘ 그리μΈμ.
|
| 55 |
+
μ: | Am - F - C - G |
|
| 56 |
+
2. [MUSIC]: λΆμΌμ νΉνλ κ³ μμ§ μμ
ν둬ννΈλ₯Ό μ μΌμΈμ.
|
| 57 |
+
(High-quality, studio recording, master audio νμ ν¬ν¨)
|
|
|
|
|
|
|
|
|
|
| 58 |
"""
|
| 59 |
|
|
|
|
| 60 |
url = f"https://generativelanguage.googleapis.com/v1beta/models/{ACTIVE_MODEL}:generateContent?key={GENAI_KEY}"
|
| 61 |
+
payload = {"contents": [{"parts": [{"text": f"{system_instruction}\nμ§λ¬Έ: {user_input}"}]}]}
|
| 62 |
+
res = requests.post(url, json=payload)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
ai_text_raw = res.json()['candidates'][0]['content']['parts'][0]['text']
|
| 64 |
|
| 65 |
+
# μ
보([TAB])μ μμ
([MUSIC]) μΆμΆ
|
| 66 |
+
tab_match = re.search(r'\[TAB\](.*?)\[', ai_text_raw + '[', re.DOTALL)
|
| 67 |
+
music_match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE)
|
|
|
|
| 68 |
|
| 69 |
+
tab_display = tab_match.group(1).strip() if tab_match else "No Score Available"
|
| 70 |
+
clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL)
|
| 71 |
+
clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text).strip()
|
|
|
|
| 72 |
|
| 73 |
+
# 1. λͺ©μ리 μμ±
|
| 74 |
+
voice_path = f"/tmp/v_{member_name}.mp3"
|
| 75 |
await edge_tts.Communicate(clean_text, member['voice']).save(voice_path)
|
| 76 |
|
| 77 |
+
# 2. μμ
μμ± (μμ§ λ³΄μ νΈλ¦ μ μ©)
|
| 78 |
music_path = None
|
| 79 |
+
if music_match and music_synthesiser:
|
| 80 |
+
p = music_match.group(1).strip() + ", high quality, studio recording, clear sound"
|
| 81 |
+
# μμ§ λ³΄μ : guidance_scaleμ λμ¬ ν둬ννΈ μΆ©μ€λ ν₯μ
|
| 82 |
+
music = music_synthesiser(p, forward_params={"max_new_tokens": 448, "guidance_scale": 4.5, "do_sample": True})
|
| 83 |
+
music_path = f"/tmp/m_{member_name}.wav"
|
| 84 |
+
scipy.io.wavfile.write(music_path, music["sampling_rate"], music["audio"][0].T)
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
return clean_text, voice_path, music_path, tab_display
|
| 87 |
|
| 88 |
except Exception as e:
|
| 89 |
+
return f"Error: {e}", None, None, ""
|
| 90 |
|
| 91 |
+
# Gradio μΈν°νμ΄μ€
|
| 92 |
with gr.Blocks() as demo:
|
| 93 |
with gr.Row():
|
| 94 |
+
inp = gr.Textbox()
|
| 95 |
+
mem = gr.Textbox()
|
| 96 |
+
cat = gr.Textbox() # λΆμΌ
|
| 97 |
+
btn = gr.Button("Predict")
|
| 98 |
+
btn.click(band_consulting, [inp, mem, cat], [gr.Textbox(), gr.Audio(), gr.Audio(), gr.Textbox()], api_name="predict")
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
demo.launch()
|