Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,95 +3,76 @@ 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 |
GENAI_KEY = os.getenv("GEMINI_KEY")
|
| 12 |
-
|
| 13 |
try:
|
| 14 |
-
# ์์ง ๋ณด์ ๋ชจ๋ธ ๋ก๋
|
| 15 |
music_synthesiser = pipeline("text-to-audio", "facebook/musicgen-small")
|
| 16 |
-
except
|
| 17 |
music_synthesiser = None
|
| 18 |
|
| 19 |
-
|
| 20 |
-
url = f"https://generativelanguage.googleapis.com/v1beta/models?key={GENAI_KEY}"
|
| 21 |
-
try:
|
| 22 |
-
response = requests.get(url)
|
| 23 |
-
data = response.json()
|
| 24 |
-
candidates = [m['name'].replace("models/", "") for m in data.get('models', []) if 'generateContent' in m.get('supportedGenerationMethods', [])]
|
| 25 |
-
preferred = ["gemini-1.5-flash", "gemini-pro"]
|
| 26 |
-
for p in preferred:
|
| 27 |
-
if p in candidates: return p
|
| 28 |
-
return candidates[0] if candidates else "gemini-pro"
|
| 29 |
-
except: return "gemini-pro"
|
| 30 |
-
|
| 31 |
-
ACTIVE_MODEL = find_working_model()
|
| 32 |
-
|
| 33 |
MEMBERS = {
|
| 34 |
-
"์์ค (Korea)":
|
| 35 |
-
"Chloe (USA)":
|
| 36 |
-
"Beatrice (Brazil)":
|
| 37 |
-
"Naomi (Japan)":
|
| 38 |
-
"Elena (Spain)":
|
| 39 |
-
"Amira (Egypt)":
|
| 40 |
-
"Liwei (China)":
|
| 41 |
-
"Sophie (France)":
|
| 42 |
}
|
| 43 |
|
| 44 |
-
async def band_consulting(user_input, member_name,
|
| 45 |
try:
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
system_instruction = f"""
|
| 51 |
-
|
| 52 |
-
[
|
| 53 |
-
[
|
| 54 |
-
|
| 55 |
-
|
| 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 |
-
|
|
|
|
| 64 |
payload = {"contents": [{"parts": [{"text": f"{system_instruction}\n์ง๋ฌธ: {user_input}"}]}]}
|
| 65 |
-
res = requests.post(url, json=payload
|
| 66 |
-
ai_text_raw = res
|
| 67 |
|
|
|
|
| 68 |
tab_match = re.search(r'\[TAB\](.*?)(\[|$)', ai_text_raw, re.DOTALL)
|
| 69 |
music_match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE)
|
| 70 |
-
tab_display = tab_match.group(1).strip() if tab_match else "
|
| 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 |
-
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
|
|
|
| 78 |
|
|
|
|
| 79 |
music_path = None
|
| 80 |
if music_match and music_synthesiser:
|
| 81 |
p = music_match.group(1).strip() + ", high quality, studio recording"
|
| 82 |
-
|
| 83 |
-
|
| 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 |
|
| 87 |
return clean_text, voice_path, music_path, tab_display
|
| 88 |
except Exception as e:
|
| 89 |
-
return
|
| 90 |
|
|
|
|
| 91 |
with gr.Blocks() as demo:
|
| 92 |
-
|
| 93 |
-
i1 = gr.Textbox(); i2 = gr.Textbox(); i3 = gr.Textbox()
|
| 94 |
o1 = gr.Textbox(); o2 = gr.Audio(); o3 = gr.Audio(); o4 = gr.Textbox()
|
| 95 |
btn = gr.Button("GO"); btn.click(band_consulting, [i1, i2, i3], [o1, o2, o3, o4], api_name="predict")
|
| 96 |
-
|
| 97 |
demo.launch()
|
|
|
|
| 3 |
import edge_tts
|
| 4 |
import os
|
| 5 |
import re
|
|
|
|
| 6 |
import scipy.io.wavfile
|
|
|
|
| 7 |
from transformers import pipeline
|
| 8 |
|
| 9 |
GENAI_KEY = os.getenv("GEMINI_KEY")
|
|
|
|
| 10 |
try:
|
|
|
|
| 11 |
music_synthesiser = pipeline("text-to-audio", "facebook/musicgen-small")
|
| 12 |
+
except:
|
| 13 |
music_synthesiser = None
|
| 14 |
|
| 15 |
+
# ๋ฉค๋ฒ ์ค์ (๋ชฉ์๋ฆฌ ๋งค์นญ์ฉ)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
MEMBERS = {
|
| 17 |
+
"์์ค (Korea)": "ko-KR-SunHiNeural",
|
| 18 |
+
"Chloe (USA)": "en-US-AriaNeural",
|
| 19 |
+
"Beatrice (Brazil)": "pt-BR-FranciscaNeural",
|
| 20 |
+
"Naomi (Japan)": "ja-JP-NanamiNeural",
|
| 21 |
+
"Elena (Spain)": "es-ES-ElviraNeural",
|
| 22 |
+
"Amira (Egypt)": "ar-EG-SalmaNeural",
|
| 23 |
+
"Liwei (China)": "zh-CN-XiaoxiaoNeural",
|
| 24 |
+
"Sophie (France)": "fr-FR-DeniseNeural"
|
| 25 |
}
|
| 26 |
|
| 27 |
+
async def band_consulting(user_input, member_name, consult_lang_code):
|
| 28 |
try:
|
| 29 |
+
# ์ ํ๋ ์ธ์ด ์ฝ๋(ko, en, ja ๋ฑ)์ ๋ฐ๋ผ ๋ต๋ณ ์ธ์ด ๊ฐ์
|
| 30 |
+
lang_map = {"ko": "Korean", "en": "English", "ja": "Japanese", "pt": "Portuguese", "es": "Spanish", "ar": "Arabic", "zh": "Chinese", "fr": "French"}
|
| 31 |
+
target_lang = lang_map.get(consult_lang_code, "Korean")
|
| 32 |
+
|
| 33 |
system_instruction = f"""
|
| 34 |
+
๋น์ ์ ๋ฐด๋ ๋ฉค๋ฒ '{member_name}'์
๋๋ค.
|
| 35 |
+
[๊ท์น 1] ๋ฌด์กฐ๊ฑด '{target_lang}'๋ก๋ง ๋ต๋ณํ์ธ์.
|
| 36 |
+
[๊ท์น 2] ์์ฑ ๋ต๋ณ์ฉ ๋ณธ๋ฌธ์ 5์ค ์ด๋ด๋ก ์งง๊ฒ ์์ฝํ์ธ์.
|
| 37 |
+
[๊ท์น 3] ๋ชจ๋ ์์ธ ์ค๋ช
๊ณผ ์
๋ณด๋ [TAB] ์น์
์ ๋ฃ์ผ์ธ์.
|
| 38 |
+
[๊ท์น 4] ์์
์์ฑ์ฉ ์์ด ํ๋กฌํํธ๋ [MUSIC: ํ๋กฌํํธ] ํ์์ผ๋ก ๋์ ํฌํจํ์ธ์.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
"""
|
| 40 |
|
| 41 |
+
# Gemini API ํธ์ถ (๋ชจ๋ธ๋ช
์ ํ๊ฒฝ์ ๋ง๊ฒ ์๋ ์กฐ์ ๋๋ค๊ณ ๊ฐ์ )
|
| 42 |
+
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={GENAI_KEY}"
|
| 43 |
payload = {"contents": [{"parts": [{"text": f"{system_instruction}\n์ง๋ฌธ: {user_input}"}]}]}
|
| 44 |
+
res = requests.post(url, json=payload).json()
|
| 45 |
+
ai_text_raw = res['candidates'][0]['content']['parts'][0]['text']
|
| 46 |
|
| 47 |
+
# ๋ฐ์ดํฐ ํ์ฑ
|
| 48 |
tab_match = re.search(r'\[TAB\](.*?)(\[|$)', ai_text_raw, re.DOTALL)
|
| 49 |
music_match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE)
|
| 50 |
+
tab_display = tab_match.group(1).strip() if tab_match else "์์ธ ์
๋ณด ์์"
|
| 51 |
|
| 52 |
+
# TTS์ฉ ํด๋ฆฐ ํ
์คํธ (์
๋ณด/ํ๋กฌํํธ ์ ๊ฑฐ + 5์ค ์ปท)
|
| 53 |
clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL)
|
| 54 |
clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text).strip()
|
| 55 |
+
clean_text = "\n".join(clean_text.split("\n")[:5])
|
| 56 |
|
| 57 |
+
# ๋ชฉ์๋ฆฌ ์์ฑ
|
| 58 |
+
voice_path = f"/tmp/v_{member_name.split()[0]}.mp3"
|
| 59 |
+
await edge_tts.Communicate(clean_text, MEMBERS.get(member_name, "ko-KR-SunHiNeural")).save(voice_path)
|
| 60 |
|
| 61 |
+
# ์์
์์ฑ
|
| 62 |
music_path = None
|
| 63 |
if music_match and music_synthesiser:
|
| 64 |
p = music_match.group(1).strip() + ", high quality, studio recording"
|
| 65 |
+
music_output = music_synthesiser(p, forward_params={"max_new_tokens": 512, "do_sample": True})
|
| 66 |
+
music_path = f"/tmp/m_{member_name.split()[0]}.wav"
|
|
|
|
| 67 |
scipy.io.wavfile.write(music_path, music_output["sampling_rate"], music_output["audio"][0].T)
|
| 68 |
|
| 69 |
return clean_text, voice_path, music_path, tab_display
|
| 70 |
except Exception as e:
|
| 71 |
+
return str(e), None, None, "Error"
|
| 72 |
|
| 73 |
+
# Gradio ์ธํฐํ์ด์ค๋ API์ฉ์ผ๋ก ์ต์ํ
|
| 74 |
with gr.Blocks() as demo:
|
| 75 |
+
i1 = gr.Textbox(); i2 = gr.Textbox(); i3 = gr.Textbox()
|
|
|
|
| 76 |
o1 = gr.Textbox(); o2 = gr.Audio(); o3 = gr.Audio(); o4 = gr.Textbox()
|
| 77 |
btn = gr.Button("GO"); btn.click(band_consulting, [i1, i2, i3], [o1, o2, o3, o4], api_name="predict")
|
|
|
|
| 78 |
demo.launch()
|