Spaces:
Running
Running
Isra Info commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,9 +10,9 @@ from huggingface_hub import hf_hub_download
|
|
| 10 |
import warnings
|
| 11 |
warnings.filterwarnings('ignore')
|
| 12 |
|
| 13 |
-
# ============================================
|
| 14 |
-
# 1. LOAD MODELS
|
| 15 |
-
# ============================================
|
| 16 |
print("Loading YOLOv11 model from Hugging Face Hub...")
|
| 17 |
model_path = hf_hub_download(
|
| 18 |
repo_id="IsraInfo2004/drone-detection-yolov11",
|
|
@@ -30,9 +30,9 @@ blip_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image
|
|
| 30 |
blip_model.eval()
|
| 31 |
print("BLIP model loaded successfully.")
|
| 32 |
|
| 33 |
-
# ============================================
|
| 34 |
-
# 2. HEATMAP FUNCTIONS
|
| 35 |
-
# ============================================
|
| 36 |
layer_outputs = {}
|
| 37 |
|
| 38 |
def hook_fn(module, input, output):
|
|
@@ -90,9 +90,9 @@ def generate_heatmap(model, image):
|
|
| 90 |
print(f"Heatmap error: {e}")
|
| 91 |
return None
|
| 92 |
|
| 93 |
-
# ============================================
|
| 94 |
# 3. DYNAMIC CAPTION (BLIP)
|
| 95 |
-
# ============================================
|
| 96 |
def generate_dynamic_caption(image):
|
| 97 |
try:
|
| 98 |
if isinstance(image, np.ndarray):
|
|
@@ -114,9 +114,9 @@ def generate_dynamic_caption(image):
|
|
| 114 |
print(f"Caption error: {e}")
|
| 115 |
return "AI model is analyzing the scene."
|
| 116 |
|
| 117 |
-
# ============================================
|
| 118 |
-
# 4. XAI REPORT (
|
| 119 |
-
# ============================================
|
| 120 |
def build_xai_report(is_drone, confidence, drone_count, processing_time, image_caption):
|
| 121 |
confidence_percent = confidence * 100
|
| 122 |
if is_drone:
|
|
@@ -209,9 +209,9 @@ Scattered activation pattern confirms absence of strong drone-like features.
|
|
| 209 |
"""
|
| 210 |
return report
|
| 211 |
|
| 212 |
-
# ============================================
|
| 213 |
# 5. MAIN PIPELINE FUNCTION
|
| 214 |
-
# ============================================
|
| 215 |
def drone_detection_pipeline(input_image):
|
| 216 |
try:
|
| 217 |
if isinstance(input_image, Image.Image):
|
|
@@ -258,97 +258,49 @@ def drone_detection_pipeline(input_image):
|
|
| 258 |
blank = np.zeros((480, 640, 3), dtype=np.uint8)
|
| 259 |
return blank, blank, error_msg
|
| 260 |
|
| 261 |
-
# ============================================
|
| 262 |
-
# 6. PROFESSIONAL
|
| 263 |
-
# ============================================
|
| 264 |
-
|
| 265 |
-
body {
|
| 266 |
-
background-color: #0a0f1c;
|
| 267 |
-
}
|
| 268 |
-
.gradio-container {
|
| 269 |
-
background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
|
| 270 |
-
font-family: 'Segoe UI', 'Roboto', sans-serif;
|
| 271 |
-
}
|
| 272 |
-
.gr-button-primary {
|
| 273 |
-
background: linear-gradient(90deg, #1e3a8a, #3b82f6) !important;
|
| 274 |
-
border: none !important;
|
| 275 |
-
color: white !important;
|
| 276 |
-
font-weight: 600 !important;
|
| 277 |
-
border-radius: 8px !important;
|
| 278 |
-
padding: 10px 24px !important;
|
| 279 |
-
transition: all 0.2s ease !important;
|
| 280 |
-
}
|
| 281 |
-
.gr-button-primary:hover {
|
| 282 |
-
transform: translateY(-2px);
|
| 283 |
-
box-shadow: 0 10px 20px -5px rgba(59,130,246,0.4);
|
| 284 |
-
}
|
| 285 |
-
.gr-tabs {
|
| 286 |
-
border: none;
|
| 287 |
-
}
|
| 288 |
-
.gr-tabs .tab-nav button {
|
| 289 |
-
background-color: #1e293b !important;
|
| 290 |
-
color: #cbd5e1 !important;
|
| 291 |
-
border-radius: 8px 8px 0 0 !important;
|
| 292 |
-
font-weight: 500 !important;
|
| 293 |
-
}
|
| 294 |
-
.gr-tabs .tab-nav button.selected {
|
| 295 |
-
background-color: #0f172a !important;
|
| 296 |
-
color: #3b82f6 !important;
|
| 297 |
-
border-bottom: 2px solid #3b82f6;
|
| 298 |
-
}
|
| 299 |
-
h1, h2, h3 {
|
| 300 |
-
color: #f0f9ff;
|
| 301 |
-
}
|
| 302 |
-
p, .gr-markdown, label {
|
| 303 |
-
color: #e2e8f0;
|
| 304 |
-
}
|
| 305 |
-
.gr-box, .gr-form {
|
| 306 |
-
background-color: #0f172a !important;
|
| 307 |
-
border: 1px solid #334155 !important;
|
| 308 |
-
border-radius: 12px !important;
|
| 309 |
-
}
|
| 310 |
-
"""
|
| 311 |
-
|
| 312 |
-
with gr.Blocks(title="Drone Detection System", theme=gr.themes.Soft(), css=custom_css) as demo:
|
| 313 |
gr.Markdown(
|
| 314 |
"""
|
| 315 |
<div style="text-align: center; padding: 1rem 0 0.5rem 0;">
|
| 316 |
-
<h1 style="font-weight:
|
| 317 |
-
<p style="
|
| 318 |
</div>
|
| 319 |
"""
|
| 320 |
)
|
| 321 |
|
| 322 |
-
with gr.Row(
|
| 323 |
-
with gr.Column(scale=1
|
| 324 |
-
input_image = gr.Image(label="Upload Image", type="pil"
|
| 325 |
-
|
| 326 |
with gr.Column(scale=2):
|
| 327 |
with gr.Tabs():
|
| 328 |
-
with gr.TabItem("Detection
|
| 329 |
output_image = gr.Image(label="Bounding Boxes")
|
| 330 |
-
with gr.TabItem("
|
| 331 |
-
heatmap_image = gr.Image(label="
|
| 332 |
-
with gr.TabItem("
|
| 333 |
-
report_text = gr.Markdown(label="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 334 |
|
| 335 |
gr.Markdown(
|
| 336 |
"""
|
| 337 |
-
<div style="text-align: center; margin-top: 20px; font-size: 12px; color: #
|
| 338 |
-
<hr
|
| 339 |
-
<p>Powered by YOLOv11, Gradio, Hugging Face Spaces
|
| 340 |
</div>
|
| 341 |
"""
|
| 342 |
)
|
| 343 |
-
|
| 344 |
-
submit_btn.click(
|
| 345 |
-
fn=drone_detection_pipeline,
|
| 346 |
-
inputs=[input_image],
|
| 347 |
-
outputs=[output_image, heatmap_image, report_text]
|
| 348 |
-
)
|
| 349 |
|
| 350 |
-
# ============================================
|
| 351 |
# 7. RUN APP
|
| 352 |
-
# ============================================
|
| 353 |
if __name__ == "__main__":
|
| 354 |
demo.launch()
|
|
|
|
| 10 |
import warnings
|
| 11 |
warnings.filterwarnings('ignore')
|
| 12 |
|
| 13 |
+
# ============================================
|
| 14 |
+
# 1. LOAD MODELS
|
| 15 |
+
# ============================================
|
| 16 |
print("Loading YOLOv11 model from Hugging Face Hub...")
|
| 17 |
model_path = hf_hub_download(
|
| 18 |
repo_id="IsraInfo2004/drone-detection-yolov11",
|
|
|
|
| 30 |
blip_model.eval()
|
| 31 |
print("BLIP model loaded successfully.")
|
| 32 |
|
| 33 |
+
# ============================================
|
| 34 |
+
# 2. HEATMAP FUNCTIONS
|
| 35 |
+
# ============================================
|
| 36 |
layer_outputs = {}
|
| 37 |
|
| 38 |
def hook_fn(module, input, output):
|
|
|
|
| 90 |
print(f"Heatmap error: {e}")
|
| 91 |
return None
|
| 92 |
|
| 93 |
+
# ============================================
|
| 94 |
# 3. DYNAMIC CAPTION (BLIP)
|
| 95 |
+
# ============================================
|
| 96 |
def generate_dynamic_caption(image):
|
| 97 |
try:
|
| 98 |
if isinstance(image, np.ndarray):
|
|
|
|
| 114 |
print(f"Caption error: {e}")
|
| 115 |
return "AI model is analyzing the scene."
|
| 116 |
|
| 117 |
+
# ============================================
|
| 118 |
+
# 4. XAI REPORT (ENGLISH)
|
| 119 |
+
# ============================================
|
| 120 |
def build_xai_report(is_drone, confidence, drone_count, processing_time, image_caption):
|
| 121 |
confidence_percent = confidence * 100
|
| 122 |
if is_drone:
|
|
|
|
| 209 |
"""
|
| 210 |
return report
|
| 211 |
|
| 212 |
+
# ============================================
|
| 213 |
# 5. MAIN PIPELINE FUNCTION
|
| 214 |
+
# ============================================
|
| 215 |
def drone_detection_pipeline(input_image):
|
| 216 |
try:
|
| 217 |
if isinstance(input_image, Image.Image):
|
|
|
|
| 258 |
blank = np.zeros((480, 640, 3), dtype=np.uint8)
|
| 259 |
return blank, blank, error_msg
|
| 260 |
|
| 261 |
+
# ============================================
|
| 262 |
+
# 6. PROFESSIONAL INTERFACE (CLEAN, NO EMOJIS)
|
| 263 |
+
# ============================================
|
| 264 |
+
with gr.Blocks(title="Drone Detection System", theme=gr.themes.Soft()) as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
gr.Markdown(
|
| 266 |
"""
|
| 267 |
<div style="text-align: center; padding: 1rem 0 0.5rem 0;">
|
| 268 |
+
<h1 style="font-weight: 600; margin-bottom: 0.2rem;">Drone Detection System</h1>
|
| 269 |
+
<p style="font-size: 1rem; opacity: 0.8;">YOLOv11 | Explainable AI Heatmap | BLIP Captioning</p>
|
| 270 |
</div>
|
| 271 |
"""
|
| 272 |
)
|
| 273 |
|
| 274 |
+
with gr.Row():
|
| 275 |
+
with gr.Column(scale=1):
|
| 276 |
+
input_image = gr.Image(label="Upload Image", type="pil")
|
| 277 |
+
analyze_btn = gr.Button("Analyze", variant="primary")
|
| 278 |
with gr.Column(scale=2):
|
| 279 |
with gr.Tabs():
|
| 280 |
+
with gr.TabItem("Detection"):
|
| 281 |
output_image = gr.Image(label="Bounding Boxes")
|
| 282 |
+
with gr.TabItem("Heatmap"):
|
| 283 |
+
heatmap_image = gr.Image(label="XAI Heatmap")
|
| 284 |
+
with gr.TabItem("Report"):
|
| 285 |
+
report_text = gr.Markdown(label="XAI Analysis Report")
|
| 286 |
+
|
| 287 |
+
analyze_btn.click(
|
| 288 |
+
fn=drone_detection_pipeline,
|
| 289 |
+
inputs=[input_image],
|
| 290 |
+
outputs=[output_image, heatmap_image, report_text]
|
| 291 |
+
)
|
| 292 |
|
| 293 |
gr.Markdown(
|
| 294 |
"""
|
| 295 |
+
<div style="text-align: center; margin-top: 20px; font-size: 12px; color: #666;">
|
| 296 |
+
<hr>
|
| 297 |
+
<p>Powered by YOLOv11, Gradio, Hugging Face Spaces</p>
|
| 298 |
</div>
|
| 299 |
"""
|
| 300 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
|
| 302 |
+
# ============================================
|
| 303 |
# 7. RUN APP
|
| 304 |
+
# ============================================
|
| 305 |
if __name__ == "__main__":
|
| 306 |
demo.launch()
|