Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,28 +2,35 @@ from fastapi import FastAPI, UploadFile, File
|
|
| 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 |
-
#
|
| 11 |
-
upscaler =
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
@app.get("/")
|
| 18 |
def home():
|
| 19 |
-
return {"status": "upscale
|
| 20 |
|
| 21 |
@app.post("/upscale")
|
| 22 |
async def upscale(image: UploadFile = File(...)):
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
result =
|
| 27 |
|
| 28 |
buffer = io.BytesIO()
|
| 29 |
result.save(buffer, format="PNG")
|
|
|
|
| 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 |
+
# ⚠️ lazy load (IMPORTANT FIX)
|
| 10 |
+
upscaler = None
|
| 11 |
+
|
| 12 |
+
def load_model():
|
| 13 |
+
global upscaler
|
| 14 |
+
if upscaler is None:
|
| 15 |
+
upscaler = pipeline(
|
| 16 |
+
"image-to-image",
|
| 17 |
+
model="eugenesiow/edsr-base",
|
| 18 |
+
device=-1
|
| 19 |
+
)
|
| 20 |
+
return upscaler
|
| 21 |
|
| 22 |
@app.get("/")
|
| 23 |
def home():
|
| 24 |
+
return {"status": "upscale API running"}
|
| 25 |
|
| 26 |
@app.post("/upscale")
|
| 27 |
async def upscale(image: UploadFile = File(...)):
|
| 28 |
+
model = load_model() # load only when needed
|
| 29 |
+
|
| 30 |
+
img_bytes = await image.read()
|
| 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")
|