Spaces:
Sleeping
Sleeping
Mohammed Abdeldayem
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -16,19 +16,23 @@ def init():
|
|
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}")
|
|
|
23 |
|
24 |
# Step 2: Load the ViT-GPT2 captioning model from Hugging Face
|
25 |
try:
|
|
|
26 |
captioning_model = VisionEncoderDecoderModel.from_pretrained("motheecreator/ViT-GPT2-Image-Captioning")
|
27 |
tokenizer = AutoTokenizer.from_pretrained("motheecreator/ViT-GPT2-Image-Captioning")
|
28 |
captioning_processor = AutoImageProcessor.from_pretrained("motheecreator/ViT-GPT2-Image-Captioning")
|
29 |
print("ViT-GPT2 model loaded successfully.")
|
30 |
except Exception as e:
|
31 |
print(f"Error loading captioning model: {e}")
|
|
|
32 |
|
33 |
# Utility function to crop objects from the image based on bounding boxes
|
34 |
def crop_objects(image, boxes):
|
@@ -44,7 +48,7 @@ def process_image(image):
|
|
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 |
-
|
48 |
|
49 |
try:
|
50 |
# Step 1: Perform object detection with YOLOv5
|
|
|
16 |
|
17 |
# Step 1: Load the YOLOv5 model from Hugging Face
|
18 |
try:
|
19 |
+
print("Loading YOLOv5 model...")
|
20 |
object_detection_model = torch.hub.load('ultralytics/yolov5', 'custom', path='yolov5/weights/best14.pt', trust_repo=True)
|
21 |
print("YOLOv5 model loaded successfully.")
|
22 |
except Exception as e:
|
23 |
print(f"Error loading YOLOv5 model: {e}")
|
24 |
+
object_detection_model = None
|
25 |
|
26 |
# Step 2: Load the ViT-GPT2 captioning model from Hugging Face
|
27 |
try:
|
28 |
+
print("Loading ViT-GPT2 model...")
|
29 |
captioning_model = VisionEncoderDecoderModel.from_pretrained("motheecreator/ViT-GPT2-Image-Captioning")
|
30 |
tokenizer = AutoTokenizer.from_pretrained("motheecreator/ViT-GPT2-Image-Captioning")
|
31 |
captioning_processor = AutoImageProcessor.from_pretrained("motheecreator/ViT-GPT2-Image-Captioning")
|
32 |
print("ViT-GPT2 model loaded successfully.")
|
33 |
except Exception as e:
|
34 |
print(f"Error loading captioning model: {e}")
|
35 |
+
captioning_model, tokenizer, captioning_processor = None, None, None
|
36 |
|
37 |
# Utility function to crop objects from the image based on bounding boxes
|
38 |
def crop_objects(image, boxes):
|
|
|
48 |
|
49 |
# Ensure models are loaded
|
50 |
if object_detection_model is None or captioning_model is None or tokenizer is None or captioning_processor is None:
|
51 |
+
return None, {"error": "Models are not loaded properly"}, None
|
52 |
|
53 |
try:
|
54 |
# Step 1: Perform object detection with YOLOv5
|