Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from torchvision.transforms import ToTensor, ToPILImage
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import numpy as np
|
| 6 |
+
import cv2
|
| 7 |
+
|
| 8 |
+
# Load a lightweight model (example: ESRGAN)
|
| 9 |
+
model = torch.hub.load('facebookresearch/esrgan', 'esrgan', pretrained=True)
|
| 10 |
+
model.eval()
|
| 11 |
+
|
| 12 |
+
def enhance_image(input_img):
|
| 13 |
+
# Convert to PIL Image if not already
|
| 14 |
+
if isinstance(input_img, np.ndarray):
|
| 15 |
+
input_img = Image.fromarray(input_img)
|
| 16 |
+
|
| 17 |
+
# Preprocess
|
| 18 |
+
input_tensor = ToTensor()(input_img).unsqueeze(0)
|
| 19 |
+
|
| 20 |
+
# Enhance (disable gradients for CPU)
|
| 21 |
+
with torch.no_grad():
|
| 22 |
+
output_tensor = model(input_tensor)
|
| 23 |
+
|
| 24 |
+
# Convert back to PIL Image
|
| 25 |
+
output_img = ToPILImage()(output_tensor.squeeze(0))
|
| 26 |
+
return output_img
|
| 27 |
+
|
| 28 |
+
# Gradio Interface
|
| 29 |
+
iface = gr.Interface(
|
| 30 |
+
fn=enhance_image,
|
| 31 |
+
inputs=gr.Image(label="Upload Image"),
|
| 32 |
+
outputs=gr.Image(label="Enhanced Image"),
|
| 33 |
+
title="🚀 Image Enhancer (CPU)",
|
| 34 |
+
description="Upload an image to enhance its quality (runs on CPU)."
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
iface.launch()
|