Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,24 +2,30 @@ from fastapi import FastAPI, UploadFile, File
|
|
| 2 |
from fastapi.responses import Response
|
| 3 |
from PIL import Image
|
| 4 |
import io
|
| 5 |
-
|
| 6 |
-
from
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
@app.get("/")
|
| 11 |
def home():
|
| 12 |
-
return {"status": "upscale
|
| 13 |
|
| 14 |
@app.post("/upscale")
|
| 15 |
async def upscale(image: UploadFile = File(...)):
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
img = Image.open(io.BytesIO(input_data))
|
| 19 |
|
| 20 |
-
|
| 21 |
|
| 22 |
buffer = io.BytesIO()
|
| 23 |
-
|
| 24 |
|
| 25 |
return Response(content=buffer.getvalue(), media_type="image/png")
|
|
|
|
| 2 |
from fastapi.responses import Response
|
| 3 |
from PIL import Image
|
| 4 |
import io
|
| 5 |
+
import torch
|
| 6 |
+
from transformers import pipeline
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
+
# load once (important)
|
| 11 |
+
upscaler = pipeline(
|
| 12 |
+
"image-to-image",
|
| 13 |
+
model="caidas/swin2SR-realworld-sr-x2-64-bsrgan-psnr",
|
| 14 |
+
device=-1
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
@app.get("/")
|
| 18 |
def home():
|
| 19 |
+
return {"status": "upscale api running"}
|
| 20 |
|
| 21 |
@app.post("/upscale")
|
| 22 |
async def upscale(image: UploadFile = File(...)):
|
| 23 |
+
input_bytes = await image.read()
|
| 24 |
+
img = Image.open(io.BytesIO(input_bytes)).convert("RGB")
|
|
|
|
| 25 |
|
| 26 |
+
result = upscaler(img)[0]["generated_image"]
|
| 27 |
|
| 28 |
buffer = io.BytesIO()
|
| 29 |
+
result.save(buffer, format="PNG")
|
| 30 |
|
| 31 |
return Response(content=buffer.getvalue(), media_type="image/png")
|