Commit ·
af61ad1
0
Parent(s):
Initial Real-ESRGAN custom handler
Browse files- README.md +5 -0
- handler.py +56 -0
- requirements.txt +7 -0
README.md
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Real-ESRGAN Custom Inference Handler
|
| 2 |
+
|
| 3 |
+
Upscales images x4 using the Real-ESRGAN model.
|
| 4 |
+
Provides a Hugging Face Inference Endpoint compatible `EndpointHandler`.
|
| 5 |
+
|
handler.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# handler.py
|
| 2 |
+
from base64 import b64encode, b64decode
|
| 3 |
+
from io import BytesIO
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import numpy as np
|
| 7 |
+
from realesrgan import RealESRGANer
|
| 8 |
+
from basicsr.archs.rrdbnet_arch import RRDBNet
|
| 9 |
+
|
| 10 |
+
class EndpointHandler:
|
| 11 |
+
def __init__(self, model_dir: str = "", **kwargs):
|
| 12 |
+
"""
|
| 13 |
+
Called once when the endpoint starts.
|
| 14 |
+
Loads the Real-ESRGAN model weights.
|
| 15 |
+
"""
|
| 16 |
+
print("🔹 Initializing Real-ESRGAN x4 model...")
|
| 17 |
+
model_path = str(Path(model_dir) / "RealESRGAN_x4plus.pth")
|
| 18 |
+
|
| 19 |
+
# Build model
|
| 20 |
+
rrdbnet = RRDBNet(num_in_ch=3, num_out_ch=3)
|
| 21 |
+
self.upsampler = RealESRGANer(
|
| 22 |
+
scale=4,
|
| 23 |
+
model_path=model_path,
|
| 24 |
+
model=rrdbnet,
|
| 25 |
+
tile=0,
|
| 26 |
+
pre_pad=0,
|
| 27 |
+
half=True,
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
def __call__(self, data):
|
| 31 |
+
"""
|
| 32 |
+
Called for each request.
|
| 33 |
+
Expects a dict with 'inputs' = base64-encoded image or bytes.
|
| 34 |
+
Returns base64-encoded upscaled image.
|
| 35 |
+
"""
|
| 36 |
+
image = data.get("inputs")
|
| 37 |
+
|
| 38 |
+
if isinstance(image, str):
|
| 39 |
+
image = Image.open(BytesIO(b64decode(image)))
|
| 40 |
+
elif isinstance(image, bytes):
|
| 41 |
+
image = Image.open(BytesIO(image))
|
| 42 |
+
else:
|
| 43 |
+
raise ValueError("Input must be base64 string or bytes")
|
| 44 |
+
|
| 45 |
+
image = np.array(image)
|
| 46 |
+
image = image[:, :, ::-1] # RGB→BGR
|
| 47 |
+
output, _ = self.upsampler.enhance(image, outscale=4)
|
| 48 |
+
output = output[:, :, ::-1] # BGR→RGB
|
| 49 |
+
out_img = Image.fromarray(output)
|
| 50 |
+
|
| 51 |
+
buf = BytesIO()
|
| 52 |
+
out_img.save(buf, format="PNG")
|
| 53 |
+
encoded = b64encode(buf.getvalue()).decode("utf-8")
|
| 54 |
+
|
| 55 |
+
return {"image": encoded}
|
| 56 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch>=2.0.0
|
| 2 |
+
torchvision>=0.15.0
|
| 3 |
+
realesrgan>=0.3.0
|
| 4 |
+
basicsr>=1.4.2
|
| 5 |
+
numpy
|
| 6 |
+
Pillow
|
| 7 |
+
|