Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,12 +7,13 @@ 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",
|
|
@@ -26,39 +27,39 @@ MEMBERS = {
|
|
| 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 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 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 |
-
#
|
| 53 |
clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL)
|
| 54 |
clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text).strip()
|
| 55 |
-
|
|
|
|
| 56 |
|
| 57 |
-
#
|
|
|
|
| 58 |
voice_path = f"/tmp/v_{member_name.split()[0]}.mp3"
|
| 59 |
-
await edge_tts.Communicate(
|
| 60 |
|
| 61 |
-
# ์์
์์ฑ
|
| 62 |
music_path = None
|
| 63 |
if music_match and music_synthesiser:
|
| 64 |
p = music_match.group(1).strip() + ", high quality, studio recording"
|
|
@@ -66,11 +67,10 @@ async def band_consulting(user_input, member_name, consult_lang_code):
|
|
| 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
|
| 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()
|
|
|
|
| 7 |
from transformers import pipeline
|
| 8 |
|
| 9 |
GENAI_KEY = os.getenv("GEMINI_KEY")
|
| 10 |
+
|
| 11 |
try:
|
| 12 |
music_synthesiser = pipeline("text-to-audio", "facebook/musicgen-small")
|
| 13 |
except:
|
| 14 |
music_synthesiser = None
|
| 15 |
|
| 16 |
+
# 8์ธ ๋ฉค๋ฒ ๋ณด์ด์ค ๋งค์นญ
|
| 17 |
MEMBERS = {
|
| 18 |
"์์ค (Korea)": "ko-KR-SunHiNeural",
|
| 19 |
"Chloe (USA)": "en-US-AriaNeural",
|
|
|
|
| 27 |
|
| 28 |
async def band_consulting(user_input, member_name, consult_lang_code):
|
| 29 |
try:
|
|
|
|
| 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. ์์ฑ ๋ต๋ณ(TTS)์ 5์ค ์ด๋ด๋ก ํต์ฌ๋ง ์์ฝํ์ธ์.
|
| 37 |
+
3. ๋ชจ๋ ์์ธ ์ค๋ช
, ํ์ฑํ ์กฐ์ธ, ํ
์คํธ ์
๋ณด๋ [TAB] ์น์
์ ๋ฃ์ผ์ธ์.
|
| 38 |
+
4. ์์
์์ฑ์ฉ ์์ด ํ๋กฌํํธ๋ ๋ง์ง๋ง์ [MUSIC: ํ๋กฌํํธ] ํ์์ผ๋ก ํฌํจํ์ธ์.
|
| 39 |
"""
|
| 40 |
|
|
|
|
| 41 |
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={GENAI_KEY}"
|
| 42 |
payload = {"contents": [{"parts": [{"text": f"{system_instruction}\n์ง๋ฌธ: {user_input}"}]}]}
|
| 43 |
res = requests.post(url, json=payload).json()
|
| 44 |
ai_text_raw = res['candidates'][0]['content']['parts'][0]['text']
|
| 45 |
|
| 46 |
+
# ๋ฐ์ดํฐ ์ ๋ฐ ํ์ฑ
|
| 47 |
tab_match = re.search(r'\[TAB\](.*?)(\[|$)', ai_text_raw, re.DOTALL)
|
| 48 |
music_match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE)
|
| 49 |
+
tab_display = tab_match.group(1).strip() if tab_match else "No Detailed Score"
|
| 50 |
|
| 51 |
+
# โ
์
๋ณด ์ฝ๊ธฐ ๋ฐฉ์ง: [TAB] ์ดํ ๋ด์ฉ์ ํต์งธ๋ก ์ญ์ ํ์ฌ ๋ชฉ์๋ฆฌ์ฉ ํ
์คํธ ์์ฑ
|
| 52 |
clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL)
|
| 53 |
clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text).strip()
|
| 54 |
+
tts_lines = clean_text.split('\n')
|
| 55 |
+
tts_final = "\n".join(tts_lines[:5]) # 5์ค ์์ฝ
|
| 56 |
|
| 57 |
+
# ์์ฑ ์์ฑ (ํน์๊ธฐํธ ์ ๊ฑฐ)
|
| 58 |
+
tts_input = re.sub(r'[\*\#\-\_\~\|]', '', tts_final)
|
| 59 |
voice_path = f"/tmp/v_{member_name.split()[0]}.mp3"
|
| 60 |
+
await edge_tts.Communicate(tts_input, MEMBERS.get(member_name, "ko-KR-SunHiNeural")).save(voice_path)
|
| 61 |
|
| 62 |
+
# ์์
์์ฑ (512 tokens = ์ฝ 10~12์ด)
|
| 63 |
music_path = None
|
| 64 |
if music_match and music_synthesiser:
|
| 65 |
p = music_match.group(1).strip() + ", high quality, studio recording"
|
|
|
|
| 67 |
music_path = f"/tmp/m_{member_name.split()[0]}.wav"
|
| 68 |
scipy.io.wavfile.write(music_path, music_output["sampling_rate"], music_output["audio"][0].T)
|
| 69 |
|
| 70 |
+
return tts_final, voice_path, music_path, tab_display
|
| 71 |
except Exception as e:
|
| 72 |
return str(e), None, None, "Error"
|
| 73 |
|
|
|
|
| 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()
|