sagar007 commited on
Commit
599a500
·
verified ·
1 Parent(s): aaf62b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -114
app.py CHANGED
@@ -493,141 +493,63 @@ print("Preloading finished (check logs above for errors).")
493
  # --- Gradio Interface Definition ---
494
  # (Your Gradio Blocks code remains largely the same, but ensure the outputs match the function returns)
495
 
496
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
 
 
 
497
  gr.Markdown("# CLIP & FastSAM Demo")
498
- gr.Markdown("Explore Zero-Shot Classification, 'Segment Everything', and Text-Prompted Segmentation.")
499
 
500
  with gr.Tabs():
501
- # --- CLIP Tab ---
502
  with gr.TabItem("CLIP Zero-Shot Classification"):
503
- # ... (CLIP UI definition - seems ok) ...
 
 
 
 
 
 
 
 
 
 
 
504
  clip_button.click(
505
  run_clip_zero_shot,
506
  inputs=[clip_input_image, clip_text_labels],
507
- # Output matches: Label (dict/str), Image (PIL/None)
508
  outputs=[clip_output_label, clip_output_image_display]
509
  )
510
- # ... (CLIP Examples - seems ok) ...
511
-
512
 
513
- # --- FastSAM Everything Tab ---
514
  with gr.TabItem("FastSAM Segment Everything"):
515
- gr.Markdown("Upload an image to segment all objects/regions.")
516
- with gr.Row():
517
- with gr.Column(scale=1):
518
- fastsam_input_image_all = gr.Image(type="pil", label="Input Image")
519
- with gr.Row():
520
- fastsam_conf_all = gr.Slider(minimum=0.1, maximum=1.0, value=0.4, step=0.05, label="Confidence Threshold")
521
- fastsam_iou_all = gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="IoU Threshold")
522
- fastsam_button_all = gr.Button("Run FastSAM Segmentation", variant="primary")
523
- with gr.Column(scale=1):
524
- # Output for the image
525
- fastsam_output_image_all = gr.Image(type="pil", label="Segmented Image")
526
- # Add a Textbox for status messages/errors
527
- fastsam_status_all = gr.Textbox(label="Status", interactive=False)
528
 
 
529
  fastsam_button_all.click(
530
  run_fastsam_segmentation,
531
- inputs=[fastsam_input_image_all, fastsam_conf_all, fastsam_iou_all],
532
- # Outputs: Image (PIL/None), Status (str)
533
- outputs=[fastsam_output_image_all, fastsam_status_all] # Updated outputs
534
  )
535
- # Update examples if needed to match new output structure (add None/str for status)
536
- # Note: Examples might need adjustment if they expect only image output
537
- gr.Examples(
538
- examples=[
539
- ["examples/dogs.jpg", 0.4, 0.9],
540
- ["examples/fruits.jpg", 0.5, 0.8],
541
- ["examples/lion.jpg", 0.45, 0.9],
542
- ],
543
- inputs=[fastsam_input_image_all, fastsam_conf_all, fastsam_iou_all],
544
- # Need to adjust outputs for examples if function signature changed
545
- # This might require a wrapper if examples expect single output
546
- # For now, comment out example outputs or adjust function signature for examples
547
- outputs=[fastsam_output_image_all, fastsam_status_all],
548
- fn=run_fastsam_segmentation,
549
- cache_examples=False, # Keep False for debugging
550
- )
551
-
552
- # --- Text-Prompted Segmentation Tab ---
553
  with gr.TabItem("Text-Prompted Segmentation"):
554
- gr.Markdown("Upload an image and provide comma-separated prompts (e.g., 'person, dog').")
555
- with gr.Row():
556
- with gr.Column(scale=1):
557
- prompt_input_image = gr.Image(type="pil", label="Input Image")
558
- prompt_text_input = gr.Textbox(label="Comma-Separated Text Prompts", placeholder="e.g., glasses, watch")
559
- with gr.Row():
560
- prompt_conf = gr.Slider(minimum=0.1, maximum=1.0, value=0.4, step=0.05, label="Confidence Threshold")
561
- prompt_iou = gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="IoU Threshold")
562
- prompt_button = gr.Button("Segment by Text", variant="primary")
563
- with gr.Column(scale=1):
564
- # Output Image
565
- prompt_output_image = gr.Image(type="pil", label="Text-Prompted Segmentation")
566
- # Status Textbox (already exists, correctly)
567
- prompt_status_message = gr.Textbox(label="Status", interactive=False)
568
 
 
569
  prompt_button.click(
570
  run_text_prompted_segmentation,
571
- inputs=[prompt_input_image, prompt_text_input, prompt_conf, prompt_iou],
572
- # Outputs: Image (PIL/None), Status (str) - Matches function
573
- outputs=[prompt_output_image, prompt_status_message]
574
  )
