Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,27 +2,25 @@ from fastapi import FastAPI, UploadFile, File, Form
|
|
| 2 |
from fastapi.responses import Response
|
| 3 |
from PIL import Image
|
| 4 |
import io
|
| 5 |
-
|
| 6 |
-
from
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
|
|
|
|
|
|
| 10 |
models = {}
|
| 11 |
|
| 12 |
def get_model(scale: int):
|
| 13 |
if scale not in models:
|
| 14 |
if scale == 2:
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
device=-1
|
| 19 |
-
)
|
| 20 |
elif scale == 4:
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
device=-1
|
| 25 |
-
)
|
| 26 |
return models[scale]
|
| 27 |
|
| 28 |
@app.post("/upscale")
|
|
@@ -34,7 +32,7 @@ async def upscale(
|
|
| 34 |
|
| 35 |
img = Image.open(io.BytesIO(await image.read())).convert("RGB")
|
| 36 |
|
| 37 |
-
result = model(img)
|
| 38 |
|
| 39 |
buffer = io.BytesIO()
|
| 40 |
result.save(buffer, format="PNG")
|
|
|
|
| 2 |
from fastapi.responses import Response
|
| 3 |
from PIL import Image
|
| 4 |
import io
|
| 5 |
+
import torch
|
| 6 |
+
from realesrgan import RealESRGAN
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
+
device = torch.device("cpu")
|
| 11 |
+
|
| 12 |
models = {}
|
| 13 |
|
| 14 |
def get_model(scale: int):
|
| 15 |
if scale not in models:
|
| 16 |
if scale == 2:
|
| 17 |
+
m = RealESRGAN(device, scale=2)
|
| 18 |
+
m.load_weights("RealESRGAN_x2.pth", download=True)
|
| 19 |
+
models[2] = m
|
|
|
|
|
|
|
| 20 |
elif scale == 4:
|
| 21 |
+
m = RealESRGAN(device, scale=4)
|
| 22 |
+
m.load_weights("RealESRGAN_x4.pth", download=True)
|
| 23 |
+
models[4] = m
|
|
|
|
|
|
|
| 24 |
return models[scale]
|
| 25 |
|
| 26 |
@app.post("/upscale")
|
|
|
|
| 32 |
|
| 33 |
img = Image.open(io.BytesIO(await image.read())).convert("RGB")
|
| 34 |
|
| 35 |
+
result = model.predict(img)
|
| 36 |
|
| 37 |
buffer = io.BytesIO()
|
| 38 |
result.save(buffer, format="PNG")
|