Spaces:
Sleeping
Sleeping
Download app.py from benjamin5607/error404-api: direct link, hf CLI and curl.
- Browser
- Download file 4.5 kB
-
https://huggingface.co/spaces/benjamin5607/error404-api/resolve/fb6ce6a66b578a013a0cd78ff467dccf41395620/app.py
- Command line
-
hf download hf://spaces/benjamin5607/error404-api@fb6ce6a66b578a013a0cd78ff467dccf41395620/app.py
-
curl -L -o app.py https://huggingface.co/spaces/benjamin5607/error404-api/resolve/fb6ce6a66b578a013a0cd78ff467dccf41395620/app.py
4.5 kB
| import gradio as gr | |
| import requests | |
| import edge_tts | |
| import os | |
| import re | |
| import torch | |
| import scipy.io.wavfile | |
| import numpy as np | |
| from transformers import pipeline | |
| # API ํค ์ค์ | |
| GENAI_KEY = os.getenv("GEMINI_KEY") | |
| # 1. [์๊ณก๊ฐ] MusicGen ๋ก๋ (์์ง ๋ณด์ ์ค์ ์ค๋น) | |
| print("โณ ์๊ณก๊ฐ ์ญ์ธ ์ค... (์์ง ๋ณด์ ๋ชจ๋)") | |
| try: | |
| music_synthesiser = pipeline("text-to-audio", "facebook/musicgen-small") | |
| print("โ ์๊ณก๊ฐ ์ค๋น ์๋ฃ!") | |
| except Exception as e: | |
| print(f"โ ๏ธ ๋ชจ๋ธ ๋ก๋ ์คํจ: {e}") | |
| music_synthesiser = None | |
| def find_working_model(): | |
| url = f"https://generativelanguage.googleapis.com/v1beta/models?key={GENAI_KEY}" | |
| try: | |
| response = requests.get(url) | |
| data = response.json() | |
| candidates = [m['name'].replace("models/", "") for m in data.get('models', []) if 'generateContent' in m.get('supportedGenerationMethods', [])] | |
| preferred = ["gemini-1.5-flash", "gemini-pro"] | |
| for p in preferred: | |
| if p in candidates: return p | |
| return candidates[0] if candidates else "gemini-pro" | |
| except: return "gemini-pro" | |
| ACTIVE_MODEL = find_working_model() | |
| # ๋ฉค๋ฒ ํ๋ฅด์๋ (์ ๋ณด ์์ฑ ๋ฅ๋ ฅ ์ถ๊ฐ) | |
| MEMBERS = { | |
| "์์ค (Korea)": { "voice": "ko-KR-SunHiNeural", "prompt": "๋น์ ์ ๋ณด์ปฌ '์์ค'์ ๋๋ค. ์๋์ปฌํ ๋งํฌ. ์๋ด ๋ถ์ผ์ ๋ง์ถฐ ์์ ์ ์กฐ์ธ๊ณผ ์ฝ๋ ์งํ์ ์๋ ค์ฃผ์ธ์." }, | |
| "Chloe (USA)": { "voice": "en-US-AriaNeural", "prompt": "You are 'Chloe', the guitarist. Speak in English. Focus on guitar tabs and tones." }, | |
| "Beatrice (Brazil)": { "voice": "pt-BR-FranciscaNeural", "prompt": "You are 'Beatrice', the drummer. Focus on rhythm patterns and groovy beats." } | |
| } | |
| # app.py์ band_consulting ํจ์ ๋ถ๋ถ๋ง ์ด ๋ด์ฉ์ผ๋ก ๊ต์ฒดํ์ธ์ | |
| async def band_consulting(user_input, member_name, consult_category): | |
| try: | |
| # ๋ฉค๋ฒ ์ด๋ฆ ์คํ ์์ | |
| member = MEMBERS.get(member_name, MEMBERS["์์ค (Korea)"]) | |
| system_instruction = f""" | |
| {member['prompt']} | |
| [์๋ด ๋ถ์ผ: {consult_category}] | |
| ๋น์ ์ ์ ๋ฌธ๊ฐ๋ก์ ์กฐ์ธ์ ํ๊ณ , ๋ง์ง๋ง์ ๋ ๊ฐ์ง๋ฅผ ๋ฐ๋์ ํฌํจํ์ธ์: | |
| 1. [TAB]: ์์ ์ ๊ฐ๋จํ ์ ๋ณด๋ ์ฝ๋ ์งํ์ ํ ์คํธ ํ์์ผ๋ก ๊ทธ๋ฆฌ์ธ์. | |
| 2. [MUSIC]: ๋ถ์ผ์ ํนํ๋ ๊ณ ์์ง ์์ ํ๋กฌํํธ๋ฅผ ์ ์ผ์ธ์. | |
| """ | |
| url = f"https://generativelanguage.googleapis.com/v1beta/models/{ACTIVE_MODEL}:generateContent?key={GENAI_KEY}" | |
| payload = {"contents": [{"parts": [{"text": f"{system_instruction}\n์ง๋ฌธ: {user_input}"}]}]} | |
| res = requests.post(url, json=payload) | |
| ai_text_raw = res.json()['candidates'][0]['content']['parts'][0]['text'] | |
| tab_match = re.search(r'\[TAB\](.*?)\[', ai_text_raw + '[', re.DOTALL) | |
| music_match = re.search(r'\[MUSIC:(.*?)\]', ai_text_raw, re.IGNORECASE) | |
| tab_display = tab_match.group(1).strip() if tab_match else "No Score" | |
| # ํน์๋ฌธ์ ์ฝ๊ธฐ ๋ฐฉ์ง ํํฐ (TTS์ฉ ํ ์คํธ์์ ํน์๋ฌธ์ ์ ๊ฑฐ) | |
| raw_clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL) | |
| raw_clean_text = re.sub(r'\[MUSIC:.*?\]', '', raw_clean_text).strip() | |
| tts_text = re.sub(r'[\*\#\-\_\~]', '', raw_clean_text) # ์์ค์ด๊ฐ ์ฝ์ ๋ ๋ณํ ๋ฑ์ ๋ฌด์ํจ | |
| # 1. ๋ชฉ์๋ฆฌ ์์ฑ (ํํฐ๋ง๋ tts_text ์ฌ์ฉ) | |
| voice_path = f"/tmp/v_{member_name}.mp3" | |
| await edge_tts.Communicate(tts_text, member['voice']).save(voice_path) | |
| # 2. ์์ ์์ฑ | |
| music_path = None | |
| if music_match and music_synthesiser: | |
| p = music_match.group(1).strip() + ", high quality, studio recording" | |
| music = music_synthesiser(p, forward_params={"max_new_tokens": 448, "guidance_scale": 4.5}) | |
| music_path = f"/tmp/m_{member_name}.wav" | |
| scipy.io.wavfile.write(music_path, music["sampling_rate"], music["audio"][0].T) | |
| return raw_clean_text, voice_path, music_path, tab_display | |
| except Exception as e: | |
| return f"Error: {e}", None, None, "" | |
| # Gradio ์ธํฐํ์ด์ค | |
| with gr.Blocks() as demo: | |
| with gr.Row(): | |
| inp = gr.Textbox() | |
| mem = gr.Textbox() | |
| cat = gr.Textbox() # ๋ถ์ผ | |
| btn = gr.Button("Predict") | |
| btn.click(band_consulting, [inp, mem, cat], [gr.Textbox(), gr.Audio(), gr.Audio(), gr.Textbox()], api_name="predict") | |
| demo.launch() |