Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,10 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import re
|
| 3 |
-
import uuid
|
| 4 |
-
import torch
|
| 5 |
import numpy as np
|
| 6 |
-
import scipy.io.wavfile
|
| 7 |
import gradio as gr
|
| 8 |
-
import edge_tts
|
| 9 |
-
import asyncio
|
| 10 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 11 |
from groq import Groq
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 15 |
-
|
| 16 |
class ModelManager:
|
| 17 |
_llm_pipeline = None
|
| 18 |
_music_pipeline = None
|
|
@@ -29,13 +21,15 @@ class ModelManager:
|
|
| 29 |
|
| 30 |
@classmethod
|
| 31 |
def get_groq(cls):
|
| 32 |
-
|
| 33 |
-
|
|
|
|
| 34 |
return cls._groq_client
|
| 35 |
|
| 36 |
@classmethod
|
| 37 |
def get_music(cls):
|
| 38 |
if cls._music_pipeline is None:
|
|
|
|
| 39 |
cls._music_pipeline = pipeline("text-to-audio", "facebook/musicgen-small", device="cpu")
|
| 40 |
return cls._music_pipeline
|
| 41 |
|
|
@@ -51,12 +45,12 @@ async def band_consulting(user_input, member_name, lang_code, g_inst, b_inst, d_
|
|
| 51 |
voice_path = f"/tmp/v_{req_id}.mp3"
|
| 52 |
music_path = f"/tmp/m_{req_id}.wav"
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
|
| 56 |
-
system_prompt = f"""๋น์ ์ ๋ฝ
|
| 57 |
-
|
| 58 |
-
[TAB] ์น์
์๋ ํ๋ธ๋ผ์
๋ณด
|
| 59 |
-
[MUSIC] ์น์
์๋ ๋ค์ JAM ์์ฒญ์ ๋ฐ์ํ ์์ด ํ๋กฌํํธ๋ฅผ ์์ฑํ์ธ์: {
|
| 60 |
|
| 61 |
ai_text_raw = ""
|
| 62 |
groq_client = ModelManager.get_groq()
|
|
@@ -71,24 +65,25 @@ async def band_consulting(user_input, member_name, lang_code, g_inst, b_inst, d_
|
|
| 71 |
|
| 72 |
if not ai_text_raw:
|
| 73 |
qwen = ModelManager.get_qwen()
|
| 74 |
-
|
|
|
|
| 75 |
ai_text_raw = out[0]['generated_text'].split("assistant\n")[-1]
|
| 76 |
|
| 77 |
-
# ํ์ฑ
|
| 78 |
tab_match = re.search(r'\[TAB\](.*?)(\[|$)', ai_text_raw, re.DOTALL | re.IGNORECASE)
|
| 79 |
music_match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE)
|
| 80 |
-
tab_display = tab_match.group(1).strip() if tab_match else "No
|
| 81 |
clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL | re.IGNORECASE)
|
| 82 |
clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text, flags=re.IGNORECASE).strip()
|
| 83 |
|
| 84 |
-
#
|
| 85 |
voice_name = MEMBERS_VOICE.get(member_name, "ko-KR-SunHiNeural")
|
| 86 |
communicate = edge_tts.Communicate(re.sub(r'[\*\#\-\_\~\|]', '', clean_text), voice_name)
|
| 87 |
await communicate.save(voice_path)
|
| 88 |
|
| 89 |
-
#
|
| 90 |
music_gen = ModelManager.get_music()
|
| 91 |
-
music_p = music_match.group(1).strip() if music_match else "
|
| 92 |
music_output = music_gen(music_p, forward_params={"max_new_tokens": 512})
|
| 93 |
audio_data = np.squeeze(music_output["audio"])
|
| 94 |
audio_int16 = (audio_data * 32767).astype(np.int16)
|
|
@@ -96,15 +91,11 @@ async def band_consulting(user_input, member_name, lang_code, g_inst, b_inst, d_
|
|
| 96 |
|
| 97 |
return clean_text, voice_path, music_path, tab_display
|
| 98 |
|
| 99 |
-
with gr.Blocks(
|
| 100 |
-
#
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
i_g = gr.Textbox(visible=False); i_b = gr.Textbox(visible=False); i_d = gr.Textbox(visible=False); i_c = gr.Textbox(visible=False)
|
| 104 |
-
|
| 105 |
-
o_text = gr.Textbox(visible=False); o_voice = gr.Audio(visible=False); o_music = gr.Audio(visible=False); o_tab = gr.Textbox(visible=False)
|
| 106 |
-
|
| 107 |
btn = gr.Button("API", visible=False)
|
| 108 |
-
btn.click(band_consulting,
|
| 109 |
|
| 110 |
demo.queue().launch()
|
|
|
|
| 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
|
| 5 |
from groq import Groq
|
| 6 |
|
| 7 |
+
# ์ฑ๊ธํค ๋ชจ๋ธ ๊ด๋ฆฌ์: CPU ๋ฉ๋ชจ๋ฆฌ ๋ถ์กฑ์ผ๋ก ์ธํ ํฌ๋์ ๋ฐฉ์ง
|
|
|
|
|
|
|
| 8 |
class ModelManager:
|
| 9 |
_llm_pipeline = None
|
| 10 |
_music_pipeline = None
|
|
|
|
| 21 |
|
| 22 |
@classmethod
|
| 23 |
def get_groq(cls):
|
| 24 |
+
key = os.getenv("GROQ_API_KEY")
|
| 25 |
+
if cls._groq_client is None and key:
|
| 26 |
+
cls._groq_client = Groq(api_key=key)
|
| 27 |
return cls._groq_client
|
| 28 |
|
| 29 |
@classmethod
|
| 30 |
def get_music(cls):
|
| 31 |
if cls._music_pipeline is None:
|
| 32 |
+
# CPU ํ๊ฒฝ์์ ๊ฐ์ฅ ์์ ์ ์ธ small ๋ชจ๋ธ ์ฌ์ฉ
|
| 33 |
cls._music_pipeline = pipeline("text-to-audio", "facebook/musicgen-small", device="cpu")
|
| 34 |
return cls._music_pipeline
|
| 35 |
|
|
|
|
| 45 |
voice_path = f"/tmp/v_{req_id}.mp3"
|
| 46 |
music_path = f"/tmp/m_{req_id}.wav"
|
| 47 |
|
| 48 |
+
# JAM ์ง์์ฌํญ๊ณผ ์๋ด ์ธ์ด ๋ฐ์
|
| 49 |
+
jam_context = f"Guitar: {g_inst}, Bass: {b_inst}, Drums: {d_inst}, Chords: {chords}"
|
| 50 |
+
system_prompt = f"""๋น์ ์ ๋ฝ์คํ {member_name}์
๋๋ค. ๋ฐ๋์ {lang_code} ์ธ์ด๋ก๋ง ๋ต๋ณํ์ธ์.
|
| 51 |
+
์ ๋ฌธ ๋ฎค์ง์
์ผ๋ก์ 5~7๋ฌธ์ฅ์ ๊น์ด ์๊ณ ์ฑ์ํ ์๋ด์ ์ ๊ณตํ์ธ์.
|
| 52 |
+
[TAB] ์น์
์๋ ์ฝ๋ ์งํ์ด๋ ํ๋ธ๋ผ์
๋ณด๋ฅผ ์์ธํ ์ ์ผ์ธ์.
|
| 53 |
+
[MUSIC] ์น์
์๋ ๋ค์ JAM ์์ฒญ์ ๋ฐ์ํ ์์ด ํ๋กฌํํธ๋ฅผ ์์ฑํ์ธ์: {jam_context}"""
|
| 54 |
|
| 55 |
ai_text_raw = ""
|
| 56 |
groq_client = ModelManager.get_groq()
|
|
|
|
| 65 |
|
| 66 |
if not ai_text_raw:
|
| 67 |
qwen = ModelManager.get_qwen()
|
| 68 |
+
input_text = f"<|im_start|>system\n{system_prompt}<|im_end|>\n<|im_start|>user\n{user_input}<|im_end|>\nassistant\n"
|
| 69 |
+
out = qwen(input_text, max_new_tokens=1024, do_sample=True)
|
| 70 |
ai_text_raw = out[0]['generated_text'].split("assistant\n")[-1]
|
| 71 |
|
| 72 |
+
# ์ ๊ท์ ํ์ฑ
|
| 73 |
tab_match = re.search(r'\[TAB\](.*?)(\[|$)', ai_text_raw, re.DOTALL | re.IGNORECASE)
|
| 74 |
music_match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE)
|
| 75 |
+
tab_display = tab_match.group(1).strip() if tab_match else "No Tab Data"
|
| 76 |
clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL | re.IGNORECASE)
|
| 77 |
clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text, flags=re.IGNORECASE).strip()
|
| 78 |
|
| 79 |
+
# TTS ์์ฑ
|
| 80 |
voice_name = MEMBERS_VOICE.get(member_name, "ko-KR-SunHiNeural")
|
| 81 |
communicate = edge_tts.Communicate(re.sub(r'[\*\#\-\_\~\|]', '', clean_text), voice_name)
|
| 82 |
await communicate.save(voice_path)
|
| 83 |
|
| 84 |
+
# MusicGen ์์ฑ (512ํ ํฐ = ์ฝ 12์ด)
|
| 85 |
music_gen = ModelManager.get_music()
|
| 86 |
+
music_p = music_match.group(1).strip() if music_match else "energetic rock guitar solo"
|
| 87 |
music_output = music_gen(music_p, forward_params={"max_new_tokens": 512})
|
| 88 |
audio_data = np.squeeze(music_output["audio"])
|
| 89 |
audio_int16 = (audio_data * 32767).astype(np.int16)
|
|
|
|
| 91 |
|
| 92 |
return clean_text, voice_path, music_path, tab_display
|
| 93 |
|
| 94 |
+
with gr.Blocks() as demo:
|
| 95 |
+
# 7๊ฐ์ ์
๋ ฅ (์๋ด, ๋ฉค๋ฒ, ์๋ด์ธ์ด, ๊ธฐํ, ๋ฒ ์ด์ค, ๋๋ผ, ์ฝ๋)
|
| 96 |
+
in_list = [gr.Textbox(visible=False) for _ in range(7)]
|
| 97 |
+
out_list = [gr.Textbox(visible=False), gr.Audio(visible=False), gr.Audio(visible=False), gr.Textbox(visible=False)]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
btn = gr.Button("API", visible=False)
|
| 99 |
+
btn.click(band_consulting, in_list, out_list, api_name="predict")
|
| 100 |
|
| 101 |
demo.queue().launch()
|