Spaces:
Sleeping
Sleeping
File size: 1,043 Bytes
d2505cb 0cafa25 d2505cb d7f2276 1b4ae51 d2505cb a75060d d2505cb a75060d d2505cb 03e0bc9 d2505cb 03e0bc9 d2505cb 03e0bc9 d2505cb 03e0bc9 d2505cb 1fc1f90 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import io
import torch
from fastapi import FastAPI, File, UploadFile, HTTPException
from fastapi.responses import StreamingResponse
from PIL import Image
from RealESRGAN import RealESRGAN
app = FastAPI()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = RealESRGAN(device, scale=2)
model.load_weights("weights/RealESRGAN_x2.pth", download=True)
@app.get("/")
async def root():
return {"message": "API is running"}
@app.post("/enhance-img")
async def enhance_img(file: UploadFile = File(...)):
try:
print('Inside the function')
data = await file.read()
print('File read')
img = Image.open(io.BytesIO(data)).convert("RGB")
print('Passing file into network')
sr = model.predict(img)
print("File output")
buf = io.BytesIO()
sr.save(buf, format="PNG")
buf.seek(0)
print('Done')
return StreamingResponse(buf, media_type="image/png")
except Exception as e:
raise HTTPException(500, detail=str(e))
|