Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, UploadFile, File, Form
|
| 2 |
from fastapi.responses import Response
|
| 3 |
from PIL import Image
|
| 4 |
import io
|
| 5 |
-
import numpy as np
|
| 6 |
-
import torch
|
| 7 |
from super_image import MsrnModel, ImageLoader
|
| 8 |
-
import cv2
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
models = {}
|
| 13 |
|
|
|
|
| 14 |
def get_model(scale: int):
|
| 15 |
if scale not in models:
|
| 16 |
model = MsrnModel.from_pretrained("eugenesiow/msrn", scale=scale)
|
|
@@ -18,11 +18,9 @@ def get_model(scale: int):
|
|
| 18 |
models[scale] = model
|
| 19 |
return models[scale]
|
| 20 |
|
|
|
|
| 21 |
@app.post("/upscale")
|
| 22 |
-
async def upscale(
|
| 23 |
-
image: UploadFile = File(...),
|
| 24 |
-
scale: int = Form(2)
|
| 25 |
-
):
|
| 26 |
try:
|
| 27 |
model = get_model(scale)
|
| 28 |
except Exception as e:
|
|
@@ -37,16 +35,19 @@ async def upscale(
|
|
| 37 |
inputs = ImageLoader.load_image(img)
|
| 38 |
pred = model(inputs)
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
result.save(buffer, format="PNG")
|
| 46 |
-
return Response(buffer.getvalue(), media_type="image/png")
|
| 47 |
except Exception as e:
|
| 48 |
return Response(
|
| 49 |
f"Upscale failed: {type(e).__name__}: {e}",
|
| 50 |
status_code=500,
|
| 51 |
media_type="text/plain",
|
| 52 |
-
)
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import tempfile
|
| 3 |
from fastapi import FastAPI, UploadFile, File, Form
|
| 4 |
from fastapi.responses import Response
|
| 5 |
from PIL import Image
|
| 6 |
import io
|
|
|
|
|
|
|
| 7 |
from super_image import MsrnModel, ImageLoader
|
|
|
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
models = {}
|
| 12 |
|
| 13 |
+
|
| 14 |
def get_model(scale: int):
|
| 15 |
if scale not in models:
|
| 16 |
model = MsrnModel.from_pretrained("eugenesiow/msrn", scale=scale)
|
|
|
|
| 18 |
models[scale] = model
|
| 19 |
return models[scale]
|
| 20 |
|
| 21 |
+
|
| 22 |
@app.post("/upscale")
|
| 23 |
+
async def upscale(image: UploadFile = File(...), scale: int = Form(2)):
|
|
|
|
|
|
|
|
|
|
| 24 |
try:
|
| 25 |
model = get_model(scale)
|
| 26 |
except Exception as e:
|
|
|
|
| 35 |
inputs = ImageLoader.load_image(img)
|
| 36 |
pred = model(inputs)
|
| 37 |
|
| 38 |
+
fd, path = tempfile.mkstemp(suffix=".png")
|
| 39 |
+
try:
|
| 40 |
+
ImageLoader.save_image(pred, path)
|
| 41 |
+
os.close(fd)
|
| 42 |
+
with open(path, "rb") as f:
|
| 43 |
+
data = f.read()
|
| 44 |
+
finally:
|
| 45 |
+
os.unlink(path)
|
| 46 |
|
| 47 |
+
return Response(data, media_type="image/png")
|
|
|
|
|
|
|
| 48 |
except Exception as e:
|
| 49 |
return Response(
|
| 50 |
f"Upscale failed: {type(e).__name__}: {e}",
|
| 51 |
status_code=500,
|
| 52 |
media_type="text/plain",
|
| 53 |
+
)
|