575
- # Update examples similarly if needed
576
- gr.Examples(
577
- examples=[
578
- ["examples/dog_bike.jpg", "person, bicycle", 0.4, 0.9],
579
- ["examples/astronaut.jpg", "person, helmet", 0.35, 0.9],
580
- ["examples/dogs.jpg", "dog", 0.4, 0.9],
581
- ["examples/fruits.jpg", "banana, apple", 0.5, 0.8],
582
- ["examples/teacher.jpg", "person, glasses", 0.4, 0.9],
583
- ],
584
- inputs=[prompt_input_image, prompt_text_input, prompt_conf, prompt_iou],
585
- outputs=[prompt_output_image, prompt_status_message],
586
- fn=run_text_prompted_segmentation,
587
- cache_examples=False, # Keep False for debugging
588
- )
589
-
590
-
591
- # --- Example File Download ---
592
- # (Download logic seems okay, ensure 'wget' is installed: pip install wget)
593
- if not os.path.exists("examples"):
594
- os.makedirs("examples")
595
- print("Created 'examples' directory.")
596
- example_files = {
597
- "astronaut.jpg": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/d1/Astronaut_-_St._Jean_Bay.jpg/640px-Astronaut_-_St._Jean_Bay.jpg",
598
- "dog_bike.jpg": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/gradio/outputs_multimodal.jpg",
599
- "clip_logo.png": "https://raw.githubusercontent.com/openai/CLIP/main/CLIP.png",
600
- "dogs.jpg": "https://raw.githubusercontent.com/ultralytics/assets/main/im/image8.jpg",
601
- "fruits.jpg": "https://raw.githubusercontent.com/ultralytics/assets/main/im/image9.jpg",
602
- "lion.jpg": "https://huggingface.co/spaces/gradio/image-segmentation/resolve/main/images/lion.jpg",
603
- "teacher.jpg": "https://images.pexels.com/photos/848117/pexels-photo-848117.jpeg?auto=compress&cs=tinysrgb&w=600"
604
- }
605
- def download_example_file(filename, url, retries=3):
606
- filepath = os.path.join("examples", filename)
607
- if not os.path.exists(filepath):
608
- print(f"Attempting to download {filename}...")
609
- for attempt in range(retries):
610
- try:
611
- wget.download(url, filepath)
612
- print(f"Downloaded {filename} successfully.")
613
- return # Exit function on success
614
- except Exception as e:
615
- print(f"Download attempt {attempt + 1}/{retries} for {filename} failed: {e}")
616
- if os.path.exists(filepath): # Clean up partial download
617
- try: os.remove(filepath)
618
- except OSError: pass
619
- if attempt + 1 == retries:
620
- print(f"Failed to download {filename} after {retries} attempts.")
621
- else:
622
- print(f"Example file {filename} already exists.")
623
-
624
- # Trigger downloads
625
- for filename, url in example_files.items():
626
- download_example_file(filename, url)
627
- print("Example file check/download complete.")
628
 
 
 
 
629
 
630
- # --- Launch App ---
631
  if __name__ == "__main__":
632
  print("Launching Gradio Demo...")
633
- demo.launch(debug=True) # Keep debug=True
 
493
  # --- Gradio Interface Definition ---
494
  # (Your Gradio Blocks code remains largely the same, but ensure the outputs match the function returns)
495
 
496
+ # --- Gradio Interface ---
497
+ # ... (imports and functions) ...
498
+
499
+ with gr.Blocks(theme=gr.themes.Soft()) as demo: # START of the block
500
  gr.Markdown("# CLIP & FastSAM Demo")
501
+ # ... other UI elements ...
502
 
503
  with gr.Tabs():
 
504
  with gr.TabItem("CLIP Zero-Shot Classification"):
505
+ gr.Markdown("Upload an image and provide comma-separated labels...")
506
+ with gr.Row():
507
+ with gr.Column(scale=1):
508
+ clip_input_image = gr.Image(type="pil", label="Input Image")
509
+ clip_text_labels = gr.Textbox(label="Comma-Separated Labels", ...)
510
+ # DEFINE the button
511
+ clip_button = gr.Button("Run CLIP Classification", variant="primary")
512
+ with gr.Column(scale=1):
513
+ clip_output_label = gr.Label(label="Classification Probabilities")
514
+ clip_output_image_display = gr.Image(type="pil", label="Input Image Preview")
515
+
516
+ # ATTACH the click handler *inside* the block, after the button is defined
517
  clip_button.click(
518
  run_clip_zero_shot,
519
  inputs=[clip_input_image, clip_text_labels],
 
520
  outputs=[clip_output_label, clip_output_image_display]
521
  )
522
+ # ... CLIP examples ...
 
523
 
 
524
  with gr.TabItem("FastSAM Segment Everything"):
525
+ # ... FastSAM Everything UI elements ...
526
+ fastsam_button_all = gr.Button(...) # Define button
 
 
 
 
 
 
 
 
 
 
 
527
 
528
+ # Attach click handler *inside* the block
529
  fastsam_button_all.click(
530
  run_fastsam_segmentation,
531
+ inputs=[...],
532
+ outputs=[...]
 
533
  )
534
+ # ... FastSAM Everything examples ...
535
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
536
  with gr.TabItem("Text-Prompted Segmentation"):
537
+ # ... Text-Prompted UI elements ...
538
+ prompt_button = gr.Button(...) # Define button
 
 
 
 
 
 
 
 
 
 
 
 
539
 
540
+ # Attach click handler *inside* the block
541
  prompt_button.click(
542
  run_text_prompted_segmentation,
543
+ inputs=[...],
544
+ outputs=[...]
 
545
  )
546
+ # ... Text-Prompted examples ...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
547
 
548
+ # The `with` block ends here.
549
+ # --- Example File Download (This is correctly outside the block) ---
550
+ # ... download logic ...
551
 
552
+ # --- Launch App (This is correctly outside the block) ---
553
  if __name__ == "__main__":
554
  print("Launching Gradio Demo...")
555
+ demo.launch(debug=True)