PebllaRyan commited on
Commit
7893bcb
·
verified ·
1 Parent(s): 4862987

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -25
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
- ("韩语", "ko"),
10
- ("葡萄牙语", "pt"),
11
- ("简体中文", "zh-CN"),
12
- ("法语", "fr"),
13
- ("德语", "de"),
14
- ("西班牙语", "es"),
15
- ("日语", "ja"),
16
- ("意大利语", "it"),
17
- ("荷兰语", "nl"),
18
- ("阿拉伯语", "ar"),
19
- ("俄语", "ru"),
20
- ("越南语", "vi"),
21
- ("印地语", "hi"),
22
- ("泰语", "th"),
23
- ("印尼语", "id"),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 translatable units
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"✅ 系统检测源语言:{lang_info},共 {len(files)} 个文件,用时约 {round(duration, 1)} 秒"
63
 
64
  gr.Interface(
65
  fn=handle_upload,
66
  inputs=[
67
- gr.File(label="上传一个或多个 PPTX 文件", file_types=[".pptx"], file_count="multiple"),
68
- gr.Dropdown(label="目标语言", choices=LANGUAGE_CHOICES, value="zh-CN")
69
  ],
70
  outputs=[
71
- gr.File(label="下载翻译后文件", file_types=[".pptx"], file_count="multiple"),
72
- gr.Textbox(label="处理信息", lines=2)
73
  ],
74
- title="PPT 批量翻译工具",
75
- description="上传英文 PPTX 文件,系统自动识别源语言并翻译为目标语言,保留格式。支持多语言,支持实时进度展示。"
 
 
 
 
 
 
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()