Spaces:
Sleeping
Sleeping
| 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") | |