Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,15 +10,32 @@ model.eval()
|
|
| 10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
model.to(device)
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# Inference function
|
| 14 |
def generate_caption(image):
|
| 15 |
if image.mode != "RGB":
|
| 16 |
image = image.convert("RGB")
|
| 17 |
|
|
|
|
| 18 |
inputs = processor(image, return_tensors="pt").to(device, torch.float16)
|
| 19 |
output = model.generate(**inputs, max_new_tokens=50)
|
| 20 |
caption = processor.decode(output[0], skip_special_tokens=True)
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
# Gradio interface
|
| 24 |
iface = gr.Interface(
|
|
@@ -26,7 +43,7 @@ iface = gr.Interface(
|
|
| 26 |
inputs=gr.Image(type="pil"),
|
| 27 |
outputs="text",
|
| 28 |
title="Construction Site Image-to-Text Generator",
|
| 29 |
-
description="Upload a site photo. The model will detect and describe construction activities."
|
| 30 |
)
|
| 31 |
|
| 32 |
iface.launch()
|
|
|
|
| 10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
model.to(device)
|
| 12 |
|
| 13 |
+
# List of construction-related terms
|
| 14 |
+
construction_terms = [
|
| 15 |
+
"concrete", "scaffolding", "steel rods", "piling", "excavation",
|
| 16 |
+
"mixer", "cement", "brickwork", "crane", "rebar", "construction",
|
| 17 |
+
"foundation", "building", "formwork", "drywall", "steel beams",
|
| 18 |
+
"hammer", "saw", "nails", "jackhammer"
|
| 19 |
+
]
|
| 20 |
+
|
| 21 |
# Inference function
|
| 22 |
def generate_caption(image):
|
| 23 |
if image.mode != "RGB":
|
| 24 |
image = image.convert("RGB")
|
| 25 |
|
| 26 |
+
# Preprocess the image and generate a caption
|
| 27 |
inputs = processor(image, return_tensors="pt").to(device, torch.float16)
|
| 28 |
output = model.generate(**inputs, max_new_tokens=50)
|
| 29 |
caption = processor.decode(output[0], skip_special_tokens=True)
|
| 30 |
+
|
| 31 |
+
# Filter the caption to only include construction-related terms
|
| 32 |
+
filtered_caption = " ".join([word for word in caption.split() if word.lower() in construction_terms])
|
| 33 |
+
|
| 34 |
+
# If no construction-related terms are found, return a default message
|
| 35 |
+
if not filtered_caption:
|
| 36 |
+
filtered_caption = "No construction-related activities detected."
|
| 37 |
+
|
| 38 |
+
return filtered_caption
|
| 39 |
|
| 40 |
# Gradio interface
|
| 41 |
iface = gr.Interface(
|
|
|
|
| 43 |
inputs=gr.Image(type="pil"),
|
| 44 |
outputs="text",
|
| 45 |
title="Construction Site Image-to-Text Generator",
|
| 46 |
+
description="Upload a site photo. The model will detect and describe construction activities and materials (e.g., concrete pouring, scaffolding, steel rods)."
|
| 47 |
)
|
| 48 |
|
| 49 |
iface.launch()
|