Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
import os, re, uuid, torch, scipy.io.wavfile, edge_tts, asyncio
|
| 2 |
import numpy as np
|
| 3 |
import gradio as gr
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
|
@@ -31,23 +31,35 @@ class ModelManager:
|
|
| 31 |
cls._music_pipeline = pipeline("text-to-audio", "facebook/musicgen-small", device="cpu")
|
| 32 |
return cls._music_pipeline
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
"
|
| 37 |
-
"
|
| 38 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
}
|
| 40 |
|
| 41 |
-
async def band_consulting(user_input, member_name,
|
| 42 |
req_id = str(uuid.uuid4())[:8]
|
| 43 |
voice_path = f"/tmp/v_{req_id}.mp3"
|
| 44 |
music_path = f"/tmp/m_{req_id}.wav"
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
jam_context = f"Guitar: {g_inst}, Bass: {b_inst}, Drums: {d_inst}, Chords: {chords}"
|
| 47 |
-
system_prompt = f"""
|
| 48 |
-
|
| 49 |
-
[TAB]
|
| 50 |
-
[MUSIC]
|
| 51 |
|
| 52 |
ai_text_raw = ""
|
| 53 |
groq_client = ModelManager.get_groq()
|
|
@@ -65,16 +77,19 @@ async def band_consulting(user_input, member_name, lang_code, g_inst, b_inst, d_
|
|
| 65 |
out = qwen(f"<|im_start|>system\n{system_prompt}<|im_end|>\n<|im_start|>user\n{user_input}<|im_end|>\nassistant\n", max_new_tokens=1024)
|
| 66 |
ai_text_raw = out[0]['generated_text'].split("assistant\n")[-1]
|
| 67 |
|
|
|
|
| 68 |
tab_match = re.search(r'\[TAB\](.*?)(\[|$)', ai_text_raw, re.DOTALL | re.IGNORECASE)
|
| 69 |
music_match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE)
|
| 70 |
tab_display = tab_match.group(1).strip() if tab_match else "No Data"
|
| 71 |
clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL | re.IGNORECASE)
|
| 72 |
clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text, flags=re.IGNORECASE).strip()
|
| 73 |
|
| 74 |
-
|
|
|
|
| 75 |
communicate = edge_tts.Communicate(re.sub(r'[\*\#\-\_\~\|]', '', clean_text), voice_name)
|
| 76 |
await communicate.save(voice_path)
|
| 77 |
|
|
|
|
| 78 |
music_gen = ModelManager.get_music()
|
| 79 |
music_p = music_match.group(1).strip() if music_match else "rock"
|
| 80 |
music_output = music_gen(music_p, forward_params={"max_new_tokens": 512})
|
|
@@ -82,13 +97,12 @@ async def band_consulting(user_input, member_name, lang_code, g_inst, b_inst, d_
|
|
| 82 |
audio_int16 = (audio_data * 32767).astype(np.int16)
|
| 83 |
scipy.io.wavfile.write(music_path, music_output["sampling_rate"], audio_int16)
|
| 84 |
|
| 85 |
-
return clean_text, voice_path, music_path, tab_display
|
| 86 |
|
| 87 |
with gr.Blocks() as demo:
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
outputs = [gr.Textbox(visible=False), gr.Audio(visible=False), gr.Audio(visible=False), gr.Textbox(visible=False)]
|
| 91 |
btn = gr.Button("API", visible=False)
|
| 92 |
-
btn.click(band_consulting,
|
| 93 |
|
| 94 |
demo.queue().launch()
|
|
|
|
| 1 |
+
import os, re, uuid, torch, scipy.io.wavfile, edge_tts, asyncio, random
|
| 2 |
import numpy as np
|
| 3 |
import gradio as gr
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
|
|
|
| 31 |
cls._music_pipeline = pipeline("text-to-audio", "facebook/musicgen-small", device="cpu")
|
| 32 |
return cls._music_pipeline
|
| 33 |
|
| 34 |
+
# 8μΈ λ©€λ² μ€μ (κ΅κ°λ³ μΈμ΄ λ° λ³΄μ΄μ€)
|
| 35 |
+
MEMBERS_CONFIG = {
|
| 36 |
+
"μμ€ (Korea)": {"voice": "ko-KR-SunHiNeural", "lang": "Korean"},
|
| 37 |
+
"Chloe (USA)": {"voice": "en-US-AriaNeural", "lang": "English"},
|
| 38 |
+
"Naomi (Japan)": {"voice": "ja-JP-NanamiNeural", "lang": "Japanese"},
|
| 39 |
+
"Beatrice (Brazil)": {"voice": "pt-BR-FranciscaNeural", "lang": "Portuguese"},
|
| 40 |
+
"Elena (Spain)": {"voice": "es-ES-ElviraNeural", "lang": "Spanish"},
|
| 41 |
+
"Amira (Egypt)": {"voice": "ar-EG-SalmaNeural", "lang": "Arabic"},
|
| 42 |
+
"Liwei (China)": {"voice": "zh-CN-XiaoxiaoNeural", "lang": "Chinese"},
|
| 43 |
+
"Sophie (France)": {"voice": "fr-FR-DeniseNeural", "lang": "French"}
|
| 44 |
}
|
| 45 |
|
| 46 |
+
async def band_consulting(user_input, member_name, consult_lang, g_inst, b_inst, d_inst, chords):
|
| 47 |
req_id = str(uuid.uuid4())[:8]
|
| 48 |
voice_path = f"/tmp/v_{req_id}.mp3"
|
| 49 |
music_path = f"/tmp/m_{req_id}.wav"
|
| 50 |
|
| 51 |
+
# 1. λ©€λ² μ μ λ‘μ§: μμ΄κ° μ νλλ©΄ λλ€ λ©€λ²κ° λ΄λΉ
|
| 52 |
+
actual_member = member_name
|
| 53 |
+
if consult_lang == "English":
|
| 54 |
+
actual_member = random.choice(list(MEMBERS_CONFIG.keys()))
|
| 55 |
+
|
| 56 |
+
target_lang = MEMBERS_CONFIG[actual_member]["lang"] if consult_lang == "Native" else consult_lang
|
| 57 |
+
|
| 58 |
jam_context = f"Guitar: {g_inst}, Bass: {b_inst}, Drums: {d_inst}, Chords: {chords}"
|
| 59 |
+
system_prompt = f"""You are {actual_member}. Respond ONLY in {target_lang}.
|
| 60 |
+
Provide 5-7 lines of professional music advice.
|
| 61 |
+
[TAB] Section: Detailed chords/tabs.
|
| 62 |
+
[MUSIC] Section: English prompt reflecting: {jam_context}"""
|
| 63 |
|
| 64 |
ai_text_raw = ""
|
| 65 |
groq_client = ModelManager.get_groq()
|
|
|
|
| 77 |
out = qwen(f"<|im_start|>system\n{system_prompt}<|im_end|>\n<|im_start|>user\n{user_input}<|im_end|>\nassistant\n", max_new_tokens=1024)
|
| 78 |
ai_text_raw = out[0]['generated_text'].split("assistant\n")[-1]
|
| 79 |
|
| 80 |
+
# νμ±
|
| 81 |
tab_match = re.search(r'\[TAB\](.*?)(\[|$)', ai_text_raw, re.DOTALL | re.IGNORECASE)
|
| 82 |
music_match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE)
|
| 83 |
tab_display = tab_match.group(1).strip() if tab_match else "No Data"
|
| 84 |
clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL | re.IGNORECASE)
|
| 85 |
clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text, flags=re.IGNORECASE).strip()
|
| 86 |
|
| 87 |
+
# TTS
|
| 88 |
+
voice_name = MEMBERS_CONFIG[actual_member]["voice"]
|
| 89 |
communicate = edge_tts.Communicate(re.sub(r'[\*\#\-\_\~\|]', '', clean_text), voice_name)
|
| 90 |
await communicate.save(voice_path)
|
| 91 |
|
| 92 |
+
# MusicGen (12μ΄)
|
| 93 |
music_gen = ModelManager.get_music()
|
| 94 |
music_p = music_match.group(1).strip() if music_match else "rock"
|
| 95 |
music_output = music_gen(music_p, forward_params={"max_new_tokens": 512})
|
|
|
|
| 97 |
audio_int16 = (audio_data * 32767).astype(np.int16)
|
| 98 |
scipy.io.wavfile.write(music_path, music_output["sampling_rate"], audio_int16)
|
| 99 |
|
| 100 |
+
return f"[{actual_member}] {clean_text}", voice_path, music_path, tab_display
|
| 101 |
|
| 102 |
with gr.Blocks() as demo:
|
| 103 |
+
in_list = [gr.Textbox(visible=False) for _ in range(7)]
|
| 104 |
+
out_list = [gr.Textbox(visible=False), gr.Audio(visible=False), gr.Audio(visible=False), gr.Textbox(visible=False)]
|
|
|
|
| 105 |
btn = gr.Button("API", visible=False)
|
| 106 |
+
btn.click(band_consulting, in_list, out_list, api_name="predict")
|
| 107 |
|
| 108 |
demo.queue().launch()
|