File size: 3,212 Bytes
e7bdcf1
3061553
 
b235b08
3061553
4862987
3061553
fd763cf
7893bcb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd763cf
 
7893bcb
d24c6c4
fd763cf
4862987
d24c6c4
 
 
 
 
 
 
4862987
 
 
 
 
 
d24c6c4
7893bcb
4862987
fd763cf
 
b235b08
 
fd763cf
 
d24c6c4
 
 
 
fd763cf
 
 
7893bcb
3061553
38bc48b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99

import gradio as gr
import os
import time
from translate_ppt import translate_pptx
from pptx import Presentation

LANGUAGE_CHOICES = [
    ("English", "en"),
    ("Simplified Chinese", "zh-CN"),
    ("Traditional Chinese", "zh-TW"),
    ("Korean", "ko"),
    ("Japanese", "ja"),
    ("French", "fr"),
    ("German", "de"),
    ("Spanish", "es"),
    ("Portuguese", "pt"),
    ("Italian", "it"),
    ("Dutch", "nl"),
    ("Russian", "ru"),
    ("Arabic", "ar"),
    ("Turkish", "tr"),
    ("Hebrew", "iw"),
    ("Vietnamese", "vi"),
    ("Indonesian", "id"),
    ("Malay", "ms"),
    ("Hindi", "hi"),
    ("Thai", "th"),
    ("Bengali", "bn"),
    ("Tamil", "ta"),
    ("Telugu", "te"),
    ("Urdu", "ur"),
    ("Polish", "pl"),
    ("Ukrainian", "uk"),
    ("Romanian", "ro"),
    ("Hungarian", "hu"),
    ("Greek", "el"),
    ("Czech", "cs"),
    ("Slovak", "sk"),
    ("Swedish", "sv"),
    ("Finnish", "fi"),
    ("Danish", "da"),
    ("Norwegian", "no")
]


def process_file(file, target_lang, progress=gr.Progress()):
    output_path = f"translated_{os.path.basename(file.name)}"
    total_units = 0
    prs = Presentation(file.name)
    for slide in prs.slides:
        for shape in slide.shapes:
            if shape.has_text_frame:
                for para in shape.text_frame.paragraphs:
                    for run in para.runs:
                        if run.text.strip():
                            total_units += 1

    completed = [0]
    def tick():
        completed[0] += 1
        progress(completed[0], total=total_units)

    progress(0, total=total_units, desc="Translating...")
    out, detected_lang = translate_pptx(file.name, output_path, target_lang, progress_callback=tick)
    return out, detected_lang

def handle_upload(files, target_lang):
    output_files = []
    detected_languages = []
    start = time.perf_counter()
    for file in files:
        out_file, lang = process_file(file, target_lang)
        output_files.append(out_file)
        detected_languages.append(lang)
    duration = time.perf_counter() - start
    unique_langs = set(detected_languages)
    lang_info = ", ".join(unique_langs)
    return output_files, f"βœ… Detected source language(s): {lang_info}. Translated {len(files)} file(s) in {round(duration, 1)} seconds."

gr.Interface(
    fn=handle_upload,
    inputs=[
        gr.File(label="Upload one or more PPTX files", file_types=[".pptx"], file_count="multiple"),
        gr.Dropdown(label="Target language", choices=LANGUAGE_CHOICES, value="zh-CN")
    ],
    outputs=[
        gr.File(label="Download translated file(s)", file_types=[".pptx"], file_count="multiple"),
        gr.Textbox(label="Translation summary", lines=2)
    ],
    title="PPTX Auto Translator",
    description=(
        "πŸ“„ Upload a PowerPoint (.pptx) file in English.\n"
        "🌍 Choose the target language.\n"
        "βš™οΈ This tool will automatically detect the original language, translate all text while keeping formatting, and let you download the result.\n\n"
        "**Note:** Make sure your file is in `.pptx` format (PowerPoint XML-based, not `.ppt`).\n"
        "Each file will be translated one by one with real-time progress shown."
    )
).launch()