Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import subprocess
|
3 |
+
import os
|
4 |
+
|
5 |
+
def compress_pdf(input_pdf):
|
6 |
+
output_pdf = "/tmp/output.pdf"
|
7 |
+
|
8 |
+
# GhostscriptコマンドでPDFを軽量化
|
9 |
+
result = subprocess.run(
|
10 |
+
["gs", "-sDEVICE=pdfwrite",
|
11 |
+
"-dPDFSETTINGS=/screen", # 軽量化レベル(/ebook や /printer も選べる)
|
12 |
+
"-dNOPAUSE",
|
13 |
+
"-dBATCH",
|
14 |
+
"-dQUIET",
|
15 |
+
f"-sOutputFile={output_pdf}",
|
16 |
+
input_pdf],
|
17 |
+
stdout=subprocess.PIPE,
|
18 |
+
stderr=subprocess.PIPE
|
19 |
+
)
|
20 |
+
|
21 |
+
# エラー処理
|
22 |
+
if result.returncode != 0:
|
23 |
+
return f"エラー: {result.stderr.decode()}", None
|
24 |
+
|
25 |
+
return "圧縮完了", output_pdf
|
26 |
+
|
27 |
+
# Gradioインターフェース
|
28 |
+
demo = gr.Interface(
|
29 |
+
fn=compress_pdf,
|
30 |
+
inputs=gr.File(file_types=[".pdf"]),
|
31 |
+
outputs=[gr.Text(), gr.File()],
|
32 |
+
title="PDF軽量化(Ghostscript)",
|
33 |
+
description="PDFファイルをGhostscriptで軽量化します(/screenモード)"
|
34 |
+
)
|
35 |
+
|
36 |
+
demo.launch()
|