Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,42 @@
|
|
| 1 |
-
from fastapi import FastAPI, UploadFile, File
|
| 2 |
from fastapi.responses import Response
|
| 3 |
from PIL import Image
|
| 4 |
import io
|
|
|
|
| 5 |
from transformers import pipeline
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
| 25 |
|
| 26 |
@app.post("/upscale")
|
| 27 |
-
async def upscale(
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
img = Image.open(io.BytesIO(img_bytes)).convert("RGB")
|
| 32 |
|
| 33 |
result = model(img)[0]["generated_image"]
|
| 34 |
|
| 35 |
buffer = io.BytesIO()
|
| 36 |
result.save(buffer, format="PNG")
|
| 37 |
|
| 38 |
-
return Response(
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File, Form
|
| 2 |
from fastapi.responses import Response
|
| 3 |
from PIL import Image
|
| 4 |
import io
|
| 5 |
+
|
| 6 |
from transformers import pipeline
|
| 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 |
+
models[2] = pipeline(
|
| 16 |
+
"image-to-image",
|
| 17 |
+
model="eugenesiow/edsr-base",
|
| 18 |
+
device=-1
|
| 19 |
+
)
|
| 20 |
+
elif scale == 4:
|
| 21 |
+
models[4] = pipeline(
|
| 22 |
+
"image-to-image",
|
| 23 |
+
model="eugenesiow/edsr-base-x4",
|
| 24 |
+
device=-1
|
| 25 |
+
)
|
| 26 |
+
return models[scale]
|
| 27 |
|
| 28 |
@app.post("/upscale")
|
| 29 |
+
async def upscale(
|
| 30 |
+
image: UploadFile = File(...),
|
| 31 |
+
scale: int = Form(2)
|
| 32 |
+
):
|
| 33 |
+
model = get_model(scale)
|
| 34 |
|
| 35 |
+
img = Image.open(io.BytesIO(await image.read())).convert("RGB")
|
|
|
|
| 36 |
|
| 37 |
result = model(img)[0]["generated_image"]
|
| 38 |
|
| 39 |
buffer = io.BytesIO()
|
| 40 |
result.save(buffer, format="PNG")
|
| 41 |
|
| 42 |
+
return Response(buffer.getvalue(), media_type="image/png")
|