Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,20 @@
|
|
2 |
import gradio as gr
|
3 |
import google.generativeai as genai
|
4 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
# --- UI ๋ฐ ์ฑ๋ด ์ค๋ช
---
|
7 |
# Gradio Blocks๋ฅผ ์ฌ์ฉํ์ฌ ์ข ๋ ์ ์ฐํ UI๋ฅผ ๊ตฌ์ฑํฉ๋๋ค.
|
@@ -10,11 +24,23 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
|
|
10 |
"""
|
11 |
# โ๏ธ Gemini API ์ฑ๋ด (Secrets ์ฌ์ฉ)
|
12 |
Google Gemini API๋ฅผ ์ฌ์ฉํ๋ ์ฑ๋ด์
๋๋ค.
|
13 |
-
|
|
|
|
|
14 |
[API ํค ๋ฐ๊ธ๋ฐ๊ธฐ](https://aistudio.google.com/app/apikey)
|
|
|
|
|
15 |
"""
|
16 |
)
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
# Gradio ์ฑ๋ด UI ์ปดํฌ๋ํธ
|
19 |
chatbot = gr.Chatbot(label="Gemini ์ฑ๋ด", height=600)
|
20 |
|
@@ -42,31 +68,61 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
|
|
42 |
minimum=1, maximum=4096, value=1024, step=1, label="Max new tokens"
|
43 |
)
|
44 |
|
45 |
-
#
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
49 |
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
# ํ๊ฒฝ๋ณ์์์ ๊ฐ์ ธ์จ API ํค๊ฐ ์์ผ๋ฉด ์๋ด ๋ฉ์์ง๋ฅผ ๋์๋๋ค.
|
52 |
-
if not
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
return
|
57 |
|
58 |
try:
|
59 |
# API ํค๋ฅผ ์ค์ ํฉ๋๋ค.
|
60 |
-
genai.configure(api_key=
|
|
|
61 |
except Exception as e:
|
62 |
-
yield f"API ํค ์ค์ ์ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {e}"
|
63 |
return
|
64 |
|
65 |
# ์ฌ์ฉํ ๋ชจ๋ธ๊ณผ ์์คํ
ํ๋กฌํํธ๋ฅผ ์ค์ ํฉ๋๋ค.
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
70 |
|
71 |
# Gradio์ ๋ํ ๊ธฐ๋ก์ Gemini API๊ฐ ์ดํดํ ์ ์๋ ํ์์ผ๋ก ๋ณํํฉ๋๋ค.
|
72 |
gemini_history = []
|
@@ -102,10 +158,19 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
|
|
102 |
|
103 |
except Exception as e:
|
104 |
# API ํธ์ถ ์ค ์๋ฌ๊ฐ ๋ฐ์ํ๋ฉด UI์ ํ์ํฉ๋๋ค.
|
105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
|
107 |
# --- Gradio ์ด๋ฒคํธ ๋ฆฌ์ค๋ ---
|
108 |
def on_submit(message, chat_history, system_prompt, temp, max_output_tokens):
|
|
|
|
|
|
|
109 |
chat_history.append((message, None))
|
110 |
bot_response_stream = respond(message, chat_history, system_prompt, temp, max_output_tokens)
|
111 |
|
@@ -125,4 +190,5 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
|
|
125 |
)
|
126 |
|
127 |
if __name__ == "__main__":
|
128 |
-
|
|
|
|
2 |
import gradio as gr
|
3 |
import google.generativeai as genai
|
4 |
import os
|
5 |
+
import logging
|
6 |
+
|
7 |
+
# ๋ก๊น
์ค์
|
8 |
+
logging.basicConfig(level=logging.INFO)
|
9 |
+
logger = logging.getLogger(__name__)
|
10 |
+
|
11 |
+
# ์์ ์ ํ๊ฒฝ๋ณ์ ํ์ธ
|
12 |
+
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
|
13 |
+
if GEMINI_API_KEY:
|
14 |
+
logger.info("API ํค๊ฐ ํ๊ฒฝ๋ณ์์์ ๊ฐ์ง๋์์ต๋๋ค.")
|
15 |
+
# ํค์ ์ผ๋ถ๋ง ํ์ (๋ณด์์ ์ํด)
|
16 |
+
logger.info(f"API ํค ๋ฏธ๋ฆฌ๋ณด๊ธฐ: {GEMINI_API_KEY[:8]}...")
|
17 |
+
else:
|
18 |
+
logger.warning("GEMINI_API_KEY๊ฐ ํ๊ฒฝ๋ณ์์์ ๊ฐ์ง๋์ง ์์์ต๋๋ค.")
|
19 |
|
20 |
# --- UI ๋ฐ ์ฑ๋ด ์ค๋ช
---
|
21 |
# Gradio Blocks๋ฅผ ์ฌ์ฉํ์ฌ ์ข ๋ ์ ์ฐํ UI๋ฅผ ๊ตฌ์ฑํฉ๋๋ค.
|
|
|
24 |
"""
|
25 |
# โ๏ธ Gemini API ์ฑ๋ด (Secrets ์ฌ์ฉ)
|
26 |
Google Gemini API๋ฅผ ์ฌ์ฉํ๋ ์ฑ๋ด์
๋๋ค.
|
27 |
+
|
28 |
+
**์ค์**: Hugging Face Spaces์ **Settings โ Repository secrets**์ `GEMINI_API_KEY`๊ฐ ์ค์ ๋์ด ์์ด์ผ ํฉ๋๋ค.
|
29 |
+
|
30 |
[API ํค ๋ฐ๊ธ๋ฐ๊ธฐ](https://aistudio.google.com/app/apikey)
|
31 |
+
|
32 |
+
**๋๋ฒ๊น
์ ๋ณด**:
|
33 |
"""
|
34 |
)
|
35 |
|
36 |
+
# ํ๊ฒฝ๋ณ์ ์ํ ํ์
|
37 |
+
with gr.Row():
|
38 |
+
env_status = gr.Textbox(
|
39 |
+
label="ํ๊ฒฝ๋ณ์ ์ํ",
|
40 |
+
value=f"GEMINI_API_KEY: {'โ
์ค์ ๋จ' if GEMINI_API_KEY else 'โ ์ค์ ๋์ง ์์'}",
|
41 |
+
interactive=False
|
42 |
+
)
|
43 |
+
|
44 |
# Gradio ์ฑ๋ด UI ์ปดํฌ๋ํธ
|
45 |
chatbot = gr.Chatbot(label="Gemini ์ฑ๋ด", height=600)
|
46 |
|
|
|
68 |
minimum=1, maximum=4096, value=1024, step=1, label="Max new tokens"
|
69 |
)
|
70 |
|
71 |
+
# ํ๊ฒฝ๋ณ์ ์๋ก๊ณ ์นจ ๋ฒํผ
|
72 |
+
refresh_button = gr.Button("๐ ํ๊ฒฝ๋ณ์ ์๋ก๊ณ ์นจ", size="sm")
|
73 |
+
|
74 |
+
def refresh_env_status():
|
75 |
+
"""ํ๊ฒฝ๋ณ์ ์ํ๋ฅผ ์๋ก๊ณ ์นจํฉ๋๋ค."""
|
76 |
+
global GEMINI_API_KEY
|
77 |
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
|
78 |
+
status = f"GEMINI_API_KEY: {'โ
์ค์ ๋จ' if GEMINI_API_KEY else 'โ ์ค์ ๋์ง ์์'}"
|
79 |
+
if GEMINI_API_KEY:
|
80 |
+
status += f" (๋ฏธ๋ฆฌ๋ณด๊ธฐ: {GEMINI_API_KEY[:8]}...)"
|
81 |
+
return status
|
82 |
+
|
83 |
+
refresh_button.click(refresh_env_status, outputs=[env_status])
|
84 |
|
85 |
+
# --- Gemini API ํธ์ถ ํจ์ ---
|
86 |
+
def respond(message, chat_history, system_prompt, temp, max_output_tokens):
|
87 |
+
# ํจ์๊ฐ ํธ์ถ๋ ๋๋ง๋ค ํ๊ฒฝ๋ณ์์์ API ํค๋ฅผ ๋ค์ ํ์ธ
|
88 |
+
api_key = os.environ.get("GEMINI_API_KEY")
|
89 |
+
|
90 |
+
# ๋๋ฒ๊น
์ ์ํ ์ถ๊ฐ ์ ๋ณด
|
91 |
+
logger.info(f"API ํค ํ์ธ: {'์์' if api_key else '์์'}")
|
92 |
+
|
93 |
# ํ๊ฒฝ๋ณ์์์ ๊ฐ์ ธ์จ API ํค๊ฐ ์์ผ๋ฉด ์๋ด ๋ฉ์์ง๋ฅผ ๋์๋๋ค.
|
94 |
+
if not api_key:
|
95 |
+
error_msg = """โ ๏ธ **์ค๋ฅ**: `GEMINI_API_KEY`๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.
|
96 |
+
|
97 |
+
**ํด๊ฒฐ ๋ฐฉ๋ฒ**:
|
98 |
+
1. Hugging Face Spaces์ **Settings** ํญ์ผ๋ก ์ด๋
|
99 |
+
2. **Repository secrets** ์น์
์ฐพ๊ธฐ
|
100 |
+
3. **New secret** ๋ฒํผ ํด๋ฆญ
|
101 |
+
4. Name: `GEMINI_API_KEY`, Value: ์ค์ API ํค ์
๋ ฅ
|
102 |
+
5. **Save** ํด๋ฆญ
|
103 |
+
6. Space๋ฅผ **์ฌ์์** (Settings โ Factory reboot)
|
104 |
+
|
105 |
+
**์ฐธ๊ณ **: Private space๊ฐ ์๋ ๊ฒฝ์ฐ์๋ secrets๋ ์์ ํ๊ฒ ๋ณดํธ๋ฉ๋๋ค."""
|
106 |
+
yield error_msg
|
107 |
return
|
108 |
|
109 |
try:
|
110 |
# API ํค๋ฅผ ์ค์ ํฉ๋๋ค.
|
111 |
+
genai.configure(api_key=api_key)
|
112 |
+
logger.info("API ํค ์ค์ ์ฑ๊ณต")
|
113 |
except Exception as e:
|
114 |
+
yield f"API ํค ์ค์ ์ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
115 |
return
|
116 |
|
117 |
# ์ฌ์ฉํ ๋ชจ๋ธ๊ณผ ์์คํ
ํ๋กฌํํธ๋ฅผ ์ค์ ํฉ๋๋ค.
|
118 |
+
try:
|
119 |
+
model = genai.GenerativeModel(
|
120 |
+
model_name='gemini-2.0-flash-exp', # ๋๋ 'gemini-1.5-flash'
|
121 |
+
system_instruction=system_prompt
|
122 |
+
)
|
123 |
+
except Exception as e:
|
124 |
+
yield f"๋ชจ๋ธ ์ด๊ธฐํ ์ค๋ฅ: {str(e)}\n์ฌ์ฉ ๊ฐ๋ฅํ ๋ชจ๋ธ: gemini-1.5-flash, gemini-1.5-pro"
|
125 |
+
return
|
126 |
|
127 |
# Gradio์ ๋ํ ๊ธฐ๋ก์ Gemini API๊ฐ ์ดํดํ ์ ์๋ ํ์์ผ๋ก ๋ณํํฉ๋๋ค.
|
128 |
gemini_history = []
|
|
|
158 |
|
159 |
except Exception as e:
|
160 |
# API ํธ์ถ ์ค ์๋ฌ๊ฐ ๋ฐ์ํ๋ฉด UI์ ํ์ํฉ๋๋ค.
|
161 |
+
error_detail = str(e)
|
162 |
+
if "API_KEY_INVALID" in error_detail:
|
163 |
+
yield "โ API ํค๊ฐ ์ ํจํ์ง ์์ต๋๋ค. ์ฌ๋ฐ๋ฅธ API ํค์ธ์ง ํ์ธํด์ฃผ์ธ์."
|
164 |
+
elif "QUOTA_EXCEEDED" in error_detail:
|
165 |
+
yield "โ API ์ฌ์ฉ๋ ํ๋๋ฅผ ์ด๊ณผํ์ต๋๋ค."
|
166 |
+
else:
|
167 |
+
yield f"์๋ต ์์ฑ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {error_detail}"
|
168 |
|
169 |
# --- Gradio ์ด๋ฒคํธ ๋ฆฌ์ค๋ ---
|
170 |
def on_submit(message, chat_history, system_prompt, temp, max_output_tokens):
|
171 |
+
if not message.strip():
|
172 |
+
return "", chat_history
|
173 |
+
|
174 |
chat_history.append((message, None))
|
175 |
bot_response_stream = respond(message, chat_history, system_prompt, temp, max_output_tokens)
|
176 |
|
|
|
190 |
)
|
191 |
|
192 |
if __name__ == "__main__":
|
193 |
+
# ๋๋ฒ๊น
๋ชจ๋๋ก ์คํ
|
194 |
+
demo.launch(debug=True)
|