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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
import gradio as gr
|
3 |
+
import time
|
4 |
+
import logging
|
5 |
+
import random
|
6 |
+
import uuid
|
7 |
+
from datetime import datetime
|
8 |
+
from langdetect import detect, DetectorFactory
|
9 |
+
import google.generativeai as genai
|
10 |
+
from dotenv import load_dotenv
|
11 |
+
|
12 |
+
DetectorFactory.seed = 0
|
13 |
+
|
14 |
+
logging.basicConfig(
|
15 |
+
level=logging.INFO,
|
16 |
+
format='%(asctime)s - %(levelname)s - %(message)s'
|
17 |
+
)
|
18 |
+
logger = logging.getLogger(__name__)
|
19 |
+
|
20 |
+
# ํ๊ฒฝ ๋ณ์ ๋ก๋
|
21 |
+
load_dotenv()
|
22 |
+
|
23 |
+
# ํ๊ฒฝ๋ณ์์์ API ํค ์ค์ ๋ก๋
|
24 |
+
def get_gemini_api_configs():
|
25 |
+
"""ํ๊ฒฝ๋ณ์์์ Gemini API ํค ์ค์ ์ ๋ก๋"""
|
26 |
+
api_configs_str = os.getenv('GEMINI_API_CONFIGS', '')
|
27 |
+
|
28 |
+
if not api_configs_str:
|
29 |
+
logger.error("GEMINI_API_CONFIGS ํ๊ฒฝ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
30 |
+
return []
|
31 |
+
|
32 |
+
try:
|
33 |
+
# ํ๊ฒฝ๋ณ์ ๊ฐ์ exec๋ก ์คํํ์ฌ ์ค์ ๋ก๋
|
34 |
+
local_vars = {}
|
35 |
+
exec(api_configs_str, {}, local_vars)
|
36 |
+
|
37 |
+
return local_vars.get('API_KEYS_LIST', [])
|
38 |
+
except Exception as e:
|
39 |
+
logger.error(f"ํ๊ฒฝ๋ณ์ ํ์ฑ ์ค๋ฅ: {e}")
|
40 |
+
return []
|
41 |
+
|
42 |
+
# API ํค ๊ด๋ฆฌ๋ฅผ ์ํ ์ ์ญ ๋ณ์
|
43 |
+
api_key_manager = {
|
44 |
+
'keys': [],
|
45 |
+
'current_index': -1,
|
46 |
+
'failed_keys': set(), # ์คํจํ ํค๋ค์ ์ถ์
|
47 |
+
'is_initialized': False
|
48 |
+
}
|
49 |
+
|
50 |
+
# API ํค ๋ก๋ ํจ์ (๊ฐ์ ๋ ๋ฒ์ )
|
51 |
+
def load_api_keys():
|
52 |
+
"""ํ๊ฒฝ๋ณ์์์ API ํค๋ฅผ ๋ก๋ํฉ๋๋ค."""
|
53 |
+
# ํ๊ฒฝ๋ณ์์์ API ํค ๋ชฉ๋ก ๊ฐ์ ธ์ค๊ธฐ
|
54 |
+
api_keys_from_env = get_gemini_api_configs()
|
55 |
+
|
56 |
+
# ๋น ํค๋ ํ๋ ์ด์คํ๋ ์ ๊ฑฐ
|
57 |
+
api_keys = [
|
58 |
+
key.strip() for key in api_keys_from_env
|
59 |
+
if key and key.strip() and not key.startswith("YOUR_") and not key.startswith("your_")
|
60 |
+
]
|
61 |
+
|
62 |
+
# ์ค๋ณต ์ ๊ฑฐ
|
63 |
+
api_keys = list(dict.fromkeys(api_keys))
|
64 |
+
|
65 |
+
if not api_keys:
|
66 |
+
logger.error("API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. GEMINI_API_CONFIGS ํ๊ฒฝ๋ณ์์ ์ค์ API ํค๋ฅผ ์ถ๊ฐํ์ธ์.")
|
67 |
+
raise ValueError("API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. GEMINI_API_CONFIGS ํ๊ฒฝ๋ณ์์ ์ค์ API ํค๋ฅผ ์ถ๊ฐํด์ฃผ์ธ์.")
|
68 |
+
|
69 |
+
logger.info(f"์ด {len(api_keys)}๊ฐ์ API ํค๊ฐ ๋ก๋๋์์ต๋๋ค.")
|
70 |
+
return api_keys
|
71 |
+
|
72 |
+
def initialize_api_keys():
|
73 |
+
"""API ํค ๊ด๋ฆฌ์๋ฅผ ์ด๊ธฐํํฉ๋๋ค."""
|
74 |
+
global api_key_manager
|
75 |
+
|
76 |
+
if api_key_manager['is_initialized']:
|
77 |
+
return
|
78 |
+
|
79 |
+
try:
|
80 |
+
api_key_manager['keys'] = load_api_keys()
|
81 |
+
api_key_manager['is_initialized'] = True
|
82 |
+
logger.info("API ํค ๊ด๋ฆฌ์ ์ด๊ธฐํ ์๋ฃ")
|
83 |
+
except Exception as e:
|
84 |
+
logger.error(f"API ํค ์ด๊ธฐํ ์คํจ: {str(e)}")
|
85 |
+
raise e
|
86 |
+
|
87 |
+
def get_next_api_key():
|
88 |
+
"""๋ค์ ์ฌ์ฉํ API ํค๋ฅผ ๋ฐํํฉ๋๋ค. ์คํจํ ํค๋ ๊ฑด๋๋๋๋ค."""
|
89 |
+
global api_key_manager
|
90 |
+
|
91 |
+
# ์ด๊ธฐํ ํ์ธ
|
92 |
+
if not api_key_manager['is_initialized']:
|
93 |
+
initialize_api_keys()
|
94 |
+
|
95 |
+
available_keys = [
|
96 |
+
key for i, key in enumerate(api_key_manager['keys'])
|
97 |
+
if i not in api_key_manager['failed_keys']
|
98 |
+
]
|
99 |
+
|
100 |
+
if not available_keys:
|
101 |
+
# ๋ชจ๋ ํค๊ฐ ์คํจํ์ผ๋ฉด ์คํจ ๋ชฉ๋ก์ ์ด๊ธฐํํ๊ณ ๋ค์ ์๋
|
102 |
+
logger.warning("๋ชจ๋ API ํค๊ฐ ์คํจํ์ต๋๋ค. ์คํจ ๋ชฉ๋ก์ ์ด๊ธฐํํ๊ณ ๋ค์ ์๋ํฉ๋๋ค.")
|
103 |
+
api_key_manager['failed_keys'].clear()
|
104 |
+
available_keys = api_key_manager['keys']
|
105 |
+
|
106 |
+
# ์ฒซ ๋ฒ์งธ ์ฌ์ฉ์ ๋๋ค์ผ๋ก ์ ํ
|
107 |
+
if api_key_manager['current_index'] == -1:
|
108 |
+
available_indices = [
|
109 |
+
i for i, key in enumerate(api_key_manager['keys'])
|
110 |
+
if i not in api_key_manager['failed_keys']
|
111 |
+
]
|
112 |
+
api_key_manager['current_index'] = random.choice(available_indices)
|
113 |
+
logger.info(f"์ฒซ ๋ฒ์งธ API ํค ์ ํ: ๋๋ค ์ธ๋ฑ์ค {api_key_manager['current_index'] + 1}")
|
114 |
+
else:
|
115 |
+
# ์ดํ ์ฌ์ฉ์ ์์ฐจ์ ์ผ๋ก ๋ค์ ํค ์ ํ (์คํจํ ํค๋ ๊ฑด๋๋)
|
116 |
+
original_index = api_key_manager['current_index']
|
117 |
+
for _ in range(len(api_key_manager['keys'])):
|
118 |
+
api_key_manager['current_index'] = (api_key_manager['current_index'] + 1) % len(api_key_manager['keys'])
|
119 |
+
if api_key_manager['current_index'] not in api_key_manager['failed_keys']:
|
120 |
+
break
|
121 |
+
|
122 |
+
if api_key_manager['current_index'] in api_key_manager['failed_keys']:
|
123 |
+
# ๋ชจ๋ ํค๋ฅผ ์๋ํ์ง๋ง ์ฌ์ฉํ ์ ์๋ ํค๊ฐ ์์
|
124 |
+
logger.warning("์ฌ์ฉ ๊ฐ๋ฅํ API ํค๊ฐ ์์ต๋๋ค. ์คํจ ๋ชฉ๋ก์ ์ด๊ธฐํํฉ๋๋ค.")
|
125 |
+
api_key_manager['failed_keys'].clear()
|
126 |
+
api_key_manager['current_index'] = 0
|
127 |
+
|
128 |
+
logger.info(f"๋ค์ API ํค ์ ํ: ์ธ๋ฑ์ค {api_key_manager['current_index'] + 1}")
|
129 |
+
|
130 |
+
return api_key_manager['keys'][api_key_manager['current_index']]
|
131 |
+
|
132 |
+
def mark_api_key_failed(api_key):
|
133 |
+
"""API ํค๋ฅผ ์คํจ ๋ชฉ๋ก์ ์ถ๊ฐํฉ๋๋ค."""
|
134 |
+
global api_key_manager
|
135 |
+
|
136 |
+
try:
|
137 |
+
key_index = api_key_manager['keys'].index(api_key)
|
138 |
+
api_key_manager['failed_keys'].add(key_index)
|
139 |
+
logger.warning(f"API ํค ์ธ๋ฑ์ค {key_index + 1}๋ฅผ ์คํจ ๋ชฉ๋ก์ ์ถ๊ฐํ์ต๋๋ค.")
|
140 |
+
except ValueError:
|
141 |
+
logger.error("์คํจํ API ํค๋ฅผ ๋ชฉ๋ก์์ ์ฐพ์ ์ ์์ต๋๋ค.")
|
142 |
+
|
143 |
+
def test_api_key(api_key):
|
144 |
+
"""API ํค๊ฐ ์ ํจํ์ง ํ
์คํธํฉ๋๋ค."""
|
145 |
+
try:
|
146 |
+
genai.configure(api_key=api_key)
|
147 |
+
model = genai.GenerativeModel(model_name="gemini-2.0-flash")
|
148 |
+
|
149 |
+
# ๊ฐ๋จํ ํ
์คํธ ์์ฒญ
|
150 |
+
response = model.generate_content("Test", generation_config={
|
151 |
+
"max_output_tokens": 10,
|
152 |
+
"temperature": 0.1,
|
153 |
+
})
|
154 |
+
|
155 |
+
if response and response.text:
|
156 |
+
return True
|
157 |
+
return False
|
158 |
+
except Exception as e:
|
159 |
+
logger.error(f"API ํค ํ
์คํธ ์คํจ: {str(e)}")
|
160 |
+
return False
|
161 |
+
|
162 |
+
def get_working_api_key():
|
163 |
+
"""์๋ํ๋ API ํค๋ฅผ ์ฐพ์ ๋ฐํํฉ๋๋ค."""
|
164 |
+
max_attempts = len(api_key_manager['keys']) if api_key_manager['is_initialized'] else 5
|
165 |
+
|
166 |
+
for attempt in range(max_attempts):
|
167 |
+
try:
|
168 |
+
api_key = get_next_api_key()
|
169 |
+
if test_api_key(api_key):
|
170 |
+
logger.info(f"์๋ํ๋ API ํค๋ฅผ ์ฐพ์์ต๋๋ค. (์๋ {attempt + 1}ํ)")
|
171 |
+
return api_key
|
172 |
+
else:
|
173 |
+
mark_api_key_failed(api_key)
|
174 |
+
logger.warning(f"API ํค ํ
์คํธ ์คํจ. ๋ค์ ํค๋ก ์๋ํฉ๋๋ค. (์๋ {attempt + 1}ํ)")
|
175 |
+
except Exception as e:
|
176 |
+
logger.error(f"API ํค ๊ฐ์ ธ์ค๊ธฐ ์คํจ: {str(e)}")
|
177 |
+
|
178 |
+
raise Exception("์ฌ์ฉ ๊ฐ๋ฅํ API ํค๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.")
|
179 |
+
|
180 |
+
# ํ๊ตญ์ด ์์ฐ์ค๋ฝ๊ฒ ์กฐ๊ฑด (ํ-์ ๋ฒ์ญ์ฉ)
|
181 |
+
KO_EN_CONDITIONS = """
|
182 |
+
์ถ๊ฐ ์กฐ๊ฑด (ํ๊ตญ์ด ์์ฐ์ค๋ฝ๊ฒ ํ๊ธฐ):
|
183 |
+
- ๋ฒ์ญ์ฒด๊ฐ ์๋ ์์ฐ์ค๋ฝ๊ณ ๋งค๋๋ฌ์ด ํ๊ตญ์ด๋ฅผ ์ต์ฐ์ ์ ์ผ๋ก ์์ฑ
|
184 |
+
- ์๋ ์กฐ๊ฑด์ ๋ฐ๋ผ์ฃผ์ธ์:
|
185 |
+
1. ๋ฌธ๋ฒ์ ์ ํ์ฑ
|
186 |
+
- ํผ๋๋ฌธ๋ณด๋ค ๋ฅ๋๋ฌธ ์ฐ์
|
187 |
+
- ๋๋ช
์ฌ ์ต์ํ
|
188 |
+
- ๋ช
์ฌํ๋ณด๋ค ๋์ฌ/ํ์ฉ์ฌ ์ฐ์
|
189 |
+
- ํ์ฌ์งํํ๋ณด๋ค ๋จ์ํ์ฌ๋ ์๋ฃํ ์ฐ์
|
190 |
+
- ์ฃผ์ด-๋ชฉ์ ์ด-์์ ์ด ์์ ์ ์ง
|
191 |
+
- "~์์ด์" ํํ ์ง์
|
192 |
+
2. ์ฃผ์ ๋ฌธ๋งฅ์ ๋ง๋ ์์ฐ์ค๋ฌ์ด ํ๋ฆ
|
193 |
+
3. ์ฃผ์ ์ ์ํฉ์ ์ด์ธ๋ฆฌ๋ ์ดํ ์ ํ
|
194 |
+
4. ๋ฌธํ์ ์ ํฉ์ฑ ๊ณ ๋ ค
|
195 |
+
5. ์ ์์ ๋์์ค ๋ฐ์ (๊ณต๊ฐ ์ด๋๋ ํํ)
|
196 |
+
6. ์ง์ญ/์์ญ ๊ท ํ ์ ์ง
|
197 |
+
7. ์ฃผ์ ํค์๋ ๋ฐ๋ณต ์ธ๊ธ ๊ธ์ง
|
198 |
+
8. AI๊ฐ ์ด ๊ธ์ฒ๋ผ ๋ณด์ด์ง ์๋๋ก ์ฃผ์
|
199 |
+
"""
|
200 |
+
|
201 |
+
# ์์คํ
๋ฉ์์ง (ํ-์ค ๋ฒ์ญ์ฉ)
|
202 |
+
KO_ZH_SYSTEM_MESSAGE = """๋น์ ์ ํ๊ตญ์ด-์ค๊ตญ์ด ์๋ฐฉํฅ ๋ฒ์ญ ์ด์์คํดํธ์
๋๋ค.
|
203 |
+
์๋ ์กฐ๊ฑด์ ์ถฉ์คํ ๋ฐ๋ฅด์ธ์.
|
204 |
+
[๋ชฉ์ ๋ฐ ์ํฉ]
|
205 |
+
- ์ฌ์ฉ์ ๋ชฉ์ : 1688 ์์ฑ, ๋๋งค, ์ ํต ๊ด๋ จ๋ ์ํฉ์์ ์ธ์ด ์ฅ๋ฒฝ ์์ด ์ํํ ์ํต์ ๋๋๋ค.
|
206 |
+
- ์ฌ์ฉ์ ์
๋ ฅ์ด ํ๊ตญ์ด์ผ ๊ฒฝ์ฐ ์์ฐ์ค๋ฝ๊ณ ๋งฅ๋ฝ์ ๋ง๊ฒ ์ค๊ตญ์ด๋ก ๋ฒ์ญํ๋ค.
|
207 |
+
- ์ฌ์ฉ์ ์
๋ ฅ์ด ์ค๊ตญ์ด์ผ ๊ฒฝ์ฐ ์์ฐ์ค๋ฝ๊ณ ๋งฅ๋ฝ์ ๋ง๊ฒ ํ๊ตญ์ด๋ก ๋ฒ์ญํ๋ค.
|
208 |
+
[ํ๊ตญ์ด ์์ฐ์ค๋ฝ๊ฒ ์กฐ๊ฑด์ ๋ฆฌ]
|
209 |
+
1. ๋ฌธ๋ฒ์ ์ ํ์ฑ
|
210 |
+
- ํผ๋๋ฌธ ๋์ ๋ฅ๋๋ฌธ
|
211 |
+
- ๋๋ช
์ฌ ์ต์ํ
|
212 |
+
- ๋ช
์ฌํ๋ณด๋ค ๋์ฌ ๋ฐ ํ์ฉ์ฌ ์ฐ์
|
213 |
+
- ๋จ์ํ์ฌํ์ด๋ ์๋ฃํ ์ฐ์
|
214 |
+
- ๋ฌธ์ฅ ๊ตฌ์กฐ๋ ์ฃผ์ด-๋ชฉ์ ์ด-๋์ฌ
|
215 |
+
- "~์์ด์" ํํ ์ฌ์ฉ ์ง์
|
216 |
+
2. ์ฃผ์ ๋งฅ๋ฝ์ ๋ง๋ ์์ฐ์ค๋ฌ์ด ํํ
|
217 |
+
3. ์ ์ ํ ์ดํ ์ ํ(์์ฑ, ๋๋งค, ์ ํต ์ํฉ ๊ณ ๋ ค)
|
218 |
+
4. ๋ฌธํ์ , ์ํฉ์ ์ ํฉ์ฑ
|
219 |
+
5. ์ ์์ ๋์์ค ์กฐํ
|
220 |
+
6. ์ง์ญ๊ณผ ์์ญ์ ๊ท ํ
|
221 |
+
7. ์ฃผ์ ํค์๋ ๋ฐ๋ณต ๊ธ์ง
|
222 |
+
8. ์์ฑ AI ํฐ ๋์ง ์๋๋ก ์ฃผ์
|
223 |
+
[์ถ๊ฐ์ฌํญ]
|
224 |
+
- ๋ฒ์ญ ์ ์ด์ํ ๋ฒ์ญ์ฒด๋ฅผ ํผํ๊ณ ์์ฐ์ค๋ฌ์ด ํํ์ ์ฌ์ฉํ๋ค.
|
225 |
+
- ์ถ๋ ฅ์ ๋ฒ์ญ๋ ๊ฒฐ๊ณผ๋ฌธ๋ง ์ ์ํ๋ค.
|
226 |
+
"""
|
227 |
+
|
228 |
+
# 1์ฐจ ์นดํ
๊ณ ๋ฆฌ ๋ฐ 2์ฐจ ์ง๋ฌธ ๋ชฉ๋ก ์ ์
|
229 |
+
categories = {
|
230 |
+
"๊ธฐ๋ณธ ์ธ์ฌ ๋ฐ ๊ฑฐ๋ ์์": [
|
231 |
+
"์๋
ํ์ธ์, ๊ท์ฌ์ ์ ํ์ด ๋ง์์ ๋ญ๋๋ค. ์์ธํ ์ค๋ช
๋ถํ๋๋ฆฝ๋๋ค.",
|
232 |
+
"์ ํ์ด ํ์ฌ ์ฌ๊ณ ๊ฐ ์๋์?",
|
233 |
+
"๋๋งค ๊ฑฐ๋๋ฅผ ํ๊ณ ์ถ์ต๋๋ค. ๊ฐ๋ฅํ๊ฐ์?",
|
234 |
+
"์๋ก์ด ๊ณ ๊ฐ์ธ๋ฐ ๊ฑฐ๋๋ฅผ ์์ํ ์ ์์๊น์?",
|
235 |
+
"ํน์ ํ๊ตญ ๊ณ ๊ฐ๋ค๊ณผ๋ ๊ฑฐ๋๋ฅผ ํ๊ณ ๊ณ์ ๊ฐ์?",
|
236 |
+
"์ ํ์ ๋ํ ์์ธํ ์ค๋ช
์๋ ๋ธ๋ก์์ด๊ฐ ์๋์?",
|
237 |
+
"์ ํ์ด ์ธ๊ธฐ ์ํ์ธ๊ฐ์? ํ๋งค ๋ฐ์ดํฐ๋ฅผ ๊ณต์ ํ ์ ์๋์?",
|
238 |
+
"ํ๋งค ์ง์ญ์ด๋ ์ ํ์ด ์๋์?",
|
239 |
+
"์ด ์ ํ์ ํ์ฌ ์์ฅ ํธ๋ ๋๋ ์ด๋ค๊ฐ์?",
|
240 |
+
"ํ๋งค ๊ธฐ๋ก์ด ์๋ ์ ํ์ธ์ง ํ์ธํ ์ ์๋์?"
|
241 |
+
],
|
242 |
+
"์ ํ ์์ธ ๋ฐ ํ์ง ๊ด๋ จ ์ง๋ฌธ": [
|
243 |
+
"์ด ์ ํ์ ์ฃผ์ ๊ธฐ๋ฅ์ ๋ฌด์์ธ๊ฐ์?",
|
244 |
+
"ํ์ง ํ
์คํธ๋ฅผ ํต๊ณผํ ์ ํ์ธ๊ฐ์?",
|
245 |
+
"์ ํ์ ์์ฌ๋ฃ๋ ๋ฌด์์ธ๊ฐ์?",
|
246 |
+
"์์ฐ์ง๋ ์ด๋์ธ๊ฐ์?",
|
247 |
+
"์ ํ์ ์ฌ์ฉํ๊ธฐ์ ์์ ํ๊ฐ์?",
|
248 |
+
"์นํ๊ฒฝ ์ธ์ฆ์ ๋ฐ์ ์ ํ์ธ๊ฐ์?",
|
249 |
+
"์ ํ ์ฌ์ฉ ๊ธฐ๊ฐ(๋ด๊ตฌ์ฑ)์ ์ด๋ ์ ๋์ธ๊ฐ์?",
|
250 |
+
"๋ณด์ฆ ๊ธฐ๊ฐ์ ์ด๋ป๊ฒ ๋๋์?",
|
251 |
+
"์ฌ์ฉ ์ค ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ฉด ์ด๋ป๊ฒ ์ฒ๋ฆฌํด์ผ ํ๋์?",
|
252 |
+
"์ฌ์ฉ ์ค๋ช
์๊ฐ ํฌํจ๋์ด ์๋์?"
|
253 |
+
],
|
254 |
+
"๊ฐ๊ฒฉ ๋ฐ ๊ฒฐ์ ๊ด๋ จ ์ง๋ฌธ": [
|
255 |
+
"๋๋งค ๊ฐ๊ฒฉ์ ์ผ๋ง์ธ๊ฐ์?",
|
256 |
+
"๋๋ ๊ตฌ๋งค ์ ํ ์ธ์ด ๊ฐ๋ฅํ๊ฐ์?",
|
257 |
+
"์ฒซ ๊ฑฐ๋ ๊ณ ๊ฐ์ ์ํ ํน๋ณ ํํ์ด ์๋์?",
|
258 |
+
"๊ฒฐ์ ์กฐ๊ฑด์ ์๋ ค์ฃผ์ธ์.",
|
259 |
+
"๋ถ๋ถ ๊ฒฐ์ ํ ์๊ธ ๊ฒฐ์ ๊ฐ ๊ฐ๋ฅํ๊ฐ์?",
|
260 |
+
"๋๋ ์ฃผ๋ฌธ ์ ์ด๊ธฐ ๋ณด์ฆ๊ธ์ ์๊ตฌํ์๋์?",
|
261 |
+
"๋ฐฐ์ก๋ฃ ํฌํจ ๊ฐ๊ฒฉ์ธ๊ฐ์?",
|
262 |
+
"์ถ๊ฐ ๋น์ฉ์ด ๋ฐ์ํ ์ ์๋์?",
|
263 |
+
"ํ ์ธ ์ฟ ํฐ์ด๋ ํ๋ก๋ชจ์
์ฝ๋๋ ์๋์?",
|
264 |
+
"์ ๊ธฐ ๊ตฌ๋งค ๊ณ ๊ฐ์ ์ํ ํ ์ธ ์ ์ฑ
์ด ์๋์?"
|
265 |
+
],
|
266 |
+
"๋ฐฐ์ก ๋ฐ ๋ฌผ๋ฅ ๊ด๋ จ ์ง๋ฌธ": [
|
267 |
+
"๋ฐฐ์ก์ ์ด๋ ํ๋ฐฐ์ฌ๋ฅผ ํตํด ์งํ๋๋์?",
|
268 |
+
"์์ ๋ฐฐ์ก ๊ธฐ๊ฐ์ ์ผ๋ง๋ ๋๋์?",
|
269 |
+
"ํ๊ตญ๊น์ง ๋ฐฐ์ก์ด ๊ฐ๋ฅํ๊ฐ์?",
|
270 |
+
"๊ตญ์ ๋ฐฐ์ก๋ฃ๋ ์ด๋ป๊ฒ ๊ณ์ฐ๋๋์?",
|
271 |
+
"๋๋ ์ฃผ๋ฌธ ์ ๋ฐฐ์ก๋น ํ ์ธ ํํ์ด ์๋์?",
|
272 |
+
"๋ฐฐ์ก ์ค ํ์์ด ๋ฐ์ํ๋ฉด ์ด๋ป๊ฒ ์ฒ๋ฆฌํ๋์?",
|
273 |
+
"์ฃผ๋ฌธ ํ ๋ช ์ผ ๋ด์ ๋ฐฐ์ก์ด ์์๋๋์?",
|
274 |
+
"๋ฐฐ์ก ์ถ์ ๋ฒํธ๋ฅผ ์ ๊ณตํ์๋์?",
|
275 |
+
"์ฃผ๋ฌธ ์ํ๋ฅผ ์ด๋ป๊ฒ ํ์ธํ ์ ์๋์?",
|
276 |
+
"๋ฐฐ์ก๋ฃ๋ฅผ ์ง์ ๋ถ๋ดํด์ผ ํ๋์?"
|
277 |
+
]
|
278 |
+
}
|
279 |
+
|
280 |
+
def guess_lang(text: str) -> str:
|
281 |
+
text = text.strip()
|
282 |
+
if not text:
|
283 |
+
return ""
|
284 |
+
try:
|
285 |
+
lang = detect(text)
|
286 |
+
if lang in ['ko', 'en', 'zh-cn', 'zh-tw']:
|
287 |
+
if lang.startswith('zh'):
|
288 |
+
return 'zh'
|
289 |
+
return lang
|
290 |
+
else:
|
291 |
+
# langdetect ๊ฒฐ๊ณผ๊ฐ ko, en, zh๊ฐ ์๋ ๊ฒฝ์ฐ ํด๋ฆฌ์คํฑ ์ ์ฉ
|
292 |
+
if all('a' <= c.lower() <= 'z' for c in text if c.isalpha()):
|
293 |
+
return 'en'
|
294 |
+
# ํ๊ธ ์์ ๋ฒ์ ํ๋จ
|
295 |
+
elif any('\uac00' <= c <= '\ud7a3' for c in text):
|
296 |
+
return 'ko'
|
297 |
+
# ์ค๊ตญ์ด ๋ฒ์ ํ๋จ
|
298 |
+
elif any('\u4e00' <= c <= '\u9fff' for c in text):
|
299 |
+
return 'zh'
|
300 |
+
else:
|
301 |
+
# ๋ํดํธ ์์ด ์ฒ๋ฆฌ
|
302 |
+
return 'en'
|
303 |
+
except:
|
304 |
+
# langdetect ์คํจ ์ ํด๋ฆฌ์คํฑ ์ ์ฉ
|
305 |
+
if all('a' <= c.lower() <= 'z' for c in text if c.isalpha()):
|
306 |
+
return 'en'
|
307 |
+
elif any('\uac00' <= c <= '\ud7a3' for c in text):
|
308 |
+
return 'ko'
|
309 |
+
elif any('\u4e00' <= c <= '\u9fff' for c in text):
|
310 |
+
return 'zh'
|
311 |
+
else:
|
312 |
+
return 'en'
|
313 |
+
|
314 |
+
def validate_input(text: str, target_langs: list) -> bool:
|
315 |
+
lang = guess_lang(text)
|
316 |
+
logger.info(f"๊ฐ์ง๋(์ถ์ ) ์ธ์ด: {lang}")
|
317 |
+
return lang in target_langs
|
318 |
+
|
319 |
+
def translate_ko_en(input_text: str):
|
320 |
+
logger.info("ํ-์ ๋ฒ์ญ ์์")
|
321 |
+
|
322 |
+
if not validate_input(input_text, ['ko', 'en']):
|
323 |
+
return "์ ํจํ์ง ์์ ์
๋ ฅ์
๋๋ค. ํ๊ตญ์ด ๋๋ ์์ด ํ
์คํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์."
|
324 |
+
|
325 |
+
try:
|
326 |
+
detected_lang = guess_lang(input_text)
|
327 |
+
logger.info(f"๊ฐ์ง๋(์ถ์ ) ์ธ์ด: {detected_lang}")
|
328 |
+
|
329 |
+
if detected_lang == 'ko':
|
330 |
+
direction = "Korean to English"
|
331 |
+
input_lang = "Korean"
|
332 |
+
output_lang = "English"
|
333 |
+
else:
|
334 |
+
direction = "English to Korean"
|
335 |
+
input_lang = "English"
|
336 |
+
output_lang = "Korean"
|
337 |
+
|
338 |
+
logger.info(f"๋ฒ์ญ ๋ฐฉํฅ: {direction}")
|
339 |
+
|
340 |
+
current_time = int(time.time() * 1000)
|
341 |
+
random.seed(current_time)
|
342 |
+
temperature = random.uniform(0.4, 0.85)
|
343 |
+
top_p = random.uniform(0.9, 0.98)
|
344 |
+
|
345 |
+
request_id = str(uuid.uuid4())[:8]
|
346 |
+
timestamp_micro = int(time.time() * 1000000) % 1000
|
347 |
+
|
348 |
+
current_hour = datetime.now().hour
|
349 |
+
time_context = f"Consider the current time is {current_hour}:00."
|
350 |
+
|
351 |
+
# ํ๊ตญ์ด ์ถ๋ ฅ ์ ์์ฐ์ค๋ฌ์ด ํ๊ตญ์ด ์กฐ๊ฑด ์ถ๊ฐ
|
352 |
+
korean_conditions = ""
|
353 |
+
if output_lang == "Korean":
|
354 |
+
korean_conditions = KO_EN_CONDITIONS
|
355 |
+
|
356 |
+
system_prompt = "You are a helpful translator."
|
357 |
+
|
358 |
+
prompt = f"""
|
359 |
+
Translate the following {input_lang} text into {output_lang}:
|
360 |
+
{input_text}
|
361 |
+
Rules:
|
362 |
+
1. Output ONLY the {output_lang} translation without additional labels or formatting.
|
363 |
+
2. Preserve the original meaning and intent.
|
364 |
+
3. No explanations or meta-commentary.
|
365 |
+
4. No formatting beyond basic text.
|
366 |
+
5. {time_context}
|
367 |
+
[Seed: {current_time}]
|
368 |
+
{korean_conditions}
|
369 |
+
"""
|
370 |
+
|
371 |
+
logger.debug(f"ํ๋กฌํํธ:\n{prompt}")
|
372 |
+
|
373 |
+
# ์๋ํ๋ API ํค ๊ฐ์ ธ์ค๊ธฐ
|
374 |
+
api_key = get_working_api_key()
|
375 |
+
genai.configure(api_key=api_key)
|
376 |
+
|
377 |
+
# Gemini ๋ชจ๋ธ ์ค์
|
378 |
+
generation_config = {
|
379 |
+
"max_output_tokens": 200,
|
380 |
+
"temperature": temperature,
|
381 |
+
"top_p": top_p,
|
382 |
+
}
|
383 |
+
|
384 |
+
# Gemini ๋ชจ๋ธ ๋ถ๋ฌ์ค๊ธฐ
|
385 |
+
model = genai.GenerativeModel(
|
386 |
+
model_name="gemini-2.0-flash",
|
387 |
+
generation_config=generation_config,
|
388 |
+
)
|
389 |
+
|
390 |
+
# ๋ฒ์ญ ์คํ
|
391 |
+
full_prompt = f"{system_prompt}\n\n{prompt}"
|
392 |
+
response = model.generate_content(full_prompt)
|
393 |
+
|
394 |
+
translated = response.text.strip()
|
395 |
+
logger.info("Gemini ๋ฒ์ญ ์๋ฃ")
|
396 |
+
logger.debug(f"๋ฒ์ญ ๊ฒฐ๊ณผ:\n{translated}")
|
397 |
+
return translated
|
398 |
+
|
399 |
+
except Exception as e:
|
400 |
+
logger.error(f"๋ฒ์ญ ์ค ์์์น ๋ชปํ ์ค๋ฅ ๋ฐ์: {str(e)}")
|
401 |
+
# API ํค ๋ฌธ์ ์ธ ๊ฒฝ์ฐ ํด๋น ํค๋ฅผ ์คํจ ๋ชฉ๋ก์ ์ถ๊ฐ
|
402 |
+
if "api" in str(e).lower() or "key" in str(e).lower() or "quota" in str(e).lower():
|
403 |
+
try:
|
404 |
+
current_key = api_key_manager['keys'][api_key_manager['current_index']]
|
405 |
+
mark_api_key_failed(current_key)
|
406 |
+
logger.info("API ํค ๋ฌธ์ ๋ก ์ธํ ์ค๋ฅ. ๋ค์ ํค๋ก ์ฌ์๋ํ์ธ์.")
|
407 |
+
except:
|
408 |
+
pass
|
409 |
+
return f"๋ฒ์ญ ์ค ์์์น ๋ชปํ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
410 |
+
|
411 |
+
def translate_ko_zh(input_text: str):
|
412 |
+
logger.info("ํ-์ค ๋ฒ์ญ ์์")
|
413 |
+
|
414 |
+
if not validate_input(input_text, ['ko', 'zh']):
|
415 |
+
return "์ ํจํ์ง ์์ ์
๋ ฅ์
๋๋ค. ํ๊ตญ์ด ๋๋ ์ค๊ตญ์ด ํ
์คํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์."
|
416 |
+
|
417 |
+
try:
|
418 |
+
detected_lang = guess_lang(input_text)
|
419 |
+
logger.info(f"๊ฐ์ง๋(์ถ์ ) ์ธ์ด: {detected_lang}")
|
420 |
+
|
421 |
+
if detected_lang == 'ko':
|
422 |
+
direction = "Korean to Chinese"
|
423 |
+
input_lang = "Korean"
|
424 |
+
output_lang = "Chinese"
|
425 |
+
else:
|
426 |
+
direction = "Chinese to Korean"
|
427 |
+
input_lang = "Chinese"
|
428 |
+
output_lang = "Korean"
|
429 |
+
|
430 |
+
logger.info(f"๋ฒ์ญ ๋ฐฉํฅ: {direction}")
|
431 |
+
|
432 |
+
current_time = int(time.time() * 1000)
|
433 |
+
random.seed(current_time)
|
434 |
+
temperature = random.uniform(0.5, 0.8)
|
435 |
+
top_p = random.uniform(0.9, 0.98)
|
436 |
+
|
437 |
+
# ์๋ํ๋ API ํค ๊ฐ์ ธ์ค๊ธฐ
|
438 |
+
api_key = get_working_api_key()
|
439 |
+
genai.configure(api_key=api_key)
|
440 |
+
|
441 |
+
# Gemini ๋ชจ๋ธ ์ค์
|
442 |
+
generation_config = {
|
443 |
+
"max_output_tokens": 1024,
|
444 |
+
"temperature": temperature,
|
445 |
+
"top_p": top_p,
|
446 |
+
}
|
447 |
+
|
448 |
+
# Gemini ๋ชจ๋ธ ๋ถ๋ฌ์ค๊ธฐ
|
449 |
+
model = genai.GenerativeModel(
|
450 |
+
model_name="gemini-2.0-flash",
|
451 |
+
generation_config=generation_config,
|
452 |
+
)
|
453 |
+
|
454 |
+
# ํ๋กฌํํธ ์ค๋น
|
455 |
+
prompt = f"{KO_ZH_SYSTEM_MESSAGE}\n\n{input_text}"
|
456 |
+
|
457 |
+
# ๋ฒ์ญ ์คํ
|
458 |
+
response = model.generate_content(prompt)
|
459 |
+
translated = response.text.strip()
|
460 |
+
|
461 |
+
logger.info("Gemini ๋ฒ์ญ ์๋ฃ")
|
462 |
+
logger.debug(f"๋ฒ์ญ ๊ฒฐ๊ณผ:\n{translated}")
|
463 |
+
return translated
|
464 |
+
|
465 |
+
except Exception as e:
|
466 |
+
logger.error(f"๋ฒ์ญ ์ค ์์์น ๋ชปํ ์ค๋ฅ ๋ฐ์: {str(e)}")
|
467 |
+
# API ํค ๋ฌธ์ ์ธ ๊ฒฝ์ฐ ํด๋น ํค๋ฅผ ์คํจ ๋ชฉ๋ก์ ์ถ๊ฐ
|
468 |
+
if "api" in str(e).lower() or "key" in str(e).lower() or "quota" in str(e).lower():
|
469 |
+
try:
|
470 |
+
current_key = api_key_manager['keys'][api_key_manager['current_index']]
|
471 |
+
mark_api_key_failed(current_key)
|
472 |
+
logger.info("API ํค ๋ฌธ์ ๋ก ์ธํ ์ค๋ฅ. ๋ค์ ํค๋ก ์ฌ์๋ํ์ธ์.")
|
473 |
+
except:
|
474 |
+
pass
|
475 |
+
return f"๋ฒ์ญ ์ค ์์์น ๋ชปํ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
476 |
+
|
477 |
+
def update_subcategories(category):
|
478 |
+
if category in categories:
|
479 |
+
return gr.update(choices=categories[category], value=None)
|
480 |
+
else:
|
481 |
+
return gr.update(choices=[], value=None)
|
482 |
+
|
483 |
+
def set_input_text(selected_text):
|
484 |
+
return selected_text
|
485 |
+
|
486 |
+
# ์ปค์คํ
CSS ์คํ์ผ
|
487 |
+
custom_css = """
|
488 |
+
:root {
|
489 |
+
--primary-color: #FB7F0D;
|
490 |
+
--secondary-color: #ff9a8b;
|
491 |
+
--accent-color: #FF6B6B;
|
492 |
+
--background-color: #FFF3E9;
|
493 |
+
--card-bg: #ffffff;
|
494 |
+
--text-color: #334155;
|
495 |
+
--border-radius: 18px;
|
496 |
+
--shadow: 0 8px 30px rgba(251, 127, 13, 0.08);
|
497 |
+
}
|
498 |
+
body {
|
499 |
+
font-family: 'Pretendard', 'Noto Sans KR', -apple-system, BlinkMacSystemFont, sans-serif;
|
500 |
+
background-color: var(--background-color);
|
501 |
+
color: var(--text-color);
|
502 |
+
line-height: 1.6;
|
503 |
+
margin: 0;
|
504 |
+
padding: 0;
|
505 |
+
}
|
506 |
+
.gradio-container {
|
507 |
+
width: 100%;
|
508 |
+
margin: 0 auto;
|
509 |
+
padding: 20px;
|
510 |
+
background-color: var(--background-color);
|
511 |
+
}
|
512 |
+
.custom-header {
|
513 |
+
background: #FF7F00;
|
514 |
+
padding: 2rem;
|
515 |
+
border-radius: 15px;
|
516 |
+
margin-bottom: 20px;
|
517 |
+
box-shadow: var(--shadow);
|
518 |
+
text-align: center;
|
519 |
+
}
|
520 |
+
.custom-header h1 {
|
521 |
+
margin: 0;
|
522 |
+
font-size: 2.5rem;
|
523 |
+
font-weight: 700;
|
524 |
+
color: black;
|
525 |
+
}
|
526 |
+
.custom-header p {
|
527 |
+
margin: 10px 0 0;
|
528 |
+
font-size: 1.2rem;
|
529 |
+
color: black;
|
530 |
+
}
|
531 |
+
.custom-frame {
|
532 |
+
background-color: var(--card-bg);
|
533 |
+
border: 1px solid rgba(0, 0, 0, 0.04);
|
534 |
+
border-radius: var(--border-radius);
|
535 |
+
padding: 20px;
|
536 |
+
margin: 10px 0;
|
537 |
+
box-shadow: var(--shadow);
|
538 |
+
}
|
539 |
+
.custom-section-group {
|
540 |
+
margin-top: 20px;
|
541 |
+
padding: 0;
|
542 |
+
border: none;
|
543 |
+
border-radius: 0;
|
544 |
+
background-color: var(--background-color);
|
545 |
+
box-shadow: none !important;
|
546 |
+
}
|
547 |
+
.custom-button {
|
548 |
+
border-radius: 30px !important;
|
549 |
+
background: linear-gradient(135deg, var(--primary-color), var(--secondary-color)) !important;
|
550 |
+
color: white !important;
|
551 |
+
font-size: 18px !important;
|
552 |
+
padding: 10px 20px !important;
|
553 |
+
border: none;
|
554 |
+
box-shadow: 0 4px 8px rgba(251, 127, 13, 0.25);
|
555 |
+
transition: transform 0.3s ease;
|
556 |
+
}
|
557 |
+
.custom-button:hover {
|
558 |
+
transform: translateY(-2px);
|
559 |
+
box-shadow: 0 6px 12px rgba(251, 127, 13, 0.3);
|
560 |
+
}
|
561 |
+
.custom-title {
|
562 |
+
font-size: 28px;
|
563 |
+
font-weight: bold;
|
564 |
+
margin-bottom: 10px;
|
565 |
+
color: var(--text-color);
|
566 |
+
border-bottom: 2px solid var(--primary-color);
|
567 |
+
padding-bottom: 5px;
|
568 |
+
}
|
569 |
+
.image-container {
|
570 |
+
border-radius: var(--border-radius);
|
571 |
+
overflow: hidden;
|
572 |
+
border: 1px solid rgba(0, 0, 0, 0.08);
|
573 |
+
transition: all 0.3s ease;
|
574 |
+
background-color: white;
|
575 |
+
}
|
576 |
+
.image-container:hover {
|
577 |
+
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
|
578 |
+
}
|
579 |
+
.gr-input, .gr-text-input, .gr-sample-inputs {
|
580 |
+
border-radius: var(--border-radius) !important;
|
581 |
+
border: 1px solid #dddddd !important;
|
582 |
+
padding: 12px !important;
|
583 |
+
box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.05) !important;
|
584 |
+
transition: all 0.3s ease !important;
|
585 |
+
}
|
586 |
+
.gr-input:focus, .gr-text-input:focus {
|
587 |
+
border-color: var(--primary-color) !important;
|
588 |
+
outline: none !important;
|
589 |
+
box-shadow: 0 0 0 2px rgba(251, 127, 13, 0.2) !important;
|
590 |
+
}
|
591 |
+
::-webkit-scrollbar {
|
592 |
+
width: 8px;
|
593 |
+
height: 8px;
|
594 |
+
}
|
595 |
+
::-webkit-scrollbar-track {
|
596 |
+
background: rgba(0, 0, 0, 0.05);
|
597 |
+
border-radius: 10px;
|
598 |
+
}
|
599 |
+
::-webkit-scrollbar-thumb {
|
600 |
+
background: var(--primary-color);
|
601 |
+
border-radius: 10px;
|
602 |
+
}
|
603 |
+
@keyframes fadeIn {
|
604 |
+
from { opacity: 0; transform: translateY(10px); }
|
605 |
+
to { opacity: 1; transform: translateY(0); }
|
606 |
+
}
|
607 |
+
.fade-in {
|
608 |
+
animation: fadeIn 0.5s ease-out;
|
609 |
+
}
|
610 |
+
@media (max-width: 768px) {
|
611 |
+
.button-grid {
|
612 |
+
grid-template-columns: repeat(2, 1fr);
|
613 |
+
}
|
614 |
+
}
|
615 |
+
.section-title {
|
616 |
+
display: flex;
|
617 |
+
align-items: center;
|
618 |
+
font-size: 20px;
|
619 |
+
font-weight: 700;
|
620 |
+
color: #333333;
|
621 |
+
margin-bottom: 10px;
|
622 |
+
padding-bottom: 5px;
|
623 |
+
border-bottom: 2px solid #FB7F0D;
|
624 |
+
font-family: 'Pretendard', 'Noto Sans KR', -apple-system, BlinkMacSystemFont, sans-serif;
|
625 |
+
}
|
626 |
+
.section-title img {
|
627 |
+
margin-right: 10px;
|
628 |
+
width: 24px;
|
629 |
+
height: 24px;
|
630 |
+
}
|
631 |
+
.guide-container {
|
632 |
+
background-color: var(--card-bg);
|
633 |
+
border-radius: var(--border-radius);
|
634 |
+
box-shadow: var(--shadow);
|
635 |
+
padding: 1.5rem;
|
636 |
+
margin-bottom: 1.5rem;
|
637 |
+
border: 1px solid rgba(0, 0, 0, 0.04);
|
638 |
+
}
|
639 |
+
.guide-title {
|
640 |
+
font-size: 1.5rem;
|
641 |
+
font-weight: 700;
|
642 |
+
color: var(--primary-color);
|
643 |
+
margin-bottom: 1.5rem;
|
644 |
+
padding-bottom: 0.5rem;
|
645 |
+
border-bottom: 2px solid var(--primary-color);
|
646 |
+
display: flex;
|
647 |
+
align-items: center;
|
648 |
+
}
|
649 |
+
.guide-title i {
|
650 |
+
margin-right: 0.8rem;
|
651 |
+
font-size: 1.5rem;
|
652 |
+
}
|
653 |
+
.guide-item {
|
654 |
+
display: flex;
|
655 |
+
margin-bottom: 1rem;
|
656 |
+
align-items: flex-start;
|
657 |
+
}
|
658 |
+
.guide-number {
|
659 |
+
background-color: var(--primary-color);
|
660 |
+
color: white;
|
661 |
+
width: 25px;
|
662 |
+
height: 25px;
|
663 |
+
border-radius: 50%;
|
664 |
+
display: flex;
|
665 |
+
align-items: center;
|
666 |
+
justify-content: center;
|
667 |
+
font-weight: bold;
|
668 |
+
margin-right: 10px;
|
669 |
+
flex-shrink: 0;
|
670 |
+
}
|
671 |
+
.guide-text {
|
672 |
+
flex: 1;
|
673 |
+
line-height: 1.6;
|
674 |
+
}
|
675 |
+
.guide-text a {
|
676 |
+
color: var(--primary-color);
|
677 |
+
text-decoration: underline;
|
678 |
+
font-weight: 600;
|
679 |
+
}
|
680 |
+
.guide-text a:hover {
|
681 |
+
color: var(--accent-color);
|
682 |
+
}
|
683 |
+
.guide-highlight {
|
684 |
+
background-color: rgba(251, 127, 13, 0.1);
|
685 |
+
padding: 2px 5px;
|
686 |
+
border-radius: 4px;
|
687 |
+
font-weight: 500;
|
688 |
+
}
|
689 |
+
"""
|
690 |
+
|
691 |
+
# FontAwesome ์์ด์ฝ ํฌํจ
|
692 |
+
fontawesome_link = """
|
693 |
+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" crossorigin="anonymous" referrerpolicy="no-referrer" />
|
694 |
+
"""
|
695 |
+
|
696 |
+
def create_interface():
|
697 |
+
with gr.Blocks(css=custom_css, theme=gr.themes.Soft(
|
698 |
+
primary_hue=gr.themes.Color(
|
699 |
+
c50="#FFF7ED",
|
700 |
+
c100="#FFEDD5",
|
701 |
+
c200="#FED7AA",
|
702 |
+
c300="#FDBA74",
|
703 |
+
c400="#FB923C",
|
704 |
+
c500="#F97316",
|
705 |
+
c600="#EA580C",
|
706 |
+
c700="#C2410C",
|
707 |
+
c800="#9A3412",
|
708 |
+
c900="#7C2D12",
|
709 |
+
c950="#431407",
|
710 |
+
),
|
711 |
+
secondary_hue="zinc",
|
712 |
+
neutral_hue="zinc",
|
713 |
+
font=("Pretendard", "sans-serif")
|
714 |
+
)) as demo:
|
715 |
+
|
716 |
+
gr.HTML(fontawesome_link)
|
717 |
+
|
718 |
+
with gr.Tabs() as tabs:
|
719 |
+
with gr.TabItem("ํ๊ตญ์ด-์ค๊ตญ์ด ๋ฒ์ญ"):
|
720 |
+
with gr.Column(elem_classes="custom-frame"):
|
721 |
+
gr.HTML('<div class="section-title"><img src="https://cdn-icons-png.flaticon.com/512/3097/3097412.png"> ์นดํ
๊ณ ๋ฆฌ ์ ํ</div>')
|
722 |
+
with gr.Row():
|
723 |
+
category_dropdown = gr.Dropdown(
|
724 |
+
label="1์ฐจ ์นดํ
๊ณ ๋ฆฌ ์ ํ",
|
725 |
+
choices=list(categories.keys()),
|
726 |
+
value=None
|
727 |
+
)
|
728 |
+
subcategory_dropdown = gr.Dropdown(
|
729 |
+
label="2์ฐจ ์ง๋ฌธ ์ ํ",
|
730 |
+
choices=[],
|
731 |
+
value=None
|
732 |
+
)
|
733 |
+
|
734 |
+
with gr.Column(elem_classes="custom-frame"):
|
735 |
+
gr.HTML('<div class="section-title"><img src="https://cdn-icons-png.flaticon.com/512/4297/4297825.png"> ๋ฒ์ญ</div>')
|
736 |
+
with gr.Row():
|
737 |
+
with gr.Column(scale=1):
|
738 |
+
kz_input_box = gr.Textbox(
|
739 |
+
label="์
๋ ฅ (ํ๊ตญ์ด ๋๋ ์ค๊ตญ์ด)",
|
740 |
+
lines=8,
|
741 |
+
placeholder="๋ฒ์ญํ ํ
์คํธ๋ฅผ ์
๋ ฅํ์ธ์..."
|
742 |
+
)
|
743 |
+
with gr.Column(scale=1):
|
744 |
+
kz_output_box = gr.Textbox(
|
745 |
+
label="๋ฒ์ญ ๊ฒฐ๊ณผ",
|
746 |
+
lines=8
|
747 |
+
)
|
748 |
+
|
749 |
+
with gr.Column(elem_classes="custom-frame"):
|
750 |
+
gr.HTML('<div class="section-title"><img src="https://cdn-icons-png.flaticon.com/512/1022/1022293.png"> ๋ฒ์ญ ์คํ</div>')
|
751 |
+
kz_translate_button = gr.Button("๋ฒ์ญํ๊ธฐ", elem_classes="custom-button")
|
752 |
+
|
753 |
+
# 1์ฐจ ์ ํ ์ 2์ฐจ ํญ๋ชฉ ์
๋ฐ์ดํธ
|
754 |
+
category_dropdown.change(
|
755 |
+
fn=update_subcategories,
|
756 |
+
inputs=category_dropdown,
|
757 |
+
outputs=subcategory_dropdown
|
758 |
+
)
|
759 |
+
|
760 |
+
# 2์ฐจ ์ ํ ์ ์
๋ ฅ๋์ ํด๋น ๊ฐ ์๋ ์
๋ ฅ
|
761 |
+
subcategory_dropdown.change(
|
762 |
+
fn=set_input_text,
|
763 |
+
inputs=subcategory_dropdown,
|
764 |
+
outputs=kz_input_box
|
765 |
+
)
|
766 |
+
|
767 |
+
# ๋ฒ์ญ ํจ์
|
768 |
+
kz_translate_button.click(
|
769 |
+
fn=translate_ko_zh,
|
770 |
+
inputs=kz_input_box,
|
771 |
+
outputs=kz_output_box
|
772 |
+
)
|
773 |
+
|
774 |
+
with gr.TabItem("ํ๊ตญ์ด-์์ด ๋ฒ์ญ"):
|
775 |
+
with gr.Column(elem_classes="custom-frame"):
|
776 |
+
gr.HTML('<div class="section-title"><img src="https://cdn-icons-png.flaticon.com/512/4297/4297825.png"> ๋ฒ์ญ</div>')
|
777 |
+
with gr.Row():
|
778 |
+
with gr.Column(scale=1):
|
779 |
+
ke_input_box = gr.Textbox(
|
780 |
+
label="์
๋ ฅ ํ
์คํธ (ํ๊ตญ์ด ๋๋ ์์ด)",
|
781 |
+
placeholder="๋ฒ์ญํ ํ
์คํธ๋ฅผ ์
๋ ฅํ์ธ์...",
|
782 |
+
lines=8
|
783 |
+
)
|
784 |
+
with gr.Column(scale=1):
|
785 |
+
ke_output_box = gr.Textbox(
|
786 |
+
label="๋ฒ์ญ ๊ฒฐ๊ณผ",
|
787 |
+
lines=8,
|
788 |
+
interactive=False
|
789 |
+
)
|
790 |
+
|
791 |
+
with gr.Column(elem_classes="custom-frame"):
|
792 |
+
gr.HTML('<div class="section-title"><img src="https://cdn-icons-png.flaticon.com/512/1022/1022293.png"> ๋ฒ์ญ ์คํ</div>')
|
793 |
+
ke_translate_button = gr.Button("๋ฒ์ญํ๊ธฐ", elem_classes="custom-button")
|
794 |
+
|
795 |
+
# ๋ฒ์ญ ํจ์
|
796 |
+
ke_translate_button.click(
|
797 |
+
fn=translate_ko_en,
|
798 |
+
inputs=ke_input_box,
|
799 |
+
outputs=ke_output_box
|
800 |
+
)
|
801 |
+
|
802 |
+
return demo
|
803 |
+
|
804 |
+
if __name__ == "__main__":
|
805 |
+
demo = create_interface()
|
806 |
+
demo.launch()
|