Update app.py
Browse files
app.py
CHANGED
|
@@ -1,112 +1,67 @@
|
|
| 1 |
-
import
|
| 2 |
import json
|
| 3 |
-
import os
|
| 4 |
from reportlab.lib.pagesizes import letter
|
| 5 |
from reportlab.pdfgen import canvas
|
| 6 |
from reportlab.lib import colors
|
| 7 |
from io import BytesIO
|
| 8 |
-
|
| 9 |
PAGE_WIDTH, PAGE_HEIGHT = letter
|
| 10 |
-
|
| 11 |
-
def
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
pdf_output = BytesIO()
|
| 18 |
-
|
| 19 |
c = canvas.Canvas(pdf_output, pagesize=letter)
|
| 20 |
page_created = False
|
| 21 |
-
|
| 22 |
for ad_size, ad_data in data.items():
|
| 23 |
for item in ad_data:
|
| 24 |
for line in item.get("text_lines", []):
|
| 25 |
-
page_created = True
|
| 26 |
-
|
| 27 |
text = line.get("text", "")
|
| 28 |
-
bbox = line.get("bbox"
|
| 29 |
-
|
| 30 |
if not bbox:
|
| 31 |
continue
|
| 32 |
-
|
| 33 |
x1, y1, x2, y2 = bbox
|
| 34 |
-
|
| 35 |
pdf_x = x1
|
| 36 |
pdf_y = PAGE_HEIGHT - y2
|
| 37 |
-
|
| 38 |
box_height = y2 - y1
|
| 39 |
font_size = max(6, min(18, box_height * 0.8))
|
| 40 |
-
|
| 41 |
-
# Debug box
|
| 42 |
c.setStrokeColor(colors.lightgrey)
|
| 43 |
-
c.rect(
|
| 44 |
-
|
| 45 |
-
PAGE_HEIGHT - y2,
|
| 46 |
-
x2 - x1,
|
| 47 |
-
box_height,
|
| 48 |
-
fill=0
|
| 49 |
-
)
|
| 50 |
-
|
| 51 |
if "<b>" in text:
|
| 52 |
c.setFont("Helvetica-Bold", font_size)
|
| 53 |
text = text.replace("<b>", "").replace("</b>", "")
|
| 54 |
else:
|
| 55 |
c.setFont("Helvetica", font_size)
|
| 56 |
-
|
| 57 |
c.setFillColor(colors.black)
|
| 58 |
-
c.drawString(
|
| 59 |
-
|
| 60 |
-
pdf_y + (box_height - font_size),
|
| 61 |
-
text
|
| 62 |
-
)
|
| 63 |
-
|
| 64 |
if page_created:
|
| 65 |
c.showPage()
|
| 66 |
-
|
| 67 |
-
# 🔥 GUARANTEE at least one page
|
| 68 |
if not page_created:
|
| 69 |
c.setFont("Helvetica", 12)
|
| 70 |
c.drawString(50, 750, "No OCR text found")
|
| 71 |
c.showPage()
|
| 72 |
-
|
| 73 |
c.save()
|
| 74 |
-
|
| 75 |
-
# Move the pointer to the beginning of the stream for download
|
| 76 |
pdf_output.seek(0)
|
| 77 |
-
return pdf_output
|
| 78 |
-
|
| 79 |
-
#
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
# File uploader
|
| 91 |
-
uploaded_file = st.file_uploader("Choose a JSON file", type=["json"])
|
| 92 |
-
|
| 93 |
-
if uploaded_file is not None:
|
| 94 |
-
try:
|
| 95 |
-
data = json.load(uploaded_file)
|
| 96 |
-
st.success("File uploaded successfully!")
|
| 97 |
-
|
| 98 |
-
# Generate PDF from the uploaded data
|
| 99 |
-
pdf_file = generate_pdf(data)
|
| 100 |
-
|
| 101 |
-
# Provide a download link
|
| 102 |
-
st.download_button(
|
| 103 |
-
label="Download PDF",
|
| 104 |
-
data=pdf_file,
|
| 105 |
-
file_name="output_fixed.pdf",
|
| 106 |
-
mime="application/pdf"
|
| 107 |
-
)
|
| 108 |
-
except Exception as e:
|
| 109 |
-
st.error(f"Error processing the file: {e}")
|
| 110 |
-
|
| 111 |
-
if __name__ == "__main__":
|
| 112 |
-
main()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import json
|
|
|
|
| 3 |
from reportlab.lib.pagesizes import letter
|
| 4 |
from reportlab.pdfgen import canvas
|
| 5 |
from reportlab.lib import colors
|
| 6 |
from io import BytesIO
|
| 7 |
+
|
| 8 |
PAGE_WIDTH, PAGE_HEIGHT = letter
|
| 9 |
+
|
| 10 |
+
def generate_pdf(file):
|
| 11 |
+
try:
|
| 12 |
+
data = json.load(file)
|
| 13 |
+
except Exception as e:
|
| 14 |
+
return None, f"Error reading JSON: {e}"
|
| 15 |
+
|
| 16 |
pdf_output = BytesIO()
|
|
|
|
| 17 |
c = canvas.Canvas(pdf_output, pagesize=letter)
|
| 18 |
page_created = False
|
| 19 |
+
|
| 20 |
for ad_size, ad_data in data.items():
|
| 21 |
for item in ad_data:
|
| 22 |
for line in item.get("text_lines", []):
|
|
|
|
|
|
|
| 23 |
text = line.get("text", "")
|
| 24 |
+
bbox = line.get("bbox")
|
|
|
|
| 25 |
if not bbox:
|
| 26 |
continue
|
| 27 |
+
page_created = True
|
| 28 |
x1, y1, x2, y2 = bbox
|
|
|
|
| 29 |
pdf_x = x1
|
| 30 |
pdf_y = PAGE_HEIGHT - y2
|
|
|
|
| 31 |
box_height = y2 - y1
|
| 32 |
font_size = max(6, min(18, box_height * 0.8))
|
| 33 |
+
|
|
|
|
| 34 |
c.setStrokeColor(colors.lightgrey)
|
| 35 |
+
c.rect(x1, PAGE_HEIGHT - y2, x2 - x1, box_height, fill=0)
|
| 36 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
if "<b>" in text:
|
| 38 |
c.setFont("Helvetica-Bold", font_size)
|
| 39 |
text = text.replace("<b>", "").replace("</b>", "")
|
| 40 |
else:
|
| 41 |
c.setFont("Helvetica", font_size)
|
| 42 |
+
|
| 43 |
c.setFillColor(colors.black)
|
| 44 |
+
c.drawString(pdf_x, pdf_y + (box_height - font_size), text)
|
| 45 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
if page_created:
|
| 47 |
c.showPage()
|
| 48 |
+
|
|
|
|
| 49 |
if not page_created:
|
| 50 |
c.setFont("Helvetica", 12)
|
| 51 |
c.drawString(50, 750, "No OCR text found")
|
| 52 |
c.showPage()
|
| 53 |
+
|
| 54 |
c.save()
|
|
|
|
|
|
|
| 55 |
pdf_output.seek(0)
|
| 56 |
+
return pdf_output, "PDF generated successfully!"
|
| 57 |
+
|
| 58 |
+
# Gradio interface
|
| 59 |
+
iface = gr.Interface(
|
| 60 |
+
fn=generate_pdf,
|
| 61 |
+
inputs=gr.File(file_types=[".json"], label="Upload JSON"),
|
| 62 |
+
outputs=[gr.File(label="Download PDF"), gr.Textbox(label="Status")],
|
| 63 |
+
title="JSON to PDF Converter",
|
| 64 |
+
description="Upload a `results.json` file and download the generated PDF."
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|