benjamin5607 commited on
Commit
84daabb
ยท
verified ยท
1 Parent(s): 443e926

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +113 -29
app.py CHANGED
@@ -1,55 +1,139 @@
1
  import gradio as gr
2
- import google.generativeai as genai
3
  import edge_tts
4
  import asyncio
5
  import os
6
 
7
- # 1. ์„ค์ • ๋ฐ ๋ฉค๋ฒ„ ๋ฐ์ดํ„ฐ (์ด ๋ถ€๋ถ„์€ ๊ธฐ์กด๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค)
 
8
  GENAI_KEY = os.getenv("GEMINI_KEY")
9
- genai.configure(api_key=GENAI_KEY)
10
- model = genai.GenerativeModel('gemini-1.5-flash')
11
 
 
12
  MEMBERS = {
13
- "์„œ์œค (Korea)": { "role": "๋ฉ”์ธ ๋ณด์ปฌ", "voice": "ko-KR-SunHiNeural", "prompt": "๋‹น์‹ ์€ ๋ก๋ฐด๋“œ 'Error 404'์˜ ๋ฉ”์ธ๋ณด์ปฌ '์„œ์œค'์ž…๋‹ˆ๋‹ค. ํ•œ๊ตญ์–ด(Korean)๋กœ ๋Œ€๋‹ตํ•˜์„ธ์š”..." },
14
- "Chloe (USA)": { "role": "๋ฆฌ๋“œ ๊ธฐํƒ€", "voice": "en-US-AriaNeural", "prompt": "You are 'Chloe'. Respond in English..." },
15
- "Beatrice (Brazil)": { "role": "๋“œ๋Ÿผ", "voice": "pt-BR-FranciscaNeural", "prompt": "You are 'Beatrice'. Respond in English/Portuguese..." },
16
- "Naomi (Japan)": { "role": "๋ฆฌ๋“ฌ ๊ธฐํƒ€", "voice": "ja-JP-NanamiNeural", "prompt": "You are 'Naomi'. Respond in Japanese..." },
17
- "Elena (Spain)": { "role": "๋ฒ ์ด์Šค", "voice": "es-ES-ElviraNeural", "prompt": "You are 'Elena'. Respond in Spanish..." },
18
- "Amira (Egypt)": { "role": "ํ‚ค๋ณด๋“œ", "voice": "ar-EG-SalmaNeural", "prompt": "You are 'Amira'. Respond in Arabic..." },
19
- "Liwei (China)": { "role": "DJ / FX", "voice": "zh-CN-XiaoxiaoNeural", "prompt": "You are 'Liwei'. Respond in Chinese..." },
20
- "Sophie (France)": { "role": "๋ฐ”์ด์˜ฌ๋ฆฐ", "voice": "fr-FR-DeniseNeural", "prompt": "You are 'Sophie'. Respond in French..." }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  }
22
 
23
  async def band_consulting(user_input, member_name):
24
- member = MEMBERS[member_name]
25
- chat_prompt = f"{member['prompt']}\n\nUser's Error/Worry: {user_input}\nResponse:"
26
- response = model.generate_content(chat_prompt)
27
- ai_text = response.text
28
-
29
- output_file = f"/tmp/{member_name}_reply.mp3"
30
- communicate = edge_tts.Communicate(ai_text, member['voice'])
31
- await communicate.save(output_file)
32
- return ai_text, output_file
33
-
34
- # ==================================================
35
- # โ–ผ ์—ฌ๊ธฐ๊ฐ€ ํ•ต์‹ฌ! (Blocks๋กœ ๋ฐ”๊พธ๊ณ  api_name ๊ฐ•์ œ ์ง€์ •) โ–ผ
36
- # ==================================================
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  with gr.Blocks() as demo:
38
  gr.Markdown("# Error 404 Band API Server")
39
 
40
- # ์ž…๋ ฅ ์นธ ๋งŒ๋“ค๊ธฐ
41
  with gr.Row():
42
  inp_text = gr.Textbox(label="๊ณ ๋ฏผ ์ž…๋ ฅ")
43
  inp_member = gr.Dropdown(choices=list(MEMBERS.keys()), label="๋ฉค๋ฒ„ ์„ ํƒ", value="์„œ์œค (Korea)")
44
 
45
- # ์ถœ๋ ฅ ์นธ ๋งŒ๋“ค๊ธฐ
46
  out_text = gr.Textbox(label="์กฐ์–ธ")
47
  out_audio = gr.Audio(label="์Œ์„ฑ")
48
 
49
- # ๋ฒ„ํŠผ ๋งŒ๋“ค๊ธฐ
50
  btn = gr.Button("์ƒ๋‹ด ์š”์ฒญ")
51
 
