Brazela commited on
Commit
b50ecbc
·
verified ·
1 Parent(s): 15581d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -8
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 model import upscale_image
7
 
8
  app = FastAPI()
9
 
 
 
 
 
 
 
 
10
  @app.get("/")
11
  def home():
12
- return {"status": "upscale API running"}
13
 
14
  @app.post("/upscale")
15
  async def upscale(image: UploadFile = File(...)):
16
- input_data = await image.read()
17
-
18
- img = Image.open(io.BytesIO(input_data))
19
 
20
- output_img = upscale_image(img)
21
 
22
  buffer = io.BytesIO()
23
- output_img.save(buffer, format="PNG")
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")