Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| from PIL import Image | |
| from ultralytics import YOLO | |
| from transformers import pipeline | |
| # Load YOLOv8 model (you can use 'yolov8n.pt' for small, or upload a custom model) | |
| model = YOLO("yolov8n.pt") | |
| # Load language model pipeline (can swap with Groq/LLaMA3 API if needed) | |
| summarizer = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.1") | |
| # Function to detect objects | |
| def detect_objects(image_path): | |
| results = model(image_path) | |
| names = results[0].names | |
| boxes = results[0].boxes | |
| detected = [names[int(cls)] for cls in boxes.cls] | |
| return results, detected | |
| # Function to generate report from detected objects | |
| def generate_report(detected_items): | |
| prompt = f""" | |
| Generate a construction site report based on the following detected items: | |
| {', '.join(detected_items)}. | |
| Mention safety compliance issues if helmets, vests, or barriers are missing. | |
| """ | |
| output = summarizer(prompt, max_length=250, do_sample=True)[0]["generated_text"] | |
| return output | |
| # Streamlit UI | |
| st.set_page_config(page_title="Photo to Construction Report", layout="centered") | |
| st.title("π· Photo to Construction Report Generator") | |
| uploaded_image = st.file_uploader("Upload a construction site photo", type=["jpg", "jpeg", "png"]) | |
| if uploaded_image is not None: | |
| with open("uploaded.jpg", "wb") as f: | |
| f.write(uploaded_image.read()) | |
| st.image(uploaded_image, caption="Uploaded Image", use_column_width=True) | |
| with st.spinner("Running object detection..."): | |
| results, detected_items = detect_objects("uploaded.jpg") | |
| st.image(results[0].plot(), caption="Detected Objects", use_column_width=True) | |
| with st.spinner("Generating AI report..."): | |
| report = generate_report(detected_items) | |
| st.subheader("π AI-Generated Report") | |
| st.write(report) | |