Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,130 +3,193 @@ from PIL import Image
|
|
| 3 |
import numpy as np
|
| 4 |
import pandas as pd
|
| 5 |
import tempfile
|
|
|
|
|
|
|
| 6 |
from reportlab.pdfgen import canvas
|
| 7 |
from ultralytics import YOLO
|
| 8 |
from transformers import pipeline
|
| 9 |
-
import
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
def generate_map(lat, lon):
|
| 26 |
-
m = folium.Map(location=[lat, lon], zoom_start=
|
| 27 |
folium.Marker([lat, lon], popup="Incident Location").add_to(m)
|
| 28 |
-
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".html")
|
| 29 |
-
m.save(tmp.name)
|
| 30 |
-
return f"<iframe src='{tmp.name}' width='100%' height='350' style='border-radius:8px;border:2px solid #444;'></iframe>"
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
if img is None:
|
| 36 |
-
return None, "Upload
|
| 37 |
|
| 38 |
-
|
| 39 |
-
image_np = np.array(img)
|
| 40 |
|
| 41 |
-
# YOLO
|
| 42 |
-
results = detection_model.predict(
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
| 49 |
severity = "High"
|
| 50 |
-
elif "car" in
|
| 51 |
severity = "Medium"
|
| 52 |
-
else:
|
| 53 |
-
severity = "Low"
|
| 54 |
|
| 55 |
-
# AI
|
| 56 |
-
|
| 57 |
try:
|
| 58 |
-
summary = summarizer(
|
| 59 |
except:
|
| 60 |
-
summary =
|
| 61 |
-
|
| 62 |
-
# Update history
|
| 63 |
-
new_row = pd.DataFrame([[', '.join(names), severity, summary, lat, lon]],
|
| 64 |
-
columns=["Objects","Severity","Summary","Latitude","Longitude"])
|
| 65 |
-
history_df = pd.concat([history_df, new_row], ignore_index=True)
|
| 66 |
|
| 67 |
-
# Annotated
|
| 68 |
annotated = Image.fromarray(results[0].plot())
|
| 69 |
|
| 70 |
# PDF
|
| 71 |
-
|
| 72 |
-
c = canvas.Canvas(pdf_temp.name)
|
| 73 |
-
c.drawString(80, 800, "InfraGuard AI Incident Report")
|
| 74 |
-
c.drawString(80, 770, f"Objects: {', '.join(names)}")
|
| 75 |
-
c.drawString(80, 750, f"Severity: {severity}")
|
| 76 |
-
c.drawString(80, 730, f"Summary: {summary}")
|
| 77 |
-
c.drawString(80, 710, f"Location: {lat}, {lon}")
|
| 78 |
-
c.save()
|
| 79 |
|
| 80 |
# Map
|
| 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 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import pandas as pd
|
| 5 |
import tempfile
|
| 6 |
+
import base64
|
| 7 |
+
import folium
|
| 8 |
from reportlab.pdfgen import canvas
|
| 9 |
from ultralytics import YOLO
|
| 10 |
from transformers import pipeline
|
| 11 |
+
import os
|
| 12 |
|
| 13 |
+
# ------------------------------
|
| 14 |
+
# LOAD MODELS
|
| 15 |
+
# ------------------------------
|
| 16 |
+
detection_model = YOLO("yolov5s.pt")
|
| 17 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 18 |
|
| 19 |
+
# ------------------------------
|
| 20 |
+
# GLOBAL STORAGE
|
| 21 |
+
# ------------------------------
|
| 22 |
+
history = []
|
| 23 |
+
|
| 24 |
+
# ------------------------------
|
| 25 |
+
# COLOR BADGES
|
| 26 |
+
# ------------------------------
|
| 27 |
+
def severity_badge(sev):
|
| 28 |
+
colors = {
|
| 29 |
+
"High": "#ff4b4b",
|
| 30 |
+
"Medium": "#ffa500",
|
| 31 |
+
"Low": "#2ecc71"
|
| 32 |
+
}
|
| 33 |
+
return f"<span style='padding:6px 12px;border-radius:5px;background:{colors[sev]};color:white;font-weight:bold'>{sev}</span>"
|
| 34 |
+
|
| 35 |
+
# ------------------------------
|
| 36 |
+
# MAP GENERATOR
|
| 37 |
+
# ------------------------------
|
| 38 |
def generate_map(lat, lon):
|
| 39 |
+
m = folium.Map(location=[lat, lon], zoom_start=15)
|
| 40 |
folium.Marker([lat, lon], popup="Incident Location").add_to(m)
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
f = tempfile.NamedTemporaryFile(delete=False, suffix=".html")
|
| 43 |
+
m.save(f.name)
|
| 44 |
+
f.close()
|
| 45 |
+
|
| 46 |
+
# Embed map in iframe
|
| 47 |
+
html = f"""<iframe src="{f.name}" width="100%" height="400" style="border:none;"></iframe>"""
|
| 48 |
+
return html
|
| 49 |
+
|
| 50 |
+
# ------------------------------
|
| 51 |
+
# PDF GENERATOR
|
| 52 |
+
# ------------------------------
|
| 53 |
+
def create_pdf(objects, severity, summary, lat, lon):
|
| 54 |
+
f = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
|
| 55 |
+
c = canvas.Canvas(f.name)
|
| 56 |
+
|
| 57 |
+
c.setFont("Helvetica-Bold", 18)
|
| 58 |
+
c.drawString(100, 800, "InfraGuard Incident Report")
|
| 59 |
+
|
| 60 |
+
c.setFont("Helvetica", 12)
|
| 61 |
+
c.drawString(100, 770, f"Objects: {objects}")
|
| 62 |
+
c.drawString(100, 750, f"Severity: {severity}")
|
| 63 |
+
c.drawString(100, 730, f"AI Summary: {summary}")
|
| 64 |
+
c.drawString(100, 710, f"Location: {lat}, {lon}")
|
| 65 |
+
|
| 66 |
+
c.save()
|
| 67 |
+
return f.name
|
| 68 |
+
|
| 69 |
+
# ------------------------------
|
| 70 |
+
# MAIN PROCESS
|
| 71 |
+
# ------------------------------
|
| 72 |
+
def process(img, lat, lon):
|
| 73 |
if img is None:
|
| 74 |
+
return None, "Upload Image First", "N/A", None, None, None, history
|
| 75 |
|
| 76 |
+
img_np = np.array(img)
|
|
|
|
| 77 |
|
| 78 |
+
# YOLO detection
|
| 79 |
+
results = detection_model.predict(img_np, imgsz=640)
|
| 80 |
+
objs = []
|
| 81 |
+
for box in results[0].boxes:
|
| 82 |
+
objs.append(detection_model.names[int(box.cls)])
|
| 83 |
|
| 84 |
+
objects_text = ", ".join(objs) if objs else "None"
|
| 85 |
+
|
| 86 |
+
# Severity logic
|
| 87 |
+
severity = "Low"
|
| 88 |
+
if "fire" in objs:
|
| 89 |
severity = "High"
|
| 90 |
+
elif "car" in objs or "truck" in objs:
|
| 91 |
severity = "Medium"
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
# AI summary
|
| 94 |
+
incident_text = f"Detected objects: {objects_text}. Severity: {severity}"
|
| 95 |
try:
|
| 96 |
+
summary = summarizer(incident_text, max_length=40, min_length=10, do_sample=False)[0]['summary_text']
|
| 97 |
except:
|
| 98 |
+
summary = incident_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
+
# Annotated Image
|
| 101 |
annotated = Image.fromarray(results[0].plot())
|
| 102 |
|
| 103 |
# PDF
|
| 104 |
+
pdf_path = create_pdf(objects_text, severity, summary, lat, lon)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
# Map
|
| 107 |
+
map_html = generate_map(lat, lon)
|
| 108 |
+
|
| 109 |
+
# Save to history
|
| 110 |
+
thumb = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
| 111 |
+
img.save(thumb.name)
|
| 112 |
+
history.append({
|
| 113 |
+
"Image": thumb.name,
|
| 114 |
+
"Objects": objects_text,
|
| 115 |
+
"Severity": severity,
|
| 116 |
+
"Summary": summary,
|
| 117 |
+
"Latitude": lat,
|
| 118 |
+
"Longitude": lon
|
| 119 |
+
})
|
| 120 |
+
|
| 121 |
+
# History Table
|
| 122 |
+
hist_df = pd.DataFrame(history)
|
| 123 |
+
|
| 124 |
+
# Timeline Card
|
| 125 |
+
card_html = f"""
|
| 126 |
+
<div style='background:#0a1a2b;padding:15px;margin:10px;border-radius:10px;border:1px solid #0ff;'>
|
| 127 |
+
<h3 style='color:#00eaff'>New Incident Logged</h3>
|
| 128 |
+
<p><b>Objects:</b> {objects_text}</p>
|
| 129 |
+
<p><b>Severity:</b> {severity_badge(severity)}</p>
|
| 130 |
+
<p><b>Summary:</b> {summary}</p>
|
| 131 |
+
<p><b>Location:</b> {lat}, {lon}</p>
|
| 132 |
+
</div>
|
| 133 |
+
"""
|
| 134 |
+
|
| 135 |
+
return annotated, summary, severity, pdf_path, map_html, card_html, hist_df
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
# ------------------------------
|
| 139 |
+
# CUSTOM CSS (CYBER BLUE THEME)
|
| 140 |
+
# ------------------------------
|
| 141 |
+
cyber_css = """
|
| 142 |
+
body { background-color: #051622 !important; }
|
| 143 |
+
|
| 144 |
+
.gradio-container {
|
| 145 |
+
background: #051622 !important;
|
| 146 |
+
color: white !important;
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
h1 {
|
| 150 |
+
color: #00eaff !important;
|
| 151 |
+
text-align:center;
|
| 152 |
+
font-family: 'Arial Black';
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
.gr-button {
|
| 156 |
+
background:#00eaff !important;
|
| 157 |
+
color:black !important;
|
| 158 |
+
border-radius:8px;
|
| 159 |
+
font-weight:bold;
|
| 160 |
+
border:none !important;
|
| 161 |
+
}
|
| 162 |
+
"""
|
| 163 |
+
|
| 164 |
+
# ------------------------------
|
| 165 |
+
# UI LAYOUT
|
| 166 |
+
# ------------------------------
|
| 167 |
+
with gr.Blocks(css=cyber_css, theme=gr.themes.Soft()) as demo:
|
| 168 |
+
gr.HTML("<h1>π₯ INFRA GUARD β Cyber Security AI Dashboard</h1>")
|
| 169 |
+
|
| 170 |
+
with gr.Row():
|
| 171 |
+
with gr.Column(scale=1):
|
| 172 |
+
img_in = gr.Image(type="pil", label="Upload Scene")
|
| 173 |
+
lat = gr.Number(value=24.8607, label="Latitude")
|
| 174 |
+
lon = gr.Number(value=67.0011, label="Longitude")
|
| 175 |
+
btn = gr.Button("Analyze Incident")
|
| 176 |
+
with gr.Column(scale=1):
|
| 177 |
+
img_out = gr.Image(label="Annotated Image")
|
| 178 |
+
summary = gr.Textbox(label="AI Summary")
|
| 179 |
+
severity = gr.Textbox(label="Severity")
|
| 180 |
+
pdf = gr.File(label="Download PDF Report")
|
| 181 |
+
map_view = gr.HTML(label="Incident Map")
|
| 182 |
+
|
| 183 |
+
gr.Markdown("### π Timeline Feed")
|
| 184 |
+
timeline = gr.HTML()
|
| 185 |
+
|
| 186 |
+
gr.Markdown("### π Incident History")
|
| 187 |
+
hist_table = gr.Dataframe(headers=["Image", "Objects", "Severity", "Summary", "Latitude", "Longitude"], value=[])
|
| 188 |
+
|
| 189 |
+
btn.click(
|
| 190 |
+
process,
|
| 191 |
+
inputs=[img_in, lat, lon],
|
| 192 |
+
outputs=[img_out, summary, severity, pdf, map_view, timeline, hist_table]
|
| 193 |
+
)
|
| 194 |
+
|
| 195 |
+
demo.launch()
|