Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,7 +9,6 @@ key_status_registry = {}
|
|
| 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:
|
|
@@ -46,7 +45,6 @@ class AIService:
|
|
| 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}]}],
|
|
@@ -57,7 +55,6 @@ class AIService:
|
|
| 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:
|
|
@@ -94,15 +91,16 @@ def translator_hub(text, target_lang, engine):
|
|
| 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
|
| 98 |
|
| 99 |
-
#
|
| 100 |
custom_css = """
|
| 101 |
body { background: #0f172a !important; }
|
| 102 |
.btn-trans { background: #10b981 !important; color: white !important; font-weight: bold !important; }
|
| 103 |
"""
|
| 104 |
|
| 105 |
-
|
|
|
|
| 106 |
gr.Markdown("<h1 style='text-align: center; color: #60a5fa;'>🎬 SMART TRANSLATOR PRO v2.0</h1>")
|
| 107 |
with gr.Row():
|
| 108 |
with gr.Column():
|
|
@@ -112,7 +110,8 @@ with gr.Blocks(title="SRT Pro 2.0", css=custom_css) as demo:
|
|
| 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 |
-
|
|
|
|
| 116 |
btn_copy = gr.Button("📋 Copy Result")
|
| 117 |
|
| 118 |
engine_opt.change(AIService.get_status_html, inputs=[engine_opt], outputs=[status_ui])
|
|
@@ -120,4 +119,5 @@ with gr.Blocks(title="SRT Pro 2.0", css=custom_css) as demo:
|
|
| 120 |
btn_copy.click(None, output_box, js="(v) => { navigator.clipboard.writeText(v); alert('✅ ចម្លងរួចរាល់!'); }")
|
| 121 |
|
| 122 |
if __name__ == "__main__":
|
| 123 |
-
|
|
|
|
|
|
| 9 |
class AIService:
|
| 10 |
@staticmethod
|
| 11 |
def get_all_keys(prefix):
|
|
|
|
| 12 |
env_name = prefix.rstrip('_')
|
| 13 |
key = os.environ.get(env_name)
|
| 14 |
if key:
|
|
|
|
| 45 |
keys = AIService.get_all_keys("GEMINI_API_KEY")
|
| 46 |
for key in keys:
|
| 47 |
try:
|
|
|
|
| 48 |
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={key}"
|
| 49 |
res = requests.post(url, json={
|
| 50 |
"contents": [{"parts": [{"text": prompt}]}],
|
|
|
|
| 55 |
key_status_registry[key] = "ready"
|
| 56 |
return res.json()['candidates'][0]['content']['parts'][0]['text'].strip()
|
| 57 |
|
|
|
|
| 58 |
print(f"Gemini API Error: {res.status_code} - {res.text}")
|
| 59 |
key_status_registry[key] = "dead"
|
| 60 |
except Exception as e:
|
|
|
|
| 91 |
if result:
|
| 92 |
cleaned = re.sub(r'```[a-zA-Z]*\n?|```', '', result).strip()
|
| 93 |
return cleaned, AIService.get_status_html(engine)
|
| 94 |
+
return "❌ Error: API Key មានបញ្ហា ឬអស់ Quota", AIService.get_status_html(engine)
|
| 95 |
|
| 96 |
+
# CSS configuration
|
| 97 |
custom_css = """
|
| 98 |
body { background: #0f172a !important; }
|
| 99 |
.btn-trans { background: #10b981 !important; color: white !important; font-weight: bold !important; }
|
| 100 |
"""
|
| 101 |
|
| 102 |
+
# កែសម្រួល Blocks: ដក css ចេញពីទីនេះ
|
| 103 |
+
with gr.Blocks(title="SRT Pro 2.0") as demo:
|
| 104 |
gr.Markdown("<h1 style='text-align: center; color: #60a5fa;'>🎬 SMART TRANSLATOR PRO v2.0</h1>")
|
| 105 |
with gr.Row():
|
| 106 |
with gr.Column():
|
|
|
|
| 110 |
input_box = gr.Textbox(label="Original", lines=10)
|
| 111 |
btn_trans = gr.Button("⚡ Translate", variant="primary", elem_classes="btn-trans")
|
| 112 |
with gr.Column():
|
| 113 |
+
# កែសម្រួល Textbox: ដក show_copy_button ចេញដើម្បីបំបាត់ Error
|
| 114 |
+
output_box = gr.Textbox(label="Result", lines=20, interactive=False)
|
| 115 |
btn_copy = gr.Button("📋 Copy Result")
|
| 116 |
|
| 117 |
engine_opt.change(AIService.get_status_html, inputs=[engine_opt], outputs=[status_ui])
|
|
|
|
| 119 |
btn_copy.click(None, output_box, js="(v) => { navigator.clipboard.writeText(v); alert('✅ ចម្លងរួចរាល់!'); }")
|
| 120 |
|
| 121 |
if __name__ == "__main__":
|
| 122 |
+
# រុញ css មកដាក់ក្នុង launch() តាមស្ដង់ដារ Gradio 6.0
|
| 123 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, css=custom_css, ssr_mode=False)
|