Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from pdf_to_svg import convert_pdf_to_svg
|
| 4 |
+
from svg_to_pdf import convert_svg_to_pdf
|
| 5 |
+
import datetime
|
| 6 |
+
|
| 7 |
+
# Folder paths
|
| 8 |
+
INPUT_FOLDER = "./input/"
|
| 9 |
+
OUTPUT_FOLDER = "./output/"
|
| 10 |
+
LOG_FOLDER = "./logs/"
|
| 11 |
+
SVG_OUTPUT = os.path.join(OUTPUT_FOLDER, "processed_diagram.svg")
|
| 12 |
+
PDF_OUTPUT = os.path.join(OUTPUT_FOLDER, "processed_diagram.pdf")
|
| 13 |
+
|
| 14 |
+
# Ensure required folders exist
|
| 15 |
+
os.makedirs(INPUT_FOLDER, exist_ok=True)
|
| 16 |
+
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
|
| 17 |
+
os.makedirs(LOG_FOLDER, exist_ok=True)
|
| 18 |
+
|
| 19 |
+
# Function to log conversion steps
|
| 20 |
+
def log_conversion(status, input_pdf, output_svg, output_pdf):
|
| 21 |
+
log_path = os.path.join(LOG_FOLDER, "conversion_log.txt")
|
| 22 |
+
with open(log_path, "a") as log_file:
|
| 23 |
+
log_file.write(f"{datetime.datetime.now()} - Status: {status}\n")
|
| 24 |
+
log_file.write(f"Input PDF: {input_pdf}\n")
|
| 25 |
+
log_file.write(f"Output SVG: {output_svg}\n")
|
| 26 |
+
log_file.write(f"Output PDF: {output_pdf}\n\n")
|
| 27 |
+
|
| 28 |
+
# Gradio interface function
|
| 29 |
+
def process_pdf(file, width, height):
|
| 30 |
+
input_pdf = os.path.join(INPUT_FOLDER, "uploaded_file.pdf")
|
| 31 |
+
with open(input_pdf, "wb") as f:
|
| 32 |
+
f.write(file.read())
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
# Step 1: Convert PDF to SVG
|
| 36 |
+
convert_pdf_to_svg(input_pdf, SVG_OUTPUT, width, height)
|
| 37 |
+
|
| 38 |
+
# Step 2: Convert SVG to PDF
|
| 39 |
+
convert_svg_to_pdf(SVG_OUTPUT, PDF_OUTPUT)
|
| 40 |
+
|
| 41 |
+
# Step 3: Log the process
|
| 42 |
+
log_conversion("Success", input_pdf, SVG_OUTPUT, PDF_OUTPUT)
|
| 43 |
+
|
| 44 |
+
return SVG_OUTPUT, PDF_OUTPUT
|
| 45 |
+
except Exception as e:
|
| 46 |
+
log_conversion("Failed", input_pdf, SVG_OUTPUT, PDF_OUTPUT)
|
| 47 |
+
return f"Error processing file: {e}", None
|
| 48 |
+
|
| 49 |
+
# Gradio interface
|
| 50 |
+
iface = gr.Interface(
|
| 51 |
+
fn=process_pdf,
|
| 52 |
+
inputs=[
|
| 53 |
+
gr.File(label="Upload your PDF file", file_types=[".pdf"]),
|
| 54 |
+
gr.Number(label="Width (in inches)", value=8.0),
|
| 55 |
+
gr.Number(label="Height (in inches)", value=11.0),
|
| 56 |
+
],
|
| 57 |
+
outputs=[
|
| 58 |
+
gr.File(label="Download SVG"),
|
| 59 |
+
gr.File(label="Download PDF"),
|
| 60 |
+
],
|
| 61 |
+
title="PDF to Editable SVG and PDF Converter",
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
iface.launch()
|