atalaydenknalbant commited on
Commit
af6e129
·
verified ·
1 Parent(s): 9686421

make code MCP compatible

Browse files
Files changed (1) hide show
  1. app.py +38 -2
app.py CHANGED
@@ -5,8 +5,20 @@ import spaces
5
  import os
6
  from huggingface_hub import hf_hub_download
7
 
8
- # Helper function to download models from Hugging Face
9
  def get_model_path(model_name):
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  model_cache_path = hf_hub_download(
11
  repo_id="atalaydenknalbant/budgerigar_models",
12
  filename=model_name
@@ -15,6 +27,30 @@ def get_model_path(model_name):
15
 
16
  @spaces.GPU
17
  def yolo_inference(images, model_id, conf_threshold, iou_threshold, max_detection):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  if images is None:
19
  # Create a blank image
20
  width, height = 640, 480
@@ -85,4 +121,4 @@ interface = gr.Interface(
85
  ["Female.png", "budgerigar_yolo11x.pt", 0.25, 0.45, 300],
86
  ],
87
  )
88
- interface.launch()
 
5
  import os
6
  from huggingface_hub import hf_hub_download
7
 
 
8
  def get_model_path(model_name):
9
+ """
10
+ Downloads a specified model from the 'atalaydenknalbant/budgerigar_models'
11
+ Hugging Face repository and returns its local cached path.
12
+
13
+ This helper function streamlines the process of fetching model weights
14
+ from the Hugging Face Hub, ensuring the model is available locally for use.
15
+
16
+ Args:
17
+ model_name (str): The filename of the model to download (e.g., 'budgerigar_yolo11x.pt').
18
+
19
+ Returns:
20
+ str: The file path to the locally downloaded model.
21
+ """
22
  model_cache_path = hf_hub_download(
23
  repo_id="atalaydenknalbant/budgerigar_models",
24
  filename=model_name
 
27
 
28
  @spaces.GPU
29
  def yolo_inference(images, model_id, conf_threshold, iou_threshold, max_detection):
30
+ """
31
+ Performs budgerigar gender determination inference on an image using a selected YOLO or RTDETR model.
32
+
33
+ This function handles input images, loads the appropriate model (YOLO or RTDETR)
34
+ based on the `model_id`, and then runs inference to detect budgerigars and
35
+ determine their gender. The results are then plotted onto the original image.
36
+ If no image is provided, it returns a blank image with a message.
37
+
38
+ Args:
39
+ images (PIL.Image.Image or None): The input image on which to perform detection.
40
+ Can be None if no image is uploaded.
41
+ model_id (str): The identifier of the model to use (e.g., 'budgerigar_yolo11x.pt',
42
+ 'budgerigar_rtdetr-x.pt').
43
+ conf_threshold (float): The confidence threshold for filtering detections.
44
+ Detections with confidence below this value are discarded.
45
+ iou_threshold (float): The Intersection over Union (IoU) threshold for
46
+ Non-Maximum Suppression (NMS) to remove overlapping detections.
47
+ max_detection (int): The maximum number of detections to return and display.
48
+
49
+ Returns:
50
+ PIL.Image.Image: The input image annotated with detection results, including
51
+ bounding boxes and gender labels. Returns a blank image
52
+ with a message if no input image is provided.
53
+ """
54
  if images is None:
55
  # Create a blank image
56
  width, height = 640, 480
 
121
  ["Female.png", "budgerigar_yolo11x.pt", 0.25, 0.45, 300],
122
  ],
123
  )
124
+ interface.launch(mcp_server=True)