videopix commited on
Commit
06796fd
·
verified ·
1 Parent(s): 9f86510

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -49
app.py CHANGED
@@ -1,14 +1,12 @@
1
  import os
2
- import base64
3
- import requests
4
  from io import BytesIO
5
  from PIL import Image
6
  import torch
7
  import numpy as np
8
  from transformers import AutoModelForImageSegmentation
9
- import gradio as gr
10
- from fastapi import FastAPI, Request
11
- from fastapi.responses import StreamingResponse, HTMLResponse
12
 
13
  # -------------------------
14
  # Model Setup
@@ -49,55 +47,30 @@ def process_image(image: Image.Image) -> Image.Image:
49
  image.putalpha(mask)
50
  return image
51
 
52
- # -------------------------
53
- # Gradio Function
54
- # -------------------------
55
- def remove_background_gradio(img):
56
- return process_image(img.convert("RGB"))
57
-
58
- # -------------------------
59
- # Gradio Interface for UI
60
- # -------------------------
61
- demo = gr.Interface(
62
- fn=remove_background_gradio,
63
- inputs=gr.Image(type="pil"),
64
- outputs=gr.Image(type="pil"),
65
- title="Background Removal Tool",
66
- description="Upload an image and get a transparent background."
67
- )
68
-
69
  # -------------------------
70
  # FastAPI App
71
  # -------------------------
72
- app = gr.routes.FastAPI.create_app(demo) # Wrap Gradio
73
 
74
  @app.post("/remove-background")
75
- async def remove_background(request: Request):
76
  """
77
- Custom endpoint: accepts 'file' upload or 'image_url' form.
78
- Returns PNG bytes.
79
  """
80
- form = await request.form()
81
- file = form.get("file")
82
- image_url = form.get("image_url")
83
-
84
- if file:
85
- img = Image.open(file.file).convert("RGB")
86
- elif image_url:
87
- resp = requests.get(image_url)
88
- img = Image.open(BytesIO(resp.content)).convert("RGB")
89
- else:
90
- return {"error": "Provide file or image_url"}
91
 
92
- result = process_image(img)
93
- buf = BytesIO()
94
- result.save(buf, format="PNG")
95
- buf.seek(0)
96
- return StreamingResponse(buf, media_type="image/png")
97
-
98
- # -------------------------
99
- # Optional: Root UI
100
- # -------------------------
101
- @app.get("/", response_class=HTMLResponse)
102
- async def index():
103
- return demo.launch(share=False, inline=True)[0] # Embed Gradio UI
 
1
  import os
 
 
2
  from io import BytesIO
3
  from PIL import Image
4
  import torch
5
  import numpy as np
6
  from transformers import AutoModelForImageSegmentation
7
+ from fastapi import FastAPI, File, Form, UploadFile, HTTPException
8
+ from fastapi.responses import StreamingResponse
9
+ import requests
10
 
11
  # -------------------------
12
  # Model Setup
 
47
  image.putalpha(mask)
48
  return image
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  # -------------------------
51
  # FastAPI App
52
  # -------------------------
53
+ app = FastAPI(title="Background Removal API")
54
 
55
  @app.post("/remove-background")
56
+ async def remove_background(file: UploadFile = File(None), image_url: str = Form(None)):
57
  """
58
+ Accept either an uploaded file or an image URL.
59
+ Returns PNG with transparent background.
60
  """
61
+ try:
62
+ if file:
63
+ img = Image.open(BytesIO(await file.read())).convert("RGB")
64
+ elif image_url:
65
+ resp = requests.get(image_url)
66
+ img = Image.open(BytesIO(resp.content)).convert("RGB")
67
+ else:
68
+ raise HTTPException(status_code=400, detail="Provide file or image_url")
 
 
 
69
 
70
+ result = process_image(img)
71
+ buf = BytesIO()
72
+ result.save(buf, format="PNG")
73
+ buf.seek(0)
74
+ return StreamingResponse(buf, media_type="image/png")
75
+ except Exception as e:
76
+ raise HTTPException(status_code=500, detail=str(e))