Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,139 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
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 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
-
)
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
1 |
+
# νμν λΌμ΄λΈλ¬λ¦¬λ₯Ό κ°μ Έμ΅λλ€.
|
2 |
import gradio as gr
|
3 |
+
import google.generativeai as genai
|
4 |
+
import os
|
5 |
|
6 |
+
# --- Hugging Face Spacesμ Secrets λλ λ‘컬 νκ²½λ³μμμ API ν€λ₯Ό κ°μ Έμ΅λλ€. ---
|
7 |
+
# μ΄ μ±μ Spacesμ λ°°ν¬ν λλ 'Settings' -> 'Repository secrets'μ
|
8 |
+
# 'GEMINI_API_KEY'λΌλ μ΄λ¦μΌλ‘ ν€λ₯Ό μ μ₯ν΄μΌ ν©λλ€.
|
9 |
+
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
|
10 |
|
11 |
+
# --- UI λ° μ±λ΄ μ€λͺ
---
|
12 |
+
# Gradio Blocksλ₯Ό μ¬μ©νμ¬ μ’ λ μ μ°ν UIλ₯Ό ꡬμ±ν©λλ€.
|
13 |
+
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
|
14 |
+
gr.Markdown(
|
15 |
+
"""
|
16 |
+
# βοΈ Gemini API μ±λ΄ (Secrets μ¬μ©)
|
17 |
+
Google Gemini APIλ₯Ό μ¬μ©νλ μ±λ΄μ
λλ€.
|
18 |
+
Hugging Face Spacesμ 'Settings' νμ μλ 'Repository secrets'μ `GEMINI_API_KEY`κ° μ€μ λμ΄ μμ΄μΌ ν©λλ€.
|
19 |
+
[API ν€ λ°κΈλ°κΈ°](https://aistudio.google.com/app/apikey)
|
20 |
+
"""
|
21 |
+
)
|
22 |
+
|
23 |
+
# API ν€κ° μ€μ λμλμ§ νμΈνκ³ μλ΄ λ©μμ§λ₯Ό νμν©λλ€.
|
24 |
+
if not GEMINI_API_KEY:
|
25 |
+
gr.Warning("β οΈ Gemini API ν€κ° μ€μ λμ§ μμμ΅λλ€. Hugging Face Spacesμ 'Repository secrets'μ GEMINI_API_KEYλ₯Ό μΆκ°ν΄μ£ΌμΈμ.")
|
26 |
|
27 |
+
# Gradio μ±λ΄ UI μ»΄ν¬λνΈ
|
28 |
+
chatbot = gr.Chatbot(label="Gemini μ±λ΄", height=600)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
with gr.Row():
|
31 |
+
# μ¬μ©μ λ©μμ§ μ
λ ₯λ
|
32 |
+
msg = gr.Textbox(
|
33 |
+
label="λ©μμ§ μ
λ ₯",
|
34 |
+
placeholder="무μμ΄λ λ¬Όμ΄λ³΄μΈμ...",
|
35 |
+
container=False,
|
36 |
+
scale=7,
|
37 |
+
)
|
38 |
+
# μ μ‘ λ²νΌ
|
39 |
+
submit_button = gr.Button("μ μ‘", variant="primary", scale=1)
|
40 |
|
41 |
+
with gr.Accordion("κ³ κΈ μ€μ ", open=False):
|
42 |
+
# LLMμ μν μ μ μνλ μμ€ν
λ©μμ§
|
43 |
+
system_message = gr.Textbox(
|
44 |
+
value="You are a helpful and friendly chatbot.", label="μμ€ν
λ©μμ§"
|
45 |
+
)
|
46 |
+
# λͺ¨λΈμ μ°½μμ±μ μ‘°μ νλ μ¬λΌμ΄λ
|
47 |
+
temperature = gr.Slider(
|
48 |
+
minimum=0.0, maximum=1.0, value=0.7, step=0.1, label="Temperature"
|
49 |
+
)
|
50 |
+
# μμ±ν μ΅λ ν ν° μλ₯Ό μ‘°μ νλ μ¬λΌμ΄λ
|
51 |
+
max_tokens = gr.Slider(
|
52 |
+
minimum=1, maximum=4096, value=1024, step=1, label="Max new tokens"
|
53 |
+
)
|
54 |
|
55 |
+
# --- Gemini API νΈμΆ ν¨μ ---
|
56 |
+
def respond(message, chat_history, system_prompt, temp, max_output_tokens):
|
57 |
+
# νκ²½λ³μμμ κ°μ Έμ¨ API ν€κ° μμΌλ©΄ μλ΄ λ©μμ§λ₯Ό λμλλ€.
|
58 |
+
if not GEMINI_API_KEY:
|
59 |
+
yield "Google API ν€κ° μ€μ λμ§ μμμ΅λλ€. κ΄λ¦¬μμκ² λ¬ΈμνμΈμ."
|
60 |
+
return
|
61 |
|
62 |
+
try:
|
63 |
+
# API ν€λ₯Ό μ€μ ν©λλ€.
|
64 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
65 |
+
except Exception as e:
|
66 |
+
yield f"API ν€ μ€μ μ μ€λ₯κ° λ°μνμ΅λλ€: {e}"
|
67 |
+
return
|
68 |
+
|
69 |
+
# μ¬μ©ν λͺ¨λΈκ³Ό μμ€ν
ν둬ννΈλ₯Ό μ€μ ν©λλ€.
|
70 |
+
model = genai.GenerativeModel(
|
71 |
+
model_name='gemini-2.0-flash', # μ΅μ Flash λͺ¨λΈ μ¬μ©
|
72 |
+
system_instruction=system_prompt
|
73 |
+
)
|
74 |
|
75 |
+
# Gradioμ λν κΈ°λ‘μ Gemini APIκ° μ΄ν΄ν μ μλ νμμΌλ‘ λ³νν©λλ€.
|
76 |
+
gemini_history = []
|
77 |
+
for user_msg, model_msg in chat_history:
|
78 |
+
if user_msg:
|
79 |
+
gemini_history.append({"role": "user", "parts": [user_msg]})
|
80 |
+
if model_msg:
|
81 |
+
gemini_history.append({"role": "model", "parts": [model_msg]})
|
82 |
+
|
83 |
+
# μ΄μ λν κΈ°λ‘μ λ°νμΌλ‘ μ±ν
μΈμ
μ μμν©λλ€.
|
84 |
+
chat = model.start_chat(history=gemini_history)
|
85 |
|
86 |
+
# λͺ¨λΈ μμ± κ΄λ ¨ μ€μ μ ꡬμ±ν©λλ€.
|
87 |
+
generation_config = genai.types.GenerationConfig(
|
88 |
+
temperature=temp,
|
89 |
+
max_output_tokens=int(max_output_tokens),
|
90 |
+
)
|
91 |
|
92 |
+
try:
|
93 |
+
# μ€νΈλ¦¬λ° λ°©μμΌλ‘ λ©μμ§λ₯Ό 보λ΄κ³ μλ΅μ λ°μ΅λλ€.
|
94 |
+
response = chat.send_message(
|
95 |
+
message,
|
96 |
+
stream=True,
|
97 |
+
generation_config=generation_config
|
98 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
+
# μ€νΈλ¦¬λ° μλ΅μ μ€μκ°μΌλ‘ UIμ νμν©λλ€.
|
101 |
+
full_response = ""
|
102 |
+
for chunk in response:
|
103 |
+
if hasattr(chunk, 'text'):
|
104 |
+
full_response += chunk.text
|
105 |
+
yield full_response
|
106 |
|
107 |
+
except Exception as e:
|
108 |
+
# API νΈμΆ μ€ μλ¬κ° λ°μνλ©΄ UIμ νμν©λλ€.
|
109 |
+
yield f"μλ΅ μμ± μ€ μ€λ₯κ° λ°μνμ΅λλ€: {e}"
|
110 |
+
|
111 |
+
# --- Gradio μ΄λ²€νΈ 리μ€λ ---
|
112 |
+
# μ¬μ©μκ° λ©μμ§λ₯Ό μ
λ ₯νκ³ 'μ μ‘' λ²νΌμ λλ₯΄κ±°λ μν°λ₯Ό μ³€μ λ μ€νλ λ‘μ§
|
113 |
+
def on_submit(message, chat_history, system_prompt, temp, max_output_tokens):
|
114 |
+
# λν κΈ°λ‘μ μ¬μ©μ λ©μμ§λ₯Ό μΆκ°ν©λλ€.
|
115 |
+
chat_history.append((message, None))
|
116 |
+
# μ€νΈλ¦¬λ° μλ΅μ μν΄ λΉ λ¬Έμμ΄λ‘ μλ΅μ μμν©λλ€.
|
117 |
+
bot_response_stream = respond(message, chat_history, system_prompt, temp, max_output_tokens)
|
118 |
+
|
119 |
+
# μ€νΈλ¦¬λ° μλ΅μ UIμ μ
λ°μ΄νΈν©λλ€.
|
120 |
+
for partial_response in bot_response_stream:
|
121 |
+
chat_history[-1] = (message, partial_response)
|
122 |
+
yield "", chat_history
|
123 |
+
|
124 |
+
# λ©μμ§ μ μ‘(λ²νΌ ν΄λ¦ λλ μν°) μ on_submit ν¨μλ₯Ό νΈμΆν©λλ€.
|
125 |
+
# UIμμ API ν€ μ
λ ₯λμ΄ μ κ±°λμμΌλ―λ‘, μ
λ ₯(inputs) 리μ€νΈμμλ μ μΈν©λλ€.
|
126 |
+
msg.submit(
|
127 |
+
on_submit,
|
128 |
+
[msg, chatbot, system_message, temperature, max_tokens],
|
129 |
+
[msg, chatbot]
|
130 |
+
)
|
131 |
+
submit_button.click(
|
132 |
+
on_submit,
|
133 |
+
[msg, chatbot, system_message, temperature, max_tokens],
|
134 |
+
[msg, chatbot]
|
135 |
+
)
|
136 |
+
|
137 |
+
# μ€ν¬λ¦½νΈκ° μ§μ μ€νλ λ Gradio μ±μ μ€νν©λλ€.
|
138 |
if __name__ == "__main__":
|
139 |
+
demo.launch(debug=True)
|