Spaces:
Sleeping
Sleeping
File size: 4,668 Bytes
df97270 7d8ec5e 4bdf8dd 49702ec 5fc5e6a c4e3ea5 df97270 3dfd15f df97270 c4e3ea5 5fc5e6a 49702ec 4e27730 f41a948 4e27730 c4fefad 74a72c4 df97270 74a72c4 df97270 ef4e447 df97270 ef4e447 df97270 49702ec 5fc5e6a 2552b80 49702ec 2552b80 4bdf8dd 49702ec 74a72c4 2552b80 49702ec 2552b80 cc1155e c4e3ea5 5fc5e6a |
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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
from transformers import BlipProcessor, BlipForConditionalGeneration
from PIL import Image
import gradio as gr
import torch
from datetime import datetime
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib import colors
# Load BLIP model and processor
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
model.eval()
device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)
# Inference function to generate captions dynamically based on image content
def generate_captions_from_image(image):
if image.mode != "RGB":
image = image.convert("RGB")
# Preprocess the image and generate a caption
inputs = processor(image, return_tensors="pt").to(device, torch.float16)
output = model.generate(**inputs, max_new_tokens=50)
caption = processor.decode(output[0], skip_special_tokens=True)
return caption
# Function to save DPR text to a PDF file
def save_dpr_to_pdf(dpr_text, filename):
try:
# Create a PDF document
doc = SimpleDocTemplate(filename, pagesize=letter)
styles = getSampleStyleSheet()
# Define custom styles
title_style = ParagraphStyle(
name='Title',
fontSize=16,
leading=20,
alignment=1, # Center
spaceAfter=20,
textColor=colors.black,
fontName='Helvetica-Bold'
)
body_style = ParagraphStyle(
name='Body',
fontSize=12,
leading=14,
spaceAfter=10,
textColor=colors.black,
fontName='Helvetica'
)
# Build the PDF content
flowables = []
# Add title
flowables.append(Paragraph("Daily Progress Report", title_style))
# Split DPR text into lines and add as paragraphs
for line in dpr_text.split('\n'):
# Replace problematic characters for PDF
line = line.replace('\u2019', "'").replace('\u2018', "'")
if line.strip():
flowables.append(Paragraph(line, body_style))
else:
flowables.append(Spacer(1, 12))
# Build the PDF
doc.build(flowables)
return f"PDF saved successfully as {filename}"
except Exception as e:
return f"Error saving PDF: {str(e)}"
# Function to generate the daily progress report (DPR) text and save as PDF
def generate_dpr(files):
dpr_text = []
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Add header to the DPR
dpr_text.append(f"Daily Progress Report\nGenerated on: {current_time}\n")
# Process each uploaded file (image)
for file in files:
# Open the image from file path
image = Image.open(file.name) # Using file.name for filepath
if image.mode != "RGB":
image = image.convert("RGB")
# Dynamically generate a caption based on the image
caption = generate_captions_from_image(image)
# Generate DPR section for this image with dynamic caption
dpr_section = f"\nImage: {file.name}\nDescription: {caption}\n"
dpr_text.append(dpr_section)
# Combine DPR text
dpr_output = "\n".join(dpr_text)
# Generate PDF filename with timestamp
pdf_filename = f"DPR_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.pdf"
# Save DPR text to PDF
pdf_result = save_dpr_to_pdf(dpr_output, pdf_filename)
# Return the DPR text and the PDF file path for download
return dpr_output + f"\n\n{pdf_result}", pdf_filename
# Gradio interface for uploading multiple files, displaying DPR, and downloading PDF
iface = gr.Interface(
fn=generate_dpr,
inputs=gr.Files(type="filepath", label="Upload Site Photos"), # Handle batch upload of images
outputs=[
gr.Textbox(label="Daily Progress Report"), # Display DPR text
gr.File(label="Download PDF") # Provide PDF download link
],
title="Daily Progress Report Generator",
description="Upload up to 10 site photos. The AI model will dynamically detect construction activities, materials, and progress and generate a text-based Daily Progress Report (DPR). The DPR will also be saved as a PDF, which you can download.",
allow_flagging="never" # Optional: Disable flagging
)
if __name__ == "__main__":
iface.launch() |