Spaces:
Runtime error
Runtime error
Commit
·
41e80e1
1
Parent(s):
e901714
updated app.py
Browse files
app.py
CHANGED
|
@@ -15,6 +15,7 @@ import pandas as pd
|
|
| 15 |
from reportlab.lib.pagesizes import letter
|
| 16 |
from reportlab.pdfgen import canvas
|
| 17 |
import io
|
|
|
|
| 18 |
|
| 19 |
import yake
|
| 20 |
import tempfile
|
|
@@ -108,29 +109,41 @@ def ocr_image(image: Image.Image) -> dict:
|
|
| 108 |
def generate_pdf(results: dict) -> str:
|
| 109 |
buffer = io.BytesIO()
|
| 110 |
c = canvas.Canvas(buffer, pagesize=letter)
|
|
|
|
| 111 |
c.setFont("Helvetica", 12)
|
| 112 |
-
|
|
|
|
| 113 |
|
| 114 |
-
y =
|
|
|
|
| 115 |
for key, value in results.items():
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
c.save()
|
| 123 |
buffer.seek(0)
|
| 124 |
|
| 125 |
-
# ✅ Save to a temp file and return path instead of buffer
|
| 126 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
|
| 127 |
tmp.write(buffer.getvalue())
|
| 128 |
tmp_path = tmp.name
|
| 129 |
|
| 130 |
return tmp_path
|
| 131 |
-
return tmp_path # ✅ returns file path (Gradio-friendly)
|
| 132 |
|
| 133 |
|
|
|
|
| 134 |
# ------------------------------
|
| 135 |
# 3. Multi‑Modal Analysis Function
|
| 136 |
# ------------------------------
|
|
|
|
| 15 |
from reportlab.lib.pagesizes import letter
|
| 16 |
from reportlab.pdfgen import canvas
|
| 17 |
import io
|
| 18 |
+
from textwrap import wrap
|
| 19 |
|
| 20 |
import yake
|
| 21 |
import tempfile
|
|
|
|
| 109 |
def generate_pdf(results: dict) -> str:
|
| 110 |
buffer = io.BytesIO()
|
| 111 |
c = canvas.Canvas(buffer, pagesize=letter)
|
| 112 |
+
width, height = letter
|
| 113 |
c.setFont("Helvetica", 12)
|
| 114 |
+
|
| 115 |
+
c.drawString(50, height - 50, "AI Multi-Modal Assistant Report")
|
| 116 |
|
| 117 |
+
y = height -80 # Starting y position
|
| 118 |
+
|
| 119 |
for key, value in results.items():
|
| 120 |
+
# Convert any complex types to string
|
| 121 |
+
if isinstance(value, (list, dict)):
|
| 122 |
+
value = str(value)
|
| 123 |
+
|
| 124 |
+
# Wrap long lines (max 90 chars per line)
|
| 125 |
+
wrapped_lines = wrap(f"{key}: {value}", width=90)
|
| 126 |
+
for line in wrapped_lines:
|
| 127 |
+
c.drawString(50, y, line)
|
| 128 |
+
y -= 18
|
| 129 |
+
if y < 60: # New page if needed
|
| 130 |
+
c.showPage()
|
| 131 |
+
c.setFont("Helvetica", 12)
|
| 132 |
+
y = height - 50
|
| 133 |
+
|
| 134 |
+
y -= 10 # Space between entries
|
| 135 |
|
| 136 |
c.save()
|
| 137 |
buffer.seek(0)
|
| 138 |
|
|
|
|
| 139 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
|
| 140 |
tmp.write(buffer.getvalue())
|
| 141 |
tmp_path = tmp.name
|
| 142 |
|
| 143 |
return tmp_path
|
|
|
|
| 144 |
|
| 145 |
|
| 146 |
+
|
| 147 |
# ------------------------------
|
| 148 |
# 3. Multi‑Modal Analysis Function
|
| 149 |
# ------------------------------
|