videopix commited on
Commit
d82caa8
·
verified ·
1 Parent(s): 87dba9c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -22
app.py CHANGED
@@ -1,11 +1,9 @@
1
  import io
2
  import os
3
  import threading
4
- import queue
5
- import psutil
6
- import asyncio
7
  from fastapi import FastAPI, File, UploadFile, Request
8
  from fastapi.responses import StreamingResponse, HTMLResponse
 
9
  from PIL import Image
10
  import torch
11
  import torchvision.transforms as transforms
@@ -33,19 +31,7 @@ print(f"ONNX model loaded with providers: {providers}")
33
  # Lock for thread-safe ONNX inference
34
  onnx_lock = threading.Lock()
35
 
36
- # Request queue for load management
37
- request_queue = queue.Queue()
38
-
39
- # System thresholds
40
- MAX_CPU_USAGE = 85.0 # percent
41
- MAX_RAM_USAGE = 85.0 # percent
42
-
43
- def system_under_load() -> bool:
44
- """Check if system is under heavy load."""
45
- cpu = psutil.cpu_percent(interval=0.1)
46
- mem = psutil.virtual_memory().percent
47
- return cpu > MAX_CPU_USAGE or mem > MAX_RAM_USAGE
48
-
49
  def run_model_onnx(input_tensor: torch.Tensor) -> torch.Tensor:
50
  with onnx_lock: # ensure thread safety
51
  ort_inputs = {onnx_session.get_inputs()[0].name: input_tensor.cpu().numpy()}
@@ -67,14 +53,10 @@ def process_image(image: Image.Image) -> Image.Image:
67
  return image_rgba
68
 
69
  # FastAPI app
70
- app = FastAPI(title="Background Removal API with Auto Resource Management")
71
 
72
  @app.post("/remove-background")
73
  async def remove_background(file: UploadFile = File(...)):
74
- # Queue request if system is under load
75
- while system_under_load():
76
- await asyncio.sleep(0.2)
77
-
78
  image = Image.open(file.file).convert("RGB")
79
  result_image = process_image(image)
80
 
@@ -177,4 +159,4 @@ async def home(request: Request):
177
  </script>
178
  </body>
179
  </html>
180
- """
 
1
  import io
2
  import os
3
  import threading
 
 
 
4
  from fastapi import FastAPI, File, UploadFile, Request
5
  from fastapi.responses import StreamingResponse, HTMLResponse
6
+ from fastapi.staticfiles import StaticFiles
7
  from PIL import Image
8
  import torch
9
  import torchvision.transforms as transforms
 
31
  # Lock for thread-safe ONNX inference
32
  onnx_lock = threading.Lock()
33
 
34
+ # Helper functions
 
 
 
 
 
 
 
 
 
 
 
 
35
  def run_model_onnx(input_tensor: torch.Tensor) -> torch.Tensor:
36
  with onnx_lock: # ensure thread safety
37
  ort_inputs = {onnx_session.get_inputs()[0].name: input_tensor.cpu().numpy()}
 
53
  return image_rgba
54
 
55
  # FastAPI app
56
+ app = FastAPI(title="Background Removal API")
57
 
58
  @app.post("/remove-background")
59
  async def remove_background(file: UploadFile = File(...)):
 
 
 
 
60
  image = Image.open(file.file).convert("RGB")
61
  result_image = process_image(image)
62
 
 
159
  </script>
160
  </body>
161
  </html>
162
+ """