manojkumarsh commited on
Commit
951f098
·
verified ·
1 Parent(s): 484ce67

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +16 -10
main.py CHANGED
@@ -9,27 +9,33 @@ import io
9
  from PIL import Image
10
 
11
  api = FastAPI()
12
- session = new_session()
 
13
 
14
  @api.post("/api/remove-bg")
15
  async def remove_bg(file: UploadFile = File(...)):
16
  contents = await file.read()
 
17
  output = remove(contents, session=session)
18
  return Response(content=output, media_type="image/png")
19
 
20
  @api.post("/api/enhance-photo")
21
  async def enhance_photo(file: UploadFile = File(...)):
22
  contents = await file.read()
 
23
  API_URL = "https://api-inference.huggingface.co/models/TencentARC/GFPGAN"
24
- response = requests.post(API_URL, data=contents)
25
- if response.status_code != 200:
26
- return Response(content=contents, media_type="image/png")
27
- return Response(content=response.content, media_type="image/png")
 
 
 
 
 
 
 
28
 
29
  @api.get("/")
30
  async def root():
31
- return {"engine": "ALLBgremove Ultra Lite ACTIVE - Inference API Mode"}
32
-
33
- if __name__ == "__main__":
34
- import uvicorn
35
- uvicorn.run(api, host="0.0.0.0", port=7860)
 
9
  from PIL import Image
10
 
11
  api = FastAPI()
12
+ # Using birefnet-general for high quality background removal
13
+ session = new_session("birefnet-general")
14
 
15
  @api.post("/api/remove-bg")
16
  async def remove_bg(file: UploadFile = File(...)):
17
  contents = await file.read()
18
+ # Process with BiRefNet
19
  output = remove(contents, session=session)
20
  return Response(content=output, media_type="image/png")
21
 
22
  @api.post("/api/enhance-photo")
23
  async def enhance_photo(file: UploadFile = File(...)):
24
  contents = await file.read()
25
+ # Remini style enhancement using Hugging Face Inference API (GFPGAN)
26
  API_URL = "https://api-inference.huggingface.co/models/TencentARC/GFPGAN"
27
+
28
+ try:
29
+ response = requests.post(API_URL, data=contents, timeout=30)
30
+ if response.status_code == 200:
31
+ return Response(content=response.content, media_type="image/png")
32
+ else:
33
+ # If API fails or is busy, return original image as fallback
34
+ return Response(content=contents, media_type="image/png")
35
+ except Exception as e:
36
+ # Fallback to original image on error
37
+ return Response(content=contents, media_type="image/png")
38
 
39
  @api.get("/")
40
  async def root():
41
+ return {"message": "All Background Remove API is running with BiRefNet and GFPGAN Enhancement"}