Spaces:
Sleeping
Sleeping
File size: 3,082 Bytes
e8994f0 05c5be7 e8994f0 05c5be7 1fcfcb5 e8994f0 05c5be7 e8994f0 1fcfcb5 e8994f0 1fcfcb5 e8994f0 05c5be7 e8994f0 1fcfcb5 05c5be7 e8994f0 1fcfcb5 05c5be7 e8994f0 05c5be7 e8994f0 05c5be7 1fcfcb5 05c5be7 e8994f0 05c5be7 e8994f0 05c5be7 1fcfcb5 05c5be7 1fcfcb5 05c5be7 e8994f0 1fcfcb5 05c5be7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | from fpdf import FPDF
import requests
from PIL import Image
from io import BytesIO
import datetime
def create_pdf_report(prompt, user_image, top_results):
pdf = FPDF()
pdf.set_auto_page_break(auto=True, margin=15)
# ---------------------------------
# Page 1: Cover + Prompt + Gen Image
# ---------------------------------
pdf.add_page()
# Title
pdf.set_font("Arial", 'B', 24)
pdf.cell(0, 15, "Search-Based Influence Analyzer", ln=True, align="L")
# Description
pdf.set_font("Arial", '', 12)
pdf.ln(10)
description = (
"This tool analyzes your generated image and finds the most similar "
"public web images using DuckDuckGo search and semantic embeddings."
)
pdf.multi_cell(0, 10, description)
# Prompt box
pdf.ln(8)
pdf.set_font("Arial", 'B', 14)
pdf.cell(0, 10, "Prompt:", ln=True)
pdf.set_fill_color(240, 240, 240)
pdf.set_font("Arial", '', 12)
pdf.multi_cell(0, 10, prompt, fill=True)
# Generated Image
pdf.ln(8)
pdf.set_font("Arial", 'B', 14)
pdf.cell(0, 10, "Generated Image:", ln=True)
user_image.save("temp_input.jpg")
img_w, img_h = user_image.size
max_w = 160
scale = max_w / img_w
scaled_w = img_w * scale
scaled_h = img_h * scale
x_center = (210 - scaled_w) / 2
pdf.image("temp_input.jpg", x=x_center, y=None, w=scaled_w)
# ---------------------------------
# Page 2: Top Similar Images
# ---------------------------------
pdf.add_page()
pdf.set_font("Arial", 'B', 16)
pdf.cell(0, 10, "Top Similar Images:", ln=True)
pdf.ln(5)
# Grid settings
col_width = 90
row_height = 60
margin = 10
spacing = 10
x_start = margin
y = pdf.get_y()
col = 0
for i, (url, sim) in enumerate(top_results):
try:
response = requests.get(url, timeout=5)
if response.status_code == 200:
img = Image.open(BytesIO(response.content)).convert("RGB")
temp_filename = f"temp_sim_{i}.jpg"
img.save(temp_filename)
scaled_w = col_width
scaled_h = row_height
x = x_start + col * (col_width + spacing)
pdf.image(temp_filename, x=x, y=y, w=scaled_w, h=scaled_h)
# Caption
pdf.set_xy(x, y + scaled_h + 2)
pdf.set_font("Arial", '', 12)
pdf.cell(col_width, 10, f"Similarity: {sim:.3f}", ln=0, align="C")
col += 1
if col >= 2:
col = 0
y += scaled_h + 20
pdf.set_y(y)
pdf.set_x(x_start)
except Exception as e:
print(f"⚠️ Error loading image {url}: {e}")
continue
# Footer with date
pdf.set_y(-15)
pdf.set_font("Arial", 'I', 9)
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
pdf.cell(0, 10, f"Report generated on {timestamp}", 0, 0, "C")
return pdf.output(dest="S").encode("latin1")
|