Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -17,24 +17,45 @@ import logging
|
|
17 |
logging.basicConfig(level=logging.INFO)
|
18 |
logger = logging.getLogger(__name__)
|
19 |
|
20 |
-
# Initialize CarveKit
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
# Create FastAPI app
|
40 |
app = FastAPI(
|
@@ -98,7 +119,8 @@ async def health_check():
|
|
98 |
"""Health check endpoint"""
|
99 |
return {
|
100 |
"status": "healthy",
|
101 |
-
"carvekit_ready": interface is not None
|
|
|
102 |
}
|
103 |
|
104 |
@app.post("/api/remove-background")
|
|
|
17 |
logging.basicConfig(level=logging.INFO)
|
18 |
logger = logging.getLogger(__name__)
|
19 |
|
20 |
+
# Initialize CarveKit with proper cache handling
|
21 |
+
import os
|
22 |
+
interface = None
|
23 |
+
|
24 |
+
def initialize_carvekit():
|
25 |
+
"""Initialize CarveKit with proper error handling and cache setup"""
|
26 |
+
global interface
|
27 |
+
try:
|
28 |
+
# Set cache directory
|
29 |
+
cache_dir = os.environ.get('CARVEKIT_CACHE_DIR', '/app/.cache/carvekit')
|
30 |
+
os.makedirs(cache_dir, exist_ok=True)
|
31 |
+
|
32 |
+
# Set environment variable for CarveKit
|
33 |
+
os.environ['CARVEKIT_CACHE_DIR'] = cache_dir
|
34 |
+
|
35 |
+
# Import CarveKit after setting up cache
|
36 |
+
from carvekit.api.high import HiInterface
|
37 |
+
|
38 |
+
interface = HiInterface(
|
39 |
+
object_type="object", # Can be "object" or "hairs-like"
|
40 |
+
batch_size_seg=5,
|
41 |
+
batch_size_matting=1,
|
42 |
+
device='cpu', # Use 'cuda' if GPU is available
|
43 |
+
seg_mask_size=640,
|
44 |
+
matting_mask_size=2048,
|
45 |
+
trimap_prob_threshold=231,
|
46 |
+
trimap_kernel_size=30,
|
47 |
+
trimap_erosion_iters=5,
|
48 |
+
fp16=False
|
49 |
+
)
|
50 |
+
logger.info("CarveKit interface initialized successfully")
|
51 |
+
return True
|
52 |
+
except Exception as e:
|
53 |
+
logger.error(f"Failed to initialize CarveKit: {e}")
|
54 |
+
interface = None
|
55 |
+
return False
|
56 |
+
|
57 |
+
# Try to initialize CarveKit
|
58 |
+
carvekit_ready = initialize_carvekit()
|
59 |
|
60 |
# Create FastAPI app
|
61 |
app = FastAPI(
|
|
|
119 |
"""Health check endpoint"""
|
120 |
return {
|
121 |
"status": "healthy",
|
122 |
+
"carvekit_ready": interface is not None,
|
123 |
+
"carvekit_initialized": carvekit_ready
|
124 |
}
|
125 |
|
126 |
@app.post("/api/remove-background")
|