Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import subprocess
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
# 程序根目录
|
6 |
+
BASE_DIR = os.path.dirname(__file__)
|
7 |
+
# 可执行文件路径:app.py 与 PptxToJpgZip 同目录
|
8 |
+
BINARY_PATH = os.path.join(BASE_DIR, "PptxToJpgZip")
|
9 |
+
|
10 |
+
# 启动时确保二进制有执行权限(在 Hugging Face Spaces 里无法手动 chmod)
|
11 |
+
try:
|
12 |
+
os.chmod(BINARY_PATH, 0o755)
|
13 |
+
except Exception as e:
|
14 |
+
# 如果权限设置失败,也无需中断,只要后续能调用即可
|
15 |
+
print(f"⚠️ 警告:设置执行权限失败:{e}")
|
16 |
+
|
17 |
+
def convert_pptx_to_zip(pptx_file):
|
18 |
+
"""
|
19 |
+
调用外部 PptxToJpgZip,可执行文件将上传的 PPTX 转为 JPG 并打包 ZIP。
|
20 |
+
pptx_file.name 是 Gradio 上传后的临时文件路径。
|
21 |
+
"""
|
22 |
+
src_path = pptx_file.name # Gradio 会在后台保存上传文件,并提供 .name 属性
|
23 |
+
|
24 |
+
# 调用外部程序:PptxToJpgZip <输入文件.pptx>
|
25 |
+
proc = subprocess.run(
|
26 |
+
[BINARY_PATH, src_path],
|
27 |
+
stdout=subprocess.PIPE,
|
28 |
+
stderr=subprocess.PIPE
|
29 |
+
)
|
30 |
+
if proc.returncode != 0:
|
31 |
+
stderr = proc.stderr.decode("utf-8", errors="ignore")
|
32 |
+
raise RuntimeError(f"转换失败:\n{stderr}")
|
33 |
+
|
34 |
+
# 可执行程序会在同目录生成 <原文件名>_images.zip
|
35 |
+
zip_path = os.path.splitext(src_path)[0] + "_images.zip"
|
36 |
+
if not os.path.isfile(zip_path):
|
37 |
+
raise FileNotFoundError(f"转换完成,但未找到输出 ZIP:{zip_path}")
|
38 |
+
|
39 |
+
return zip_path # Gradio 会自动为此路径生成下载链接
|
40 |
+
|
41 |
+
# —— 构建 Gradio 界面 ——
|
42 |
+
with gr.Blocks(title="PPTX→JPG→ZIP 转换器") as demo:
|
43 |
+
gr.Markdown("""
|
44 |
+
# PPTX→JPG→ZIP
|
45 |
+
上传一个 `.pptx` 文件,后台调用 `PptxToJpgZip` 可执行程序,
|
46 |
+
将每页幻灯片导出为高质量 JPG,并打包成 ZIP,最后提供下载。
|
47 |
+
""")
|
48 |
+
with gr.Row():
|
49 |
+
pptx_in = gr.File(label="上传 PPTX (.pptx)", file_types=[".pptx"])
|
50 |
+
btn = gr.Button("开始转换")
|
51 |
+
zip_out = gr.File(label="下载 ZIP 文件")
|
52 |
+
|
53 |
+
btn.click(fn=convert_pptx_to_zip, inputs=pptx_in, outputs=zip_out)
|
54 |
+
|
55 |
+
if __name__ == "__main__":
|
56 |
+
demo.launch(
|
57 |
+
server_name="0.0.0.0",
|
58 |
+
server_port=7860,
|
59 |
+
share=False
|
60 |
+
)
|