DawnC commited on
Commit
e9902af
1 Parent(s): 2fe6736

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -32
app.py CHANGED
@@ -586,31 +586,15 @@ class BaseModel(nn.Module):
586
  attended_features = self.attention(features)
587
  logits = self.classifier(attended_features)
588
  return logits, attended_features
 
589
 
590
-
591
- class ModelManager:
592
- _instance = None
593
- _initialized = False
594
-
595
- def __init__(self):
596
- self.model_yolo = None
597
- self.model = None
598
-
599
- @classmethod
600
- async def get_instance(cls):
601
- if not cls._instance:
602
- cls._instance = cls()
603
- return cls._instance
604
-
605
- async def initialize(self):
606
- if not self._initialized:
607
- self.model_yolo = YOLO('yolov8l.pt')
608
- num_classes = len(dog_breeds)
609
- self.model = BaseModel(num_classes=num_classes)
610
- checkpoint = torch.load('124_best_model_dog.pth')
611
- self.model.load_state_dict(checkpoint['base_model'], strict=False)
612
- self.model.eval()
613
- self._initialized = True
614
 
615
 
616
  # Image preprocessing function
@@ -637,8 +621,6 @@ async def predict_single_dog(image):
637
  Returns:
638
  tuple: (top1_prob, topk_breeds, relative_probs)
639
  """
640
- manager = await ModelManager.get_instance()
641
- await manager.initialize()
642
 
643
  image_tensor = preprocess_image(image).to(device)
644
 
@@ -665,10 +647,8 @@ async def predict_single_dog(image):
665
 
666
  @spaces.GPU
667
  async def detect_multiple_dogs(image, conf_threshold=0.3, iou_threshold=0.55):
668
- manager = await ModelManager.get_instance()
669
- await manager.initialize()
670
 
671
- results = manager.model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
672
  dogs = []
673
  boxes = []
674
  for box in results.boxes:
@@ -759,8 +739,6 @@ async def predict(image):
759
  Returns:
760
  tuple: (html_output, annotated_image, initial_state)
761
  """
762
- manager = await ModelManager.get_instance()
763
- await manager.initialize()
764
 
765
  if image is None:
766
  return format_warning_html("Please upload an image to start."), None, None
@@ -903,7 +881,6 @@ def show_details_html(choice, previous_output, initial_state):
903
 
904
  def main():
905
  with gr.Blocks(css=get_css_styles()) as iface:
906
- spaces.GPU.setup()
907
 
908
  gr.HTML("""
909
  <header style='text-align: center; padding: 20px; margin-bottom: 20px;'>
 
586
  attended_features = self.attention(features)
587
  logits = self.classifier(attended_features)
588
  return logits, attended_features
589
+
590
 
591
+ model_yolo = YOLO('yolov8l.pt')
592
+ num_classes = len(dog_breeds)
593
+ model = BaseModel(num_classes=num_classes)
594
+ model_path = '124_best_model_dog.pth'
595
+ checkpoint = torch.load(model_path)
596
+ model.load_state_dict(checkpoint['base_model'], strict=False)
597
+ model.eval()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
598
 
599
 
600
  # Image preprocessing function
 
621
  Returns:
622
  tuple: (top1_prob, topk_breeds, relative_probs)
623
  """
 
 
624
 
625
  image_tensor = preprocess_image(image).to(device)
626
 
 
647
 
648
  @spaces.GPU
649
  async def detect_multiple_dogs(image, conf_threshold=0.3, iou_threshold=0.55):
 
 
650
 
651
+ results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
652
  dogs = []
653
  boxes = []
654
  for box in results.boxes:
 
739
  Returns:
740
  tuple: (html_output, annotated_image, initial_state)
741
  """
 
 
742
 
743
  if image is None:
744
  return format_warning_html("Please upload an image to start."), None, None
 
881
 
882
  def main():
883
  with gr.Blocks(css=get_css_styles()) as iface:
 
884
 
885
  gr.HTML("""
886
  <header style='text-align: center; padding: 20px; margin-bottom: 20px;'>