benjamin5607 commited on
Commit
899be9a
ยท
verified ยท
1 Parent(s): e7c5637

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -21
app.py CHANGED
@@ -1,13 +1,59 @@
1
  import gradio as gr
2
- from google import genai
3
  import edge_tts
4
  import os
 
5
 
6
- # 1. API ํ‚ค ์„ค์ •
7
  GENAI_KEY = os.getenv("GEMINI_KEY")
8
- client = genai.Client(api_key=GENAI_KEY)
9
 
10
- # 2. ๋ฉค๋ฒ„ ๋ฐ์ดํ„ฐ
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  MEMBERS = {
12
  "์„œ์œค (Korea)": {
13
  "role": "๋ฉ”์ธ ๋ณด์ปฌ",
@@ -17,37 +63,37 @@ MEMBERS = {
17
  "Chloe (USA)": {
18
  "role": "๋ฆฌ๋“œ ๊ธฐํƒ€",
19
  "voice": "en-US-AriaNeural",
20
- "prompt": "You are 'Chloe', the lead guitarist. Respond in English. Personality: Energetic, cool rockstar. Tone: 'Hey! Pump up the volume!'"
21
  },
22
  "Beatrice (Brazil)": {
23
  "role": "๋“œ๋Ÿผ",
24
  "voice": "pt-BR-FranciscaNeural",
25
- "prompt": "You are 'Beatrice', the drummer from Brazil. Respond in English mixed with Portuguese. Personality: Passionate, loud. Tone: 'Vamos! Feel the beat!'"
26
  },
27
  "Naomi (Japan)": {
28
  "role": "๋ฆฌ๋“ฌ ๊ธฐํƒ€",
29
  "voice": "ja-JP-NanamiNeural",
30
- "prompt": "You are 'Naomi', the rhythm guitarist. Respond in Japanese. Personality: Calm, mysterious. Tone: 'ๅคงไธˆๅคซ (It's okay). Keep it steady.'"
31
  },
32
  "Elena (Spain)": {
33
  "role": "๋ฒ ์ด์Šค",
34
  "voice": "es-ES-ElviraNeural",
35
- "prompt": "You are 'Elena', the bassist. Respond in Spanish mixed with English. Personality: Sexy, mature. Tone: 'Hola. Feel the bass.'"
36
  },
37
  "Amira (Egypt)": {
38
  "role": "ํ‚ค๋ณด๋“œ",
39
  "voice": "ar-EG-SalmaNeural",
40
- "prompt": "You are 'Amira', the keyboardist. Respond in Arabic mixed with English. Personality: Dreamy, mystical. Tone: 'Salam. Like a desert wind...'"
41
  },
42
  "Liwei (China)": {
43
  "role": "DJ / FX",
44
  "voice": "zh-CN-XiaoxiaoNeural",
45
- "prompt": "You are 'Liwei', the DJ. Respond in Chinese mixed with English. Personality: Trendy Gen-Z. Tone: 'ๅฌ็€ (Listen)! Glitch is art.'"
46
  },
47
  "Sophie (France)": {
48
  "role": "๋ฐ”์ด์˜ฌ๋ฆฐ",
49
  "voice": "fr-FR-DeniseNeural",
50
- "prompt": "You are 'Sophie', the violinist. Respond in French mixed with English. Personality: Elegant, artistic. Tone: 'C'est magnifique.'"
51
  }
52
  }
53
 
@@ -58,20 +104,16 @@ async def band_consulting(user_input, member_name):
58
 
59
  member = MEMBERS[member_name]
60
 
61
- # [Step 1] ๋‡Œ: ๋ณธ๋ช…(gemini-1.5-flash-latest) ์‚ฌ์šฉ!
62
  chat_prompt = f"{member['prompt']}\n\nUser's Error/Worry: {user_input}\nResponse:"
63
 
64
  try:
65
- # โ˜… ์ˆ˜์ •๋œ ๋ถ€๋ถ„: ์ด๋ฆ„ ๋’ค์— '-latest'๋ฅผ ๋ถ™์˜€์Šต๋‹ˆ๋‹ค.
66
- response = client.models.generate_content(
67
- model='gemini-1.5-flash-latest',
68
- contents=chat_prompt
69
- )
70
  ai_text = response.text
71
  except Exception as e:
72
- return f"Error (Gemini): {str(e)}", None
 
73
 
74
- # [Step 2] ์ž…: Edge-TTS
75
  try:
76
  output_file = f"/tmp/{member_name}_reply.mp3"
77
  communicate = edge_tts.Communicate(ai_text, member['voice'])
@@ -84,9 +126,9 @@ async def band_consulting(user_input, member_name):
84
  except Exception as e:
85
  return f"System Error: {str(e)}", None
86
 
87
- # Blocks UI ์„ค์ •
88
  with gr.Blocks() as demo:
89
- gr.Markdown("# Error 404 Band API Server")
90
  with gr.Row():
91
  inp_text = gr.Textbox(label="๊ณ ๋ฏผ ์ž…๋ ฅ")
92
  inp_member = gr.Dropdown(choices=list(MEMBERS.keys()), label="๋ฉค๋ฒ„ ์„ ํƒ", value="์„œ์œค (Korea)")
 
1
  import gradio as gr
2
+ import google.generativeai as genai
3
  import edge_tts
4
  import os
5
+ import re
6
 
7
+ # 1. ๊ตฌ๊ธ€ API ์„ค์ •
8
  GENAI_KEY = os.getenv("GEMINI_KEY")
9
+ genai.configure(api_key=GENAI_KEY)
10
 
11
+ # ==========================================
12
+ # ๐Ÿ•ต๏ธโ€โ™‚๏ธ [์ƒ์กด์ž ํƒ์ƒ‰] ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ชจ๋ธ ์ž๋™ ์ฐพ๊ธฐ
13
+ # ==========================================
14
+ def get_alive_model():
15
+ print("๐Ÿ“ก ๊ตฌ๊ธ€ ์„œ๋ฒ„์— ์ƒ์กด ๋ชจ๋ธ์„ ๋ฌธ์˜ํ•ฉ๋‹ˆ๋‹ค...")
16
+ available_models = []
17
+
18
+ try:
19
+ # ๊ตฌ๊ธ€์— ์žˆ๋Š” ๋ชจ๋“  ๋ชจ๋ธ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
20
+ for m in genai.list_models():
21
+ if 'generateContent' in m.supported_generation_methods:
22
+ available_models.append(m.name)
23
+
24
+ print(f"๐Ÿ“‹ ๋ฐœ๊ฒฌ๋œ ๋ชจ๋ธ๋“ค: {available_models}")
25
+
26
+ # ์šฐ์„ ์ˆœ์œ„: ์ตœ์‹  ํ”Œ๋ž˜์‹œ -> ํ”„๋กœ -> ๋ ˆ๊ฑฐ์‹œ ์ˆœ์„œ๋กœ ์ฐพ๊ธฐ
27
+ # (๋ฆฌ๋”๋‹˜์ด ๋ง์”€ํ•˜์‹  2.5, 3.0์ด ์žˆ๋‹ค๋ฉด ์—ฌ๊ธฐ์„œ ๊ฑธ๋ฆฝ๋‹ˆ๋‹ค!)
28
+ priority_keywords = [
29
+ "gemini-2.0-flash",
30
+ "gemini-1.5-flash",
31
+ "gemini-1.5-pro",
32
+ "gemini-pro"
33
+ ]
34
+
35
+ for keyword in priority_keywords:
36
+ for model_name in available_models:
37
+ if keyword in model_name:
38
+ print(f"โœ… ์ƒ์กด ํ™•์ธ! [{model_name}] ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.")
39
+ return genai.GenerativeModel(model_name)
40
+
41
+ # ๋งŒ์•ฝ ์šฐ์„ ์ˆœ์œ„์— ์—†์œผ๋ฉด, ๋ชฉ๋ก์˜ ์ฒซ ๋ฒˆ์งธ gemini ๋ชจ๋ธ์„ ์”๋‹ˆ๋‹ค.
42
+ fallback = [m for m in available_models if "gemini" in m][0]
43
+ print(f"โš ๏ธ ์šฐ์„ ์ˆœ์œ„ ๋ชจ๋ธ ์—†์Œ. ๋Œ€์ฒด ๋ชจ๋ธ [{fallback}] ์‚ฌ์šฉ.")
44
+ return genai.GenerativeModel(fallback)
45
+
46
+ except Exception as e:
47
+ print(f"โŒ ๋ชจ๋ธ ํƒ์ƒ‰ ์‹คํŒจ: {str(e)}")
48
+ # ์ตœํ›„์˜ ์ˆ˜๋‹จ: ๊ทธ๋ƒฅ ๋ฌธ์ž์—ด๋กœ ๋ฐ•์•„๋ฒ„๋ฆฌ๊ธฐ (Legacy)
49
+ return genai.GenerativeModel("gemini-pro")
50
+
51
+ # ์„œ๋ฒ„ ์ผœ์งˆ ๋•Œ ๋”ฑ ํ•œ ๋ฒˆ ์‹คํ–‰ํ•ด์„œ ๋ชจ๋ธ์„ ํ™•์ •ํ•ฉ๋‹ˆ๋‹ค.
52
+ model = get_alive_model()
53
+
54
+ # ==========================================
55
+ # 2. ๋ฉค๋ฒ„ ๋ฐ์ดํ„ฐ (๋ณ€๊ฒฝ ์—†์Œ)
56
+ # ==========================================
57
  MEMBERS = {
58
  "์„œ์œค (Korea)": {
59
  "role": "๋ฉ”์ธ ๋ณด์ปฌ",
 
63
  "Chloe (USA)": {
64
  "role": "๋ฆฌ๋“œ ๊ธฐํƒ€",
65
  "voice": "en-US-AriaNeural",
66
+ "prompt": "You are 'Chloe'. Respond in English. Personality: Energetic, cool rockstar. Tone: 'Hey! Pump up the volume!'"
67
  },
68
  "Beatrice (Brazil)": {
69
  "role": "๋“œ๋Ÿผ",
70
  "voice": "pt-BR-FranciscaNeural",
71
+ "prompt": "You are 'Beatrice'. Respond in English mixed with Portuguese. Personality: Passionate, loud. Tone: 'Vamos! Feel the beat!'"
72
  },
73
  "Naomi (Japan)": {
74
  "role": "๋ฆฌ๋“ฌ ๊ธฐํƒ€",
75
  "voice": "ja-JP-NanamiNeural",
76
+ "prompt": "You are 'Naomi'. Respond in Japanese. Personality: Calm, mysterious. Tone: 'ๅคงไธˆๅคซ (It's okay). Keep it steady.'"
77
  },
78
  "Elena (Spain)": {
79
  "role": "๋ฒ ์ด์Šค",
80
  "voice": "es-ES-ElviraNeural",
81
+ "prompt": "You are 'Elena'. Respond in Spanish mixed with English. Personality: Sexy, mature. Tone: 'Hola. Feel the bass.'"
82
  },
83
  "Amira (Egypt)": {
84
  "role": "ํ‚ค๋ณด๋“œ",
85
  "voice": "ar-EG-SalmaNeural",
86
+ "prompt": "You are 'Amira'. Respond in Arabic mixed with English. Personality: Dreamy, mystical. Tone: 'Salam. Like a desert wind...'"
87
  },
88
  "Liwei (China)": {
89
  "role": "DJ / FX",
90
  "voice": "zh-CN-XiaoxiaoNeural",
91
+ "prompt": "You are 'Liwei'. Respond in Chinese mixed with English. Personality: Trendy Gen-Z. Tone: 'ๅฌ็€ (Listen)! Glitch is art.'"
92
  },
93
  "Sophie (France)": {
94
  "role": "๋ฐ”์ด์˜ฌ๋ฆฐ",
95
  "voice": "fr-FR-DeniseNeural",
96
+ "prompt": "You are 'Sophie'. Respond in French mixed with English. Personality: Elegant, artistic. Tone: 'C'est magnifique.'"
97
  }
98
  }
99
 
 
104
 
105
  member = MEMBERS[member_name]
106
 
 
107
  chat_prompt = f"{member['prompt']}\n\nUser's Error/Worry: {user_input}\nResponse:"
108
 
109
  try:
110
+ # ํ™•์ •๋œ ๋ชจ๋ธ๋กœ ์ƒ์„ฑ ์‹œ๋„
111
+ response = model.generate_content(chat_prompt)
 
 
 
112
  ai_text = response.text
113
  except Exception as e:
114
+ # ๋ชจ๋ธ์ด ์žˆ์–ด๋„ Quota(ํ•œ๋„) ์—๋Ÿฌ๊ฐ€ ๋‚  ์ˆ˜ ์žˆ์Œ -> ์—๋Ÿฌ ๋ฉ”์‹œ์ง€ ๋ฐ˜ํ™˜
115
+ return f"Error (Gemini): ๋ชจ๋ธ ์—ฐ๊ฒฐ ์‹คํŒจ.\n{str(e)}", None
116
 
 
117
  try:
118
  output_file = f"/tmp/{member_name}_reply.mp3"
119
  communicate = edge_tts.Communicate(ai_text, member['voice'])
 
126
  except Exception as e:
127
  return f"System Error: {str(e)}", None
128
 
129
+ # Blocks UI
130
  with gr.Blocks() as demo:
131
+ gr.Markdown("# Error 404 Band API Server (Auto-Discovery Mode)")
132
  with gr.Row():
133
  inp_text = gr.Textbox(label="๊ณ ๋ฏผ ์ž…๋ ฅ")
134
  inp_member = gr.Dropdown(choices=list(MEMBERS.keys()), label="๋ฉค๋ฒ„ ์„ ํƒ", value="์„œ์œค (Korea)")