Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from ultralytics import YOLO
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
|
| 6 |
+
# Load YOLOv8 model (you can use 'yolov8n.pt' for small, or upload a custom model)
|
| 7 |
+
model = YOLO("yolov8n.pt")
|
| 8 |
+
|
| 9 |
+
# Load language model pipeline (can swap with Groq/LLaMA3 API if needed)
|
| 10 |
+
summarizer = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.1")
|
| 11 |
+
|
| 12 |
+
# Function to detect objects
|
| 13 |
+
def detect_objects(image_path):
|
| 14 |
+
results = model(image_path)
|
| 15 |
+
names = results[0].names
|
| 16 |
+
boxes = results[0].boxes
|
| 17 |
+
detected = [names[int(cls)] for cls in boxes.cls]
|
| 18 |
+
return results, detected
|
| 19 |
+
|
| 20 |
+
# Function to generate report from detected objects
|
| 21 |
+
def generate_report(detected_items):
|
| 22 |
+
prompt = f"""
|
| 23 |
+
Generate a construction site report based on the following detected items:
|
| 24 |
+
{', '.join(detected_items)}.
|
| 25 |
+
|
| 26 |
+
Mention safety compliance issues if helmets, vests, or barriers are missing.
|
| 27 |
+
"""
|
| 28 |
+
output = summarizer(prompt, max_length=250, do_sample=True)[0]["generated_text"]
|
| 29 |
+
return output
|
| 30 |
+
|
| 31 |
+
# Streamlit UI
|
| 32 |
+
st.set_page_config(page_title="Photo to Construction Report", layout="centered")
|
| 33 |
+
st.title("📷 Photo to Construction Report Generator")
|
| 34 |
+
|
| 35 |
+
uploaded_image = st.file_uploader("Upload a construction site photo", type=["jpg", "jpeg", "png"])
|
| 36 |
+
|
| 37 |
+
if uploaded_image is not None:
|
| 38 |
+
with open("uploaded.jpg", "wb") as f:
|
| 39 |
+
f.write(uploaded_image.read())
|
| 40 |
+
|
| 41 |
+
st.image(uploaded_image, caption="Uploaded Image", use_column_width=True)
|
| 42 |
+
|
| 43 |
+
with st.spinner("Running object detection..."):
|
| 44 |
+
results, detected_items = detect_objects("uploaded.jpg")
|
| 45 |
+
st.image(results[0].plot(), caption="Detected Objects", use_column_width=True)
|
| 46 |
+
|
| 47 |
+
with st.spinner("Generating AI report..."):
|
| 48 |
+
report = generate_report(detected_items)
|
| 49 |
+
st.subheader("📄 AI-Generated Report")
|
| 50 |
+
st.write(report)
|