Spaces:
Sleeping
Sleeping
Update app.py
Browse files18 unmodified lines19202122232422232425262728262728292930313233313234353637383940414234353637384344454642434748495046515253545518 unmodified lines]
# Inference functiondef generate_caption(image): if image.mode != "RGB": image = image.convert("RGB")def generate_captions(images): captions = [] for image in images: 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) # 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)
# Filter the caption to only include construction-related terms filtered_caption = " ".join([word for word in caption.split() if word.lower() in construction_terms]) # Filter the caption to only include construction-related terms filtered_caption = " ".join([word for word in caption.split() if word.lower() in construction_terms]) # If no construction-related terms are found, return a default message if not filtered_caption: filtered_caption = "No construction-related activities detected." captions.append(filtered_caption) # If no construction-related terms are found, return a default message if not filtered_caption: filtered_caption = "No construction-related activities detected." return filtered_caption return captions
# Gradio interfaceiface = gr.Interface( fn=generate_caption, inputs=gr.Image(type="pil"), fn=generate_captions, inputs=gr.Image(type="pil", tool="editor", label="Upload Site Photos", multiple=True), outputs="text", title="Construction Site Image-to-Text Generator", description="Upload a site photo. The model will detect and describe construction activities and materials (e.g., concrete pouring, scaffolding, steel rods)." description="Upload up to 10 site photos. The model will detect and describe construction activities and materials (e.g., concrete pouring, scaffolding, steel rods).", allow_flagging="never")
iface.launch()app.py+23-17