Isra Info commited on
Commit
071fc64
Β·
verified Β·
1 Parent(s): 71c5684

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -29
app.py CHANGED
@@ -11,7 +11,7 @@ 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(
@@ -31,7 +31,7 @@ blip_model.eval()
31
  print("BLIP model loaded successfully.")
32
 
33
  # ============================================================
34
- # 2. HEATMAP FUNCTIONS
35
  # ============================================================
36
  layer_outputs = {}
37
 
@@ -115,7 +115,7 @@ def generate_dynamic_caption(image):
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
@@ -259,42 +259,93 @@ def drone_detection_pipeline(input_image):
259
  return blank, blank, error_msg
260
 
261
  # ============================================================
262
- # 6. GRADIO INTERFACE (ENGLISH)
263
  # ============================================================
264
- with gr.Blocks(title="Drone Detection with XAI", theme=gr.themes.Soft()) as demo:
265
- gr.Markdown("""
266
- <div style="text-align: center;">
267
- <h1>🚁 Drone Detection System with Explainable AI (XAI)</h1>
268
- <p>Using <strong>YOLOv11</strong> for detection, <strong>heatmap</strong> for neural network focus visualization, and <strong>BLIP</strong> for dynamic image captioning.</p>
269
- <p>πŸ“Œ <strong>Note:</strong> The heatmap is extracted from the deepest convolutional layer of YOLOv11, showing where the model focuses to make decisions.</p>
270
- </div>
271
- """)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
272
 
273
- with gr.Row():
274
- with gr.Column(scale=1):
275
- input_image = gr.Image(label="πŸ“Έ Upload an image for analysis", type="pil")
276
- submit_btn = gr.Button("Start Analysis", variant="primary", size="lg")
277
  with gr.Column(scale=2):
278
  with gr.Tabs():
279
- with gr.TabItem("πŸ” Detection Result"):
280
- output_image = gr.Image(label="Image with bounding boxes")
281
- with gr.TabItem("πŸ”₯ XAI Heatmap"):
282
- heatmap_image = gr.Image(label="Neural activation heatmap (Red = high focus)")
283
- with gr.TabItem("πŸ“„ Detailed XAI Report"):
284
- report_text = gr.Markdown(label="Complete report")
 
 
 
 
 
 
 
 
 
285
 
286
  submit_btn.click(
287
  fn=drone_detection_pipeline,
288
  inputs=[input_image],
289
  outputs=[output_image, heatmap_image, report_text]
290
  )
291
-
292
- gr.Markdown("""
293
- <div style="text-align: center; margin-top: 30px; font-size: 12px; color: gray;">
294
- <hr>
295
- <p>Developed with YOLOv11, Gradio, Hugging Face Spaces πŸ€— | Model hosted on Hugging Face Hub</p>
296
- </div>
297
- """)
298
 
299
  # ============================================================
300
  # 7. RUN APP
 
11
  warnings.filterwarnings('ignore')
12
 
13
  # ============================================================
14
+ # 1. LOAD MODELS (same as before)
15
  # ============================================================
16
  print("Loading YOLOv11 model from Hugging Face Hub...")
17
  model_path = hf_hub_download(
 
31
  print("BLIP model loaded successfully.")
32
 
33
  # ============================================================
34
+ # 2. HEATMAP FUNCTIONS (unchanged)
35
  # ============================================================
36
  layer_outputs = {}
37
 
 
115
  return "AI model is analyzing the scene."
116
 
117
  # ============================================================
118
+ # 4. XAI REPORT (English, professional)
119
  # ============================================================
120
  def build_xai_report(is_drone, confidence, drone_count, processing_time, image_caption):
121
  confidence_percent = confidence * 100
 
259
  return blank, blank, error_msg
260
 
261
  # ============================================================
262
+ # 6. PROFESSIONAL GRADIO INTERFACE (NO EMOJIS, AI COLORS)
263
  # ============================================================
264
+ custom_css = """
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: 700; letter-spacing: -0.5px; margin-bottom: 0.2rem;">Drone Detection System</h1>
317
+ <p style="color: #94a3b8; font-size: 1rem;">YOLOv11 Object Detection | Explainable AI Heatmap | BLIP Captioning</p>
318
+ </div>
319
+ """
320
+ )
321
 
322
+ with gr.Row(equal_height=False):
323
+ with gr.Column(scale=1, min_width=300):
324
+ input_image = gr.Image(label="Upload Image", type="pil", elem_classes="gr-box")
325
+ submit_btn = gr.Button("Analyze", variant="primary", size="lg")
326
  with gr.Column(scale=2):
327
  with gr.Tabs():
328
+ with gr.TabItem("Detection Result"):
329
+ output_image = gr.Image(label="Bounding Boxes")
330
+ with gr.TabItem("XAI Heatmap"):
331
+ heatmap_image = gr.Image(label="Neural Activation Heatmap")
332
+ with gr.TabItem("XAI Report"):
333
+ report_text = gr.Markdown(label="Interpretability Report")
334
+
335
+ gr.Markdown(
336
+ """
337
+ <div style="text-align: center; margin-top: 20px; font-size: 12px; color: #475569;">
338
+ <hr style="border-color: #334155;">
339
+ <p>Powered by YOLOv11, Gradio, Hugging Face Spaces | Model hosted on Hugging Face Hub</p>
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