Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,17 @@
|
|
| 1 |
import os
|
| 2 |
-
import datetime
|
| 3 |
-
from PIL import Image
|
| 4 |
-
from fpdf import FPDF
|
| 5 |
import gradio as gr
|
| 6 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Folder paths
|
| 9 |
INPUT_FOLDER = "./input/"
|
| 10 |
OUTPUT_FOLDER = "./output/"
|
| 11 |
LOG_FOLDER = "./logs/"
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
LOG_FILE = os.path.join(LOG_FOLDER, "conversion_log.txt")
|
| 15 |
|
| 16 |
# Ensure required folders exist
|
| 17 |
os.makedirs(INPUT_FOLDER, exist_ok=True)
|
|
@@ -19,89 +19,62 @@ os.makedirs(OUTPUT_FOLDER, exist_ok=True)
|
|
| 19 |
os.makedirs(LOG_FOLDER, exist_ok=True)
|
| 20 |
|
| 21 |
# Function to log conversion steps
|
| 22 |
-
def log_conversion(status,
|
| 23 |
-
|
|
|
|
| 24 |
log_file.write(f"{datetime.datetime.now()} - Status: {status}\n")
|
| 25 |
-
log_file.write(f"Input
|
| 26 |
-
log_file.write(f"Output
|
|
|
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
def process_pdf(
|
| 30 |
try:
|
| 31 |
-
# Save
|
| 32 |
-
|
| 33 |
-
with open(
|
| 34 |
-
f.write(
|
| 35 |
-
|
| 36 |
-
# Read the PDF
|
| 37 |
-
reader = PdfReader(input_pdf_path)
|
| 38 |
-
writer = PdfWriter()
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
if "/XObject" in page["/Resources"]:
|
| 43 |
-
xObject = page["/Resources"]["/XObject"].get_object()
|
| 44 |
-
for obj in xObject:
|
| 45 |
-
if xObject[obj]["/Subtype"] == "/Image":
|
| 46 |
-
size = (xObject[obj]["/Width"], xObject[obj]["/Height"])
|
| 47 |
-
data = xObject[obj].get_data()
|
| 48 |
-
img = Image.frombytes("RGB", size, data)
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
width_pixels = int(width_inches * dpi)
|
| 53 |
-
height_pixels = int(height_inches * dpi)
|
| 54 |
-
resized_image = grayscale_image.resize((width_pixels, height_pixels))
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
resized_image.save(processed_image_path)
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
pdf.add_page()
|
| 63 |
-
pdf.image(processed_image_path, x=0, y=0, w=width_inches, h=height_inches)
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
# Log success
|
| 69 |
-
log_conversion("Success", input_pdf_path, OUTPUT_PDF)
|
| 70 |
-
|
| 71 |
-
# Return processed PDF and log file
|
| 72 |
-
return OUTPUT_PDF, LOG_FILE
|
| 73 |
|
| 74 |
except Exception as e:
|
| 75 |
-
log_conversion("Failed",
|
| 76 |
-
return
|
| 77 |
-
|
| 78 |
-
# Gradio
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
return demo
|
| 103 |
-
|
| 104 |
-
# Launch the Gradio app
|
| 105 |
-
if __name__ == "__main__":
|
| 106 |
-
app = gradio_app()
|
| 107 |
-
app.launch()
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
+
from pdf_to_svg import convert_pdf_to_svg
|
| 4 |
+
from svg_editor import apply_transformations
|
| 5 |
+
from svg_to_pdf import convert_svg_to_pdf
|
| 6 |
+
import datetime
|
| 7 |
+
import tempfile
|
| 8 |
|
| 9 |
# Folder paths
|
| 10 |
INPUT_FOLDER = "./input/"
|
| 11 |
OUTPUT_FOLDER = "./output/"
|
| 12 |
LOG_FOLDER = "./logs/"
|
| 13 |
+
SVG_OUTPUT = os.path.join(OUTPUT_FOLDER, "processed_diagram.svg")
|
| 14 |
+
PDF_OUTPUT = os.path.join(OUTPUT_FOLDER, "processed_diagram.pdf")
|
|
|
|
| 15 |
|
| 16 |
# Ensure required folders exist
|
| 17 |
os.makedirs(INPUT_FOLDER, exist_ok=True)
|
|
|
|
| 19 |
os.makedirs(LOG_FOLDER, exist_ok=True)
|
| 20 |
|
| 21 |
# Function to log conversion steps
|
| 22 |
+
def log_conversion(status, input_pdf, output_svg, output_pdf):
|
| 23 |
+
log_path = os.path.join(LOG_FOLDER, "conversion_log.txt")
|
| 24 |
+
with open(log_path, "a") as log_file:
|
| 25 |
log_file.write(f"{datetime.datetime.now()} - Status: {status}\n")
|
| 26 |
+
log_file.write(f"Input PDF: {input_pdf}\n")
|
| 27 |
+
log_file.write(f"Output SVG: {output_svg}\n")
|
| 28 |
+
log_file.write(f"Output PDF: {output_pdf}\n\n")
|
| 29 |
|
| 30 |
+
# Gradio interface function
|
| 31 |
+
def process_pdf(uploaded_file, brightness, text_scale, line_scale, width, height):
|
| 32 |
try:
|
| 33 |
+
# Save uploaded file
|
| 34 |
+
input_pdf = os.path.join(INPUT_FOLDER, "uploaded_file.pdf")
|
| 35 |
+
with open(input_pdf, "wb") as f:
|
| 36 |
+
f.write(uploaded_file.read())
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
# Step 1: Convert PDF to SVG
|
| 39 |
+
convert_pdf_to_svg(input_pdf, SVG_OUTPUT, width, height)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
# Step 2: Apply transformations to the SVG
|
| 42 |
+
apply_transformations(SVG_OUTPUT, brightness, text_scale, line_scale)
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
# Step 3: Convert SVG to PDF
|
| 45 |
+
convert_svg_to_pdf(SVG_OUTPUT, PDF_OUTPUT)
|
|
|
|
| 46 |
|
| 47 |
+
# Step 4: Log the process
|
| 48 |
+
log_conversion("Success", input_pdf, SVG_OUTPUT, PDF_OUTPUT)
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
# Return output files for download
|
| 51 |
+
return (SVG_OUTPUT, PDF_OUTPUT)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
except Exception as e:
|
| 54 |
+
log_conversion("Failed", input_pdf, SVG_OUTPUT, PDF_OUTPUT)
|
| 55 |
+
return str(e)
|
| 56 |
+
|
| 57 |
+
# Gradio interface setup
|
| 58 |
+
inputs = [
|
| 59 |
+
gr.File(label="Upload your PDF file", type="file"),
|
| 60 |
+
gr.Slider(minimum=0.5, maximum=5.0, step=0.5, default=1.0, label="Brightness (0.5 to 5.0)"),
|
| 61 |
+
gr.Slider(minimum=0.5, maximum=2.0, step=0.1, default=1.0, label="Text Size Scale (0.5 to 2.0)"),
|
| 62 |
+
gr.Slider(minimum=0.5, maximum=5.0, step=0.5, default=1.0, label="Line Thickness Scale (0.5 to 5.0)"),
|
| 63 |
+
gr.Number(value=8.0, label="Width (in inches)", precision=0.1),
|
| 64 |
+
gr.Number(value=11.0, label="Height (in inches)", precision=0.1)
|
| 65 |
+
]
|
| 66 |
+
|
| 67 |
+
outputs = [
|
| 68 |
+
gr.File(label="Download SVG", type="file"),
|
| 69 |
+
gr.File(label="Download PDF", type="file")
|
| 70 |
+
]
|
| 71 |
+
|
| 72 |
+
# Launch Gradio interface
|
| 73 |
+
gr.Interface(
|
| 74 |
+
fn=process_pdf,
|
| 75 |
+
inputs=inputs,
|
| 76 |
+
outputs=outputs,
|
| 77 |
+
live=True,
|
| 78 |
+
title="PDF to Editable SVG and PDF Converter",
|
| 79 |
+
description="Upload a PDF to convert it to both an editable SVG and a PDF with transformations applied."
|
| 80 |
+
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|