um41r commited on
Commit
e875c80
·
verified ·
1 Parent(s): e60ef99

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -9
app.py CHANGED
@@ -7,25 +7,24 @@ from io import BytesIO
7
  from transformers import AutoModelForImageSegmentation
8
  import uvicorn
9
  from fastapi.middleware.cors import CORSMiddleware
10
- import subprocess
11
- import threading
12
 
13
  app = FastAPI()
 
14
  app.add_middleware(
15
  CORSMiddleware,
16
- allow_origins=["*"],
17
  allow_credentials=True,
18
  allow_methods=["*"],
19
  allow_headers=["*"],
20
  )
21
 
22
- # Load model
23
  device = "cuda" if torch.cuda.is_available() else "cpu"
24
  model = AutoModelForImageSegmentation.from_pretrained('ZhengPeng7/BiRefNet', trust_remote_code=True)
25
  model.to(device)
26
  model.eval()
27
 
28
- # Define transforms
29
  transform = transforms.Compose([
30
  transforms.Resize((1024, 1024)),
31
  transforms.ToTensor(),
@@ -41,8 +40,9 @@ async def remove_bg(file: UploadFile = File(...)):
41
  # Transform the image
42
  input_tensor = transform(image).unsqueeze(0).to(device)
43
 
44
- # Perform inference
45
  with torch.no_grad():
 
46
  pred = model(input_tensor)[-1].sigmoid().cpu()[0].squeeze()
47
 
48
  # Convert to PIL image and resize back to original size
@@ -60,11 +60,11 @@ async def remove_bg(file: UploadFile = File(...)):
60
 
61
  return Response(content=img_io.getvalue(), media_type="image/png")
62
 
 
63
  @app.get("/")
64
  def read_root():
65
  return {"status": "ok", "message": "Background removal API is running"}
66
 
 
67
  if __name__ == "__main__":
68
-
69
- # Start the FastAPI application
70
- uvicorn.run(app, host="0.0.0.0", port=8000)
 
7
  from transformers import AutoModelForImageSegmentation
8
  import uvicorn
9
  from fastapi.middleware.cors import CORSMiddleware
 
 
10
 
11
  app = FastAPI()
12
+
13
  app.add_middleware(
14
  CORSMiddleware,
15
+ allow_origins=["*"], # For production, specify your frontend domain
16
  allow_credentials=True,
17
  allow_methods=["*"],
18
  allow_headers=["*"],
19
  )
20
 
21
+ # Load model directly from Hugging Face with trust_remote_code=True
22
  device = "cuda" if torch.cuda.is_available() else "cpu"
23
  model = AutoModelForImageSegmentation.from_pretrained('ZhengPeng7/BiRefNet', trust_remote_code=True)
24
  model.to(device)
25
  model.eval()
26
 
27
+ # Define transforms based on the other implementation
28
  transform = transforms.Compose([
29
  transforms.Resize((1024, 1024)),
30
  transforms.ToTensor(),
 
40
  # Transform the image
41
  input_tensor = transform(image).unsqueeze(0).to(device)
42
 
43
+ # Perform inference - notice the key differences here
44
  with torch.no_grad():
45
+ # Take the last element [-1] from the model output and apply sigmoid
46
  pred = model(input_tensor)[-1].sigmoid().cpu()[0].squeeze()
47
 
48
  # Convert to PIL image and resize back to original size
 
60
 
61
  return Response(content=img_io.getvalue(), media_type="image/png")
62
 
63
+ # Add a simple root route for health check
64
  @app.get("/")
65
  def read_root():
66
  return {"status": "ok", "message": "Background removal API is running"}
67
 
68
+ # Make sure this is included for Hugging Face Spaces
69
  if __name__ == "__main__":
70
+ uvicorn.run(app, host="0.0.0.0", port=7860)