Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,8 +3,9 @@ import requests
|
|
| 3 |
import gradio as gr
|
| 4 |
import re
|
| 5 |
import random
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
key_status_registry = {}
|
| 9 |
|
| 10 |
class AIService:
|
|
@@ -24,192 +25,128 @@ class AIService:
|
|
| 24 |
|
| 25 |
@staticmethod
|
| 26 |
def get_status_html(engine):
|
| 27 |
-
if "Gemini" in engine
|
| 28 |
-
elif "Llama" in engine: prefix = "GROQ_API_KEY_"
|
| 29 |
-
else: prefix = "SEA_LION_API_KEY"
|
| 30 |
-
|
| 31 |
keys = AIService.get_all_keys(prefix)
|
| 32 |
label = engine.split(" ")[-1]
|
| 33 |
-
html = f"<div style='display: flex; gap:
|
| 34 |
-
for
|
| 35 |
state = key_status_registry.get(k, "ready")
|
| 36 |
color = "#22c55e" if state == "ready" else "#ef4444"
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
html += "</div>"
|
| 40 |
-
return html
|
| 41 |
|
| 42 |
@staticmethod
|
| 43 |
-
def
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
try:
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
"
|
| 51 |
-
"generationConfig": {"temperature": 0.75}
|
| 52 |
-
}, timeout=30)
|
| 53 |
-
if res.status_code == 200:
|
| 54 |
-
key_status_registry[key] = "ready"
|
| 55 |
-
return res.json()['candidates'][0]['content']['parts'][0]['text'].strip()
|
| 56 |
-
key_status_registry[key] = "dead"
|
| 57 |
-
except: key_status_registry[key] = "dead"; continue
|
| 58 |
-
return None
|
| 59 |
-
|
| 60 |
-
@staticmethod
|
| 61 |
-
def translate_llama(prompt):
|
| 62 |
-
keys = AIService.get_all_keys("GROQ_API_KEY_")
|
| 63 |
-
rand_val = random.randint(1, 1000)
|
| 64 |
-
for key in keys:
|
| 65 |
-
try:
|
| 66 |
-
headers = {"Authorization": f"Bearer {key}", "Content-Type": "application/json"}
|
| 67 |
-
payload = {
|
| 68 |
-
"model": "llama-3.3-70b-versatile",
|
| 69 |
-
"messages": [
|
| 70 |
-
{"role": "system", "content": "You are a professional SRT translator. Keep the exact format. Translate with high-level vocabulary."},
|
| 71 |
-
{"role": "user", "content": f"{prompt}\nSeed: {rand_val}"}
|
| 72 |
-
],
|
| 73 |
-
"temperature": 0.7
|
| 74 |
-
}
|
| 75 |
-
res = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=payload, timeout=30)
|
| 76 |
if res.status_code == 200:
|
| 77 |
key_status_registry[key] = "ready"
|
| 78 |
return res.json()['choices'][0]['message']['content'].strip()
|
| 79 |
key_status_registry[key] = "dead"
|
| 80 |
-
except:
|
| 81 |
-
return None
|
| 82 |
-
|
| 83 |
-
@staticmethod
|
| 84 |
-
def translate_sealion(prompt):
|
| 85 |
-
key = os.environ.get("SEA_LION_API_KEY")
|
| 86 |
-
rand_val = random.randint(1, 1000)
|
| 87 |
-
try:
|
| 88 |
-
url = "https://api.sea-lion.ai/v1/chat/completions"
|
| 89 |
-
payload = {
|
| 90 |
-
"model": "aisingapore/Gemma-SEA-LION-v4-27B-IT",
|
| 91 |
-
"messages": [{"role": "user", "content": f"{prompt}\nID: {rand_val}"}],
|
| 92 |
-
"temperature": 0.7,
|
| 93 |
-
"max_new_tokens": 2048
|
| 94 |
-
}
|
| 95 |
-
res = requests.post(url, headers={"Authorization": f"Bearer {key}"}, json=payload, timeout=60)
|
| 96 |
-
if res.status_code == 200:
|
| 97 |
-
key_status_registry[key] = "ready"
|
| 98 |
-
return res.json()['choices'][0]['message']['content'].strip()
|
| 99 |
-
key_status_registry[key] = "dead"
|
| 100 |
-
except: key_status_registry[key] = "dead"
|
| 101 |
return None
|
| 102 |
|
| 103 |
def translator_hub(text, target_lang, engine):
|
| 104 |
if not text.strip(): return "", AIService.get_status_html(engine)
|
| 105 |
-
|
| 106 |
is_srt = bool(re.search(r'\d+\n\d{2}:\d{2}:\d{2}', text))
|
|
|
|
| 107 |
|
| 108 |
-
# αααααα±αααααααα α‘α αααααΆαα ααΎααααΈαα»αα±ααααΆαααα·αααααα
|
| 109 |
-
chunk_limit = 10
|
| 110 |
-
|
| 111 |
if is_srt:
|
| 112 |
blocks = re.split(r'\n(?=\d+\n\d{2}:\d{2}:\d{2})', text.strip())
|
| 113 |
-
chunks = ["\n".join(blocks[i:i +
|
| 114 |
else:
|
| 115 |
lines = text.split('\n')
|
| 116 |
-
chunks = ["\n".join(lines[i:i +
|
| 117 |
|
| 118 |
-
|
| 119 |
for chunk in chunks:
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
f"6. Return ONLY the translated SRT content.\n\n"
|
| 131 |
-
f"CONTENT:\n{chunk}"
|
| 132 |
-
)
|
| 133 |
-
else:
|
| 134 |
-
prompt = (
|
| 135 |
-
f"Translate this text to {lang_name} naturally. Maintain the tone of the context.\n"
|
| 136 |
-
f"Rules: Return ONLY translation, No names translation, No mixing languages.\n\n"
|
| 137 |
-
f"CONTENT:\n{chunk}"
|
| 138 |
-
)
|
| 139 |
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
else:
|
|
|
|
| 146 |
|
| 147 |
separator = "\n\n" if is_srt else "\n"
|
| 148 |
-
return separator.join(
|
| 149 |
|
| 150 |
-
#
|
| 151 |
-
|
| 152 |
-
#
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
document.head.appendChild(s);
|
| 158 |
-
document.addEventListener('click', (e) => {
|
| 159 |
-
if (e.target.tagName === 'BUTTON' && typeof confetti === 'function') {
|
| 160 |
-
const rect = e.target.getBoundingClientRect();
|
| 161 |
-
confetti({
|
| 162 |
-
particleCount: 20,
|
| 163 |
-
spread: 30,
|
| 164 |
-
origin: { x: (rect.left + rect.width/2) / window.innerWidth, y: (rect.top + rect.height/2) / window.innerHeight },
|
| 165 |
-
colors: ['#60a5fa', '#3b82f6', '#ffffff'],
|
| 166 |
-
ticks: 100,
|
| 167 |
-
gravity: 1.5,
|
| 168 |
-
scalar: 0.7
|
| 169 |
-
});
|
| 170 |
-
}
|
| 171 |
-
});
|
| 172 |
-
}
|
| 173 |
-
function copyText(v) {
|
| 174 |
-
if(!v) return;
|
| 175 |
-
const el = document.createElement('textarea');
|
| 176 |
-
el.value = v;
|
| 177 |
-
document.body.appendChild(el);
|
| 178 |
-
el.select();
|
| 179 |
-
document.execCommand('copy');
|
| 180 |
-
document.body.removeChild(el);
|
| 181 |
-
alert('ααΆαα
αααααααααα! π');
|
| 182 |
-
}
|
| 183 |
-
function clearNotice() {
|
| 184 |
-
alert('ααΆααααα’αΆαα’ααααααα½α
ααΆαα! π§Ή');
|
| 185 |
-
}
|
| 186 |
"""
|
| 187 |
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
demo.load(None, None, None, js="() => { initPWA(); }")
|
| 192 |
-
gr.Markdown("<h2 style='text-align: center; color: #60a5fa;'>π SRT SMART ULTIMATE π°π</h2>")
|
| 193 |
|
| 194 |
-
with gr.
|
| 195 |
-
with gr.
|
| 196 |
-
engine_opt = gr.Radio(
|
| 197 |
-
lang_opt = gr.Dropdown(
|
| 198 |
-
|
| 199 |
-
api_status_ui = gr.HTML(AIService.get_status_html("π Gemini"), elem_classes="api-box")
|
| 200 |
-
input_text = gr.Textbox(label="α’αααααααΎα", lines=8, placeholder="αα·αααααΆααα’αααααααααααΆ α¬ SRT αααα...")
|
| 201 |
-
|
| 202 |
-
with gr.Row(elem_classes="center-btn"):
|
| 203 |
-
clear_btn = gr.Button("ποΈ αα»α", variant="secondary", elem_classes="small-btn")
|
| 204 |
-
submit_btn = gr.Button("β‘ ααααααα₯α‘αΌα", variant="primary", elem_classes="translate-btn")
|
| 205 |
-
copy_btn = gr.Button("π α
αααα", variant="secondary", elem_classes="small-btn")
|
| 206 |
|
| 207 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
|
|
|
| 213 |
|
| 214 |
if __name__ == "__main__":
|
| 215 |
demo.launch()
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
import re
|
| 5 |
import random
|
| 6 |
+
import time
|
| 7 |
|
| 8 |
+
# Registry αααααΆαααααααααΆαααΆα Key
|
| 9 |
key_status_registry = {}
|
| 10 |
|
| 11 |
class AIService:
|
|
|
|
| 25 |
|
| 26 |
@staticmethod
|
| 27 |
def get_status_html(engine):
|
| 28 |
+
prefix = "GEMINI_API_KEY_" if "Gemini" in engine else "GROQ_API_KEY_" if "Llama" in engine else "SEA_LION_API_KEY"
|
|
|
|
|
|
|
|
|
|
| 29 |
keys = AIService.get_all_keys(prefix)
|
| 30 |
label = engine.split(" ")[-1]
|
| 31 |
+
html = f"<div style='display: flex; gap: 8px; align-items: center; margin-bottom: 10px;'><b style='color: #94a3b8; font-size: 12px;'>{label} Status:</b>"
|
| 32 |
+
for k in keys:
|
| 33 |
state = key_status_registry.get(k, "ready")
|
| 34 |
color = "#22c55e" if state == "ready" else "#ef4444"
|
| 35 |
+
html += f"<div style='width: 10px; height: 10px; background: {color}; border-radius: 50%; box-shadow: 0 0 5px {color};'></div>"
|
| 36 |
+
return html + "</div>"
|
|
|
|
|
|
|
| 37 |
|
| 38 |
@staticmethod
|
| 39 |
+
def call_api(engine, prompt):
|
| 40 |
+
if "Gemini" in engine:
|
| 41 |
+
keys = AIService.get_all_keys("GEMINI_API_KEY_")
|
| 42 |
+
for key in keys:
|
| 43 |
+
try:
|
| 44 |
+
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-exp:generateContent?key={key}"
|
| 45 |
+
res = requests.post(url, json={
|
| 46 |
+
"contents": [{"parts": [{"text": prompt}]}],
|
| 47 |
+
"generationConfig": {"temperature": 0.6}
|
| 48 |
+
}, timeout=30)
|
| 49 |
+
if res.status_code == 200:
|
| 50 |
+
key_status_registry[key] = "ready"
|
| 51 |
+
return res.json()['candidates'][0]['content']['parts'][0]['text'].strip()
|
| 52 |
+
key_status_registry[key] = "dead"
|
| 53 |
+
except: continue
|
| 54 |
+
elif "Llama" in engine:
|
| 55 |
+
keys = AIService.get_all_keys("GROQ_API_KEY_")
|
| 56 |
+
for key in keys:
|
| 57 |
+
try:
|
| 58 |
+
res = requests.post("https://api.groq.com/openai/v1/chat/completions",
|
| 59 |
+
headers={"Authorization": f"Bearer {key}"},
|
| 60 |
+
json={"model": "llama-3.3-70b-versatile", "messages": [{"role": "user", "content": prompt}], "temperature": 0.5}, timeout=30)
|
| 61 |
+
if res.status_code == 200:
|
| 62 |
+
key_status_registry[key] = "ready"
|
| 63 |
+
return res.json()['choices'][0]['message']['content'].strip()
|
| 64 |
+
key_status_registry[key] = "dead"
|
| 65 |
+
except: continue
|
| 66 |
+
else: # SEA-LION
|
| 67 |
+
key = os.environ.get("SEA_LION_API_KEY")
|
| 68 |
try:
|
| 69 |
+
res = requests.post("https://api.sea-lion.ai/v1/chat/completions",
|
| 70 |
+
headers={"Authorization": f"Bearer {key}"},
|
| 71 |
+
json={"model": "aisingapore/Gemma-SEA-LION-v4-27B-IT", "messages": [{"role": "user", "content": prompt}], "temperature": 0.5}, timeout=60)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
if res.status_code == 200:
|
| 73 |
key_status_registry[key] = "ready"
|
| 74 |
return res.json()['choices'][0]['message']['content'].strip()
|
| 75 |
key_status_registry[key] = "dead"
|
| 76 |
+
except: pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
return None
|
| 78 |
|
| 79 |
def translator_hub(text, target_lang, engine):
|
| 80 |
if not text.strip(): return "", AIService.get_status_html(engine)
|
| 81 |
+
|
| 82 |
is_srt = bool(re.search(r'\d+\n\d{2}:\d{2}:\d{2}', text))
|
| 83 |
+
chunk_size = 10 # αααααααααα α‘α αααααΆααααΎααααΈααΆαααΆαααΆαααΆαααα·αααααα
|
| 84 |
|
|
|
|
|
|
|
|
|
|
| 85 |
if is_srt:
|
| 86 |
blocks = re.split(r'\n(?=\d+\n\d{2}:\d{2}:\d{2})', text.strip())
|
| 87 |
+
chunks = ["\n".join(blocks[i:i + chunk_size]) for i in range(0, len(blocks), chunk_size)]
|
| 88 |
else:
|
| 89 |
lines = text.split('\n')
|
| 90 |
+
chunks = ["\n".join(lines[i:i + chunk_size]) for i in range(0, len(lines), chunk_size)]
|
| 91 |
|
| 92 |
+
final_output = []
|
| 93 |
for chunk in chunks:
|
| 94 |
+
prompt = (
|
| 95 |
+
f"You are a professional movie translator. Translate to {target_lang.split()[-1]}.\n"
|
| 96 |
+
f"STRICT INSTRUCTIONS:\n"
|
| 97 |
+
f"1. RELATIONSHIPS: Use 'αα/α’αΌα' for couples. Use family terms like 'αααΆαα, αααΆ, ααΌα, αα, ααα’αΌα, α’αα, ααΌ, ααΈα' based on context. NEVER use 'ααα/α’ααα' in family or close friends context.\n"
|
| 98 |
+
f"2. NAMES: DO NOT translate character names or places. Keep them as they are.\n"
|
| 99 |
+
f"3. COMPLETENESS: Translate every single line. Do not skip or merge lines.\n"
|
| 100 |
+
f"4. FORMAT: If input is SRT, keep index and timecodes exactly the same.\n"
|
| 101 |
+
f"5. TONE: Adapt to the scene (Historical, Modern, Angry, or Romantic).\n\n"
|
| 102 |
+
f"CONTENT TO TRANSLATE:\n{chunk}"
|
| 103 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
result = AIService.call_api(engine, prompt)
|
| 106 |
+
if result:
|
| 107 |
+
# αα»α Text α₯αααααααααααα AI α
αΌαα
α·αααααα
|
| 108 |
+
cleaned = re.sub(r'(?i)(here is the translation:|```srt|```)', '', result).strip()
|
| 109 |
+
final_output.append(cleaned)
|
| 110 |
+
else:
|
| 111 |
+
final_output.append("β [ααΆααααααΆααααΆααααα αΆ]")
|
| 112 |
|
| 113 |
separator = "\n\n" if is_srt else "\n"
|
| 114 |
+
return separator.join(final_output), AIService.get_status_html(engine)
|
| 115 |
|
| 116 |
+
# --- UI Setup with Custom CSS ---
|
| 117 |
+
css = """
|
| 118 |
+
body { background-color: #0d1117; color: #c9d1d9; font-family: 'Kantumruy Pro', sans-serif; }
|
| 119 |
+
.gradio-container { border: none !important; }
|
| 120 |
+
.main-box { background: #161b22; border-radius: 12px; padding: 20px; border: 1px solid #30363d; }
|
| 121 |
+
.btn-trans { background: linear-gradient(90deg, #1f6feb, #388bfd) !important; color: white !important; font-weight: bold !important; }
|
| 122 |
+
.status-area { background: #0d1117; padding: 10px; border-radius: 8px; margin-bottom: 10px; border: 1px dashed #30363d; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
"""
|
| 124 |
|
| 125 |
+
with gr.Blocks(css=css, title="SRT Translator Pro") as demo:
|
| 126 |
+
gr.Markdown("<h1 style='text-align: center; color: #58a6ff;'>π¬ SRT SMART TRANSLATOR ULTIMATE</h1>")
|
| 127 |
+
gr.Markdown("<p style='text-align: center; color: #8b949e;'>ααααααααΏαααΆα αααααααααααΆαααΆαααααα αα·ααααααΆααααααααΎα</p>")
|
|
|
|
|
|
|
| 128 |
|
| 129 |
+
with gr.Row(elem_classes="main-box"):
|
| 130 |
+
with gr.Column(scale=1):
|
| 131 |
+
engine_opt = gr.Radio(["π Gemini", "π¦ Llama", "π¦ SEA-LION"], value="π Gemini", label="ααααΎαααΎααααΌααα AI")
|
| 132 |
+
lang_opt = gr.Dropdown(["π°π Khmer", "πΊπΈ English", "πΉπ Thai", "π¨π³ Chinese"], value="π°οΏ½οΏ½ Khmer", label="ααααααααΆααΆααΆ")
|
| 133 |
+
status_ui = gr.HTML(AIService.get_status_html("π Gemini"), elem_classes="status-area")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
+
input_box = gr.Textbox(label="α’αααααααΎα (Paste SRT here)", lines=12, placeholder="αα·αααααΆααα’ααααα α¬α―αααΆα SRT αα
ααΈααα...")
|
| 136 |
+
|
| 137 |
+
with gr.Row():
|
| 138 |
+
btn_clear = gr.Button("ποΈ αααα’αΆα")
|
| 139 |
+
btn_trans = gr.Button("β‘ α
αΆααααααΎααααααα", variant="primary", elem_classes="btn-trans")
|
| 140 |
+
|
| 141 |
+
with gr.Column(scale=1):
|
| 142 |
+
output_box = gr.Textbox(label="αααααααααααα", lines=22, interactive=False, show_copy_button=True)
|
| 143 |
+
btn_copy_all = gr.Button("π α
ααααααααααααΆααα’αα")
|
| 144 |
|
| 145 |
+
# Events
|
| 146 |
+
engine_opt.change(AIService.get_status_html, engine_opt, status_ui)
|
| 147 |
+
btn_trans.click(translator_hub, [input_box, lang_opt, engine_opt], [output_box, status_ui])
|
| 148 |
+
btn_clear.click(lambda: ["", ""], None, [input_box, output_box])
|
| 149 |
+
btn_copy_all.click(None, output_box, js="(v) => { navigator.clipboard.writeText(v); alert('α
αααααααααα!'); }")
|
| 150 |
|
| 151 |
if __name__ == "__main__":
|
| 152 |
demo.launch()
|