| | """ |
| | Gemini ์ฑ๋ด - Gradio ๋ฒ์ |
| | Google Search๋ก ์ต์ ์ ๋ณด๋ฅผ ๊ฒ์ํ๋ AI ์ฑ๋ด |
| | """ |
| |
|
| | import os |
| | import json |
| | import datetime |
| | from google import genai |
| | from google.genai import types |
| | import gradio as gr |
| |
|
| | |
| | |
| | |
| |
|
| | def send_message(user_message, chat_history, api_key, system_prompt, use_search): |
| | """์ฌ์ฉ์ ๋ฉ์์ง๋ฅผ Gemini์๊ฒ ๋ณด๋ด๊ณ ์๋ต์ ๋ฐ์ต๋๋ค.""" |
| | |
| | if not api_key or not api_key.strip(): |
| | chat_history.append({"role": "user", "content": user_message}) |
| | chat_history.append({"role": "assistant", "content": "โ ๏ธ API Key๋ฅผ ์
๋ ฅํด์ฃผ์ธ์."}) |
| | return "", chat_history |
| |
|
| | |
| | if not user_message or not user_message.strip(): |
| | return "", chat_history |
| |
|
| | |
| | client = genai.Client(api_key=api_key.strip()) |
| |
|
| | |
| | today = datetime.date.today().strftime("%Y-%m-%d") |
| | base_instruction = f"์ค๋ ๋ ์ง๋ {today}์
๋๋ค. ์ต์ ์ ๋ณด๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ต๋ณํ์ธ์." |
| | if system_prompt and system_prompt.strip(): |
| | full_instruction = f"{base_instruction}\n{system_prompt.strip()}" |
| | else: |
| | full_instruction = base_instruction |
| |
|
| | |
| | tools = [] |
| | if use_search: |
| | tools = [types.Tool(google_search=types.GoogleSearch())] |
| |
|
| | |
| | config = types.GenerateContentConfig( |
| | system_instruction=full_instruction, |
| | tools=tools if tools else None, |
| | ) |
| |
|
| | |
| | contents = [] |
| | for msg in chat_history: |
| | content = msg["content"] |
| | if isinstance(content, list): |
| | content = "".join(part["text"] for part in content if "text" in part) |
| | role = "user" if msg["role"] == "user" else "model" |
| | contents.append(types.Content(role=role, parts=[types.Part(text=content)])) |
| | contents.append(types.Content(role="user", parts=[types.Part(text=user_message)])) |
| |
|
| | |
| | try: |
| | response = client.models.generate_content( |
| | model="gemini-2.5-flash-lite", |
| | contents=contents, |
| | config=config, |
| | ) |
| | reply = response.text |
| |
|
| | except Exception as e: |
| | reply = f"โ ์ค๋ฅ ๋ฐ์: {str(e)}" |
| |
|
| | |
| | chat_history.append({"role": "user", "content": user_message}) |
| | chat_history.append({"role": "assistant", "content": reply}) |
| |
|
| | return "", chat_history |
| |
|
| |
|
| | |
| | |
| | |
| |
|
| | def save_chat(chat_history): |
| | """๋ํ ๊ธฐ๋ก์ JSON ํ์ผ๋ก ์ ์ฅํฉ๋๋ค.""" |
| | if not chat_history: |
| | return gr.update(value=None) |
| |
|
| | now = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") |
| | filename = f"chat_{now}.json" |
| | filepath = os.path.join("saved_chats", filename) |
| |
|
| | os.makedirs("saved_chats", exist_ok=True) |
| |
|
| | with open(filepath, "w", encoding="utf-8") as f: |
| | json.dump(chat_history, f, ensure_ascii=False, indent=2) |
| |
|
| | return gr.update(value=filepath) |
| |
|
| |
|
| | def clear_chat(): |
| | """๋ํ ๊ธฐ๋ก์ ์ด๊ธฐํํฉ๋๋ค.""" |
| | return [] |
| |
|
| |
|
| | |
| | |
| | |
| |
|
| | with gr.Blocks( |
| | title="Gemini ์ฑ๋ด", |
| | ) as app: |
| |
|
| | gr.Markdown("# Gemini ์ฑ๋ด") |
| | gr.Markdown("Google Gemini API๋ฅผ ์ฌ์ฉํ๋ AI ์ฑ๋ด์
๋๋ค.") |
| |
|
| | with gr.Row(): |
| |
|
| | |
| | with gr.Column(scale=1): |
| | gr.Markdown("## ์ค์ ") |
| |
|
| | api_key = gr.Textbox( |
| | label="Gemini API Key", |
| | placeholder="API Key๋ฅผ ์
๋ ฅํ์ธ์", |
| | type="password", |
| | ) |
| |
|
| | system_prompt = gr.Textbox( |
| | label="์์คํ
ํ๋กฌํํธ", |
| | placeholder="์: ๋น์ ์ ์น์ ํ ํ๊ตญ์ด AI ๋น์์
๋๋ค.", |
| | lines=3, |
| | ) |
| |
|
| | use_search = gr.Checkbox( |
| | label="Google Search ์ฌ์ฉ", |
| | value=True, |
| | ) |
| |
|
| | gr.Markdown("---") |
| | gr.Markdown("## ๋ํ ๊ด๋ฆฌ") |
| |
|
| | save_btn = gr.Button("๋ํ ์ ์ฅ", variant="secondary") |
| | save_output = gr.File(label="์ ์ฅ๋ ํ์ผ") |
| |
|
| | |
| | with gr.Column(scale=3): |
| | chatbot = gr.Chatbot( |
| | label="๋ํ", |
| | height=500, |
| | ) |
| |
|
| | with gr.Row(): |
| | msg = gr.Textbox( |
| | label="๋ฉ์์ง ์
๋ ฅ", |
| | placeholder="๋ฉ์์ง๋ฅผ ์
๋ ฅํ์ธ์...", |
| | scale=4, |
| | show_label=False, |
| | ) |
| | send_btn = gr.Button("์ ์ก", variant="primary", scale=1) |
| |
|
| | clear_btn = gr.Button("๋ํ ์ด๊ธฐํ") |
| |
|
| | |
| |
|
| | send_btn.click( |
| | fn=send_message, |
| | inputs=[msg, chatbot, api_key, system_prompt, use_search], |
| | outputs=[msg, chatbot], |
| | ) |
| |
|
| | msg.submit( |
| | fn=send_message, |
| | inputs=[msg, chatbot, api_key, system_prompt, use_search], |
| | outputs=[msg, chatbot], |
| | ) |
| |
|
| | clear_btn.click(fn=clear_chat, outputs=[chatbot]) |
| | save_btn.click(fn=save_chat, inputs=[chatbot], outputs=[save_output]) |
| |
|
| |
|
| | |
| | |
| | |
| |
|
| | if __name__ == "__main__": |
| | app.launch(server_name="0.0.0.0", server_port=7860) |
| |
|