-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathserver.py
More file actions
39 lines (29 loc) · 1.09 KB
/
server.py
File metadata and controls
39 lines (29 loc) · 1.09 KB
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
32
33
34
35
36
37
38
39
from fastapi import FastAPI
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from fastapi import Response
from removebg import RemoveBgDTO, process
app = FastAPI()
app.mount("/assets", StaticFiles(directory="static/assets"), name="assets")
@app.get("/")
async def index_html():
return FileResponse('static/index.html')
@app.get("/vite.svg")
async def index_html():
return FileResponse('static/vite.svg')
@app.post("/removebg")
async def removebg_post(dto: RemoveBgDTO):
code, msg, result = process(dto=dto)
if code != 0:
return {'code': code, 'msg': msg, 'result': ''}
if dto.responseFormat == 0:
return {'code': code, 'msg': msg, 'result': f"data:image/png;base64,{result}"}
else:
return Response(content=result, media_type="image/png")
@app.get("/removebg")
async def removebg_get(url: str):
dto = RemoveBgDTO(url=url, responseFormat=1)
code, msg, result = process(dto=dto)
if code != 0:
return {'code': code, 'msg': msg, 'result': ''}
return Response(content=result, media_type="image/png")