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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Gemini ์ฑ๋ด - Gradio ๋ฒ์
|
| 3 |
+
Google Search๋ก ์ต์ ์ ๋ณด๋ฅผ ๊ฒ์ํ๋ AI ์ฑ๋ด
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
import json
|
| 8 |
+
import datetime
|
| 9 |
+
from google import genai
|
| 10 |
+
from google.genai import types
|
| 11 |
+
import gradio as gr
|
| 12 |
+
|
| 13 |
+
# ============================================================
|
| 14 |
+
# 1. Gemini์๊ฒ ๋ฉ์์ง ๋ณด๋ด๊ธฐ
|
| 15 |
+
# ============================================================
|
| 16 |
+
|
| 17 |
+
def send_message(user_message, chat_history, api_key, system_prompt, use_search):
|
| 18 |
+
"""์ฌ์ฉ์ ๋ฉ์์ง๋ฅผ Gemini์๊ฒ ๋ณด๋ด๊ณ ์๋ต์ ๋ฐ์ต๋๋ค."""
|
| 19 |
+
# API Key ํ์ธ
|
| 20 |
+
if not api_key or not api_key.strip():
|
| 21 |
+
chat_history.append({"role": "user", "content": user_message})
|
| 22 |
+
chat_history.append({"role": "assistant", "content": "โ ๏ธ API Key๋ฅผ ์
๋ ฅํด์ฃผ์ธ์."})
|
| 23 |
+
return "", chat_history
|
| 24 |
+
|
| 25 |
+
# ๋น ๋ฉ์์ง ํ์ธ
|
| 26 |
+
if not user_message or not user_message.strip():
|
| 27 |
+
return "", chat_history
|
| 28 |
+
|
| 29 |
+
# ํด๋ผ์ด์ธํธ ์์ฑ
|
| 30 |
+
client = genai.Client(api_key=api_key.strip())
|
| 31 |
+
|
| 32 |
+
# ํ์ฌ ๋ ์ง๋ฅผ ์์คํ
ํ๋กฌํํธ์ ์ถ๊ฐ
|
| 33 |
+
today = datetime.date.today().strftime("%Y-%m-%d")
|
| 34 |
+
base_instruction = f"์ค๋ ๋ ์ง๋ {today}์
๋๋ค. ์ต์ ์ ๋ณด๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ต๋ณํ์ธ์."
|
| 35 |
+
if system_prompt and system_prompt.strip():
|
| 36 |
+
full_instruction = f"{base_instruction}\n{system_prompt.strip()}"
|
| 37 |
+
else:
|
| 38 |
+
full_instruction = base_instruction
|
| 39 |
+
|
| 40 |
+
# ๋๊ตฌ ์ค์ (Google Search)
|
| 41 |
+
tools = []
|
| 42 |
+
if use_search:
|
| 43 |
+
tools = [types.Tool(google_search=types.GoogleSearch())]
|
| 44 |
+
|
| 45 |
+
# ์ค์
|
| 46 |
+
config = types.GenerateContentConfig(
|
| 47 |
+
system_instruction=full_instruction,
|
| 48 |
+
tools=tools if tools else None,
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# ๋ํ ๊ธฐ๋ก์ Gemini ํ์์ผ๋ก ๋ณํ
|
| 52 |
+
contents = []
|
| 53 |
+
for msg in chat_history:
|
| 54 |
+
content = msg["content"]
|
| 55 |
+
if isinstance(content, list):
|
| 56 |
+
content = "".join(part["text"] for part in content if "text" in part)
|
| 57 |
+
role = "user" if msg["role"] == "user" else "model"
|
| 58 |
+
contents.append(types.Content(role=role, parts=[types.Part(text=content)]))
|
| 59 |
+
contents.append(types.Content(role="user", parts=[types.Part(text=user_message)]))
|
| 60 |
+
|
| 61 |
+
# ๋ฉ์์ง ์ ์ก
|
| 62 |
+
try:
|
| 63 |
+
response = client.models.generate_content(
|
| 64 |
+
model="gemini-2.5-flash-lite",
|
| 65 |
+
contents=contents,
|
| 66 |
+
config=config,
|
| 67 |
+
)
|
| 68 |
+
reply = response.text
|
| 69 |
+
|
| 70 |
+
except Exception as e:
|
| 71 |
+
reply = f"โ ์ค๋ฅ ๋ฐ์: {str(e)}"
|
| 72 |
+
|
| 73 |
+
# ๋ํ ๊ธฐ๋ก์ ์ถ๊ฐ
|
| 74 |
+
chat_history.append({"role": "user", "content": user_message})
|
| 75 |
+
chat_history.append({"role": "assistant", "content": reply})
|
| 76 |
+
|
| 77 |
+
return "", chat_history
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
# ============================================================
|
| 81 |
+
# 2. ๋ํ ๊ธฐ๋ก ์ ์ฅ
|
| 82 |
+
# ============================================================
|
| 83 |
+
|
| 84 |
+
def save_chat(chat_history):
|
| 85 |
+
"""๋ํ ๊ธฐ๋ก์ JSON ํ์ผ๋ก ์ ์ฅํฉ๋๋ค."""
|
| 86 |
+
if not chat_history:
|
| 87 |
+
return gr.update(value=None)
|
| 88 |
+
|
| 89 |
+
now = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 90 |
+
filename = f"chat_{now}.json"
|
| 91 |
+
filepath = os.path.join("saved_chats", filename)
|
| 92 |
+
|
| 93 |
+
os.makedirs("saved_chats", exist_ok=True)
|
| 94 |
+
|
| 95 |
+
with open(filepath, "w", encoding="utf-8") as f:
|
| 96 |
+
json.dump(chat_history, f, ensure_ascii=False, indent=2)
|
| 97 |
+
|
| 98 |
+
return gr.update(value=filepath)
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
def clear_chat():
|
| 102 |
+
"""๋ํ ๊ธฐ๋ก์ ์ด๊ธฐํํฉ๋๋ค."""
|
| 103 |
+
return []
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
# ============================================================
|
| 107 |
+
# 3. Gradio UI ๋ง๋ค๊ธฐ
|
| 108 |
+
# ============================================================
|
| 109 |
+
|
| 110 |
+
with gr.Blocks(
|
| 111 |
+
title="Gemini ์ฑ๋ด",
|
| 112 |
+
) as app:
|
| 113 |
+
|
| 114 |
+
gr.Markdown("# Gemini ์ฑ๋ด")
|
| 115 |
+
gr.Markdown("Google Gemini API๋ฅผ ์ฌ์ฉํ๋ AI ์ฑ๋ด์
๋๋ค.")
|
| 116 |
+
|
| 117 |
+
with gr.Row():
|
| 118 |
+
|
| 119 |
+
# --- ์ผ์ชฝ: ์ค์ ํจ๋ ---
|
| 120 |
+
with gr.Column(scale=1):
|
| 121 |
+
gr.Markdown("## ์ค์ ")
|
| 122 |
+
|
| 123 |
+
api_key = gr.Textbox(
|
| 124 |
+
label="Gemini API Key",
|
| 125 |
+
placeholder="API Key๋ฅผ ์
๋ ฅํ์ธ์",
|
| 126 |
+
type="password",
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
system_prompt = gr.Textbox(
|
| 130 |
+
label="์์คํ
ํ๋กฌํํธ",
|
| 131 |
+
placeholder="์: ๋น์ ์ ์น์ ํ ํ๊ตญ์ด AI ๋น์์
๋๋ค.",
|
| 132 |
+
lines=3,
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
use_search = gr.Checkbox(
|
| 136 |
+
label="Google Search ์ฌ์ฉ",
|
| 137 |
+
value=True,
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
gr.Markdown("---")
|
| 141 |
+
gr.Markdown("## ๋ํ ๊ด๋ฆฌ")
|
| 142 |
+
|
| 143 |
+
save_btn = gr.Button("๋ํ ์ ์ฅ", variant="secondary")
|
| 144 |
+
save_output = gr.File(label="์ ์ฅ๋ ํ์ผ")
|
| 145 |
+
|
| 146 |
+
# --- ์ค๋ฅธ์ชฝ: ์ฑํ
์์ญ ---
|
| 147 |
+
with gr.Column(scale=3):
|
| 148 |
+
chatbot = gr.Chatbot(
|
| 149 |
+
label="๋ํ",
|
| 150 |
+
height=500,
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
with gr.Row():
|
| 154 |
+
msg = gr.Textbox(
|
| 155 |
+
label="๋ฉ์์ง ์
๋ ฅ",
|
| 156 |
+
placeholder="๋ฉ์์ง๋ฅผ ์
๋ ฅํ์ธ์...",
|
| 157 |
+
scale=4,
|
| 158 |
+
show_label=False,
|
| 159 |
+
)
|
| 160 |
+
send_btn = gr.Button("์ ์ก", variant="primary", scale=1)
|
| 161 |
+
|
| 162 |
+
clear_btn = gr.Button("๋ํ ์ด๊ธฐํ")
|
| 163 |
+
|
| 164 |
+
# --- ์ด๋ฒคํธ ์ฐ๊ฒฐ ---
|
| 165 |
+
|
| 166 |
+
send_btn.click(
|
| 167 |
+
fn=send_message,
|
| 168 |
+
inputs=[msg, chatbot, api_key, system_prompt, use_search],
|
| 169 |
+
outputs=[msg, chatbot],
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
msg.submit(
|
| 173 |
+
fn=send_message,
|
| 174 |
+
inputs=[msg, chatbot, api_key, system_prompt, use_search],
|
| 175 |
+
outputs=[msg, chatbot],
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
clear_btn.click(fn=clear_chat, outputs=[chatbot])
|
| 179 |
+
save_btn.click(fn=save_chat, inputs=[chatbot], outputs=[save_output])
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
# ============================================================
|
| 183 |
+
# 4. ์ฑ ์คํ
|
| 184 |
+
# ============================================================
|
| 185 |
+
|
| 186 |
+
if __name__ == "__main__":
|
| 187 |
+
app.launch(server_name="0.0.0.0", server_port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
google-genai
|
| 2 |
+
gradio
|