Brazela commited on
Commit
1ac68ec
·
verified ·
1 Parent(s): bb6aded

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -32
app.py CHANGED
@@ -2,47 +2,20 @@ from fastapi import FastAPI, UploadFile, File, Form
2
  from fastapi.responses import Response
3
  from PIL import Image
4
  import io
5
- import torch
6
- import numpy as np
7
 
8
  app = FastAPI()
9
 
10
- device = torch.device("cpu")
11
- torch.set_num_threads(1)
12
-
13
- models = {}
14
-
15
- def get_model(scale: int):
16
- if scale not in models:
17
- model = torch.hub.load(
18
- "eugenesiow/edsr",
19
- "edsr_base",
20
- scale=scale,
21
- pretrained=True,
22
- skip_validation=True,
23
- )
24
- model = model.to(device).eval()
25
- models[scale] = model
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
- with torch.no_grad():
38
- sr = model(img)
39
-
40
- sr_pil = sr.squeeze(0).clamp(0, 1).cpu().numpy()
41
- sr_pil = np.transpose(sr_pil, (1, 2, 0))
42
- sr_pil = (sr_pil * 255).astype(np.uint8)
43
- sr_pil = Image.fromarray(sr_pil)
44
 
45
  buffer = io.BytesIO()
46
- sr_pil.save(buffer, format="PNG")
47
-
48
  return Response(buffer.getvalue(), media_type="image/png")
 
2
  from fastapi.responses import Response
3
  from PIL import Image
4
  import io
 
 
5
 
6
  app = FastAPI()
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  @app.post("/upscale")
9
  async def upscale(
10
  image: UploadFile = File(...),
11
  scale: int = Form(2)
12
  ):
13
+ img = Image.open(io.BytesIO(await image.read()))
14
 
15
+ # Simple LANCZOS resize — works on every machine, no deps needed
16
+ new_size = (img.width * scale, img.height * scale)
17
+ img = img.resize(new_size, Image.LANCZOS)
 
 
 
 
 
 
18
 
19
  buffer = io.BytesIO()
20
+ img.save(buffer, format="PNG")
 
21
  return Response(buffer.getvalue(), media_type="image/png")