Spaces:
Sleeping
Sleeping
Update app.py
Browse files20 unmodified lines21222324252627282930313228 unmodified lines6162636465666724 unmodified lines929394959697982 unmodified lines101102103104105106107108109110111112125 unmodified lines23823924024124224324424520 unmodified lines# pyrefly: ignore [missing-import, untyped-import]import numpy as np# ββ ONNX RUNTIME PERFORMANCE TUNING (OPTIMIZED FOR 2-CORE CONTAINERS E.G. HUGGING FACE) ββ# Explicitly set threads to 2 to avoid overhead/context switching on shared hypervisors._cpu_count = 2os.environ["OMP_NUM_THREADS"] = str(_cpu_count)os.environ["OMP_WAIT_POLICY"] = "ACTIVE"os.environ["ONNXRUNTIME_SESSION_THREAD_POOL_SIZE"] = str(_cpu_count)app = Flask(__name__, static_folder='.', static_url_path='')CORS(app)28 unmodified lines'u2netp': 320,'u2net': 320,'isnet-general-use': 1024,'birefnet-general-lite': 1024,'birefnet-general': 1024,}24 unmodified linesreturn mask_npdef process_remove(input_image: Image.Image, model_name: str = 'isnet-general-use', fill_holes: bool = False, alpha_matting: bool = False) -> Image.Image:"""High-accuracy background removal.- Optimized resolution downscaling matches the model's native input size for fast CPU inference.2 unmodified lines"""t_start = time.perf_counter()original_size = input_image.size# Map birefnet-general-lite to isnet-general-use to avoid CPU OOM crashes and speed up processif model_name == 'birefnet-general-lite':print("[process_remove] Redirecting 'birefnet-general-lite' to 'isnet-general-use' for CPU RAM safety & speed.")model_name = 'isnet-general-use'rembg_model = model_name# ββ 1. DOWNSCALE FOR FAST INFERENCE (TO MODEL NATIVE RESOLUTION) ββ125 unmodified linesprint(f"Mobile URL (phone): http://{local_ip}:5000")print("(Note: Both devices must be on the same Wi-Fi network)")print("="*50)print("Pre-loading IS-Net model for faster first request...")get_session('isnet-general-use')print("Ready!\n")app.run(host='0.0.0.0', port=5000, debug=True)20 unmodified lines2122232425262728 unmodified lines5657585960616224 unmodified lines878889909192932 unmodified lines96979899100101125 unmodified lines22722822923023123223323420 unmodified lines# pyrefly: ignore [missing-import, untyped-import]import numpy as np# ONNX Runtime automatically manages threads to utilize all available CPU cores on local machines.app = Flask(__name__, static_folder='.', static_url_path='')CORS(app)28 unmodified lines'u2netp': 320,'u2net': 320,'isnet-general-use': 1024,'birefnet-general-lite': 768,'birefnet-general': 1024,}24 unmodified linesreturn mask_npdef process_remove(input_image: Image.Image, model_name: str = 'birefnet-general-lite', fill_holes: bool = False, alpha_matting: bool = False) -> Image.Image:"""High-accuracy background removal.- Optimized resolution downscaling matches the model's native input size for fast CPU inference.2 unmodified lines"""t_start = time.perf_counter()original_size = input_image.sizerembg_model = model_name# ββ 1. DOWNSCALE FOR FAST INFERENCE (TO MODEL NATIVE RESOLUTION) ββ125 unmodified linesprint(f"Mobile URL (phone): http://{local_ip}:5000")print("(Note: Both devices must be on the same Wi-Fi network)")print("="*50)print("Pre-loading BiRefNet model for faster first request...")get_session('birefnet-general-lite')print("Ready!\n")app.run(host='0.0.0.0', port=5000, debug=False, threaded=True)
app.py+6-17