Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,133 +2,125 @@ import gradio as gr
|
|
| 2 |
import requests
|
| 3 |
import edge_tts
|
| 4 |
import os
|
| 5 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
# 1. API ν€ μ€μ
|
| 8 |
GENAI_KEY = os.getenv("GEMINI_KEY")
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
def find_working_model():
|
| 14 |
-
# λͺ¨λΈ 리μ€νΈ μ‘°ν API νΈμΆ
|
| 15 |
url = f"https://generativelanguage.googleapis.com/v1beta/models?key={GENAI_KEY}"
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
for m in data['models']:
|
| 28 |
-
if 'supportedGenerationMethods' in m and 'generateContent' in m['supportedGenerationMethods']:
|
| 29 |
-
# μ΄λ¦μμ 'models/' λΆλΆ λΌκ³ μ μ₯ (μ: models/gemini-pro -> gemini-pro)
|
| 30 |
-
clean_name = m['name'].replace("models/", "")
|
| 31 |
-
candidates.append(clean_name)
|
| 32 |
-
|
| 33 |
-
print(f"π μ¬μ© κ°λ₯ λͺ¨λΈ λͺ©λ‘: {candidates}")
|
| 34 |
-
|
| 35 |
-
# 2. μ°μ μμλλ‘ μ ν (νλμ -> νλ‘)
|
| 36 |
-
# 리λλ ν€λ‘ 'gemini-1.5-flash'κ° μλλ©΄ 'gemini-1.0-pro'λΌλ μ‘μ΅λλ€.
|
| 37 |
-
preferred_order = [
|
| 38 |
-
"gemini-1.5-flash-latest",
|
| 39 |
-
"gemini-1.5-flash",
|
| 40 |
-
"gemini-1.5-flash-001",
|
| 41 |
-
"gemini-1.5-pro",
|
| 42 |
-
"gemini-1.0-pro",
|
| 43 |
-
"gemini-pro"
|
| 44 |
-
]
|
| 45 |
-
|
| 46 |
-
for pref in preferred_order:
|
| 47 |
-
if pref in candidates:
|
| 48 |
-
print(f"β
κ²°μ λ λͺ¨λΈ: {pref}")
|
| 49 |
-
return pref
|
| 50 |
-
|
| 51 |
-
# μ νΈνλ κ² μμΌλ©΄ λͺ©λ‘μ 첫 λ²μ§Έ λ μ‘κΈ°
|
| 52 |
-
if candidates:
|
| 53 |
-
print(f"β οΈ μ νΈ λͺ¨λΈ μμ. λ체 λͺ¨λΈ μ¬μ©: {candidates[0]}")
|
| 54 |
-
return candidates[0]
|
| 55 |
-
|
| 56 |
-
return "gemini-pro" # μ§μ§ μ무κ²λ μμΌλ©΄...
|
| 57 |
-
|
| 58 |
-
# ======================================================
|
| 59 |
-
# 3. μ§μ ν΅μ ν¨μ (λͺ¨λΈλͺ
μ λμ μΌλ‘ λ£μ)
|
| 60 |
-
# ======================================================
|
| 61 |
-
def call_gemini_direct(prompt, model_name):
|
| 62 |
-
# μμμ μ°Ύμ λͺ¨λΈλͺ
μ URLμ λΌμλ£κΈ°
|
| 63 |
-
url = f"https://generativelanguage.googleapis.com/v1beta/models/{model_name}:generateContent?key={GENAI_KEY}"
|
| 64 |
-
|
| 65 |
-
headers = {'Content-Type': 'application/json'}
|
| 66 |
-
data = {
|
| 67 |
-
"contents": [{
|
| 68 |
-
"parts": [{"text": prompt}]
|
| 69 |
-
}]
|
| 70 |
-
}
|
| 71 |
-
|
| 72 |
-
response = requests.post(url, headers=headers, json=data)
|
| 73 |
-
|
| 74 |
-
if response.status_code == 200:
|
| 75 |
-
return response.json()['candidates'][0]['content']['parts'][0]['text']
|
| 76 |
-
else:
|
| 77 |
-
# μλ¬ λλ©΄ μλ¬ λ©μμ§ λ°ν
|
| 78 |
-
raise Exception(f"Google API Error ({model_name}): {response.text}")
|
| 79 |
|
|
|
|
| 80 |
|
| 81 |
-
# 4. λ©€λ²
|
| 82 |
MEMBERS = {
|
| 83 |
-
"μμ€ (Korea)": { "
|
| 84 |
-
"Chloe (USA)": { "
|
| 85 |
-
"Beatrice (Brazil)": { "
|
| 86 |
-
"Naomi (Japan)": { "
|
| 87 |
-
"Elena (Spain)": { "
|
| 88 |
-
"Amira (Egypt)": { "
|
| 89 |
-
"Liwei (China)": { "
|
| 90 |
-
"Sophie (France)": { "
|
| 91 |
}
|
| 92 |
|
| 93 |
async def band_consulting(user_input, member_name):
|
| 94 |
try:
|
| 95 |
if member_name not in MEMBERS:
|
| 96 |
-
return
|
| 97 |
|
| 98 |
member = MEMBERS[member_name]
|
| 99 |
-
full_prompt = f"{member['prompt']}\n\nUser's Error/Worry: {user_input}\nResponse:"
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
await communicate.save(output_file)
|
| 115 |
-
except Exception as e:
|
| 116 |
-
return f"Error (TTS): {str(e)}", None
|
| 117 |
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
except Exception as e:
|
| 121 |
-
return f"System Error: {
|
| 122 |
|
| 123 |
-
#
|
| 124 |
with gr.Blocks() as demo:
|
| 125 |
-
gr.Markdown("# Error 404 Band API Server (Auto-Discovery Mode)")
|
| 126 |
with gr.Row():
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
out_text = gr.Textbox(label="
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
| 133 |
|
| 134 |
demo.launch()
|
|
|
|
| 2 |
import requests
|
| 3 |
import edge_tts
|
| 4 |
import os
|
| 5 |
+
import re
|
| 6 |
+
import torch
|
| 7 |
+
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("β³ μ곑κ°(MusicGen) μμΈ μ€... (μ΅λ 1~2λΆ μμ)")
|
| 16 |
+
try:
|
| 17 |
+
# facebook/musicgen-small : κ°λ³κ³ λΉ λ₯Έ λͺ¨λΈ
|
| 18 |
+
music_synthesiser = pipeline("text-to-audio", "facebook/musicgen-small")
|
| 19 |
+
print("β
μκ³‘κ° μμΈ μλ£! Start Jamming!")
|
| 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 |
+
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-1.5-flash-latest", "gemini-pro"]
|
| 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 |
+
# 4. λ©€λ² νλ₯΄μλ μ μ
|
| 42 |
MEMBERS = {
|
| 43 |
+
"μμ€ (Korea)": { "voice": "ko-KR-SunHiNeural", "prompt": "λΉμ μ λ‘λ°΄λ 보컬 'μμ€'μ
λλ€. νκ΅μ΄(Korean)λ‘ λλ΅νμΈμ. μ±κ²©: μλ컬νμ§λ§ μμ
μ μ§μ¬. λ΅λ³ λμ μ곑 μμ΄λμ΄λ₯Ό ν¬ν¨νμΈμ." },
|
| 44 |
+
"Chloe (USA)": { "voice": "en-US-AriaNeural", "prompt": "You are 'Chloe', the lead guitarist. Speak in English. Personality: Cool rockstar. Suggest guitar riffs." },
|
| 45 |
+
"Beatrice (Brazil)": { "voice": "pt-BR-FranciscaNeural", "prompt": "You are 'Beatrice', the drummer. Speak in English/Portuguese. Focus on rhythm and groove." },
|
| 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 |
if member_name not in MEMBERS:
|
| 56 |
+
return "Error: Unknown Member", None, None
|
| 57 |
|
| 58 |
member = MEMBERS[member_name]
|
|
|
|
| 59 |
|
| 60 |
+
# β
μμ€ν
ν둬ννΈ: μ곑 λͺ
λ Ή [MUSIC:...] μ μ λ
|
| 61 |
+
system_instruction = f"""
|
| 62 |
+
{member['prompt']}
|
| 63 |
+
|
| 64 |
+
[INSTRUCTION]
|
| 65 |
+
1. Give advice based on the user's input.
|
| 66 |
+
2. IF appropriate (user asks for music, sample, or feeling), create a short music sample.
|
| 67 |
+
3. To create music, write a hidden command at the END of your response like this:
|
| 68 |
+
[MUSIC: genre, tempo, instruments, mood]
|
| 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}\nUser: {user_input}\nResponse:"}]}]}
|
| 77 |
+
res = requests.post(url, json=payload, headers={'Content-Type': 'application/json'})
|
| 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 |
+
music_prompt = None
|
| 86 |
+
clean_text = ai_text_raw
|
| 87 |
+
match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE)
|
| 88 |
+
|
| 89 |
+
if match:
|
| 90 |
+
music_prompt = match.group(1).strip()
|
| 91 |
+
clean_text = ai_text_raw.replace(match.group(0), "").strip()
|
| 92 |
+
print(f"π΅ μ곑 μμ²: {music_prompt}")
|
| 93 |
|
| 94 |
+
# TTS (λͺ©μ리) μμ±
|
| 95 |
+
voice_path = f"/tmp/{member_name}_voice.mp3"
|
| 96 |
+
await edge_tts.Communicate(clean_text, member['voice']).save(voice_path)
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
# MusicGen (μμ
) μμ±
|
| 99 |
+
music_path = None
|
| 100 |
+
if music_prompt and music_synthesiser:
|
| 101 |
+
try:
|
| 102 |
+
# max_new_tokens=256 (μ½ 5μ΄) ~ 512 (μ½ 10μ΄)
|
| 103 |
+
music = music_synthesiser(music_prompt, forward_params={"max_new_tokens": 512})
|
| 104 |
+
music_path = f"/tmp/{member_name}_music.wav"
|
| 105 |
+
scipy.io.wavfile.write(music_path, music["sampling_rate"], music["audio"][0].T)
|
| 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"System Error: {e}", None, None
|
| 113 |
|
| 114 |
+
# Gradio μ± μ€ν
|
| 115 |
with gr.Blocks() as demo:
|
|
|
|
| 116 |
with gr.Row():
|
| 117 |
+
inp = gr.Textbox(label="Input")
|
| 118 |
+
mem = gr.Textbox(label="Member")
|
| 119 |
+
out_text = gr.Textbox(label="Advice")
|
| 120 |
+
out_voice = gr.Audio(label="Voice")
|
| 121 |
+
out_music = gr.Audio(label="Music Sample")
|
| 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()
|