Spaces:
Sleeping
Sleeping
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)
|