Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import torch
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# Load YOLOv5 model from Ultralytics
|
| 10 |
+
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
|
| 11 |
+
|
| 12 |
+
# Load GPT-2 text generation pipeline
|
| 13 |
+
text_generator = pipeline('text-generation', model='gpt2')
|
| 14 |
+
|
| 15 |
+
def generate_report(image):
|
| 16 |
+
# Run YOLOv5 detection
|
| 17 |
+
results = model(image)
|
| 18 |
+
labels = results.pandas().xyxy[0]['name'].tolist()
|
| 19 |
+
|
| 20 |
+
if not labels:
|
| 21 |
+
return "No relevant objects detected in the image. Please try a clearer construction site photo."
|
| 22 |
+
|
| 23 |
+
# Prepare prompt for report generation
|
| 24 |
+
prompt = (
|
| 25 |
+
f"The following objects were detected at the construction site: {', '.join(labels)}.\n"
|
| 26 |
+
"Based on this, write a brief site progress report or highlight any visible safety concerns."
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
# Generate report using GPT-2
|
| 30 |
+
report = text_generator(prompt, max_length=150, do_sample=True, temperature=0.7)[0]['generated_text']
|
| 31 |
+
|
| 32 |
+
return report
|
| 33 |
+
|
| 34 |
+
# Gradio interface
|
| 35 |
+
interface = gr.Interface(
|
| 36 |
+
fn=generate_report,
|
| 37 |
+
inputs=gr.Image(type="pil", label="Upload Construction Site Photo"),
|
| 38 |
+
outputs=gr.Textbox(label="AI Generated Report"),
|
| 39 |
+
title="📸 Photo to Construction Report Generator",
|
| 40 |
+
description="Upload a construction site photo and get an AI-generated progress report or safety checklist based on detected objects.",
|
| 41 |
+
theme="default"
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
if __name__ == "__main__":
|
| 45 |
+
interface.launch()
|