Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
import
|
| 2 |
from PIL import Image
|
| 3 |
import torch
|
| 4 |
from transformers import pipeline
|
|
@@ -7,140 +7,106 @@ import matplotlib.pyplot as plt
|
|
| 7 |
from reportlab.pdfgen import canvas
|
| 8 |
import io
|
| 9 |
import folium
|
| 10 |
-
from streamlit_folium import st_folium
|
| 11 |
-
import cv2
|
| 12 |
import tempfile
|
|
|
|
| 13 |
import numpy as np
|
| 14 |
|
| 15 |
-
st.set_page_config(page_title="InfraGuard Ultimate – Smart City AI", layout="wide")
|
| 16 |
-
st.title("InfraGuard Ultimate – Live Smart City Incident Detection")
|
| 17 |
-
|
| 18 |
# ---------------- Load Models ----------------
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
|
| 22 |
-
return model
|
| 23 |
-
|
| 24 |
-
@st.cache_resource
|
| 25 |
-
def load_summarizer():
|
| 26 |
-
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 27 |
-
return summarizer
|
| 28 |
-
|
| 29 |
-
detection_model = load_detection_model()
|
| 30 |
-
summarizer = load_summarizer()
|
| 31 |
-
|
| 32 |
-
# ---------------- Sidebar / Tabs ----------------
|
| 33 |
-
tab1, tab2, tab3 = st.tabs(["Live Detection", "History", "About"])
|
| 34 |
|
| 35 |
# ---------- Incident History ----------
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
with
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
icon=folium.Icon(color="red" if row["Severity"]=="High" else "orange" if row["Severity"]=="Medium" else "green")
|
| 131 |
-
).add_to(m)
|
| 132 |
-
st_data = st_folium(m, width=700, height=500)
|
| 133 |
-
|
| 134 |
-
# ---------------- Tab 3: About ----------------
|
| 135 |
-
with tab3:
|
| 136 |
-
st.markdown("""
|
| 137 |
-
**InfraGuard Ultimate – Smart City AI System**
|
| 138 |
-
- Real-time live webcam / video detection
|
| 139 |
-
- Object detection using YOLOv5
|
| 140 |
-
- AI-generated incident summary using BART
|
| 141 |
-
- Severity scoring & real-time alerts
|
| 142 |
-
- Incident history with analytics
|
| 143 |
-
- Interactive map showing all incidents
|
| 144 |
-
- Downloadable PDF incident reports
|
| 145 |
-
- Fully deployable on Hugging Face Spaces
|
| 146 |
-
""")
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
from PIL import Image
|
| 3 |
import torch
|
| 4 |
from transformers import pipeline
|
|
|
|
| 7 |
from reportlab.pdfgen import canvas
|
| 8 |
import io
|
| 9 |
import folium
|
|
|
|
|
|
|
| 10 |
import tempfile
|
| 11 |
+
import cv2
|
| 12 |
import numpy as np
|
| 13 |
|
|
|
|
|
|
|
|
|
|
| 14 |
# ---------------- Load Models ----------------
|
| 15 |
+
detection_model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
|
| 16 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# ---------- Incident History ----------
|
| 19 |
+
history_df = pd.DataFrame(columns=["Image", "Objects", "Severity", "Summary", "Latitude", "Longitude"])
|
| 20 |
+
|
| 21 |
+
# ---------------- Helper Functions ----------------
|
| 22 |
+
def process_image(img, latitude=24.8607, longitude=67.0011):
|
| 23 |
+
global history_df
|
| 24 |
+
image = Image.fromarray(img) if isinstance(img, np.ndarray) else img
|
| 25 |
+
results = detection_model(image)
|
| 26 |
+
detected_objects = results.pandas().xyxy[0]['name'].tolist()
|
| 27 |
+
|
| 28 |
+
# Severity scoring
|
| 29 |
+
severity = "Low"
|
| 30 |
+
if "fire" in detected_objects or "person" in detected_objects and len(detected_objects) > 15:
|
| 31 |
+
severity = "High"
|
| 32 |
+
elif "car" in detected_objects or "truck" in detected_objects:
|
| 33 |
+
severity = "Medium"
|
| 34 |
+
|
| 35 |
+
# AI summary
|
| 36 |
+
incident_text = f"Detected objects: {', '.join(detected_objects)}. Severity: {severity}."
|
| 37 |
+
summary = summarizer(incident_text, max_length=60, min_length=25, do_sample=False)
|
| 38 |
+
ai_summary = summary[0]['summary_text']
|
| 39 |
+
|
| 40 |
+
# Save to history
|
| 41 |
+
history_df = pd.concat([history_df,
|
| 42 |
+
pd.DataFrame([[image, ', '.join(detected_objects), severity, ai_summary, latitude, longitude]],
|
| 43 |
+
columns=["Image", "Objects", "Severity", "Summary", "Latitude", "Longitude"])],
|
| 44 |
+
ignore_index=True)
|
| 45 |
+
|
| 46 |
+
# Prepare image with bounding boxes
|
| 47 |
+
output_image = np.squeeze(results.render())
|
| 48 |
+
|
| 49 |
+
# Prepare PDF
|
| 50 |
+
buffer = io.BytesIO()
|
| 51 |
+
c = canvas.Canvas(buffer)
|
| 52 |
+
c.drawString(100, 800, "InfraGuard Incident Report")
|
| 53 |
+
c.drawString(100, 780, f"Detected Objects: {', '.join(detected_objects)}")
|
| 54 |
+
c.drawString(100, 760, f"Severity: {severity}")
|
| 55 |
+
c.drawString(100, 740, f"AI Summary: {ai_summary}")
|
| 56 |
+
c.drawString(100, 720, f"Location: {latitude}, {longitude}")
|
| 57 |
+
c.save()
|
| 58 |
+
buffer.seek(0)
|
| 59 |
+
|
| 60 |
+
return output_image, ai_summary, severity, buffer
|
| 61 |
+
|
| 62 |
+
def show_history():
|
| 63 |
+
global history_df
|
| 64 |
+
return history_df
|
| 65 |
+
|
| 66 |
+
def show_map():
|
| 67 |
+
global history_df
|
| 68 |
+
m = folium.Map(location=[24.8607, 67.0011], zoom_start=12)
|
| 69 |
+
for idx, row in history_df.iterrows():
|
| 70 |
+
folium.Marker(
|
| 71 |
+
location=[row["Latitude"], row["Longitude"]],
|
| 72 |
+
popup=f"{row['Objects']} | Severity: {row['Severity']}",
|
| 73 |
+
icon=folium.Icon(color="red" if row["Severity"]=="High" else "orange" if row["Severity"]=="Medium" else "green")
|
| 74 |
+
).add_to(m)
|
| 75 |
+
tmp_file = tempfile.NamedTemporaryFile(suffix=".html", delete=False)
|
| 76 |
+
m.save(tmp_file.name)
|
| 77 |
+
return tmp_file.name
|
| 78 |
+
|
| 79 |
+
# ---------------- Gradio Interface ----------------
|
| 80 |
+
with gr.Blocks() as demo:
|
| 81 |
+
gr.Markdown("## InfraGuard Ultimate – Smart City AI (Gradio Version)")
|
| 82 |
+
|
| 83 |
+
with gr.Tab("Live Detection"):
|
| 84 |
+
with gr.Row():
|
| 85 |
+
img_input = gr.Image(type="pil", label="Upload Image / Webcam")
|
| 86 |
+
latitude = gr.Number(value=24.8607, label="Latitude")
|
| 87 |
+
longitude = gr.Number(value=67.0011, label="Longitude")
|
| 88 |
+
with gr.Row():
|
| 89 |
+
detect_btn = gr.Button("Detect Incident")
|
| 90 |
+
with gr.Row():
|
| 91 |
+
output_image = gr.Image(label="Detection Output")
|
| 92 |
+
with gr.Row():
|
| 93 |
+
ai_summary_out = gr.Textbox(label="AI Incident Summary")
|
| 94 |
+
severity_out = gr.Textbox(label="Severity")
|
| 95 |
+
with gr.Row():
|
| 96 |
+
pdf_out = gr.File(label="Download PDF Report")
|
| 97 |
+
|
| 98 |
+
detect_btn.click(process_image,
|
| 99 |
+
inputs=[img_input, latitude, longitude],
|
| 100 |
+
outputs=[output_image, ai_summary_out, severity_out, pdf_out])
|
| 101 |
+
|
| 102 |
+
with gr.Tab("History & Analytics"):
|
| 103 |
+
hist_btn = gr.Button("Show Incident History")
|
| 104 |
+
hist_table = gr.Dataframe()
|
| 105 |
+
hist_btn.click(show_history, outputs=[hist_table])
|
| 106 |
+
|
| 107 |
+
with gr.Tab("Map"):
|
| 108 |
+
map_btn = gr.Button("Show Incident Map")
|
| 109 |
+
map_html = gr.HTML()
|
| 110 |
+
map_btn.click(show_map, outputs=[map_html])
|
| 111 |
+
|
| 112 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|