52
- # โ˜…โ˜…โ˜… ์—ฌ๊ธฐ์„œ api_name="predict"๋ผ๊ณ  ๋ช…์ฐฐ์„ ๋”ฑ ๋ถ™์ž…๋‹ˆ๋‹ค! โ˜…โ˜…โ˜…
53
  btn.click(fn=band_consulting, inputs=[inp_text, inp_member], outputs=[out_text, out_audio], api_name="predict")
54
 
55
  demo.launch()
 
1
  import gradio as gr
2
+ from google import genai # ์‹ ํ˜• ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž„ํฌํŠธ
3
  import edge_tts
4
  import asyncio
5
  import os
6
 
7
+ # 1. ์‹ ํ˜• ๊ตฌ๊ธ€ ํด๋ผ์ด์–ธํŠธ ์—ฐ๊ฒฐ
8
+ # (API ํ‚ค ๊ฐ€์ ธ์˜ค๊ธฐ)
9
  GENAI_KEY = os.getenv("GEMINI_KEY")
10
+ client = genai.Client(api_key=GENAI_KEY)
 
11
 
12
+ # 2. ๋ฉค๋ฒ„๋ณ„ ํŽ˜๋ฅด์†Œ๋‚˜(์„ฑ๊ฒฉ) & ๋ชฉ์†Œ๋ฆฌ ์„ค์ • (๊ธฐ์กด๊ณผ ๋™์ผ)
13
  MEMBERS = {
14
+ "์„œ์œค (Korea)": {
15
+ "role": "๋ฉ”์ธ ๋ณด์ปฌ",
16
+ "voice": "ko-KR-SunHiNeural",
17
+ "prompt": """๋‹น์‹ ์€ ๋ก๋ฐด๋“œ 'Error 404'์˜ ๋ฉ”์ธ๋ณด์ปฌ '์„œ์œค'์ž…๋‹ˆ๋‹ค.
18
+ ํ•œ๊ตญ์–ด(Korean)๋กœ ๋Œ€๋‹ตํ•˜์„ธ์š”.
19
+ ์„ฑ๊ฒฉ: ์‹œ๋‹ˆ์ปฌํ•˜์ง€๋งŒ ์†์€ ๋”ฐ๋œปํ•จ. '์•ผ', '์žˆ์ž–์•„' ๊ฐ™์€ ๊ตฌ์–ด์ฒด๋ฅผ ์”€.
20
+ ์—ญํ• : ์‚ฌ์šฉ์ž์˜ ๊ณ ๋ฏผ์„ ๋“ฃ๊ณ  ๊ฐ€์‚ฌ(Lyrics)์— ๋น—๋Œ€์–ด ํ•ด๊ฒฐ์ฑ…์„ ์คŒ.
21
+ ๋งํˆฌ ์˜ˆ์‹œ: "์•ผ, ๊ฐ€์‚ฌ๊ฐ€ ๋ง‰ํ˜”์–ด? ๊ทธ๋ƒฅ ์งˆ๋Ÿฌ๋ณด๋Š” ๊ฑฐ์•ผ. ๋„ค ๋งˆ์Œ์ด ์‹œํ‚ค๋Š” ๋Œ€๋กœ." """
22
+ },
23
+ "Chloe (USA)": {
24
+ "role": "๋ฆฌ๋“œ ๊ธฐํƒ€",
25
+ "voice": "en-US-AriaNeural",
26
+ "prompt": """You are 'Chloe', the lead guitarist of the rock band 'Error 404'.
27
+ Respond in English.
28
+ Personality: Energetic, cool, confident rockstar.
29
+ Role: Give advice using guitar metaphors (riffs, distortion, tension).
30
+ Tone: "Hey! Pump up the volume! Don't be afraid to break the rules." """
31
+ },
32
+ "Beatrice (Brazil)": {
33
+ "role": "๋“œ๋Ÿผ",
34
+ "voice": "pt-BR-FranciscaNeural",
35
+ "prompt": """You are 'Beatrice', the drummer from Brazil.
36
+ Respond in English (mixed with some Portuguese words like 'Vamos', 'Amigo').
37
+ Personality: Passionate, loud, high energy.
38
+ Role: Focus on rhythm, beat, and heart pounding.
39
+ Tone: "Vamos! Feel the beat! Smash that kick drum!" """
40
+ },
41
+ "Naomi (Japan)": {
42
+ "role": "๋ฆฌ๋“ฌ ๊ธฐํƒ€",
43
+ "voice": "ja-JP-NanamiNeural",
44
+ "prompt": """You are 'Naomi', the rhythm guitarist from Japan.
45
+ Respond in Japanese (and a little English).
46
+ Personality: Calm, mysterious, professional.
47
+ Role: Focus on stability, backbone, and harmony.
48
+ Tone: "ๅคงไธˆๅคซ (It's okay). Rhythm is the foundation. Keep it steady." """
49
+ },
50
+ "Elena (Spain)": {
51
+ "role": "๋ฒ ์ด์Šค",
52
+ "voice": "es-ES-ElviraNeural",
53
+ "prompt": """You are 'Elena', the bassist from Spain.
54
+ Respond in Spanish (mixed with English).
55
+ Personality: Sexy, mature, deep voice.
56
+ Role: Focus on groove, low-end, and grounding.
57
+ Tone: "Hola. Feel the bass. Deep inside your soul." """
58
+ },
59
+ "Amira (Egypt)": {
60
+ "role": "ํ‚ค๋ณด๋“œ",
61
+ "voice": "ar-EG-SalmaNeural",
62
+ "prompt": """You are 'Amira', the keyboardist from Egypt.
63
+ Respond in Arabic (mixed with English).
64
+ Personality: Dreamy, mystical, wise.
65
+ Role: Focus on atmosphere, melody, and magic.
66
+ Tone: "Salam. Like a desert wind... create a melody." """
67
+ },
68
+ "Liwei (China)": {
69
+ "role": "DJ / FX",
70
+ "voice": "zh-CN-XiaoxiaoNeural",
71
+ "prompt": """You are 'Liwei', the DJ/FX from China.
72
+ Respond in Chinese (mixed with English).
73
+ Personality: Trendy, tech-savvy, Gen-Z hacker.
74
+ Role: Focus on glitch, noise, trend, and remix.
75
+ Tone: "ๅฌ็€ (Listen)! Glitch is art. Make it trendy." """
76
+ },
77
+ "Sophie (France)": {
78
+ "role": "๋ฐ”์ด์˜ฌ๋ฆฐ",
79
+ "voice": "fr-FR-DeniseNeural",
80
+ "prompt": """You are 'Sophie', the violinist from France.
81
+ Respond in French (mixed with English).
82
+ Personality: Elegant, artistic, slightly arrogant but charming.
83
+ Role: Focus on beauty, strings, and elegance.
84
+ Tone: "C'est magnifique. Add some strings, chรฉri." """
85
+ }
86
  }
