Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,59 @@
|
|
| 1 |
import io
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
|
|
|
|
|
|
| 4 |
from fastapi import FastAPI, UploadFile, File
|
| 5 |
from fastapi.responses import Response
|
| 6 |
-
from PIL import Image
|
| 7 |
from realesrgan import RealESRGANer
|
| 8 |
from basicsr.archs.rrdbnet_arch import RRDBNet
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
-
# 🧠
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
|
| 14 |
upsampler = RealESRGANer(
|
| 15 |
scale=4,
|
| 16 |
-
model_path=
|
| 17 |
model=model,
|
| 18 |
-
tile=400, #
|
| 19 |
tile_pad=10,
|
| 20 |
pre_pad=0,
|
| 21 |
-
half=False
|
| 22 |
)
|
| 23 |
|
| 24 |
@app.get("/")
|
| 25 |
def home():
|
| 26 |
-
return {"status": "
|
| 27 |
|
| 28 |
@app.post("/upscale")
|
| 29 |
async def upscale(file: UploadFile = File(...)):
|
| 30 |
-
# 1. Read uploaded file
|
| 31 |
-
data = await file.read()
|
| 32 |
-
nparr = np.frombuffer(data, np.uint8)
|
| 33 |
-
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 34 |
-
|
| 35 |
-
# 2. AI Neural Inference
|
| 36 |
try:
|
| 37 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
output, _ = upsampler.enhance(img, outscale=4)
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
_, encoded_img = cv2.imencode('.jpg', output, [int(cv2.IMWRITE_JPEG_QUALITY),
|
| 42 |
return Response(content=encoded_img.tobytes(), media_type="image/jpeg")
|
|
|
|
| 43 |
except Exception as e:
|
| 44 |
-
|
|
|
|
| 45 |
|
| 46 |
if __name__ == "__main__":
|
| 47 |
import uvicorn
|
|
|
|
| 1 |
import io
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
+
import requests
|
| 5 |
+
import os
|
| 6 |
from fastapi import FastAPI, UploadFile, File
|
| 7 |
from fastapi.responses import Response
|
|
|
|
| 8 |
from realesrgan import RealESRGANer
|
| 9 |
from basicsr.archs.rrdbnet_arch import RRDBNet
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
|
| 13 |
+
# 🧠 Setup Real-ESRGAN Model
|
| 14 |
+
model_url = 'https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth'
|
| 15 |
+
model_path = 'RealESRGAN_x4plus.pth'
|
| 16 |
+
|
| 17 |
+
if not os.path.exists(model_path):
|
| 18 |
+
print("Downloading AI model...")
|
| 19 |
+
response = requests.get(model_url)
|
| 20 |
+
with open(model_path, 'wb') as f:
|
| 21 |
+
f.write(response.content)
|
| 22 |
+
|
| 23 |
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
|
| 24 |
upsampler = RealESRGANer(
|
| 25 |
scale=4,
|
| 26 |
+
model_path=model_path,
|
| 27 |
model=model,
|
| 28 |
+
tile=400, # Prevents "Out of Memory" errors on free CPUs
|
| 29 |
tile_pad=10,
|
| 30 |
pre_pad=0,
|
| 31 |
+
half=False
|
| 32 |
)
|
| 33 |
|
| 34 |
@app.get("/")
|
| 35 |
def home():
|
| 36 |
+
return {"status": "Silent Neural HD Engine Online"}
|
| 37 |
|
| 38 |
@app.post("/upscale")
|
| 39 |
async def upscale(file: UploadFile = File(...)):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
try:
|
| 41 |
+
# Read image
|
| 42 |
+
data = await file.read()
|
| 43 |
+
nparr = np.frombuffer(data, np.uint8)
|
| 44 |
+
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 45 |
+
|
| 46 |
+
# Actual AI Processing
|
| 47 |
+
# outscale=4 makes it 4x bigger and sharper
|
| 48 |
output, _ = upsampler.enhance(img, outscale=4)
|
| 49 |
|
| 50 |
+
# Convert back to JPG
|
| 51 |
+
_, encoded_img = cv2.imencode('.jpg', output, [int(cv2.IMWRITE_JPEG_QUALITY), 90])
|
| 52 |
return Response(content=encoded_img.tobytes(), media_type="image/jpeg")
|
| 53 |
+
|
| 54 |
except Exception as e:
|
| 55 |
+
print(f"Error: {str(e)}")
|
| 56 |
+
return {"error": "AI Engine busy or image too large"}
|
| 57 |
|
| 58 |
if __name__ == "__main__":
|
| 59 |
import uvicorn
|