Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -13,35 +13,17 @@ model.eval()
|
|
13 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
14 |
model.to(device)
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
-
|
19 |
-
|
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 |
-
#
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
return
|
45 |
|
46 |
# Function to generate the daily progress report
|
47 |
def generate_dpr(files):
|
@@ -59,16 +41,11 @@ def generate_dpr(files):
|
|
59 |
if image.mode != "RGB":
|
60 |
image = image.convert("RGB")
|
61 |
|
62 |
-
#
|
63 |
-
|
64 |
-
output = model.generate(**inputs, max_new_tokens=50)
|
65 |
-
caption = processor.decode(output[0], skip_special_tokens=True)
|
66 |
-
|
67 |
-
# Get detailed construction information based on the caption
|
68 |
-
detailed_caption = detect_construction_info(caption)
|
69 |
|
70 |
-
# Generate DPR section for this image
|
71 |
-
dpr_section = f"\nImage: {file.name}\
|
72 |
dpr_text.append(dpr_section)
|
73 |
|
74 |
# Generate a PDF report
|
@@ -77,7 +54,7 @@ def generate_dpr(files):
|
|
77 |
c.drawString(100, 750, "Daily Progress Report")
|
78 |
c.drawString(100, 730, f"Generated on: {current_time}")
|
79 |
|
80 |
-
# Add the detailed captions for each image to the PDF
|
81 |
y_position = 700
|
82 |
for section in dpr_text:
|
83 |
c.drawString(100, y_position, section)
|
@@ -96,7 +73,7 @@ iface = gr.Interface(
|
|
96 |
inputs=gr.Files(type="filepath", label="Upload Site Photos"), # Handle batch upload of images
|
97 |
outputs="file",
|
98 |
title="Daily Progress Report Generator",
|
99 |
-
description="Upload up to 10 site photos. The AI model will detect construction activities, materials, and progress and generate a PDF report.",
|
100 |
allow_flagging="never" # Optional: Disable flagging
|
101 |
)
|
102 |
|
|
|
13 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
14 |
model.to(device)
|
15 |
|
16 |
+
# Inference function to generate captions from images dynamically
|
17 |
+
def generate_captions_from_image(image):
|
18 |
+
if image.mode != "RGB":
|
19 |
+
image = image.convert("RGB")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
# Preprocess the image and generate a caption
|
22 |
+
inputs = processor(image, return_tensors="pt").to(device, torch.float16)
|
23 |
+
output = model.generate(**inputs, max_new_tokens=50)
|
24 |
+
caption = processor.decode(output[0], skip_special_tokens=True)
|
25 |
+
|
26 |
+
return caption
|
27 |
|
28 |
# Function to generate the daily progress report
|
29 |
def generate_dpr(files):
|
|
|
41 |
if image.mode != "RGB":
|
42 |
image = image.convert("RGB")
|
43 |
|
44 |
+
# Dynamically generate a caption based on the image
|
45 |
+
caption = generate_captions_from_image(image)
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
+
# Generate DPR section for this image with dynamic caption
|
48 |
+
dpr_section = f"\nImage: {file.name}\nDescription: {caption}\n"
|
49 |
dpr_text.append(dpr_section)
|
50 |
|
51 |
# Generate a PDF report
|
|
|
54 |
c.drawString(100, 750, "Daily Progress Report")
|
55 |
c.drawString(100, 730, f"Generated on: {current_time}")
|
56 |
|
57 |
+
# Add the detailed captions for each image to the PDF (in text format)
|
58 |
y_position = 700
|
59 |
for section in dpr_text:
|
60 |
c.drawString(100, y_position, section)
|
|
|
73 |
inputs=gr.Files(type="filepath", label="Upload Site Photos"), # Handle batch upload of images
|
74 |
outputs="file",
|
75 |
title="Daily Progress Report Generator",
|
76 |
+
description="Upload up to 10 site photos. The AI model will dynamically detect construction activities, materials, and progress and generate a PDF report.",
|
77 |
allow_flagging="never" # Optional: Disable flagging
|
78 |
)
|
79 |
|