Spaces:
mashroo
/
Runtime error

YoussefAnso commited on
Commit
5a9aacb
·
1 Parent(s): 135bed3

Add functionality to download the DIS ONNX model if not found locally in app.py. Updated remove_background function to call new ensure_dis_onnx_model method for improved error handling. Added requests library to requirements.txt for HTTP requests.

Browse files
Files changed (2) hide show
  1. app.py +22 -7
  2. requirements.txt +1 -1
app.py CHANGED
@@ -14,6 +14,7 @@ import json
14
  import os
15
  import json
16
  import argparse
 
17
 
18
  from model import CRM
19
  from inference import generate3d
@@ -21,6 +22,7 @@ from dis_bg_remover import remove_background as dis_remove_background
21
 
22
  # Configurable ONNX model path (can be set via environment variable)
23
  DIS_ONNX_MODEL_PATH = os.environ.get("DIS_ONNX_MODEL_PATH", "isnet_dis.onnx")
 
24
 
25
  pipeline = None
26
 
@@ -41,19 +43,32 @@ def check_input_image(input_image):
41
  raise gr.Error("No image uploaded!")
42
 
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  def remove_background(
45
  image: PIL.Image.Image,
46
  rembg_session: Any = None,
47
  force: bool = False,
48
  **rembg_kwargs,
49
  ) -> PIL.Image.Image:
50
- # Check if the ONNX model exists
51
- if not os.path.exists(DIS_ONNX_MODEL_PATH):
52
- raise gr.Error(
53
- f"DIS background remover model file not found at {DIS_ONNX_MODEL_PATH}. "
54
- "Please download it from https://huggingface.co/stoned0651/isnet_dis.onnx/resolve/main/isnet_dis.onnx "
55
- "and place it in the project directory or set the DIS_ONNX_MODEL_PATH environment variable."
56
- )
57
  extracted_img, mask = dis_remove_background(DIS_ONNX_MODEL_PATH, image)
58
  return extracted_img
59
 
 
14
  import os
15
  import json
16
  import argparse
17
+ import requests
18
 
19
  from model import CRM
20
  from inference import generate3d
 
22
 
23
  # Configurable ONNX model path (can be set via environment variable)
24
  DIS_ONNX_MODEL_PATH = os.environ.get("DIS_ONNX_MODEL_PATH", "isnet_dis.onnx")
25
+ DIS_ONNX_MODEL_URL = "https://huggingface.co/stoned0651/isnet_dis.onnx/resolve/main/isnet_dis.onnx"
26
 
27
  pipeline = None
28
 
 
43
  raise gr.Error("No image uploaded!")
44
 
45
 
46
+ def ensure_dis_onnx_model():
47
+ if not os.path.exists(DIS_ONNX_MODEL_PATH):
48
+ try:
49
+ print(f"Model file not found at {DIS_ONNX_MODEL_PATH}. Downloading from {DIS_ONNX_MODEL_URL}...")
50
+ response = requests.get(DIS_ONNX_MODEL_URL, stream=True)
51
+ response.raise_for_status()
52
+ with open(DIS_ONNX_MODEL_PATH, "wb") as f:
53
+ for chunk in response.iter_content(chunk_size=8192):
54
+ if chunk:
55
+ f.write(chunk)
56
+ print(f"Downloaded model to {DIS_ONNX_MODEL_PATH}")
57
+ except Exception as e:
58
+ raise gr.Error(
59
+ f"Failed to download DIS background remover model file: {e}\n"
60
+ f"Please manually download it from {DIS_ONNX_MODEL_URL} and place it in the project directory or set the DIS_ONNX_MODEL_PATH environment variable."
61
+ )
62
+
63
+
64
  def remove_background(
65
  image: PIL.Image.Image,
66
  rembg_session: Any = None,
67
  force: bool = False,
68
  **rembg_kwargs,
69
  ) -> PIL.Image.Image:
70
+ # Ensure the ONNX model exists (download if needed)
71
+ ensure_dis_onnx_model()
 
 
 
 
 
72
  extracted_img, mask = dis_remove_background(DIS_ONNX_MODEL_PATH, image)
73
  return extracted_img
74
 
requirements.txt CHANGED
@@ -19,5 +19,5 @@ xatlas
19
  ninja
20
  pymeshlab
21
  onnxruntime
22
-
23
 
 
19
  ninja
20
  pymeshlab
21
  onnxruntime
22
+ requests
23