Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,11 +3,6 @@ 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:
|
|
@@ -20,16 +15,11 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
|
|
| 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 |
-
# container=False ์์ฑ์ ์ ๊ฑฐํ์ฌ Gradio ๋ด๋ถ ์ค๋ฅ๋ฅผ ์์ ํฉ๋๋ค.
|
| 33 |
msg = gr.Textbox(
|
| 34 |
label="๋ฉ์์ง ์
๋ ฅ",
|
| 35 |
placeholder="๋ฌด์์ด๋ ๋ฌผ์ด๋ณด์ธ์...",
|
|
@@ -54,9 +44,15 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
|
|
| 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 |
-
|
|
|
|
|
|
|
| 60 |
return
|
| 61 |
|
| 62 |
try:
|
|
@@ -109,20 +105,14 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
|
|
| 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],
|
|
@@ -134,6 +124,5 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
|
|
| 134 |
[msg, chatbot]
|
| 135 |
)
|
| 136 |
|
| 137 |
-
# ์คํฌ๋ฆฝํธ๊ฐ ์ง์ ์คํ๋ ๋ Gradio ์ฑ์ ์คํํฉ๋๋ค.
|
| 138 |
if __name__ == "__main__":
|
| 139 |
demo.launch(debug=True)
|
|
|
|
| 3 |
import google.generativeai as genai
|
| 4 |
import os
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
# --- UI ๋ฐ ์ฑ๋ด ์ค๋ช
---
|
| 7 |
# Gradio Blocks๋ฅผ ์ฌ์ฉํ์ฌ ์ข ๋ ์ ์ฐํ UI๋ฅผ ๊ตฌ์ฑํฉ๋๋ค.
|
| 8 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
|
|
|
|
| 15 |
"""
|
| 16 |
)
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
# Gradio ์ฑ๋ด UI ์ปดํฌ๋ํธ
|
| 19 |
chatbot = gr.Chatbot(label="Gemini ์ฑ๋ด", height=600)
|
| 20 |
|
| 21 |
with gr.Row():
|
| 22 |
# ์ฌ์ฉ์ ๋ฉ์์ง ์
๋ ฅ๋
|
|
|
|
| 23 |
msg = gr.Textbox(
|
| 24 |
label="๋ฉ์์ง ์
๋ ฅ",
|
| 25 |
placeholder="๋ฌด์์ด๋ ๋ฌผ์ด๋ณด์ธ์...",
|
|
|
|
| 44 |
|
| 45 |
# --- Gemini API ํธ์ถ ํจ์ ---
|
| 46 |
def respond(message, chat_history, system_prompt, temp, max_output_tokens):
|
| 47 |
+
# ํจ์๊ฐ ํธ์ถ๋ ๋๋ง๋ค ํ๊ฒฝ๋ณ์์์ API ํค๋ฅผ ์ง์ ๊ฐ์ ธ์ต๋๋ค.
|
| 48 |
+
# ์ด๋ ๊ฒ ํ๋ฉด ์ฑ ์์ ์์ ์ ํค๋ฅผ ๋ชป ๋ถ๋ฌ์ค๋ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์์ต๋๋ค.
|
| 49 |
+
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
|
| 50 |
+
|
| 51 |
# ํ๊ฒฝ๋ณ์์์ ๊ฐ์ ธ์จ API ํค๊ฐ ์์ผ๋ฉด ์๋ด ๋ฉ์์ง๋ฅผ ๋์๋๋ค.
|
| 52 |
if not GEMINI_API_KEY:
|
| 53 |
+
# UI์ ์ง์ ๊ฒฝ๊ณ ๋ฅผ ํ์ํ๊ธฐ ์ํด gr.Warning์ ์ฌ์ฉํ ์ ์์ง๋ง,
|
| 54 |
+
# ์ฌ๊ธฐ์๋ ์ฑํ
์๋ต์ผ๋ก ์ฒ๋ฆฌํฉ๋๋ค.
|
| 55 |
+
yield "โ ๏ธ **์ค๋ฅ**: `GEMINI_API_KEY`๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.\n\nHugging Face Spaces์ **Settings > Repository secrets**์ API ํค๋ฅผ ์ถ๊ฐํ๋์ง ํ์ธํด์ฃผ์ธ์."
|
| 56 |
return
|
| 57 |
|
| 58 |
try:
|
|
|
|
| 105 |
yield f"์๋ต ์์ฑ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {e}"
|
| 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 |
|
|
|
|
| 112 |
for partial_response in bot_response_stream:
|
| 113 |
chat_history[-1] = (message, partial_response)
|
| 114 |
yield "", chat_history
|
| 115 |
|
|
|
|
|
|
|
| 116 |
msg.submit(
|
| 117 |
on_submit,
|
| 118 |
[msg, chatbot, system_message, temperature, max_tokens],
|
|
|
|
| 124 |
[msg, chatbot]
|
| 125 |
)
|
| 126 |
|
|
|
|
| 127 |
if __name__ == "__main__":
|
| 128 |
demo.launch(debug=True)
|