Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,90 +1,118 @@
|
|
| 1 |
import os
|
| 2 |
import re
|
| 3 |
-
import requests
|
| 4 |
import uuid
|
| 5 |
-
import scipy.io.wavfile
|
| 6 |
-
import numpy as np
|
| 7 |
import torch
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
async def band_consulting(user_input, member_name, lang_code):
|
| 22 |
req_id = str(uuid.uuid4())[:8]
|
| 23 |
-
voice_path = f"/tmp/v_{req_id}.mp3"
|
| 24 |
music_path = f"/tmp/m_{req_id}.wav"
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
"
|
| 45 |
-
"parameters": {"max_new_tokens": 1024, "temperature": 0.7, "top_p": 0.9}
|
| 46 |
-
}
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
clean_text = "๋ง์ด ํ์ ์๋ ๋๋ด์ฃผ๋ ๋ผ์ธ์ด ์๊ฐ๋ฌ์ด. ๋ฐ๋ก ๋ค์ด๋ณด์๊ณ !"
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
# ์์ ํํฐ ๋จ์ด ์ฐํ (Vocal ๋ฑ ์ฐจ๋จ ๋ฐฉ์ง)
|
| 76 |
-
safe_p = re.sub(r'(laugh|vocal|voice|human)', 'rock guitar', music_p.lower())
|
| 77 |
-
music_output = music_synthesiser(safe_p, forward_params={"max_new_tokens": 512})
|
| 78 |
-
|
| 79 |
-
# ์ค๋์ค ์ฒ๋ฆฌ (Float32 -> Int16)
|
| 80 |
-
audio_data = np.squeeze(music_output["audio"])
|
| 81 |
-
audio_int16 = (audio_data * 32767).astype(np.int16)
|
| 82 |
-
scipy.io.wavfile.write(music_path, music_output["sampling_rate"], audio_int16)
|
| 83 |
-
else:
|
| 84 |
-
music_path = None
|
| 85 |
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
|
| 89 |
-
print(f"Critical System Error: {e}")
|
| 90 |
-
return "์์คํ
๊ณผ๋ถํ์ธ๊ฐ? ํ์ง๋ง ๋ฝ์ ๋ฉ์ถ์ง ์์! [MUSIC: basic rock drum beat]", None, None, "Retry Mode"
|
|
|
|
| 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 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 9 |
+
from groq import Groq # pip install groq
|
| 10 |
|
| 11 |
+
# 1. ๋ชจ๋ธ ์ฑ๊ธํค ๊ด๋ฆฌ ํด๋์ค (๋ฉ๋ชจ๋ฆฌ ํจ์จํ)
|
| 12 |
+
class ModelManager:
|
| 13 |
+
_llm_pipeline = None
|
| 14 |
+
_music_pipeline = None
|
| 15 |
+
_groq_client = None
|
| 16 |
|
| 17 |
+
@classmethod
|
| 18 |
+
def get_qwen(cls):
|
| 19 |
+
if cls._llm_pipeline is None:
|
| 20 |
+
model_id = "Qwen/Qwen2.5-0.5B-Instruct"
|
| 21 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 22 |
+
# CPU ํ๊ฒฝ ์ต์ ํ ๋ก๋ฉ
|
| 23 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 24 |
+
model_id,
|
| 25 |
+
device_map="cpu",
|
| 26 |
+
torch_dtype=torch.float32,
|
| 27 |
+
low_cpu_mem_usage=True
|
| 28 |
+
)
|
| 29 |
+
cls._llm_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 30 |
+
return cls._llm_pipeline
|
| 31 |
|
| 32 |
+
@classmethod
|
| 33 |
+
def get_groq(cls):
|
| 34 |
+
api_key = os.getenv("GROQ_API_KEY")
|
| 35 |
+
if cls._groq_client is None and api_key:
|
| 36 |
+
cls._groq_client = Groq(api_key=api_key)
|
| 37 |
+
return cls._groq_client
|
| 38 |
+
|
| 39 |
+
@classmethod
|
| 40 |
+
def get_music(cls):
|
| 41 |
+
if cls._music_pipeline is None:
|
| 42 |
+
# CPU์์ ๊ฐ์ฅ ๊ฐ๋ฒผ์ด ์์
๋ชจ๋ธ ์ ์ง
|
| 43 |
+
cls._music_pipeline = pipeline("text-to-audio", "facebook/musicgen-small", device="cpu")
|
| 44 |
+
return cls._music_pipeline
|
| 45 |
+
|
| 46 |
+
# 2. ํต์ฌ ๋น์ฆ๋์ค ๋ก์ง
|
| 47 |
async def band_consulting(user_input, member_name, lang_code):
|
| 48 |
req_id = str(uuid.uuid4())[:8]
|
|
|
|
| 49 |
music_path = f"/tmp/m_{req_id}.wav"
|
| 50 |
|
| 51 |
+
# ์์คํ
ํ๋กฌํํธ ์ค์
|
| 52 |
+
system_prompt = f"You are {member_name}, a rock star. Talk in {lang_code}. Format: 5 lines max + [TAB] details + [MUSIC: prompt]."
|
| 53 |
+
|
| 54 |
+
ai_text_raw = ""
|
| 55 |
+
|
| 56 |
+
# ์์ง ์ ํ (Groq ์ฐ์ -> ์คํจ ์ ๋ก์ปฌ Qwen)
|
| 57 |
+
groq_client = ModelManager.get_groq()
|
| 58 |
+
if groq_client:
|
| 59 |
+
try:
|
| 60 |
+
chat_completion = groq_client.chat.completions.create(
|
| 61 |
+
messages=[
|
| 62 |
+
{"role": "system", "content": system_prompt},
|
| 63 |
+
{"role": "user", "content": user_input}
|
| 64 |
+
],
|
| 65 |
+
model="llama-3.3-70b-versatile",
|
| 66 |
+
)
|
| 67 |
+
ai_text_raw = chat_completion.choices[0].message.content
|
| 68 |
+
except Exception as e:
|
| 69 |
+
print(f"Groq Error, switching to Qwen: {e}")
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
# Groq ์คํจ ์ ํน์ ๋ฏธ์ค์ ์ ๋ก์ปฌ Qwen ์ฌ์ฉ
|
| 72 |
+
if not ai_text_raw:
|
| 73 |
+
try:
|
| 74 |
+
qwen = ModelManager.get_qwen()
|
| 75 |
+
messages = [
|
| 76 |
+
{"role": "system", "content": system_prompt},
|
| 77 |
+
{"role": "user", "content": user_input}
|
| 78 |
+
]
|
| 79 |
+
text = qwen.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 80 |
+
output = qwen(text, max_new_tokens=512, do_sample=True, temperature=0.7)
|
| 81 |
+
ai_text_raw = output[0]['generated_text'].split("assistant\n")[-1]
|
| 82 |
+
except Exception as e:
|
| 83 |
+
ai_text_raw = "์ด๋ด ๋ฆฌ๋, ์ง๊ธ์ ๋ง๋ณด๋ค ์ฐ์ฃผ์ผ! [TAB] ์ฆํฅ ์ฐ์ฃผ ๊ฐ๋ [MUSIC: heavy rock guitar riff]"
|
| 84 |
|
| 85 |
+
# 3. ํ์ฑ ๋ฐ ์์
์์ฑ
|
| 86 |
+
tab_match = re.search(r'\[TAB\](.*?)(\[|$)', ai_text_raw, re.DOTALL | re.IGNORECASE)
|
| 87 |
+
music_match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE)
|
| 88 |
+
|
| 89 |
+
tab_display = tab_match.group(1).strip() if tab_match else "์์ธ ์
๋ณด ์ค๋น ์ค..."
|
| 90 |
+
clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL | re.IGNORECASE)
|
| 91 |
+
clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text, flags=re.IGNORECASE).strip()
|
| 92 |
+
music_p = music_match.group(1).strip() if music_match else "rock music"
|
| 93 |
|
| 94 |
+
# ์์
์์ฑ ํ์ดํ๋ผ์ธ
|
| 95 |
+
music_gen = ModelManager.get_music()
|
| 96 |
+
try:
|
| 97 |
+
# CPU ํ๊ฒฝ์ ์ํด ํ ํฐ ์๋ฅผ ์ค์ฌ ํ์์์ ๋ฐฉ์ง
|
| 98 |
+
music_output = music_gen(music_p, forward_params={"max_new_tokens": 256})
|
| 99 |
+
audio_data = np.squeeze(music_output["audio"])
|
| 100 |
+
audio_int16 = (audio_data * 32767).astype(np.int16)
|
| 101 |
+
scipy.io.wavfile.write(music_path, music_output["sampling_rate"], audio_int16)
|
| 102 |
+
except:
|
| 103 |
+
music_path = None
|
|
|
|
| 104 |
|
| 105 |
+
# TTS์ฉ ํ
์คํธ (๊ธฐ์กด ๊ตฌ์กฐ ์ ์ง)
|
| 106 |
+
tts_text = "\n".join(clean_text.split('\n')[:5])
|
| 107 |
+
|
| 108 |
+
return tts_text, None, music_path, tab_display # voice_path๋ edge_tts ๋ฑ์์ ์ฒ๋ฆฌ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
+
# 3. Gradio ์ธํฐํ์ด์ค (Minimal)
|
| 111 |
+
with gr.Blocks() as demo:
|
| 112 |
+
i1 = gr.Textbox(visible=False); i2 = gr.Textbox(visible=False); i3 = gr.Textbox(visible=False)
|
| 113 |
+
o1 = gr.Textbox(visible=False); o2 = gr.Audio(visible=False); o3 = gr.Audio(visible=False); o4 = gr.Textbox(visible=False)
|
| 114 |
+
|
| 115 |
+
btn = gr.Button("GO", visible=False)
|
| 116 |
+
btn.click(band_consulting, [i1, i2, i3], [o1, o2, o3, o4], api_name="predict")
|
| 117 |
|
| 118 |
+
demo.queue().launch()
|
|
|
|
|
|