Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,10 @@ from PIL import Image
|
|
| 3 |
import gradio as gr
|
| 4 |
import torch
|
| 5 |
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Load BLIP model and processor
|
| 8 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
|
@@ -23,7 +27,54 @@ def generate_captions_from_image(image):
|
|
| 23 |
|
| 24 |
return caption
|
| 25 |
|
| 26 |
-
# Function to
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
def generate_dpr(files):
|
| 28 |
dpr_text = []
|
| 29 |
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
@@ -45,9 +96,18 @@ def generate_dpr(files):
|
|
| 45 |
# Generate DPR section for this image with dynamic caption
|
| 46 |
dpr_section = f"\nImage: {file.name}\nDescription: {caption}\n"
|
| 47 |
dpr_text.append(dpr_section)
|
| 48 |
-
|
| 49 |
-
#
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
# Gradio interface for uploading multiple files and displaying the text-based DPR
|
| 53 |
iface = gr.Interface(
|
|
@@ -55,8 +115,9 @@ iface = gr.Interface(
|
|
| 55 |
inputs=gr.Files(type="filepath", label="Upload Site Photos"), # Handle batch upload of images
|
| 56 |
outputs="text", # Display the DPR as text in the output section
|
| 57 |
title="Daily Progress Report Generator",
|
| 58 |
-
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).",
|
| 59 |
allow_flagging="never" # Optional: Disable flagging
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
|
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
import torch
|
| 5 |
from datetime import datetime
|
| 6 |
+
from reportlab.lib.pagesizes import letter
|
| 7 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
| 8 |
+
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
| 9 |
+
from reportlab.lib import colors
|
| 10 |
|
| 11 |
# Load BLIP model and processor
|
| 12 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
|
|
|
| 27 |
|
| 28 |
return caption
|
| 29 |
|
| 30 |
+
# Function to save DPR text to a PDF file
|
| 31 |
+
def save_dpr_to_pdf(dpr_text, filename):
|
| 32 |
+
try:
|
| 33 |
+
# Create a PDF document
|
| 34 |
+
doc = SimpleDocTemplate(filename, pagesize=letter)
|
| 35 |
+
styles = getSampleStyleSheet()
|
| 36 |
+
|
| 37 |
+
# Define custom styles
|
| 38 |
+
title_style = ParagraphStyle(
|
| 39 |
+
name='Title',
|
| 40 |
+
fontSize=16,
|
| 41 |
+
leading=20,
|
| 42 |
+
alignment=1, # Center
|
| 43 |
+
spaceAfter=20,
|
| 44 |
+
textColor=colors.black,
|
| 45 |
+
fontName='Helvetica-Bold'
|
| 46 |
+
)
|
| 47 |
+
body_style = ParagraphStyle(
|
| 48 |
+
name='Body',
|
| 49 |
+
fontSize=12,
|
| 50 |
+
leading=14,
|
| 51 |
+
spaceAfter=10,
|
| 52 |
+
textColor=colors.black,
|
| 53 |
+
fontName='Helvetica'
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
# Build the PDF content
|
| 57 |
+
flowables = []
|
| 58 |
+
|
| 59 |
+
# Add title
|
| 60 |
+
flowables.append(Paragraph("Daily Progress Report", title_style))
|
| 61 |
+
|
| 62 |
+
# Split DPR text into lines and add as paragraphs
|
| 63 |
+
for line in dpr_text.split('\n'):
|
| 64 |
+
# Replace problematic characters for PDF
|
| 65 |
+
line = line.replace('\u2019', "'").replace('\u2018', "'")
|
| 66 |
+
if line.strip():
|
| 67 |
+
flowables.append(Paragraph(line, body_style))
|
| 68 |
+
else:
|
| 69 |
+
flowables.append(Spacer(1, 12))
|
| 70 |
+
|
| 71 |
+
# Build the PDF
|
| 72 |
+
doc.build(flowables)
|
| 73 |
+
return f"PDF saved successfully as {filename}"
|
| 74 |
+
except Exception as e:
|
| 75 |
+
return f"Error saving PDF: {str(e)}"
|
| 76 |
+
|
| 77 |
+
# Function to generate the daily progress report (DPR) text and save as PDF
|
| 78 |
def generate_dpr(files):
|
| 79 |
dpr_text = []
|
| 80 |
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
| 96 |
# Generate DPR section for this image with dynamic caption
|
| 97 |
dpr_section = f"\nImage: {file.name}\nDescription: {caption}\n"
|
| 98 |
dpr_text.append(dpr_section)
|
| 99 |
+
|
| 100 |
+
# Combine DPR text
|
| 101 |
+
dpr_output = "\n".join(dpr_text)
|
| 102 |
+
|
| 103 |
+
# Generate PDF filename with timestamp
|
| 104 |
+
pdf_filename = f"DPR_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.pdf"
|
| 105 |
+
|
| 106 |
+
# Save DPR text to PDF
|
| 107 |
+
pdf_result = save_dpr_to_pdf(dpr_output, pdf_filename)
|
| 108 |
+
|
| 109 |
+
# Return the DPR text and PDF save status
|
| 110 |
+
return f"{dpr_output}\n\n{pdf_result}"
|
| 111 |
|
| 112 |
# Gradio interface for uploading multiple files and displaying the text-based DPR
|
| 113 |
iface = gr.Interface(
|
|
|
|
| 115 |
inputs=gr.Files(type="filepath", label="Upload Site Photos"), # Handle batch upload of images
|
| 116 |
outputs="text", # Display the DPR as text in the output section
|
| 117 |
title="Daily Progress Report Generator",
|
| 118 |
+
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.",
|
| 119 |
allow_flagging="never" # Optional: Disable flagging
|
| 120 |
)
|
| 121 |
|
| 122 |
+
if __name__ == "__main__":
|
| 123 |
+
iface.launch()
|