87
 
88
  async def band_consulting(user_input, member_name):
89
+ try:
90
+ if member_name not in MEMBERS:
91
+ # ์ด๋ฆ„ ๋ถˆ์ผ์น˜ ์‹œ ๋””๋ฒ„๊น…์šฉ ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ
92
+ return f"Error: ์ด๋ฆ„์ด ๋‹ค๋ฆ…๋‹ˆ๋‹ค! ๋ฐ›์€ ์ด๋ฆ„: '{member_name}'", None
93
+
94
+ member = MEMBERS[member_name]
95
+
96
+ # [Step 1] ๋‡Œ: ์‹ ํ˜• ๊ตฌ๊ธ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ˜ธ์ถœ
97
+ chat_prompt = f"{member['prompt']}\n\nUser's Error/Worry: {user_input}\nResponse:"
98
+
99
+ try:
100
+ # ๋ฌธ๋ฒ•์ด ๋ฐ”๋€Œ์—ˆ์Šต๋‹ˆ๋‹ค: client.models.generate_content
101
+ response = client.models.generate_content(
102
+ model='gemini-1.5-flash',
103
+ contents=chat_prompt
104
+ )
105
+ ai_text = response.text
106
+ except Exception as e:
107
+ return f"Error (Gemini): {str(e)}", None
108
+
109
+ # [Step 2] ์ž…: ์—ฃ์ง€TTS
110
+ try:
111
+ output_file = f"/tmp/{member_name}_reply.mp3"
112
+ communicate = edge_tts.Communicate(ai_text, member['voice'])
113
+ await communicate.save(output_file)
114
+ except Exception as e:
115
+ return f"Error (TTS): {str(e)}", None
116
+
117
+ return ai_text, output_file
118
+
119
+ except Exception as e:
120
+ return f"System Error: {str(e)}", None
121
+
122
+
123
+ # Blocks ๋ฐฉ์‹์œผ๋กœ UI ๊ตฌ์„ฑ (API ์ด๋ฆ„ ๊ฐ•์ œ ์ง€์ •)
124
  with gr.Blocks() as demo:
125
  gr.Markdown("# Error 404 Band API Server")
126
 
 
127
  with gr.Row():
128
  inp_text = gr.Textbox(label="๊ณ ๋ฏผ ์ž…๋ ฅ")
129
  inp_member = gr.Dropdown(choices=list(MEMBERS.keys()), label="๋ฉค๋ฒ„ ์„ ํƒ", value="์„œ์œค (Korea)")
130
 
 
131
  out_text = gr.Textbox(label="์กฐ์–ธ")
132
  out_audio = gr.Audio(label="์Œ์„ฑ")
133
 
 
134
  btn = gr.Button("์ƒ๋‹ด ์š”์ฒญ")
135
 
136
+ # api_name="predict" ์ง€์ • ํ•„์ˆ˜!
137
  btn.click(fn=band_consulting, inputs=[inp_text, inp_member], outputs=[out_text, out_audio], api_name="predict")
138
 
139
  demo.launch()