Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import fitz # PyMuPDF for PDF manipulation
|
| 2 |
+
import svgwrite # For creating SVGs
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import io
|
| 5 |
+
import tempfile
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Step 1: Extract vector-based content from PDF and save as SVG
|
| 9 |
+
def pdf_to_svg(pdf_file, thickness_scale=1.0):
|
| 10 |
+
try:
|
| 11 |
+
# Open the PDF and load the first page
|
| 12 |
+
pdf_document = fitz.open(pdf_file.name)
|
| 13 |
+
page = pdf_document.load_page(0)
|
| 14 |
+
|
| 15 |
+
# Extract SVG content from the page at a scale of 2x for higher quality
|
| 16 |
+
svg_data = page.get_svg_image(matrix=fitz.Matrix(2, 2)) # 2x scaling
|
| 17 |
+
|
| 18 |
+
# Load SVG content into svgwrite for manipulation
|
| 19 |
+
svg_io = io.StringIO(svg_data)
|
| 20 |
+
dwg = svgwrite.Drawing(filename="output_diagram.svg")
|
| 21 |
+
dwg.embed_svg(svg_io)
|
| 22 |
+
|
| 23 |
+
# Adjust stroke width for each path if thickness_scale > 1
|
| 24 |
+
for element in dwg.elements:
|
| 25 |
+
if 'stroke-width' in element.attrib:
|
| 26 |
+
current_width = float(element.attrib['stroke-width'])
|
| 27 |
+
element.attrib['stroke-width'] = str(current_width * thickness_scale)
|
| 28 |
+
|
| 29 |
+
# Save the final SVG with modifications
|
| 30 |
+
temp_svg_path = tempfile.mktemp(suffix=".svg", prefix="enhanced_diagram_")
|
| 31 |
+
dwg.saveas(temp_svg_path)
|
| 32 |
+
|
| 33 |
+
return temp_svg_path # Return the file path of the saved SVG
|
| 34 |
+
except Exception as e:
|
| 35 |
+
print(f"Error during SVG conversion: {str(e)}")
|
| 36 |
+
return None
|
| 37 |
+
|
| 38 |
+
# Step 2: Hugging Face Gradio Interface
|
| 39 |
+
def adjust_and_convert_pdf(pdf_file, thickness_scale=1.0):
|
| 40 |
+
svg_path = pdf_to_svg(pdf_file, thickness_scale=thickness_scale)
|
| 41 |
+
if svg_path is None:
|
| 42 |
+
return "Error: Unable to convert PDF to editable SVG."
|
| 43 |
+
|
| 44 |
+
return svg_path # Return the SVG file path for download
|
| 45 |
+
|
| 46 |
+
iface = gr.Interface(
|
| 47 |
+
fn=adjust_and_convert_pdf,
|
| 48 |
+
inputs=[
|
| 49 |
+
gr.File(label="Upload PDF"),
|
| 50 |
+
gr.Slider(minimum=1, maximum=3, step=0.1, value=1.0, label="Thickness Scale")
|
| 51 |
+
],
|
| 52 |
+
outputs=gr.File(label="Editable SVG"),
|
| 53 |
+
title="PDF to Editable SVG Converter",
|
| 54 |
+
description="Upload a PDF containing a diagram to convert it to an editable SVG for CorelDRAW. Adjust the thickness scale for line elements as needed."
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
# Launch the interface on Hugging Face Spaces
|
| 58 |
+
iface.launch()
|