Mohammed Abdeldayem commited on
Commit
9711b09
·
verified ·
1 Parent(s): eed6859

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -4
app.py CHANGED
@@ -4,15 +4,19 @@ from PIL import Image
4
  from torchvision.transforms.functional import crop
5
  import gradio as gr
6
 
 
 
 
 
 
 
7
  # Load models during initialization
8
  def init():
9
  global object_detection_model, captioning_model, tokenizer, captioning_processor
10
 
11
  # Step 1: Load the YOLOv5 model from Hugging Face
12
  try:
13
- # Load the YOLOv5 model with trust_repo=True to skip the warning
14
  object_detection_model = torch.hub.load('ultralytics/yolov5', 'custom', path='yolov5/weights/best14.pt', trust_repo=True)
15
- # Assuming model is locally available
16
  print("YOLOv5 model loaded successfully.")
17
  except Exception as e:
18
  print(f"Error loading YOLOv5 model: {e}")
@@ -36,6 +40,12 @@ def crop_objects(image, boxes):
36
 
37
  # Gradio interface function
38
  def process_image(image):
 
 
 
 
 
 
39
  try:
40
  # Step 1: Perform object detection with YOLOv5
41
  results = object_detection_model(image)
@@ -78,7 +88,7 @@ def process_image(image):
78
  # Initialize models
79
  init()
80
 
81
- # Updated Gradio interface with new syntax
82
  interface = gr.Interface(
83
  fn=process_image, # Function to run
84
  inputs=gr.Image(type="pil"), # Input: Image upload
@@ -88,6 +98,5 @@ interface = gr.Interface(
88
  live=True
89
  )
90
 
91
-
92
  # Launch the Gradio app
93
  interface.launch()
 
4
  from torchvision.transforms.functional import crop
5
  import gradio as gr
6
 
7
+ # Global variables for models
8
+ object_detection_model = None
9
+ captioning_model = None
10
+ tokenizer = None
11
+ captioning_processor = None
12
+
13
  # Load models during initialization
14
  def init():
15
  global object_detection_model, captioning_model, tokenizer, captioning_processor
16
 
17
  # Step 1: Load the YOLOv5 model from Hugging Face
18
  try:
 
19
  object_detection_model = torch.hub.load('ultralytics/yolov5', 'custom', path='yolov5/weights/best14.pt', trust_repo=True)
 
20
  print("YOLOv5 model loaded successfully.")
21
  except Exception as e:
22
  print(f"Error loading YOLOv5 model: {e}")
 
40
 
41
  # Gradio interface function
42
  def process_image(image):
43
+ global object_detection_model, captioning_model, tokenizer, captioning_processor
44
+
45
+ # Ensure models are loaded
46
+ if object_detection_model is None or captioning_model is None or tokenizer is None or captioning_processor is None:
47
+ init() # Call init to load models
48
+
49
  try:
50
  # Step 1: Perform object detection with YOLOv5
51
  results = object_detection_model(image)
 
88
  # Initialize models
89
  init()
90
 
91
+ # Gradio Interface
92
  interface = gr.Interface(
93
  fn=process_image, # Function to run
94
  inputs=gr.Image(type="pil"), # Input: Image upload
 
98
  live=True
99
  )
100
 
 
101
  # Launch the Gradio app
102
  interface.launch()