Spaces:
Running
Running
Khunndev
feat: Implement concurrency limits, queue management, and performance optimizations for robust API handling.
fa700aa Download model.py from npisb/pipecountapp: direct link, hf CLI and curl.
- Browser
- Download file 1.53 kB
-
https://huggingface.co/spaces/npisb/pipecountapp/resolve/main/model.py
- Command line
-
hf download hf://spaces/npisb/pipecountapp/model.py
-
curl -L -o model.py https://huggingface.co/spaces/npisb/pipecountapp/resolve/main/model.py
1.53 kB
| import torch | |
| import os | |
| import numpy as np | |
| # ANSI color codes | |
| class Colors: | |
| RESET = "\033[0m" | |
| BOLD = "\033[1m" | |
| GREEN = "\033[92m" | |
| CYAN = "\033[96m" | |
| YELLOW = "\033[93m" | |
| def get_yolov5(confident_val): | |
| """Initialize YOLOv5 model with optimized thread settings""" | |
| # Force CPU mode | |
| os.environ['CUDA_VISIBLE_DEVICES'] = '' | |
| # Use multiple threads for better throughput (auto-scale based on CPU) | |
| num_threads = min(os.cpu_count() or 2, 4) # Cap at 4 threads | |
| torch.set_num_threads(num_threads) | |
| torch.set_num_interop_threads(num_threads) | |
| print(f"{Colors.CYAN}[MODEL]{Colors.RESET} π Loading YOLOv5 with {Colors.BOLD}{num_threads} threads{Colors.RESET}") | |
| print(f"{Colors.CYAN}[MODEL]{Colors.RESET} π Confidence threshold: {confident_val}") | |
| # Load model with optimization flags | |
| model = torch.hub.load('./yolov5', 'custom', path='./countmodel.pt', source='local', force_reload=False) | |
| # Configure model settings | |
| model.conf = confident_val # Confidence threshold | |
| model.max_det = 4000 # Maximum detections | |
| model.cpu() # Ensure CPU mode | |
| # Enable inference optimizations | |
| model.eval() # Set to evaluation mode | |
| # Enable inference mode for better performance | |
| torch.inference_mode() | |
| print(f"{Colors.GREEN}[MODEL]{Colors.RESET} β Model loaded successfully!") | |
| print(f"{Colors.CYAN}[MODEL]{Colors.RESET} π Max detections: {model.max_det}") | |
| return model | |