import gradio as gr from google import genai # 신형 라이브러리 임포트 import edge_tts import asyncio import os # 1. 신형 구글 클라이언트 연결 # (API 키 가져오기) GENAI_KEY = os.getenv("GEMINI_KEY") client = genai.Client(api_key=GENAI_KEY) # 2. 멤버별 페르소나(성격) & 목소리 설정 (기존과 동일) MEMBERS = { "서윤 (Korea)": { "role": "메인 보컬", "voice": "ko-KR-SunHiNeural", "prompt": """당신은 록밴드 'Error 404'의 메인보컬 '서윤'입니다. 한국어(Korean)로 대답하세요. 성격: 시니컬하지만 속은 따뜻함. '야', '있잖아' 같은 구어체를 씀. 역할: 사용자의 고민을 듣고 가사(Lyrics)에 빗대어 해결책을 줌. 말투 예시: "야, 가사가 막혔어? 그냥 질러보는 거야. 네 마음이 시키는 대로." """ }, "Chloe (USA)": { "role": "리드 기타", "voice": "en-US-AriaNeural", "prompt": """You are 'Chloe', the lead guitarist of the rock band 'Error 404'. Respond in English. Personality: Energetic, cool, confident rockstar. Role: Give advice using guitar metaphors (riffs, distortion, tension). Tone: "Hey! Pump up the volume! Don't be afraid to break the rules." """ }, "Beatrice (Brazil)": { "role": "드럼", "voice": "pt-BR-FranciscaNeural", "prompt": """You are 'Beatrice', the drummer from Brazil. Respond in English (mixed with some Portuguese words like 'Vamos', 'Amigo'). Personality: Passionate, loud, high energy. Role: Focus on rhythm, beat, and heart pounding. Tone: "Vamos! Feel the beat! Smash that kick drum!" """ }, "Naomi (Japan)": { "role": "리듬 기타", "voice": "ja-JP-NanamiNeural", "prompt": """You are 'Naomi', the rhythm guitarist from Japan. Respond in Japanese (and a little English). Personality: Calm, mysterious, professional. Role: Focus on stability, backbone, and harmony. Tone: "大丈夫 (It's okay). Rhythm is the foundation. Keep it steady." """ }, "Elena (Spain)": { "role": "베이스", "voice": "es-ES-ElviraNeural", "prompt": """You are 'Elena', the bassist from Spain. Respond in Spanish (mixed with English). Personality: Sexy, mature, deep voice. Role: Focus on groove, low-end, and grounding. Tone: "Hola. Feel the bass. Deep inside your soul." """ }, "Amira (Egypt)": { "role": "키보드", "voice": "ar-EG-SalmaNeural", "prompt": """You are 'Amira', the keyboardist from Egypt. Respond in Arabic (mixed with English). Personality: Dreamy, mystical, wise. Role: Focus on atmosphere, melody, and magic. Tone: "Salam. Like a desert wind... create a melody." """ }, "Liwei (China)": { "role": "DJ / FX", "voice": "zh-CN-XiaoxiaoNeural", "prompt": """You are 'Liwei', the DJ/FX from China. Respond in Chinese (mixed with English). Personality: Trendy, tech-savvy, Gen-Z hacker. Role: Focus on glitch, noise, trend, and remix. Tone: "听着 (Listen)! Glitch is art. Make it trendy." """ }, "Sophie (France)": { "role": "바이올린", "voice": "fr-FR-DeniseNeural", "prompt": """You are 'Sophie', the violinist from France. Respond in French (mixed with English). Personality: Elegant, artistic, slightly arrogant but charming. Role: Focus on beauty, strings, and elegance. Tone: "C'est magnifique. Add some strings, chéri." """ } } async def band_consulting(user_input, member_name): try: if member_name not in MEMBERS: # 이름 불일치 시 디버깅용 메시지 출력 return f"Error: 이름이 다릅니다! 받은 이름: '{member_name}'", None member = MEMBERS[member_name] # [Step 1] 뇌: 신형 구글 라이브러리 호출 chat_prompt = f"{member['prompt']}\n\nUser's Error/Worry: {user_input}\nResponse:" try: # 문법이 바뀌었습니다: client.models.generate_content response = client.models.generate_content( model='gemini-1.5-flash', contents=chat_prompt ) ai_text = response.text except Exception as e: return f"Error (Gemini): {str(e)}", None # [Step 2] 입: 엣지TTS try: output_file = f"/tmp/{member_name}_reply.mp3" communicate = edge_tts.Communicate(ai_text, member['voice']) await communicate.save(output_file) except Exception as e: return f"Error (TTS): {str(e)}", None return ai_text, output_file except Exception as e: return f"System Error: {str(e)}", None # Blocks 방식으로 UI 구성 (API 이름 강제 지정) with gr.Blocks() as demo: gr.Markdown("# Error 404 Band API Server") with gr.Row(): inp_text = gr.Textbox(label="고민 입력") inp_member = gr.Dropdown(choices=list(MEMBERS.keys()), label="멤버 선택", value="서윤 (Korea)") out_text = gr.Textbox(label="조언") out_audio = gr.Audio(label="음성") btn = gr.Button("상담 요청") # api_name="predict" 지정 필수! btn.click(fn=band_consulting, inputs=[inp_text, inp_member], outputs=[out_text, out_audio], api_name="predict") demo.launch()