PebllaRyan's picture
Update app.py
38bc48b verified
Raw History Blame Contribute Delete
3.21 kB
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()