JohanBeytell commited on
Commit
e3fd7d4
·
verified ·
1 Parent(s): 740f9f9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -0
app.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import torch.nn.functional as F
4
+ import gradio as gr
5
+ from PIL import Image
6
+ import torchvision.transforms.functional as TF
7
+
8
+ # --- 1. MODEL ARCHITECTURE ---
9
+ class PureResBlock(nn.Module):
10
+ def __init__(self, channels):
11
+ super(PureResBlock, self).__init__()
12
+ self.conv1 = nn.Conv2d(channels, channels, kernel_size=3, padding=1, padding_mode='replicate')
13
+ self.act = nn.ReLU(inplace=True)
14
+ self.conv2 = nn.Conv2d(channels, channels, kernel_size=3, padding=1, padding_mode='replicate')
15
+ self.res_scale = 1.0
16
+
17
+ def forward(self, x):
18
+ res = self.conv1(x)
19
+ res = self.act(res)
20
+ res = self.conv2(res)
21
+ return x + (res * self.res_scale)
22
+
23
+ class FastEDSR(nn.Module):
24
+ def __init__(self, scale_factor=2, num_blocks=8, channels=64):
25
+ super(FastEDSR, self).__init__()
26
+ self.scale_factor = scale_factor
27
+
28
+ self.head = nn.Conv2d(3, channels, kernel_size=3, padding=1, padding_mode='replicate')
29
+ self.body = nn.Sequential(*[PureResBlock(channels) for _ in range(num_blocks)])
30
+ self.tail = nn.Conv2d(channels, channels, kernel_size=3, padding=1, padding_mode='replicate')
31
+ self.sub_pixel = nn.Sequential(
32
+ nn.Conv2d(channels, 3 * (scale_factor ** 2), kernel_size=3, padding=1, padding_mode='replicate'),
33
+ nn.PixelShuffle(scale_factor)
34
+ )
35
+
36
+ def forward(self, x):
37
+ base_upscaled = F.interpolate(x, scale_factor=self.scale_factor, mode='bicubic', align_corners=False)
38
+ f0 = self.head(x)
39
+ f_body = self.body(f0)
40
+ f_body = self.tail(f_body)
41
+ f_out = f0 + f_body
42
+ details = self.sub_pixel(f_out)
43
+ return base_upscaled + details
44
+
45
+ # --- 2. INITIALIZATION ---
46
+ device = torch.device('cpu') # Hugging Face Free Tier runs on CPU
47
+ model = FastEDSR(scale_factor=2, num_blocks=8, channels=64)
48
+
49
+ # Load the weights (Update this string if your file is named differently in the HF root)
50
+ model_path = "FastEDSR_x2_31dB.pth"
51
+ model.load_state_dict(torch.load(model_path, map_location=device))
52
+ model.eval()
53
+
54
+ # --- 3. INFERENCE FUNCTION ---
55
+ def upscale_image(img):
56
+ if img is None:
57
+ return None
58
+
59
+ # Enforce constraints to prevent CPU OOM timeouts
60
+ # Max input 1024px -> Max output 2048px (2K)
61
+ max_input_dim = 1024
62
+ w, h = img.size
63
+
64
+ if w > max_input_dim or h > max_input_dim:
65
+ scale = max_input_dim / max(w, h)
66
+ new_w, new_h = int(w * scale), int(h * scale)
67
+ img = img.resize((new_w, new_h), Image.BICUBIC)
68
+
69
+ # Preprocess
70
+ img = img.convert('RGB')
71
+ input_tensor = TF.to_tensor(img).unsqueeze(0).to(device)
72
+
73
+ # Forward Pass
74
+ with torch.no_grad():
75
+ output_tensor = model(input_tensor)
76
+
77
+ # Postprocess
78
+ output_tensor = output_tensor.squeeze(0).clamp(0, 1)
79
+ output_img = TF.to_pil_image(output_tensor)
80
+
81
+ return output_img
82
+
83
+ # --- 4. GRADIO UI ---
84
+ with gr.Blocks(theme=gr.themes.Soft()) as app:
85
+ gr.Markdown(
86
+ """
87
+ # ⚡ FastEDSR 2x Image Upscaler
88
+ Upload an image to enhance and upscale it by 2x.
89
+ *Note: To ensure stability on CPU infrastructure, input images larger than 1024px are proportionally downscaled before processing to guarantee a maximum 2K output.*
90
+ """
91
+ )
92
+
93
+ with gr.Row():
94
+ with gr.Column():
95
+ input_image = gr.Image(type="pil", label="Low Resolution Input")
96
+ upscale_btn = gr.Button("Upscale Image", variant="primary")
97
+ with gr.Column():
98
+ output_image = gr.Image(type="pil", label="2x High Resolution Output")
99
+
100
+ upscale_btn.click(fn=upscale_image, inputs=input_image, outputs=output_image)
101
+
102
+ if __name__ == "__main__":
103
+ app.launch()