File size: 10,260 Bytes
f2dd87f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d62e8b
f2dd87f
e8196d6
f2dd87f
 
 
 
 
 
fc26908
 
 
 
 
e8196d6
f2dd87f
 
 
 
fc26908
f2dd87f
 
 
 
fc26908
 
f2dd87f
 
fc26908
 
f2dd87f
 
 
 
 
 
fc26908
 
f2dd87f
fc26908
 
 
 
 
 
e8196d6
fc26908
 
e8196d6
fc26908
e8196d6
 
fc26908
 
 
 
e8196d6
 
 
 
 
fc26908
 
e8196d6
fc26908
e8196d6
 
 
fc26908
 
f2dd87f
 
e8196d6
fc26908
e8196d6
 
 
 
 
 
fc26908
 
e8196d6
f2dd87f
3d62e8b
f2dd87f
fc26908
 
 
 
 
 
 
 
 
 
e8196d6
fc26908
 
e8196d6
f2dd87f
 
 
fc26908
 
 
e8196d6
 
 
 
 
fc26908
 
 
e8196d6
 
f2dd87f
fc26908
 
 
f2dd87f
e8196d6
fc26908
 
 
e8196d6
 
 
fc26908
e8196d6
 
 
 
fc26908
 
e8196d6
 
f2dd87f
 
fc26908
 
e8196d6
fc26908
 
 
 
e8196d6
fc26908
 
 
e8196d6
fc26908
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8196d6
fc26908
 
 
 
 
 
 
f2dd87f
 
 
 
fc26908
 
 
f2dd87f
 
 
3d62e8b
f2dd87f
fc26908
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f2dd87f
 
 
e8196d6
 
 
 
f2dd87f
 
e8196d6
 
 
f2dd87f
 
e8196d6
 
 
 
 
 
 
 
 
 
 
 
fc26908
f2dd87f
 
e8196d6
f2dd87f
 
fc26908
f2dd87f
fc26908
 
e8196d6
f2dd87f
e8196d6
 
 
 
 
 
 
 
f2dd87f
 
fc26908
 
e8196d6
 
 
 
 
 
 
f2dd87f
 
e8196d6
 
 
 
 
 
 
 
 
 
 
f2dd87f
fc26908
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import os
import time
import hashlib
import requests
import numpy as np
import pandas as pd
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from PIL import Image

import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
import gradio as gr
import spaces

# 1. Personal settings
ROLL = "2415800016"
h = int(hashlib.sha256(ROLL.encode()).hexdigest(), 16)
SEED = h % 100000
LATENT = [3, 4, 5, 6][(h >> 4) & 3]

CITIES = [
    ("Chennai", 13.0827, 80.2707), ("Mumbai", 19.0760, 72.8777), ("Kolkata", 22.5726, 88.3639),
    ("Delhi", 28.6139, 77.2090), ("Bengaluru", 12.9716, 77.5946), ("Hyderabad", 17.3850, 78.4867),
    ("Kochi", 9.9312, 76.2673), ("Bhubaneswar", 20.2961, 85.8245), ("Visakhapatnam", 17.6868, 83.2185),
    ("Ahmedabad", 23.0225, 72.5714), ("Jaipur", 26.9124, 75.7873), ("Guwahati", 26.1445, 91.7362),
    ("Lucknow", 26.8467, 80.9462), ("Pune", 18.5204, 73.8567), ("Madurai", 9.9252, 78.1198),
    ("Coimbatore", 11.0168, 76.9558),
]
CITY, LAT, LON = CITIES[(h >> 24) % len(CITIES)]
WINDOW = [5, 7, 10][(h >> 30) % 3]

# 2. Denoising Autoencoder (Tab 1)
class AE(nn.Module):
    def __init__(self, z=LATENT):
        super().__init__()
        self.enc = nn.Sequential(
            nn.Flatten(), nn.Linear(784, 256), nn.ReLU(),
            nn.Linear(256, 64), nn.ReLU(), nn.Linear(64, z),
        )
        self.dec = nn.Sequential(
            nn.Linear(z, 64), nn.ReLU(), nn.Linear(64, 256),
            nn.ReLU(), nn.Linear(256, 784), nn.Sigmoid(),
        )

    def forward(self, x):
        z = self.enc(x)
        return self.dec(z).view(-1, 1, 28, 28), z

test_ds = torchvision.datasets.MNIST("data", train=False, download=True)
Xte = (test_ds.data.float() / 255.0).unsqueeze(1)

dae_gpu = None

