Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,9 +9,8 @@ key_status_registry = {}
|
|
| 9 |
class AIService:
|
| 10 |
@staticmethod
|
| 11 |
def get_all_keys(prefix):
|
| 12 |
-
# ααααα½α
|
| 13 |
-
|
| 14 |
-
env_name = prefix.rstrip('_') # αααααααΆ _ α
ααααΎααααΈα±ααααααΌαααΉαααααα Standard
|
| 15 |
key = os.environ.get(env_name)
|
| 16 |
if key:
|
| 17 |
return [key]
|
|
@@ -47,23 +46,23 @@ class AIService:
|
|
| 47 |
keys = AIService.get_all_keys("GEMINI_API_KEY")
|
| 48 |
for key in keys:
|
| 49 |
try:
|
| 50 |
-
#
|
| 51 |
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={key}"
|
| 52 |
res = requests.post(url, json={
|
| 53 |
"contents": [{"parts": [{"text": prompt}]}],
|
| 54 |
-
"generationConfig": {"temperature": temp}
|
| 55 |
-
"safetySettings": [
|
| 56 |
-
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
|
| 57 |
-
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
|
| 58 |
-
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
|
| 59 |
-
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"}
|
| 60 |
-
]
|
| 61 |
}, timeout=45)
|
|
|
|
| 62 |
if res.status_code == 200:
|
| 63 |
key_status_registry[key] = "ready"
|
| 64 |
return res.json()['candidates'][0]['content']['parts'][0]['text'].strip()
|
|
|
|
|
|
|
|
|
|
| 65 |
key_status_registry[key] = "dead"
|
| 66 |
-
except
|
|
|
|
|
|
|
| 67 |
|
| 68 |
elif "Llama" in engine:
|
| 69 |
keys = AIService.get_all_keys("GROQ_API_KEY")
|
|
@@ -77,22 +76,6 @@ class AIService:
|
|
| 77 |
return res.json()['choices'][0]['message']['content'].strip()
|
| 78 |
key_status_registry[key] = "dead"
|
| 79 |
except: continue
|
| 80 |
-
else:
|
| 81 |
-
key = os.environ.get("SEA_LION_API_KEY")
|
| 82 |
-
try:
|
| 83 |
-
res = requests.post("https://api.sea-lion.ai/v1/chat/completions",
|
| 84 |
-
headers={"Authorization": f"Bearer {key}"},
|
| 85 |
-
json={
|
| 86 |
-
"model": "aisingapore/Gemma-SEA-LION-v4-27B-IT",
|
| 87 |
-
"messages": [{"role": "system", "content": "You are an expert translator. Output ONLY translation."},
|
| 88 |
-
{"role": "user", "content": prompt}],
|
| 89 |
-
"temperature": 0.7
|
| 90 |
-
}, timeout=60)
|
| 91 |
-
if res.status_code == 200:
|
| 92 |
-
key_status_registry[key] = "ready"
|
| 93 |
-
return res.json()['choices'][0]['message']['content'].strip()
|
| 94 |
-
key_status_registry[key] = "dead"
|
| 95 |
-
except: pass
|
| 96 |
return None
|
| 97 |
|
| 98 |
def translator_hub(text, target_lang, engine):
|
|
@@ -101,10 +84,9 @@ def translator_hub(text, target_lang, engine):
|
|
| 101 |
|
| 102 |
instruction = f"""Translate to {lang_name} STRICTLY:
|
| 103 |
1. OUTPUT ONLY translation. No notes.
|
| 104 |
-
2.
|
| 105 |
-
3.
|
| 106 |
-
4.
|
| 107 |
-
5. NO MIXED LANGUAGES: No English words in output."""
|
| 108 |
|
| 109 |
prompt = f"{instruction}\n\nCONTENT:\n{text}"
|
| 110 |
result = AIService.call_api(engine, prompt)
|
|
@@ -112,26 +94,25 @@ def translator_hub(text, target_lang, engine):
|
|
| 112 |
if result:
|
| 113 |
cleaned = re.sub(r'```[a-zA-Z]*\n?|```', '', result).strip()
|
| 114 |
return cleaned, AIService.get_status_html(engine)
|
| 115 |
-
return "β Error: α
|
| 116 |
|
| 117 |
-
#
|
| 118 |
custom_css = """
|
| 119 |
body { background: #0f172a !important; }
|
| 120 |
-
.
|
| 121 |
-
.btn-trans { background: #10b981 !important; color: white !important; }
|
| 122 |
"""
|
| 123 |
|
| 124 |
-
with gr.Blocks(title="SRT Pro") as demo:
|
| 125 |
-
gr.Markdown("<h1>π¬ SMART TRANSLATOR PRO</h1>")
|
| 126 |
with gr.Row():
|
| 127 |
with gr.Column():
|
| 128 |
-
lang_opt = gr.Dropdown(["π°π Khmer", "πΊπΈ English", "π¨π³ Chinese", "πΉπ Thai"], value="π°π Khmer", label="Language")
|
| 129 |
-
engine_opt = gr.Radio(["π Gemini", "π¦ Llama"
|
| 130 |
status_ui = gr.HTML(AIService.get_status_html("π Gemini"))
|
| 131 |
-
input_box = gr.Textbox(label="Original
|
| 132 |
btn_trans = gr.Button("β‘ Translate", variant="primary", elem_classes="btn-trans")
|
| 133 |
with gr.Column():
|
| 134 |
-
output_box = gr.Textbox(label="Result", lines=20, interactive=False)
|
| 135 |
btn_copy = gr.Button("π Copy Result")
|
| 136 |
|
| 137 |
engine_opt.change(AIService.get_status_html, inputs=[engine_opt], outputs=[status_ui])
|
|
@@ -139,5 +120,4 @@ with gr.Blocks(title="SRT Pro") as demo:
|
|
| 139 |
btn_copy.click(None, output_box, js="(v) => { navigator.clipboard.writeText(v); alert('β
α
αααααα½α
ααΆαα!'); }")
|
| 140 |
|
| 141 |
if __name__ == "__main__":
|
| 142 |
-
|
| 143 |
-
demo.launch(server_name="0.0.0.0", server_port=7860, css=custom_css, ssr_mode=False)
|
|
|
|
| 9 |
class AIService:
|
| 10 |
@staticmethod
|
| 11 |
def get_all_keys(prefix):
|
| 12 |
+
# ααααααα½αα±ααα α
Key αααα½αα
ααααΈ Environment Variable
|
| 13 |
+
env_name = prefix.rstrip('_')
|
|
|
|
| 14 |
key = os.environ.get(env_name)
|
| 15 |
if key:
|
| 16 |
return [key]
|
|
|
|
| 46 |
keys = AIService.get_all_keys("GEMINI_API_KEY")
|
| 47 |
for key in keys:
|
| 48 |
try:
|
| 49 |
+
# ααααα½α URL α±αααα
ααΆ v1beta ααΎααααΈααααΎ Gemini 2.0 Flash ααΆααααΌα
|
| 50 |
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={key}"
|
| 51 |
res = requests.post(url, json={
|
| 52 |
"contents": [{"parts": [{"text": prompt}]}],
|
| 53 |
+
"generationConfig": {"temperature": temp}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
}, timeout=45)
|
| 55 |
+
|
| 56 |
if res.status_code == 200:
|
| 57 |
key_status_registry[key] = "ready"
|
| 58 |
return res.json()['candidates'][0]['content']['parts'][0]['text'].strip()
|
| 59 |
+
|
| 60 |
+
# ααΎααΆα Error ααΆααΉα Log ααααΆαααααα»α Terminal
|
| 61 |
+
print(f"Gemini API Error: {res.status_code} - {res.text}")
|
| 62 |
key_status_registry[key] = "dead"
|
| 63 |
+
except Exception as e:
|
| 64 |
+
print(f"Connection Error: {str(e)}")
|
| 65 |
+
continue
|
| 66 |
|
| 67 |
elif "Llama" in engine:
|
| 68 |
keys = AIService.get_all_keys("GROQ_API_KEY")
|
|
|
|
| 76 |
return res.json()['choices'][0]['message']['content'].strip()
|
| 77 |
key_status_registry[key] = "dead"
|
| 78 |
except: continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
return None
|
| 80 |
|
| 81 |
def translator_hub(text, target_lang, engine):
|
|
|
|
| 84 |
|
| 85 |
instruction = f"""Translate to {lang_name} STRICTLY:
|
| 86 |
1. OUTPUT ONLY translation. No notes.
|
| 87 |
+
2. CONTEXT: Use proper pronouns (αα, α’αΌα, α’α, α―α) based on mood.
|
| 88 |
+
3. SRT FORMAT: Preserve timing/numbering.
|
| 89 |
+
4. NO MIXED LANGUAGES: No English words in output."""
|
|
|
|
| 90 |
|
| 91 |
prompt = f"{instruction}\n\nCONTENT:\n{text}"
|
| 92 |
result = AIService.call_api(engine, prompt)
|
|
|
|
| 94 |
if result:
|
| 95 |
cleaned = re.sub(r'```[a-zA-Z]*\n?|```', '', result).strip()
|
| 96 |
return cleaned, AIService.get_status_html(engine)
|
| 97 |
+
return "β Error: API Key ααΆααααα αΆ α¬α’αα Quota (ααΌααα·αα·αααααΎααααα»α Google Cloud)", AIService.get_status_html(engine)
|
| 98 |
|
| 99 |
+
# UI Layout
|
| 100 |
custom_css = """
|
| 101 |
body { background: #0f172a !important; }
|
| 102 |
+
.btn-trans { background: #10b981 !important; color: white !important; font-weight: bold !important; }
|
|
|
|
| 103 |
"""
|
| 104 |
|
| 105 |
+
with gr.Blocks(title="SRT Pro 2.0", css=custom_css) as demo:
|
| 106 |
+
gr.Markdown("<h1 style='text-align: center; color: #60a5fa;'>π¬ SMART TRANSLATOR PRO v2.0</h1>")
|
| 107 |
with gr.Row():
|
| 108 |
with gr.Column():
|
| 109 |
+
lang_opt = gr.Dropdown(["π°π Khmer", "πΊπΈ English", "π¨π³ Chinese", "πΉπ Thai"], value="π°π Khmer", label="Target Language")
|
| 110 |
+
engine_opt = gr.Radio(["π Gemini", "π¦ Llama"], value="π Gemini", label="AI Engine")
|
| 111 |
status_ui = gr.HTML(AIService.get_status_html("π Gemini"))
|
| 112 |
+
input_box = gr.Textbox(label="Original", lines=10)
|
| 113 |
btn_trans = gr.Button("β‘ Translate", variant="primary", elem_classes="btn-trans")
|
| 114 |
with gr.Column():
|
| 115 |
+
output_box = gr.Textbox(label="Result", lines=20, interactive=False, show_copy_button=True)
|
| 116 |
btn_copy = gr.Button("π Copy Result")
|
| 117 |
|
| 118 |
engine_opt.change(AIService.get_status_html, inputs=[engine_opt], outputs=[status_ui])
|
|
|
|
| 120 |
btn_copy.click(None, output_box, js="(v) => { navigator.clipboard.writeText(v); alert('β
α
αααααα½α
ααΆαα!'); }")
|
| 121 |
|
| 122 |
if __name__ == "__main__":
|
| 123 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, ssr_mode=False)
|
|
|