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