-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
52 lines (44 loc) · 1.26 KB
/
main.py
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
40
41
42
43
44
45
46
47
48
49
50
51
52
from fastapi import FastAPI, status, UploadFile
from fastapi.responses import FileResponse
from typing import Annotated
from pydantic import BaseModel
from PIL import Image
from helper import *
from subprocess import Popen
import io
import os
home = os.environ['HOME']
available_dcu = -1
app = FastAPI()
class GenerateBody(BaseModel):
file: UploadFile
class IdResponse(BaseModel):
id: int
@app.get("/avail")
async def check_avail():
available, avail = check_dcu_status()
global available_dcu
available_dcu = avail
os.environ["CUDA_VISIBLE_DEVICES"] = f"{avail}"
return {"available": available}
@app.post("/generate", status_code=status.HTTP_200_OK, response_model=IdResponse)
async def generate(file: UploadFile):
contents = await file.read()
try:
read_image = Image.open(io.BytesIO(contents))
except Exception as e:
print(str(e))
id = next_id()
read_image.save(f"./inits/{id}.png")
infer(id)
return {"id": id}
@app.get("/check")
async def check_finished(id: int):
results = os.listdir("./results")
if f"{id}.png" in results:
return {"finished": True}
else:
return {"finished": False}
@app.get("/fetch")
async def fetch_image(id: int):
return FileResponse(f"./results/{id}.png")