def get_dae_gpu():
    global dae_gpu
    if dae_gpu is not None:
        return dae_gpu

    model = AE().to("cuda")
    checkpoint = f"dae_{ROLL}.pt"
    if os.path.exists(checkpoint):
        state = torch.load(checkpoint, map_location="cuda")
        model.load_state_dict(state)
    else:
        torch.manual_seed(SEED)
        train_ds = torchvision.datasets.MNIST("data", train=True, download=True)
        x_train = (train_ds.data.float() / 255.0).unsqueeze(1).to("cuda")
        opt = torch.optim.Adam(model.parameters(), 1e-3)
        model.train()
        for _ in range(3):
            for start in range(0, len(x_train), 256):
                x = x_train[start:start + 256]
                noisy = (x + 0.5 * torch.randn_like(x)).clamp(0, 1)
                loss = F.mse_loss(model(noisy)[0], x)
                opt.zero_grad()
                loss.backward()
                opt.step()
        torch.save(model.state_dict(), checkpoint)

    model.eval()
    dae_gpu = model
    return dae_gpu

def to28(img):
    arr = np.asarray(
        Image.fromarray(np.asarray(img).astype(np.uint8)).convert("L").resize((28, 28)),
        dtype=np.float32,
    ) / 255.0
    if arr.mean() > 0.5:
        arr = 1.0 - arr
    return torch.from_numpy(arr.copy())[None, None]

def big_from_gpu(t_gpu):
    arr = t_gpu[0, 0].detach().clamp(0, 1).cpu().numpy()
    return (arr * 255).astype(np.uint8).repeat(8, 0).repeat(8, 1)

@spaces.GPU
def denoise(img, idx, sigma):
    model = get_dae_gpu()
    x_cpu = to28(img) if img is not None else Xte[int(idx)][None]
    x_gpu = x_cpu.to("cuda")
    gen = torch.Generator(device="cuda").manual_seed(0)
    noisy_gpu = (x_gpu + float(sigma) * torch.randn(x_gpu.shape, device="cuda", generator=gen)).clamp(0, 1)
    with torch.inference_mode():
        recon_gpu, _ = model(noisy_gpu)
    return big_from_gpu(x_gpu), big_from_gpu(noisy_gpu), big_from_gpu(recon_gpu)

# 3. Weather Anomaly Model (Tab 2)
VARS = [
    "temperature_2m_max", "temperature_2m_min",
    "precipitation_sum", "wind_speed_10m_max", "wind_gusts_10m_max",
]
ATTRIB = "Weather data by Open-Meteo.com (CC BY 4.0)"

def open_meteo(url, **params):
    r = requests.get(url, params=params, timeout=90)
    r.raise_for_status()
    payload = r.json()
    frame = pd.DataFrame(payload["daily"])
    frame["time"] = pd.to_datetime(frame["time"])
    return frame.set_index("time")[VARS].ffill().bfill(), payload.get("daily_units", {})

def transform(frame):
    res = frame[VARS].copy()
    res["precipitation_sum"] = np.log1p(res["precipitation_sum"].clip(lower=0))
    return res

history, _ = open_meteo(
    "https://archive-api.open-meteo.com/v1/archive",
    latitude=LAT, longitude=LON,
    start_date="1991-01-01", end_date="2024-12-31",
    daily=",".join(VARS), timezone="Asia/Kolkata",
)
T = transform(history)
tr_mask = T.index.year <= 2014
monthly_mean = T[tr_mask].groupby(T[tr_mask].index.month).mean()
monthly_std = T[tr_mask].groupby(T[tr_mask].index.month).std() + 1e-6

def standardize(frame):
    months = frame.index.month
    return (frame - monthly_mean.loc[months].values) / monthly_std.loc[months].values

def make_windows(frame, width=WINDOW):
    values = frame.values.astype(np.float32)
    count = len(values) - width + 1
    wins = np.stack([values[i:i + width].ravel() for i in range(count)])
    return wins, frame.index[:count]

Xw, starts = make_windows(standardize(T))
D = Xw.shape[1]

wae_gpu = None
thresh_val = None

def get_wae_and_thresh():
    global wae_gpu, thresh_val
    if wae_gpu is not None and thresh_val is not None:
        return wae_gpu, thresh_val

    Xw_t = torch.from_numpy(Xw.copy()).to("cuda")
    tr_idx = torch.from_numpy(np.asarray(starts.year <= 2014, dtype=np.bool_)).to("cuda")
    train_w = Xw_t[tr_idx]

    torch.manual_seed(SEED)
    model = nn.Sequential(
        nn.Linear(D, 32), nn.ReLU(), nn.Linear(32, 6),
        nn.Linear(6, 32), nn.ReLU(), nn.Linear(32, D),
    ).to("cuda")
    opt = torch.optim.Adam(model.parameters(), 1e-3)
    model.train()
    for _ in range(40):
        perm = torch.randperm(len(train_w), device="cuda")
        for start in range(0, len(perm), 256):
            batch = train_w[perm[start:start + 256]]
            loss = F.mse_loss(model(batch), batch)
            opt.zero_grad()
            loss.backward()
            opt.step()

    model.eval()
    with torch.inference_mode():
        err = ((model(Xw_t) - Xw_t) ** 2).view(len(Xw), WINDOW, len(VARS)).cpu().numpy()
    scores = err.mean(axis=(1, 2))
    thresh_val = float(np.quantile(scores[starts.year <= 2014], 0.99))
    wae_gpu = model
    return wae_gpu, thresh_val

