Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,8 +26,8 @@ class AIService:
|
|
| 26 |
prefix = "GEMINI_API_KEY_" if "Gemini" in engine else "GROQ_API_KEY_" if "Llama" in engine else "SEA_LION_API_KEY"
|
| 27 |
keys = AIService.get_all_keys(prefix)
|
| 28 |
label = engine.split(" ")[-1]
|
| 29 |
-
#
|
| 30 |
-
status_label = "แแแแถแแแถแ
|
| 31 |
html = f"<div style='display: flex; gap: 8px; align-items: center; margin-bottom: 10px;'><b style='color: #94a3b8; font-size: 12px;'>{label} {status_label}:</b>"
|
| 32 |
for k in keys:
|
| 33 |
state = key_status_registry.get(k, "ready")
|
|
@@ -71,7 +71,7 @@ class AIService:
|
|
| 71 |
headers={"Authorization": f"Bearer {key}"},
|
| 72 |
json={
|
| 73 |
"model": "aisingapore/Gemma-SEA-LION-v4-27B-IT",
|
| 74 |
-
"messages": [{"role": "system", "content": "You are an expert translator.
|
| 75 |
{"role": "user", "content": prompt}],
|
| 76 |
"temperature": 0.7
|
| 77 |
}, timeout=60)
|
|
@@ -85,16 +85,16 @@ class AIService:
|
|
| 85 |
def translator_hub(text, target_lang, engine):
|
| 86 |
if not text.strip(): return "", AIService.get_status_html(engine, target_lang)
|
| 87 |
|
| 88 |
-
# แแแแขแถแแแแแแแแถแแถแแพแแแแธแแแแพแแ
AI
|
| 89 |
lang_name = re.sub(r'[^\w\s]', '', target_lang).strip()
|
| 90 |
is_srt = bool(re.search(r'\d+\n\d{2}:\d{2}:\d{2}', text))
|
| 91 |
fmt = "SRT" if is_srt else "plain text"
|
| 92 |
|
| 93 |
-
# Prompt แแแแถแแแแ แฑแแแแถ Detect แแถแแถแแพแแแแแฝแแฏแ แแทแแแแแแแแแ
แแถแแถแแแแแ
แแแแแแ
แแแแถแ
|
| 94 |
instruction = (
|
| 95 |
-
f"Detect
|
| 96 |
-
f"
|
| 97 |
-
f"
|
|
|
|
|
|
|
| 98 |
)
|
| 99 |
|
| 100 |
prompt = f"{instruction}\n\nCONTENT:\n{text}"
|
|
@@ -107,19 +107,6 @@ def translator_hub(text, target_lang, engine):
|
|
| 107 |
|
| 108 |
return "โ Error", AIService.get_status_html(engine, target_lang)
|
| 109 |
|
| 110 |
-
def update_ui(lang):
|
| 111 |
-
# แแแแผแ Interface แแถแแแถแแถแแแแแแแพแแแพแ
|
| 112 |
-
if "Khmer" in lang:
|
| 113 |
-
return "แแแผแแแ AI", "แขแแแแแแแพแ", "โก แแแแแแแฅแกแผแ", "แแแแแแแแแแแแ", "๐ แ
แแแแ", "๐๏ธ แแแแขแถแ"
|
| 114 |
-
elif "English" in lang:
|
| 115 |
-
return "AI Model", "Original Content", "โก Translate Now", "Translation Result", "๐ Copy", "๐๏ธ Clear"
|
| 116 |
-
elif "Chinese" in lang:
|
| 117 |
-
return "AI ๆจกๅ", "ๅๆๅ
ๅฎน", "โก ็ซๅณ็ฟป่ฏ", "็ฟป่ฏ็ปๆ", "๐ ๅคๅถ", "๐๏ธ ๆธ
้ค"
|
| 118 |
-
elif "Thai" in lang:
|
| 119 |
-
return "เนเธกเนเธเธฅ AI", "เนเธเธทเนเธญเธซเธฒเธเนเธเธแเธฑเธ", "โก เนเธเธฅแฅแกแผแ", "เธเธฅเธฅเธฑเธเธเนแแถแเนเธเธฅ", "๐ เธเธฑเธเธฅเธญเธ", "๐๏ธ เธฅเนเธฒเธ"
|
| 120 |
-
else: # Default for Japanese/Korean
|
| 121 |
-
return "AI Model", "Original", "โก Translate", "Result", "๐ Copy", "๐๏ธ Clear"
|
| 122 |
-
|
| 123 |
css = """
|
| 124 |
body { background-color: #0d1117; }
|
| 125 |
.btn-trans { background: linear-gradient(90deg, #238636, #2ea043) !important; color: white !important; border: none !important; }
|
|
@@ -128,8 +115,8 @@ body { background-color: #0d1117; }
|
|
| 128 |
textarea { resize: both !important; }
|
| 129 |
"""
|
| 130 |
|
| 131 |
-
with gr.Blocks(title="SRT
|
| 132 |
-
gr.Markdown("<h1 style='text-align: center; color: #58a6ff;'>๐ฌ
|
| 133 |
|
| 134 |
with gr.Row():
|
| 135 |
with gr.Column(scale=1):
|
|
@@ -137,21 +124,19 @@ with gr.Blocks(title="SRT Multi-Pro", css=css) as demo:
|
|
| 137 |
["๐ฐ๐ญ Khmer", "๐บ๐ธ English", "๐จ๐ณ Chinese", "๐น๐ญ Thai", "๐ฏ๐ต Japanese", "๐ฐ๐ท Korean"],
|
| 138 |
value="๐ฐ๐ญ Khmer", label="Target Language (แแถแแถแแแแแ
)"
|
| 139 |
)
|
| 140 |
-
engine_opt = gr.Radio(["๐ Gemini", "๐ฆ Llama", "๐ฆ SEA-LION"], value="๐ Gemini", label="แแแผแแแ
|
| 141 |
status_ui = gr.HTML(AIService.get_status_html("๐ Gemini", "๐ฐ๐ญ Khmer"))
|
| 142 |
-
input_box = gr.Textbox(label="แขแแแแแแแพแ", lines=12, placeholder="Paste text here
|
| 143 |
|
| 144 |
with gr.Row():
|
| 145 |
-
btn_clear = gr.Button("๐๏ธ แแแแขแถแ", variant="secondary", elem_classes="btn-clear")
|
| 146 |
-
btn_trans = gr.Button("โก แแแแแแ
|
| 147 |
|
| 148 |
with gr.Column(scale=1):
|
| 149 |
-
output_box = gr.Textbox(label="แแแแแแ
|
| 150 |
-
btn_copy = gr.Button("๐ แ
แแแแ
|
| 151 |
|
| 152 |
-
#
|
| 153 |
-
lang_opt.change(update_ui, lang_opt, [engine_opt, input_box, btn_trans, output_box, btn_copy, btn_clear])
|
| 154 |
-
|
| 155 |
btn_trans.click(translator_hub, [input_box, lang_opt, engine_opt], [output_box, status_ui])
|
| 156 |
btn_clear.click(lambda: ("", ""), None, [input_box, output_box])
|
| 157 |
btn_copy.click(None, output_box, js="(v) => { navigator.clipboard.writeText(v); alert('Copied!'); }")
|
|
|
|
| 26 |
prefix = "GEMINI_API_KEY_" if "Gemini" in engine else "GROQ_API_KEY_" if "Llama" in engine else "SEA_LION_API_KEY"
|
| 27 |
keys = AIService.get_all_keys(prefix)
|
| 28 |
label = engine.split(" ")[-1]
|
| 29 |
+
# แแแแ แถแแแแแถแแแถแแแถ แข แแถแแถแแพแแแแธแแปแแฑแแแแทแแถแ Refresh
|
| 30 |
+
status_label = "แแแแถแแแถแ/Status"
|
| 31 |
html = f"<div style='display: flex; gap: 8px; align-items: center; margin-bottom: 10px;'><b style='color: #94a3b8; font-size: 12px;'>{label} {status_label}:</b>"
|
| 32 |
for k in keys:
|
| 33 |
state = key_status_registry.get(k, "ready")
|
|
|
|
| 71 |
headers={"Authorization": f"Bearer {key}"},
|
| 72 |
json={
|
| 73 |
"model": "aisingapore/Gemma-SEA-LION-v4-27B-IT",
|
| 74 |
+
"messages": [{"role": "system", "content": "You are an expert translator. Preserve proper names. Output ONLY translation."},
|
| 75 |
{"role": "user", "content": prompt}],
|
| 76 |
"temperature": 0.7
|
| 77 |
}, timeout=60)
|
|
|
|
| 85 |
def translator_hub(text, target_lang, engine):
|
| 86 |
if not text.strip(): return "", AIService.get_status_html(engine, target_lang)
|
| 87 |
|
|
|
|
| 88 |
lang_name = re.sub(r'[^\w\s]', '', target_lang).strip()
|
| 89 |
is_srt = bool(re.search(r'\d+\n\d{2}:\d{2}:\d{2}', text))
|
| 90 |
fmt = "SRT" if is_srt else "plain text"
|
| 91 |
|
|
|
|
| 92 |
instruction = (
|
| 93 |
+
f"1. Detect source language and translate to {lang_name}.\n"
|
| 94 |
+
f"2. IMPORTANT: DO NOT translate proper names or character names (e.g., Chang Sheng, Xiao Fan). Keep them in their original Latin/English characters.\n"
|
| 95 |
+
f"3. Maintain {fmt} format strictly.\n"
|
| 96 |
+
f"4. If {lang_name} is Khmer, use natural family pronouns (แแ, แขแผแ, แแแถแแ, แแแถ, แฏแ).\n"
|
| 97 |
+
f"5. Output ONLY the translation. No chat, no explanations."
|
| 98 |
)
|
| 99 |
|
| 100 |
prompt = f"{instruction}\n\nCONTENT:\n{text}"
|
|
|
|
| 107 |
|
| 108 |
return "โ Error", AIService.get_status_html(engine, target_lang)
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
css = """
|
| 111 |
body { background-color: #0d1117; }
|
| 112 |
.btn-trans { background: linear-gradient(90deg, #238636, #2ea043) !important; color: white !important; border: none !important; }
|
|
|
|
| 115 |
textarea { resize: both !important; }
|
| 116 |
"""
|
| 117 |
|
| 118 |
+
with gr.Blocks(title="SRT Pro (No Refresh)", css=css) as demo:
|
| 119 |
+
gr.Markdown("<h1 style='text-align: center; color: #58a6ff;'>๐ฌ SMART TRANSLATOR PRO</h1>")
|
| 120 |
|
| 121 |
with gr.Row():
|
| 122 |
with gr.Column(scale=1):
|
|
|
|
| 124 |
["๐ฐ๐ญ Khmer", "๐บ๐ธ English", "๐จ๐ณ Chinese", "๐น๐ญ Thai", "๐ฏ๐ต Japanese", "๐ฐ๐ท Korean"],
|
| 125 |
value="๐ฐ๐ญ Khmer", label="Target Language (แแถแแถแแแแแ
)"
|
| 126 |
)
|
| 127 |
+
engine_opt = gr.Radio(["๐ Gemini", "๐ฆ Llama", "๐ฆ SEA-LION"], value="๐ Gemini", label="AI Model (แแแผแแแ)")
|
| 128 |
status_ui = gr.HTML(AIService.get_status_html("๐ Gemini", "๐ฐ๐ญ Khmer"))
|
| 129 |
+
input_box = gr.Textbox(label="Original Content (แขแแแแแแแพแ)", lines=12, placeholder="Paste text here...")
|
| 130 |
|
| 131 |
with gr.Row():
|
| 132 |
+
btn_clear = gr.Button("๐๏ธ Clear (แแแแขแถแ)", variant="secondary", elem_classes="btn-clear")
|
| 133 |
+
btn_trans = gr.Button("โก Translate (แแแแแแ)", variant="primary", elem_classes="btn-trans")
|
| 134 |
|
| 135 |
with gr.Column(scale=1):
|
| 136 |
+
output_box = gr.Textbox(label="Result (แแแแแแ)", lines=23, interactive=False)
|
| 137 |
+
btn_copy = gr.Button("๐ Copy Result (แ
แแแแ)", elem_classes="btn-copy")
|
| 138 |
|
| 139 |
+
# Events: แแแแผแแแถแแถแแทแ Refresh UI แแ แแบแแแแถแแแแแแแแผแแแแแแแแแแแถแแแแแแแแ
|
|
|
|
|
|
|
| 140 |
btn_trans.click(translator_hub, [input_box, lang_opt, engine_opt], [output_box, status_ui])
|
| 141 |
btn_clear.click(lambda: ("", ""), None, [input_box, output_box])
|
| 142 |
btn_copy.click(None, output_box, js="(v) => { navigator.clipboard.writeText(v); alert('Copied!'); }")
|