PrashanthB461 commited on
Commit
f8f8eae
·
verified ·
1 Parent(s): 15c8303

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -12
app.py CHANGED
@@ -20,17 +20,20 @@ import pandas as pd
20
  # Suppress all warnings
21
  warnings.filterwarnings("ignore")
22
 
23
- # Set up YOLO_CONFIG_DIR
24
  yolo_config_dir = "/tmp/Ultralytics"
25
- try:
26
- if os.path.exists(yolo_config_dir):
27
- shutil.rmtree(yolo_config_dir)
28
- os.makedirs(yolo_config_dir, exist_ok=True)
29
- os.chmod(yolo_config_dir, 0o777) # Ensure directory is writable
30
- os.environ["YOLO_CONFIG_DIR"] = yolo_config_dir
31
- except Exception as e:
32
- print(f"Failed to set up YOLO_CONFIG_DIR: {e}")
33
- raise
 
 
 
34
 
35
  # --- Logging Configuration ---
36
  logging.basicConfig(
@@ -48,7 +51,7 @@ logging.getLogger("PIL").setLevel(logging.WARNING)
48
  logging.getLogger("matplotlib").setLevel(logging.WARNING)
49
  logging.getLogger("urllib3").setLevel(logging.WARNING)
50
 
51
- # Log environment variable once
52
  logger.info(f"YOLO_CONFIG_DIR set to: {os.getenv('YOLO_CONFIG_DIR')}")
53
 
54
  # --- Environment Variables ---
@@ -63,8 +66,12 @@ yolo_model = None
63
 
64
  # --- Model Class ---
65
  class YOLOv8Model:
66
- def __init__(self, model_path='yolov8n.pt'):
67
  try:
 
 
 
 
68
  logger.info("Initializing YOLOv8 model")
69
  original_stdout = sys.stdout
70
  sys.stdout = open(os.devnull, 'w')
 
20
  # Suppress all warnings
21
  warnings.filterwarnings("ignore")
22
 
23
+ # Set up YOLO_CONFIG_DIR only once
24
  yolo_config_dir = "/tmp/Ultralytics"
25
+ if not hasattr(sys, '_yolo_config_initialized'):
26
+ try:
27
+ if os.path.exists(yolo_config_dir):
28
+ shutil.rmtree(yolo_config_dir)
29
+ os.makedirs(yolo_config_dir, exist_ok=True)
30
+ os.chmod(yolo_config_dir, 0o777) # Ensure directory is writable
31
+ os.environ["YOLO_CONFIG_DIR"] = yolo_config_dir
32
+ sys._yolo_config_initialized = True
33
+ print(f"YOLO_CONFIG_DIR initialized to: {yolo_config_dir}")
34
+ except Exception as e:
35
+ print(f"Failed to set up YOLO_CONFIG_DIR: {e}")
36
+ raise
37
 
38
  # --- Logging Configuration ---
39
  logging.basicConfig(
 
51
  logging.getLogger("matplotlib").setLevel(logging.WARNING)
52
  logging.getLogger("urllib3").setLevel(logging.WARNING)
53
 
54
+ # Log environment variable
55
  logger.info(f"YOLO_CONFIG_DIR set to: {os.getenv('YOLO_CONFIG_DIR')}")
56
 
57
  # --- Environment Variables ---
 
66
 
67
  # --- Model Class ---
68
  class YOLOv8Model:
69
+ def __init__(self, model_path='/app/yolov8n.pt'):
70
  try:
71
+ logger.info("Checking for model weights at %s", model_path)
72
+ if not os.path.exists(model_path):
73
+ logger.error("Model weights file %s not found", model_path)
74
+ raise FileNotFoundError(f"Model weights file {model_path} not found")
75
  logger.info("Initializing YOLOv8 model")
76
  original_stdout = sys.stdout
77
  sys.stdout = open(os.devnull, 'w')