jonghhhh commited on
Commit
37ec542
ยท
1 Parent(s): ceed636

Initial deploy

Browse files
Files changed (3) hide show
  1. .gitignore +6 -0
  2. app.py +187 -0
  3. requirements.txt +2 -0
.gitignore ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ .venv/
2
+ __pycache__/
3
+ .env
4
+ *.pyc
5
+ saved_chats/
6
+ .ipynb_checkpoints/
app.py ADDED
@@ -0,0 +1,187 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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