Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
| 4 |
import time
|
|
@@ -6,28 +5,49 @@ from translate_ppt import translate_pptx
|
|
| 6 |
from pptx import Presentation
|
| 7 |
|
| 8 |
LANGUAGE_CHOICES = [
|
| 9 |
-
("
|
| 10 |
-
("
|
| 11 |
-
("
|
| 12 |
-
("
|
| 13 |
-
("
|
| 14 |
-
("
|
| 15 |
-
("
|
| 16 |
-
("
|
| 17 |
-
("
|
| 18 |
-
("
|
| 19 |
-
("
|
| 20 |
-
("
|
| 21 |
-
("
|
| 22 |
-
("
|
| 23 |
-
("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
]
|
| 25 |
|
|
|
|
| 26 |
def process_file(file, target_lang, progress=gr.Progress()):
|
| 27 |
output_path = f"translated_{os.path.basename(file.name)}"
|
| 28 |
total_units = 0
|
| 29 |
|
| 30 |
-
# Count total
|
| 31 |
prs = Presentation(file.name)
|
| 32 |
for slide in prs.slides:
|
| 33 |
for shape in slide.shapes:
|
|
@@ -42,7 +62,7 @@ def process_file(file, target_lang, progress=gr.Progress()):
|
|
| 42 |
completed[0] += 1
|
| 43 |
progress(completed[0], total=total_units)
|
| 44 |
|
| 45 |
-
progress(0, total=total_units, desc="
|
| 46 |
out, detected_lang = translate_pptx(file.name, output_path, target_lang, progress_callback=tick)
|
| 47 |
return out, detected_lang
|
| 48 |
|
|
@@ -59,18 +79,24 @@ def handle_upload(files, target_lang):
|
|
| 59 |
duration = time.perf_counter() - start
|
| 60 |
unique_langs = set(detected_languages)
|
| 61 |
lang_info = ", ".join(unique_langs)
|
| 62 |
-
return output_files, f"✅
|
| 63 |
|
| 64 |
gr.Interface(
|
| 65 |
fn=handle_upload,
|
| 66 |
inputs=[
|
| 67 |
-
gr.File(label="
|
| 68 |
-
gr.Dropdown(label="
|
| 69 |
],
|
| 70 |
outputs=[
|
| 71 |
-
gr.File(label="
|
| 72 |
-
gr.Textbox(label="
|
| 73 |
],
|
| 74 |
-
title="
|
| 75 |
-
description=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
).launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import time
|
|
|
|
| 5 |
from pptx import Presentation
|
| 6 |
|
| 7 |
LANGUAGE_CHOICES = [
|
| 8 |
+
("English", "en"),
|
| 9 |
+
("Simplified Chinese", "zh-CN"),
|
| 10 |
+
("Traditional Chinese", "zh-TW"),
|
| 11 |
+
("Korean", "ko"),
|
| 12 |
+
("Japanese", "ja"),
|
| 13 |
+
("French", "fr"),
|
| 14 |
+
("German", "de"),
|
| 15 |
+
("Spanish", "es"),
|
| 16 |
+
("Portuguese", "pt"),
|
| 17 |
+
("Italian", "it"),
|
| 18 |
+
("Dutch", "nl"),
|
| 19 |
+
("Russian", "ru"),
|
| 20 |
+
("Arabic", "ar"),
|
| 21 |
+
("Turkish", "tr"),
|
| 22 |
+
("Hebrew", "iw"),
|
| 23 |
+
("Vietnamese", "vi"),
|
| 24 |
+
("Indonesian", "id"),
|
| 25 |
+
("Malay", "ms"),
|
| 26 |
+
("Hindi", "hi"),
|
| 27 |
+
("Thai", "th"),
|
| 28 |
+
("Bengali", "bn"),
|
| 29 |
+
("Tamil", "ta"),
|
| 30 |
+
("Telugu", "te"),
|
| 31 |
+
("Urdu", "ur"),
|
| 32 |
+
("Polish", "pl"),
|
| 33 |
+
("Ukrainian", "uk"),
|
| 34 |
+
("Romanian", "ro"),
|
| 35 |
+
("Hungarian", "hu"),
|
| 36 |
+
("Greek", "el"),
|
| 37 |
+
("Czech", "cs"),
|
| 38 |
+
("Slovak", "sk"),
|
| 39 |
+
("Swedish", "sv"),
|
| 40 |
+
("Finnish", "fi"),
|
| 41 |
+
("Danish", "da"),
|
| 42 |
+
("Norwegian", "no")
|
| 43 |
]
|
| 44 |
|
| 45 |
+
|
| 46 |
def process_file(file, target_lang, progress=gr.Progress()):
|
| 47 |
output_path = f"translated_{os.path.basename(file.name)}"
|
| 48 |
total_units = 0
|
| 49 |
|
| 50 |
+
# Count total text runs to translate
|
| 51 |
prs = Presentation(file.name)
|
| 52 |
for slide in prs.slides:
|
| 53 |
for shape in slide.shapes:
|
|
|
|
| 62 |
completed[0] += 1
|
| 63 |
progress(completed[0], total=total_units)
|
| 64 |
|
| 65 |
+
progress(0, total=total_units, desc="Translating...")
|
| 66 |
out, detected_lang = translate_pptx(file.name, output_path, target_lang, progress_callback=tick)
|
| 67 |
return out, detected_lang
|
| 68 |
|
|
|
|
| 79 |
duration = time.perf_counter() - start
|
| 80 |
unique_langs = set(detected_languages)
|
| 81 |
lang_info = ", ".join(unique_langs)
|
| 82 |
+
return output_files, f"✅ Detected source language(s): {lang_info}. Translated {len(files)} file(s) in {round(duration, 1)} seconds."
|
| 83 |
|
| 84 |
gr.Interface(
|
| 85 |
fn=handle_upload,
|
| 86 |
inputs=[
|
| 87 |
+
gr.File(label="Upload one or more PPTX files", file_types=[".pptx"], file_count="multiple"),
|
| 88 |
+
gr.Dropdown(label="Target language", choices=LANGUAGE_CHOICES, value="zh-CN")
|
| 89 |
],
|
| 90 |
outputs=[
|
| 91 |
+
gr.File(label="Download translated file(s)", file_types=[".pptx"], file_count="multiple"),
|
| 92 |
+
gr.Textbox(label="Translation summary", lines=2)
|
| 93 |
],
|
| 94 |
+
title="PPTX Auto Translator",
|
| 95 |
+
description=(
|
| 96 |
+
"📄 Upload a PowerPoint (.pptx) file in English.\n"
|
| 97 |
+
"🌍 Choose the target language.\n"
|
| 98 |
+
"⚙️ This tool will automatically detect the original language, translate all text while keeping formatting, and let you download the result.\n\n"
|
| 99 |
+
"**Note:** Make sure your file is in `.pptx` format (PowerPoint XML-based, not `.ppt`).\n"
|
| 100 |
+
"Each file will be translated one by one with real-time progress shown."
|
| 101 |
+
)
|
| 102 |
).launch()
|