def live_window():
    live, _ = open_meteo(
        "https://api.open-meteo.com/v1/forecast",
        latitude=LAT, longitude=LON,
        daily=",".join(VARS), timezone="Asia/Kolkata",
        past_days=WINDOW, forecast_days=1,
    )
    return live.tail(WINDOW)

@spaces.GPU
def weather():
    model, thresh = get_wae_and_thresh()
    live = live_window()
    values = standardize(transform(live)).values.astype(np.float32).ravel()
    x_gpu = torch.from_numpy(values.copy()[None]).to("cuda")
    with torch.inference_mode():
        sc = F.mse_loss(model(x_gpu), x_gpu).item()

    fig, ax = plt.subplots(figsize=(7, 3))
    labels = live.index.strftime("%d %b")
    ax.bar(labels, live["precipitation_sum"], label="Rain (mm)")
    ax.set_ylabel("Rain (mm)")
    ax.set_xlabel("Date")

    ax2 = ax.twinx()
    ax2.plot(labels, live["temperature_2m_max"], "r-o", label="Tmax")
    ax2.set_ylabel("Tmax (°C)")
    ax.set_title(ATTRIB, fontsize=8)
    fig.tight_layout()

    status = "⚠️ **UNUSUAL**" if sc > thresh else "✅ **Normal**"
    verdict = f"**{CITY}**, last {WINDOW} days: score **{sc:.3f}** vs threshold **{thresh:.3f}** → {status}"
    return verdict, fig

# 4. Feedback Logger (Tab 3)
FB = f"feedback_{ROLL}.csv"

def feedback(roll, stars, comment):
    visitor_roll = str(roll or "").strip().upper()
    if not visitor_roll:
        raise gr.Error("Please enter your roll number.")

    row = pd.DataFrame([{
        "time": time.strftime("%Y-%m-%d %H:%M"),
        "visitor_roll": visitor_roll,
        "stars": int(stars),
        "comment": str(comment or "").strip(),
    }])

    if os.path.exists(FB):
        try:
            existing = pd.read_csv(FB)
            result = pd.concat([existing, row], ignore_index=True)
        except Exception:
            result = row
    else:
        result = row

    result.to_csv(FB, index=False)
    return result

# 5. Gradio Interface
with gr.Blocks(title=f"{ROLL} · Autoencoder app") as demo:
    gr.Markdown(f"# 🧠 {ROLL}'s Autoencoder App\nBuilt in LearnLab Module 3")

    with gr.Tab("Denoiser"):
        with gr.Row():
            image_input = gr.Image(type="numpy", label="Upload a digit photo (optional)")
            with gr.Column():
                image_index = gr.Slider(0, len(Xte) - 1, value=7, step=1, label="Or pick MNIST test image #")
                noise_sigma = gr.Slider(0, 1, value=0.5, step=0.05, label="Noise σ")
                denoise_button = gr.Button("Denoise", variant="primary")
        with gr.Row():
            clean_output = gr.Image(label="Clean")
            noisy_output = gr.Image(label="Noisy")
            denoised_output = gr.Image(label="Denoised")
        denoise_button.click(
            fn=denoise,
            inputs=[image_input, image_index, noise_sigma],
            outputs=[clean_output, noisy_output, denoised_output],
        )

    with gr.Tab(f"Weather alarm · {CITY}"):
        gr.Markdown(f"Anomaly detection for **{CITY}** using the previous {WINDOW} days.")
        weather_button = gr.Button("Check the last days (live Open-Meteo)", variant="primary")
        weather_verdict = gr.Markdown()
        weather_plot = gr.Plot()
        weather_button.click(
            fn=weather,
            inputs=None,
            outputs=[weather_verdict, weather_plot],
        )

    with gr.Tab("Feedback"):
        roll_input = gr.Textbox(label="Your roll number")
        stars_input = gr.Slider(1, 5, value=4, step=1, label="Stars")
        comment_input = gr.Textbox(label="Comment")
        feedback_button = gr.Button("Submit", variant="primary")
        feedback_table = gr.Dataframe()
        feedback_button.click(
            fn=feedback,
            inputs=[roll_input, stars_input, comment_input],
            outputs=feedback_table,
        )

if __name__ == "__main__":
    demo.queue().launch()