Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,9 @@ from transformers import BlipProcessor, BlipForConditionalGeneration
|
|
2 |
from PIL import Image
|
3 |
import gradio as gr
|
4 |
import torch
|
|
|
|
|
|
|
5 |
|
6 |
# Load BLIP model and processor
|
7 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
@@ -10,19 +13,40 @@ model.eval()
|
|
10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
model.to(device)
|
12 |
|
13 |
-
#
|
14 |
-
construction_terms =
|
15 |
-
"
|
16 |
-
"
|
17 |
-
"
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
for file in files:
|
27 |
# Open the image from file path
|
28 |
image = Image.open(file.name) # Using file.name for filepath
|
@@ -35,24 +59,39 @@ def generate_captions(files):
|
|
35 |
output = model.generate(**inputs, max_new_tokens=50)
|
36 |
caption = processor.decode(output[0], skip_special_tokens=True)
|
37 |
|
38 |
-
#
|
39 |
-
|
40 |
-
|
41 |
-
# If no construction-related terms are found, return a default message
|
42 |
-
if not filtered_caption:
|
43 |
-
filtered_caption = "No construction-related activities detected."
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
# Gradio interface for uploading multiple files
|
50 |
iface = gr.Interface(
|
51 |
-
fn=
|
52 |
inputs=gr.Files(type="filepath", label="Upload Site Photos"), # Handle batch upload of images
|
53 |
-
outputs="
|
54 |
-
title="
|
55 |
-
description="Upload up to 10 site photos. The model will detect
|
56 |
allow_flagging="never" # Optional: Disable flagging
|
57 |
)
|
58 |
|
|
|
2 |
from PIL import Image
|
3 |
import gradio as gr
|
4 |
import torch
|
5 |
+
from datetime import datetime
|
6 |
+
from reportlab.lib.pagesizes import letter
|
7 |
+
from reportlab.pdfgen import canvas
|
8 |
|
9 |
# Load BLIP model and processor
|
10 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
|
|
13 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
14 |
model.to(device)
|
15 |
|
16 |
+
# Define categories for construction activities and materials
|
17 |
+
construction_terms = {
|
18 |
+
"activities": ["pouring", "scaffolding", "building", "excavation", "piling", "digging", "cementing", "welding", "cutting", "assembling", "drilling"],
|
19 |
+
"materials": ["concrete", "steel", "wood", "bricks", "cement", "sand", "mortar", "rebar", "plaster", "tiles"],
|
20 |
+
"progress": ["completed", "ongoing", "in-progress", "starting", "finished", "under construction"]
|
21 |
+
}
|
22 |
+
|
23 |
+
# Function to detect activities and materials
|
24 |
+
def detect_construction_info(caption):
|
25 |
+
activity_found = []
|
26 |
+
material_found = []
|
27 |
+
progress_found = []
|
28 |
+
|
29 |
+
# Split the caption into words and check for the terms
|
30 |
+
for word in caption.split():
|
31 |
+
word_lower = word.lower()
|
32 |
+
if word_lower in construction_terms["activities"]:
|
33 |
+
activity_found.append(word)
|
34 |
+
elif word_lower in construction_terms["materials"]:
|
35 |
+
material_found.append(word)
|
36 |
+
elif word_lower in construction_terms["progress"]:
|
37 |
+
progress_found.append(word)
|
38 |
|
39 |
+
# Build the informative output
|
40 |
+
activity_str = ", ".join(activity_found) if activity_found else "No specific activities detected."
|
41 |
+
material_str = ", ".join(material_found) if material_found else "No materials detected."
|
42 |
+
progress_str = ", ".join(progress_found) if progress_found else "No progress information available."
|
43 |
+
|
44 |
+
return f"Activities: {activity_str}\nMaterials: {material_str}\nProgress: {progress_str}"
|
45 |
+
|
46 |
+
# Function to generate the daily progress report
|
47 |
+
def generate_dpr(files):
|
48 |
+
dpr_text = []
|
49 |
+
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
50 |
for file in files:
|
51 |
# Open the image from file path
|
52 |
image = Image.open(file.name) # Using file.name for filepath
|
|
|
59 |
output = model.generate(**inputs, max_new_tokens=50)
|
60 |
caption = processor.decode(output[0], skip_special_tokens=True)
|
61 |
|
62 |
+
# Get detailed construction information based on the caption
|
63 |
+
detailed_caption = detect_construction_info(caption)
|
|
|
|
|
|
|
|
|
64 |
|
65 |
+
# Generate DPR section for this image
|
66 |
+
dpr_section = f"Date: {current_time}\nImage: {file.name}\n{detailed_caption}\n\n"
|
67 |
+
dpr_text.append(dpr_section)
|
68 |
+
|
69 |
+
# Generate a PDF report
|
70 |
+
pdf_path = "dpr_report.pdf"
|
71 |
+
c = canvas.Canvas(pdf_path, pagesize=letter)
|
72 |
+
c.drawString(100, 750, "Daily Progress Report")
|
73 |
+
c.drawString(100, 730, f"Generated on: {current_time}")
|
74 |
+
|
75 |
+
# Add the image captions to the PDF
|
76 |
+
y_position = 700
|
77 |
+
for section in dpr_text:
|
78 |
+
c.drawString(100, y_position, section)
|
79 |
+
y_position -= 100 # Move down for the next section
|
80 |
+
if y_position < 100:
|
81 |
+
c.showPage()
|
82 |
+
y_position = 750
|
83 |
+
|
84 |
+
c.save()
|
85 |
+
|
86 |
+
return pdf_path
|
87 |
|
88 |
# Gradio interface for uploading multiple files
|
89 |
iface = gr.Interface(
|
90 |
+
fn=generate_dpr,
|
91 |
inputs=gr.Files(type="filepath", label="Upload Site Photos"), # Handle batch upload of images
|
92 |
+
outputs="file",
|
93 |
+
title="Daily Progress Report Generator",
|
94 |
+
description="Upload up to 10 site photos. The AI model will detect construction activities, materials, and progress and generate a PDF report.",
|
95 |
allow_flagging="never" # Optional: Disable flagging
|
96 |
)
|
97 |
|