kitsumed commited on
Commit
13170b5
·
verified ·
1 Parent(s): 458b43c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -13
app.py CHANGED
@@ -1,13 +1,57 @@
1
- import gradio as gr
 
2
  import PIL.Image as Image
 
 
3
 
4
- from ultralytics import ASSETS, YOLO
 
5
 
6
- model = YOLO("./yolov8m_seg-speech-bubble.pt")
 
 
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
- def predict_image(img, conf_threshold, iou_threshold):
10
- """Predicts objects in an image using a YOLO11 model with adjustable confidence and IOU thresholds."""
11
  results = model.predict(
12
  source=img,
13
  conf=conf_threshold,
@@ -24,17 +68,18 @@ def predict_image(img, conf_threshold, iou_threshold):
24
  return im
25
 
26
 
27
- iface = gr.Interface(
28
  fn=predict_image,
29
  inputs=[
30
- gr.Image(type="pil", label="Upload Image"),
31
- gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence threshold"),
32
- gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU threshold"),
 
33
  ],
34
- outputs=gr.Image(type="pil", label="Result"),
35
- title="Ultralytics Gradio",
36
- description="Upload images for inference.",
37
  )
38
 
39
  if __name__ == "__main__":
40
- iface.launch()
 
1
+ # Code inspired from ultralyrics example with gradio
2
+ import gradio as gradio
3
  import PIL.Image as Image
4
+ import os
5
+ import shutil
6
 
7
+ from ultralytics import YOLO
8
+ from huggingface_hub import hf_hub_download
9
 
10
+ # Directory where downloaded model will be stored
11
+ MODEL_DIR = "cached_models"
12
+ os.makedirs(MODEL_DIR, exist_ok=True)
13
 
14
+ # List of models available in the gradio ui
15
+ AVAILABLE_MODELS = {
16
+ "YOLOv8m Speech Bubble (kitsumed)": {
17
+ "repo_id": "kitsumed/yolov8m_seg-speech-bubble",
18
+ # Filename, include sub-directory if model not at root (models/v1/model.pt)
19
+ "filename": "model.pt"
20
+ },
21
+ # Add more models here
22
+ }
23
+
24
+ # Cache for currently loaded model
25
+ current_model = None
26
+ current_model_name = None
27
+
28
+
29
+ def load_model(model_name):
30
+ global current_model, current_model_name
31
+
32
+ if model_name == current_model_name:
33
+ return current_model
34
+
35
+ info = AVAILABLE_MODELS[model_name]
36
+ local_model_path = os.path.join(MODEL_DIR, f"{model_name}_{info['filename']}")
37
+
38
+ # If the model is yet existing in the cache, download and cache it
39
+ if not os.path.exists(local_model_path):
40
+ print(f"Downloading model '{model_name}'...")
41
+ downloaded_path = hf_hub_download(
42
+ repo_id=info["repo_id"],
43
+ filename=info["filename"]
44
+ )
45
+ shutil.move(downloaded_path, local_model_path)
46
+
47
+ current_model = YOLO(local_model_path)
48
+ current_model_name = model_name
49
+ return current_model
50
+
51
+
52
+ def predict_image(img, conf_threshold, iou_threshold, model_name):
53
+ model = load_model(model_name)
54
 
 
 
55
  results = model.predict(
56
  source=img,
57
  conf=conf_threshold,
 
68
  return im
69
 
70
 
71
+ iface = gradio.Interface(
72
  fn=predict_image,
73
  inputs=[
74
+ gradio.Image(type="pil", label="Upload Image"),
75
+ gradio.Slider(minimum=0, maximum=1, value=0.20, label="Confidence threshold"),
76
+ gradio.Slider(minimum=0, maximum=1, value=0.40, label="IoU threshold"),
77
+ gradio.Dropdown(choices=list(AVAILABLE_MODELS.keys()), label="Select Model", value=list(AVAILABLE_MODELS.keys())[0])
78
  ],
79
+ outputs=gradio.Image(type="pil", label="Result"),
80
+ title="Try out kitsumed YOLO models",
81
+ description="Select a model from kitsumed on Hugging Face and upload an image to perform predictions.",
82
  )
83
 
84
  if __name__ == "__main__":
85
+ iface.launch()