Nebulixlabs commited on
Commit
db602d2
·
verified ·
1 Parent(s): 3f96d07

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import torch.nn as nn
4
+ import torchvision.transforms as transforms
5
+ from PIL import Image
6
+
7
+ # ==========================================
8
+ # 1. MODEL ARCHITECTURE (Same as training)
9
+ # ==========================================
10
+ class ESPCN(nn.Module):
11
+ def __init__(self, upscale_factor):
12
+ super(ESPCN, self).__init__()
13
+ self.conv1 = nn.Conv2d(3, 64, kernel_size=5, padding=2)
14
+ self.conv2 = nn.Conv2d(64, 64, kernel_size=3, padding=1)
15
+ self.conv3 = nn.Conv2d(64, 32, kernel_size=3, padding=1)
16
+ self.conv4 = nn.Conv2d(32, 3 * (upscale_factor ** 2), kernel_size=3, padding=1)
17
+ self.pixel_shuffle = nn.PixelShuffle(upscale_factor)
18
+ self.relu = nn.ReLU()
19
+
20
+ def forward(self, x):
21
+ x = self.relu(self.conv1(x))
22
+ x = self.relu(self.conv2(x))
23
+ x = self.relu(self.conv3(x))
24
+ x = self.pixel_shuffle(self.conv4(x))
25
+ return x
26
+
27
+ # ==========================================
28
+ # 2. LOAD THE TRAINED MODEL (On CPU)
29
+ # ==========================================
30
+ # Device ko CPU set kar rahe hain taaki HF Spaces ke free tier me bina GPU ke chale
31
+ device = torch.device("cpu")
32
+ model = ESPCN(upscale_factor=4)
33
+
34
+ # Yahan hum .pth file load kar rahe hain
35
+ try:
36
+ model.load_state_dict(torch.load("universal_sr_model.pth", map_location=device))
37
+ model.eval()
38
+ print("Model loaded successfully!")
39
+ except Exception as e:
40
+ print(f"Error loading model: {e}")
41
+
42
+ # ==========================================
43
+ # 3. INFERENCE FUNCTION FOR GRADIO
44
+ # ==========================================
45
+ def enhance_image(img):
46
+ if img is None:
47
+ return None
48
+
49
+ # Image ko RGB me ensure karna
50
+ img = img.convert('RGB')
51
+
52
+ # Image ko tensor me convert karna aur batch dimension add karna
53
+ input_tensor = transforms.ToTensor()(img).unsqueeze(0).to(device)
54
+
55
+ # Model se pass karna (no gradients needed for fast inference)
56
+ with torch.no_grad():
57
+ output_tensor = model(input_tensor)
58
+
59
+ # Output tensor ko wapas image me convert karna
60
+ output_tensor = output_tensor.squeeze().clamp(0, 1)
61
+ output_img = transforms.ToPILImage()(output_tensor)
62
+
63
+ return output_img
64
+
65
+ # ==========================================
66
+ # 4. GRADIO INTERFACE SETUP
67
+ # ==========================================
68
+ # UI design setup
69
+ iface = gr.Interface(
70
+ fn=enhance_image,
71
+ inputs=gr.Image(type="pil", label="Upload Unclear/Low-Res Image"),
72
+ outputs=gr.Image(type="pil", label="4x Enhanced High-Res Image"),
73
+ title="🌟 AI Super Resolution (4x Upscaler)",
74
+ description="Apni unclear ya chhoti image upload karein aur hamara custom ESPCN model use 4x bada aur sharp kar dega. Yeh model scratch se train kiya gaya hai!",
75
+ allow_flagging="never"
76
+ )
77
+
78
+ # App launch karna
79
+ if __name__ == "__main__":
80
+ iface.launch()