Spaces:
Sleeping

File size: 3,719 Bytes
da89e23
440060b
da89e23
 
8ee2277
 
 
da89e23
 
eef5842
8ee2277
 
26c3944
8ee2277
 
eef5842
440060b
26c3944
 
 
 
 
 
 
 
440060b
1359090
26c3944
84daabb
26c3944
 
 
8ee2277
26c3944
eef5842
 
 
 
8ee2277
 
26c3944
3f6565c
26c3944
 
8ee2277
eef5842
1359090
3f6565c
eef5842
fb6ce6a
eef5842
1359090
 
eef5842
 
84daabb
eef5842
 
26c3944
eef5842
84daabb
eef5842
8ee2277
3f6565c
5473ad3
26c3944
 
1359090
8ee2277
eef5842
84daabb
26c3944
84daabb
f2609ff
26c3944
5724da8
 
f2609ff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import gradio as gr
import requests
import edge_tts
import os
import re
import scipy.io.wavfile
from transformers import pipeline

GENAI_KEY = os.getenv("GEMINI_KEY")

try:
    music_synthesiser = pipeline("text-to-audio", "facebook/musicgen-small")
except:
    music_synthesiser = None

# 8인 멀버 보이슀 λ§€μΉ­
MEMBERS = {
    "μ„œμœ€ (Korea)": "ko-KR-SunHiNeural",
    "Chloe (USA)": "en-US-AriaNeural",
    "Beatrice (Brazil)": "pt-BR-FranciscaNeural",
    "Naomi (Japan)": "ja-JP-NanamiNeural",
    "Elena (Spain)": "es-ES-ElviraNeural",
    "Amira (Egypt)": "ar-EG-SalmaNeural",
    "Liwei (China)": "zh-CN-XiaoxiaoNeural",
    "Sophie (France)": "fr-FR-DeniseNeural"
}

async def band_consulting(user_input, member_name, consult_lang_code):
    try:
        lang_map = {"ko": "Korean", "en": "English", "ja": "Japanese", "pt": "Portuguese", "es": "Spanish", "ar": "Arabic", "zh": "Chinese", "fr": "French"}
        target_lang = lang_map.get(consult_lang_code, "Korean")
        
        system_instruction = f"""
        당신은 λ°΄λ“œ 멀버 '{member_name}'μž…λ‹ˆλ‹€.
        1. λ°˜λ“œμ‹œ '{target_lang}'둜만 λ‹΅λ³€ν•˜μ„Έμš”. μ˜μ–΄λ©΄ μ˜μ–΄, 일본어면 μΌλ³Έμ–΄λ§Œ μ‚¬μš©ν•©λ‹ˆλ‹€.
        2. μŒμ„± λ‹΅λ³€(TTS)은 5쀄 μ΄λ‚΄λ‘œ ν•΅μ‹¬λ§Œ μš”μ•½ν•˜μ„Έμš”.
        3. λͺ¨λ“  상세 μ„€λͺ…, ν™”μ„±ν•™ μ‘°μ–Έ, ν…μŠ€νŠΈ μ•…λ³΄λŠ” [TAB] μ„Ήμ…˜μ— λ„£μœΌμ„Έμš”.
        4. μŒμ•… μƒμ„±μš© μ˜μ–΄ ν”„λ‘¬ν”„νŠΈλŠ” λ§ˆμ§€λ§‰μ— [MUSIC: ν”„λ‘¬ν”„νŠΈ] ν˜•μ‹μœΌλ‘œ ν¬ν•¨ν•˜μ„Έμš”.
        """
        
        url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={GENAI_KEY}"
        payload = {"contents": [{"parts": [{"text": f"{system_instruction}\n질문: {user_input}"}]}]}
        res = requests.post(url, json=payload).json()
        ai_text_raw = res['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 Detailed Score"
        
        # β˜… 악보 읽기 λ°©μ§€: [TAB] 이후 λ‚΄μš©μ„ ν†΅μ§Έλ‘œ μ‚­μ œν•˜μ—¬ λͺ©μ†Œλ¦¬μš© ν…μŠ€νŠΈ 생성
        clean_text = re.sub(r'\[TAB\].*?(\[|$)', '', ai_text_raw, flags=re.DOTALL)
        clean_text = re.sub(r'\[MUSIC:.*?\]', '', clean_text).strip()
        tts_lines = clean_text.split('\n')
        tts_final = "\n".join(tts_lines[:5]) # 5쀄 μš”μ•½

        # μŒμ„± 생성 (특수기호 제거)
        tts_input = re.sub(r'[\*\#\-\_\~\|]', '', tts_final)
        voice_path = f"/tmp/v_{member_name.split()[0]}.mp3"
        await edge_tts.Communicate(tts_input, MEMBERS.get(member_name, "ko-KR-SunHiNeural")).save(voice_path)
        
        # μŒμ•… 생성 (512 tokens = μ•½ 10~12초)
        music_path = None
        if music_match and music_synthesiser:
            p = music_match.group(1).strip() + ", high quality, studio recording"
            music_output = music_synthesiser(p, forward_params={"max_new_tokens": 512, "do_sample": True})
            music_path = f"/tmp/m_{member_name.split()[0]}.wav"
            scipy.io.wavfile.write(music_path, music_output["sampling_rate"], music_output["audio"][0].T)

        return tts_final, voice_path, music_path, tab_display
    except Exception as e:
        return str(e), None, None, "Error"

with gr.Blocks() as demo:
    i1 = gr.Textbox(); i2 = gr.Textbox(); i3 = gr.Textbox()
    o1 = gr.Textbox(); o2 = gr.Audio(); o3 = gr.Audio(); o4 = gr.Textbox()
    btn = gr.Button("GO"); btn.click(band_consulting, [i1, i2, i3], [o1, o2, o3, o4], api_name="predict")
demo.launch()