File size: 931 Bytes
cdcf1d0
6c0952e
 
6779002
6c0952e
6779002
cdcf1d0
6779002
6c0952e
 
 
 
 
 
 
18c7db1
6779002
cdcf1d0
 
6c0952e
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from fastapi import FastAPI, File, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from transformers import pipeline
import io
from PIL import Image

app = FastAPI()

# Allow CORS for Figma plugin
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"], # Update to specific origins in production
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.post("/remove-background")
async def remove_background(file: UploadFile = File(...)):
    # Load the RMBG-1.4 model
    pipe = pipeline('image-segmentation', 'briaai/RMBG-1.4')
    # Read image
    image_data = await file.read()
    image = Image.open(io.BytesIO(image_data))
    # Process image
    output = pipe(image)
    # Convert output to bytes
    output_image = Image.fromarray(output.data)
    output_bytes = io.BytesIO()
    output_image.save(output_bytes, format='PNG')
    return {"image": output_bytes.getvalue()}