atalaydenknalbant commited on
Commit
b8f34e2
·
verified ·
1 Parent(s): 157ff70

Make code MCP compatible

Browse files
Files changed (1) hide show
  1. app.py +71 -1
app.py CHANGED
@@ -8,10 +8,51 @@ import tempfile
8
  import numpy as np
9
 
10
  def download_model(model_filename):
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  return hf_hub_download(repo_id="atalaydenknalbant/Yolov13", filename=model_filename)
12
 
13
  @spaces.GPU
14
  def yolo_inference(input_type, image, video, model_id, conf_threshold, iou_threshold, max_detection):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  model_path = download_model(model_id)
16
 
17
  if input_type == "Image":
@@ -104,12 +145,41 @@ def yolo_inference(input_type, image, video, model_id, conf_threshold, iou_thres
104
  return None, None
105
 
106
  def update_visibility(input_type):
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  if input_type == "Image":
108
  return gr.update(visible=True), gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
109
  else:
110
  return gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), gr.update(visible=True)
111
 
112
  def yolo_inference_for_examples(image, model_id, conf_threshold, iou_threshold, max_detection):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  annotated_image, _ = yolo_inference(
114
  input_type="Image",
115
  image=image,
@@ -193,4 +263,4 @@ with gr.Blocks() as app:
193
  )
194
 
195
  if __name__ == '__main__':
196
- app.launch()
 
8
  import numpy as np
9
 
10
  def download_model(model_filename):
11
+ """
12
+ Downloads a YOLO model from the Hugging Face Hub.
13
+
14
+ This function fetches a specified YOLO model file from the
15
+ 'atalaydenknalbant/Yolov13' repository on the Hugging Face Hub.
16
+
17
+ Args:
18
+ model_filename (str): The name of the model file to download
19
+ (e.g., 'yolov13n.pt').
20
+
21
+ Returns:
22
+ str: The local path to the downloaded model file.
23
+ """
24
  return hf_hub_download(repo_id="atalaydenknalbant/Yolov13", filename=model_filename)
25
 
26
  @spaces.GPU
27
  def yolo_inference(input_type, image, video, model_id, conf_threshold, iou_threshold, max_detection):
28
+ """
29
+ Performs object detection inference using a YOLOv13 model on either an image or a video.
30
+
31
+ This function downloads the specified YOLO model, then applies it to the
32
+ provided input. For images, it returns an annotated image. For videos, it
33
+ processes each frame and returns an annotated video. Error handling for
34
+ missing inputs is included, returning blank outputs with messages.
35
+
36
+ Args:
37
+ input_type (str): Specifies the input type, either "Image" or "Video".
38
+ image (PIL.Image.Image or None): The input image if `input_type` is "Image".
39
+ None otherwise.
40
+ video (str or None): The path to the input video file if `input_type` is "Video".
41
+ None otherwise.
42
+ model_id (str): The identifier of the YOLO model to use (e.g., 'yolov13n.pt').
43
+ conf_threshold (float): The confidence threshold for object detection.
44
+ Detections with lower confidence are discarded.
45
+ iou_threshold (float): The Intersection over Union (IoU) threshold for
46
+ Non-Maximum Suppression (NMS).
47
+ max_detection (int): The maximum number of detections to return per image or frame.
48
+
49
+ Returns:
50
+ tuple: A tuple containing two elements:
51
+ - PIL.Image.Image or None: The annotated image if `input_type` was "Image",
52
+ otherwise None.
53
+ - str or None: The path to the annotated video file if `input_type` was "Video",
54
+ otherwise None.
55
+ """
56
  model_path = download_model(model_id)
57
 
58
  if input_type == "Image":
 
145
  return None, None
146
 
147
  def update_visibility(input_type):
148
+ """
149
+ Adjusts the visibility of Gradio components based on the selected input type.
150
+
151
+ This function dynamically shows or hides the image and video input/output
152
+ components in the Gradio interface to ensure only relevant fields are visible.
153
+
154
+ Args:
155
+ input_type (str): The selected input type, either "Image" or "Video".
156
+
157
+ Returns:
158
+ tuple: A tuple of `gr.update` objects for the visibility of:
159
+ (image input, video input, image output, video output).
160
+ """
161
  if input_type == "Image":
162
  return gr.update(visible=True), gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
163
  else:
164
  return gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), gr.update(visible=True)
165
 
166
  def yolo_inference_for_examples(image, model_id, conf_threshold, iou_threshold, max_detection):
167
+ """
168
+ Wrapper function for `yolo_inference` specifically for Gradio examples that use images.
169
+
170
+ This function simplifies the `yolo_inference` call for the `gr.Examples` component,
171
+ ensuring only image-based inference is performed for predefined examples.
172
+
173
+ Args:
174
+ image (PIL.Image.Image): The input image for the example.
175
+ model_id (str): The identifier of the YOLO model to use.
176
+ conf_threshold (float): The confidence threshold.
177
+ iou_threshold (float): The IoU threshold.
178
+ max_detection (int): The maximum number of detections.
179
+
180
+ Returns:
181
+ PIL.Image.Image or None: The annotated image. Returns None if no image is processed.
182
+ """
183
  annotated_image, _ = yolo_inference(
184
  input_type="Image",
185
  image=image,
 
263
  )
264
 
265
  if __name__ == '__main__':
266
+ app.launch(mcp_server=True)