adnlp commited on
Commit
c9b8e8c
·
verified ·
1 Parent(s): ec69fbd

Upload 2 files

Browse files

Logo changed.
Updated Main interface and integrate with custom model (using hf model id)

Files changed (2) hide show
  1. app.py +434 -353
  2. logo.png +2 -2
app.py CHANGED
@@ -1,354 +1,435 @@
1
- import os
2
- import gradio as gr
3
- import numpy as np
4
- import pandas as pd
5
- import matplotlib.pyplot as plt
6
- import io
7
- from PIL import Image
8
- import pickle
9
- import requests
10
- import cv2
11
-
12
- hf_token = {
13
- "clipqwentimer": os.environ["HF_CLIPQwenTimer_Token"],
14
- "clipllamatimer": os.environ["HF_CLIPLLaMATimer_Token"],
15
- "blipqwentimer": os.environ["HF_BLIPQwenTimer_Token"],
16
- "blipllamatimer": os.environ["HF_BLIPLLaMATimer_Token"],
17
- "clipqwenchronos": os.environ["HF_CLIPQwenChronos_Token"],
18
- "clipllamachronos": os.environ["HF_CLIPLLaMAChronos_Token"],
19
- "blipqwenchronos": os.environ["HF_BLIPQwenChronos_Token"],
20
- "blipllamachronos": os.environ["HF_BLIPLLaMAChronos_Token"]
21
- }
22
-
23
- with open('example/inputs.pkl', 'rb') as f:
24
- inputs = pickle.load(f)
25
-
26
- with open('example/targets.pkl', 'rb') as f:
27
- targets = pickle.load(f)
28
-
29
- descriptions = {
30
- "NN5 Daily": "Daily cash withdrawal volumes from automated teller machines (ATMs) in the United Kingdom, originally used in the NN5 forecasting competition.",
31
- "Australian Electricity": "Half-hourly electricity demand data across five Australian states.",
32
- "CIF 2016": "Monthly banking time series used in the CIF 2016 forecasting challenge, reflecting customer financial behaviours.",
33
- "Tourism Monthly": "Monthly tourism-related time series used in the Kaggle Tourism forecasting competition, covering various regions and visitor types."
34
- }
35
-
36
- context_length = {
37
- "NN5 Daily": 56,
38
- "Australian Electricity": 48,
39
- "CIF 2016": 12,
40
- "Tourism Monthly": 24
41
- }
42
-
43
- def selected_dataset(dataset):
44
- gallery_items = [(Image.open(f'example/img/{dataset.replace(" ", "_")}/{i}.png').convert('RGB'), str(i+1)) for i in range(3)]
45
- gallery_items.append((Image.open('example/img/custom.png').convert('RGB'), 'Custom Input'))
46
- return gr.Gallery(gallery_items, interactive=False, height="350px", object_fit="contain", preview=True), gr.Textbox(value=descriptions[dataset], label="Dataset Description", interactive=False)
47
-
48
- def selected_example(gallery, evt: gr.SelectData):
49
- if evt.index == len(gallery) -1:
50
- return -1
51
- else:
52
- return evt.index
53
-
54
- def update_guide_markdown(dataset, example_index):
55
- if example_index is None:
56
- return gr.Markdown(visible=False), gr.File(visible=False)
57
- elif example_index == -1: # Custom Input
58
- return (
59
- gr.Markdown(
60
- value=f"To use custom input, please use the sample csv file below. Do not change the name of columns. Only the first {context_length[dataset]} values will be used as input time series.",
61
- visible=True
62
- ),
63
- gr.File(value="example/sample.csv", label="Sample CSV File", visible=True)
64
- )
65
- else:
66
- df = inputs[dataset][example_index]
67
- min = df.min()
68
- max = df.max()
69
- min_timestamp = pd.Series(min["Timestamp"]).to_string(index=False)
70
- max_timestamp = pd.Series(max["Timestamp"]).to_string(index=False)
71
- min_value = min["Value"]
72
- max_value = max["Value"]
73
- return (
74
- gr.Markdown(
75
- value=f"This time series contains values from {min_timestamp} to {max_timestamp}, with a minimum value of {min_value:.4f} and a maximum value of {max_value:.4f}.",
76
- visible=True
77
- ),
78
- gr.File(visible=False)
79
- )
80
-
81
- def update_time_series_dataframe(dataset, example_index):
82
- if example_index is None:
83
- return None, None
84
- elif example_index == -1: # Custom Input
85
- return gr.File(label="Time Series CSV File", file_types=[".csv"], visible=True), gr.Dataframe(value=None, visible=False)
86
- else:
87
- df = inputs[dataset][example_index]
88
- return gr.File(value=None, visible=False), gr.Dataframe(value=df, label="Time Series Input", interactive=False, visible=True)
89
-
90
- def load_csv(example_index, file):
91
- if example_index == -1:
92
- if file is not None:
93
- return gr.Dataframe(value=pd.read_csv(file.name), visible=True)
94
- else:
95
- return gr.Dataframe(value=None, visible=False)
96
- else:
97
- return gr.skip()
98
-
99
- def vision_attention_rollout(attentions, start_layer=0, end_layer=12):
100
- seq_len = attentions.shape[-1]
101
- result = np.eye(seq_len)
102
-
103
- for attn in attentions[start_layer:end_layer]:
104
- attn_heads = attn.mean(axis=0)
105
- attn_aug = attn_heads + np.eye(seq_len)
106
- attn_aug = attn_aug / attn_aug.sum(axis=-1, keepdims=True)
107
- result = attn_aug @ result
108
-
109
- return result[0, -49:]
110
-
111
- def plot_vision_heatmap(image, rollout_attention, alpha=0.5, cmap='jet'):
112
- num_patches = rollout_attention.shape[0]
113
- grid_size = int(np.sqrt(num_patches))
114
-
115
- attn_grid = rollout_attention.reshape(grid_size, grid_size)
116
-
117
- H, W = image.shape[:2]
118
- attn_map = cv2.resize(attn_grid, (W, H), interpolation=cv2.INTER_CUBIC)
119
- attn_map = attn_map / attn_map.max()
120
-
121
- plt.figure(figsize=(6,6))
122
- plt.imshow(image)
123
- plt.imshow(attn_map, cmap=cmap, alpha=alpha)
124
- plt.axis('off')
125
- buf = io.BytesIO()
126
- plt.savefig(buf, format='png')
127
- buf.seek(0)
128
- plot_img = Image.open(buf).convert('RGB')
129
- plt.clf()
130
-
131
- return plot_img
132
-
133
- def time_series_attention_sum(attentions, context_length, start_layer=0, end_layer=12):
134
- import math
135
- seq_len = attentions.shape[-1]
136
- result = np.zeros(seq_len)
137
- for attn in attentions[start_layer:end_layer]:
138
- attn_heads = attn.mean(0).squeeze()
139
- result += attn_heads
140
- att_len = math.ceil(context_length/16)
141
- return result[-att_len:]
142
-
143
- def plot_time_series_heatmap(context, attention, time_steps):
144
- plt.figure(figsize=(8, 4))
145
- plt.plot(context, color="black", linewidth=2)
146
- attention = attention/attention.max()
147
- cmap = plt.get_cmap("coolwarm")
148
- for i, v in enumerate(attention):
149
- start = i * 16
150
- end = min((i + 1) * 16, time_steps-1)
151
- color = cmap(v)[:-1] + (v,)
152
- plt.axvspan(start, end, color=color)
153
-
154
- buf = io.BytesIO()
155
- plt.savefig(buf, format='png')
156
- buf.seek(0)
157
- plot_img = Image.open(buf).convert('RGB')
158
- plt.clf()
159
-
160
- return plot_img
161
-
162
- def predict(dataset, text, example_index, file, vision_encoder, text_encoder, tsfm):
163
- if (dataset is None or example_index is None) or (example_index == -1 and file is None):
164
- return (
165
- gr.Markdown(
166
- value=f"Please Select Example or Provide CSV File.",
167
- visible=True
168
- ),
169
- None
170
- )
171
- elif (vision_encoder is None or text_encoder is None or tsfm is None):
172
- return (
173
- gr.Markdown(
174
- value=f"Please Select Pretrained Model For UniCast.",
175
- visible=True
176
- ),
177
- None
178
- )
179
- else:
180
- pass
181
- if example_index == -1:
182
- df = pd.read_csv(file.name)
183
- df = df.iloc[:context_length[dataset]]
184
- else:
185
- df = inputs[dataset][example_index]
186
- time_series = np.array(df["Value"])
187
- mean = np.mean(time_series)
188
- std = np.std(time_series)
189
- time_series_normalized = (time_series-mean)/std
190
-
191
- text = None if text == '' else text
192
-
193
- unicast_model = f"{vision_encoder.lower()}{text_encoder.lower()}{tsfm.lower()}"
194
-
195
- url = f"https://adnlp-unicast-{unicast_model}.hf.space/predict"
196
- headers = {"Authorization": f"Bearer {hf_token[unicast_model]}"}
197
- payload = {
198
- "dataset": dataset,
199
- "context": time_series_normalized.tolist(),
200
- "text": text
201
- }
202
- res = requests.post(url, headers=headers, json=payload)
203
- res_json = res.json()
204
-
205
- # Forecast Plot
206
- prediction = np.array(res_json['prediction'])
207
- cl = context_length[dataset]
208
- prediction = prediction[:cl]
209
- prediction = prediction*std+mean
210
-
211
- input_dates_series = pd.to_datetime(df["Timestamp"])
212
- time_diff = input_dates_series.diff().mode()[0]
213
- start_time = input_dates_series.iloc[-1] + time_diff
214
- forecast_dates_series = pd.date_range(start=start_time, periods=len(input_dates_series), freq=time_diff)
215
-
216
- plt.close()
217
- with plt.style.context("seaborn-v0_8"):
218
- fig, ax = plt.subplots(figsize=(10,4))
219
- ax.plot(input_dates_series, time_series, color="black", alpha=0.7, linewidth=3, label='Input')
220
- ax.plot(forecast_dates_series, prediction, color='C2', alpha=0.7, linewidth=3, label='Forecast')
221
- if example_index == -1: # Custom Input
222
- true = df["Ground Truth"]
223
- else:
224
- true = targets[dataset][example_index].iloc[:, -1]
225
- if len(true) == context_length[dataset]:
226
- ax.plot(forecast_dates_series, true, color='C0', alpha=0.7, linewidth=3, label='Ground Truth')
227
- ax.legend()
228
-
229
- # Vision Heatmap
230
- plt.figure(figsize=(384/100, 384/100), dpi=100)
231
- plt.plot(time_series_normalized, color="black", linestyle="-", linewidth=1, marker="*", markersize=1)
232
- plt.xticks([])
233
- plt.yticks([])
234
- plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0)
235
- plt.margins(0,0)
236
-
237
- buf = io.BytesIO()
238
- plt.savefig(buf, format='png')
239
- buf.seek(0)
240
- context_image = np.array(Image.open(buf).convert('RGB'))
241
-
242
- vision_attentions = np.array(res_json['vision_attentions'])
243
- vision_heatmap_gallery_items = []
244
- for i in range(0, 12, 3):
245
- vis_attn = vision_attention_rollout(vision_attentions, i, i+3)
246
- vision_heatmap = plot_vision_heatmap(context_image, vis_attn)
247
- vision_heatmap_gallery_items.append((vision_heatmap, f"Heatmap from Layer{i}:{i+3}"))
248
-
249
- # Time Series Heatmap
250
- if tsfm == "Chronos":
251
- time_series_attentions = np.array(res_json['time_series_attentions'])
252
- time_series_heatmap_gallery_items = []
253
- for i in range(0, 12, 3):
254
- ts_attn = time_series_attention_sum(time_series_attentions, cl, i, i+3)
255
- time_series_heatmap = plot_time_series_heatmap(time_series, ts_attn, cl)
256
- time_series_heatmap_gallery_items.append((time_series_heatmap, f"Heatmap from Layer{i}:{i+3}"))
257
- else:
258
- time_series_heatmap_gallery_items = None
259
-
260
- return (
261
- gr.Markdown(visible=False),
262
- fig,
263
- gr.Gallery(vision_heatmap_gallery_items, interactive=False, height="350px", object_fit="contain", visible=True),
264
- gr.Gallery(time_series_heatmap_gallery_items, interactive=False, height="350px", object_fit="contain", visible=True if time_series_heatmap_gallery_items else False)
265
- )
266
-
267
- def add_example_gallery(dataset, gallery, example_index, file):
268
- if example_index == -1 and file:
269
- df = pd.read_csv(file.name)
270
- custom_input = df[["Timestamp", "Value"]]
271
- custom_target = df[["Timestamp", "Ground Truth"]]
272
-
273
-
274
- plt.style.use("seaborn-v0_8")
275
- ax = custom_input.plot(x="Timestamp", color="black", linewidth=3, legend=False, x_compat=True)
276
- ax.set_xlabel("")
277
- # ax.xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m-%d %H:%M"))
278
- buf = io.BytesIO()
279
- plt.savefig(buf, format='png')
280
- buf.seek(0)
281
- plot_img = Image.open(buf).convert('RGB')
282
- plt.clf()
283
- gallery.insert(-1, (plot_img, f"Custom {len(gallery)-3}"))
284
-
285
- inputs[dataset].append(custom_input)
286
- targets[dataset].append(custom_target)
287
- return gallery
288
-
289
- with gr.Blocks() as demo:
290
-
291
- gr.HTML("""
292
- <style>
293
- #logo {
294
- display: flex;
295
- justify-content: flex-start;
296
- }
297
- .gallery-container .grid-container {
298
- display: flex !important;
299
- }
300
- </style>
301
- """)
302
- gr.Image(
303
- value="logo.png",
304
- show_label=False,
305
- show_download_button=False,
306
- show_fullscreen_button=False,
307
- show_share_button=False,
308
- interactive=False,
309
- height=128,
310
- container=False,
311
- elem_id="logo"
312
- )
313
- with gr.Row():
314
- with gr.Column(scale=2):
315
- dataset_dropdown = gr.Dropdown(["NN5 Daily", "Australian Electricity"], value=None, label="Datasets", interactive=True)
316
-
317
- dataset_description_textbox = gr.Textbox(label="Dataset Description", interactive=False)
318
-
319
- example_gallery = gr.Gallery(
320
- None,
321
- interactive=False
322
- )
323
- example_index = gr.State(value=None)
324
- example_gallery.select(selected_example, inputs=example_gallery, outputs=example_index)
325
-
326
- guide_text_markdown = gr.Markdown(visible=False)
327
- sample_csv_file = gr.File(visible=False)
328
-
329
- time_series_file = gr.File(value=None, visible=False)
330
- time_series_dataframe = gr.Dataframe(visible=False)
331
-
332
- dataset_dropdown.change(selected_dataset, inputs=dataset_dropdown, outputs=[example_gallery, dataset_description_textbox])
333
- dataset_dropdown.change(update_guide_markdown, inputs=[dataset_dropdown, example_index], outputs=[guide_text_markdown, sample_csv_file])
334
- dataset_dropdown.change(update_time_series_dataframe, inputs=[dataset_dropdown, example_index], outputs=[time_series_file, time_series_dataframe])
335
- example_index.change(update_guide_markdown, inputs=[dataset_dropdown, example_index], outputs=[guide_text_markdown, sample_csv_file])
336
- example_index.change(update_time_series_dataframe, inputs=[dataset_dropdown, example_index], outputs=[time_series_file, time_series_dataframe])
337
-
338
- time_series_file.change(load_csv, inputs=[example_index, time_series_file], outputs=time_series_dataframe)
339
- with gr.Column(scale=1):
340
- vision_encoder_radio = gr.Radio(["CLIP", "BLIP"], label="Vision Encoder")
341
- text_encoder_radio = gr.Radio(["Qwen", "LLaMA"], label="Text Encoder")
342
- tsfm_radio = gr.Radio(["Timer", "Chronos"], label="Time Series Foundation Model")
343
- warning_markdown = gr.Markdown(visible=False)
344
- btn = gr.Button("Run")
345
- with gr.Column(scale=2):
346
- forecast_plot = gr.Plot(label="Forecast", format="png")
347
- vision_heatmap_gallery = gr.Gallery(visible=False)
348
- time_series_heatmap_gallery = gr.Gallery(visible=False)
349
-
350
- btn.click(predict, inputs=[dataset_dropdown, dataset_description_textbox, example_index, time_series_file, vision_encoder_radio, text_encoder_radio, tsfm_radio], outputs=[warning_markdown, forecast_plot, vision_heatmap_gallery, time_series_heatmap_gallery])
351
- btn.click(add_example_gallery, inputs=[dataset_dropdown, example_gallery, example_index, time_series_file], outputs=[example_gallery])
352
-
353
- if __name__ == "__main__":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
354
  demo.launch(ssr_mode=False)
 
1
+ import os
2
+ import gradio as gr
3
+ import numpy as np
4
+ import pandas as pd
5
+ import matplotlib.pyplot as plt
6
+ import io
7
+ from PIL import Image
8
+ import pickle
9
+ import requests
10
+ import cv2
11
+
12
+ hf_token = {
13
+ "multicastcustom": os.environ["HF_MulTiCast_Token"],
14
+ "clipqwentimer": os.environ["HF_CLIPQwenTimer_Token"],
15
+ "clipllamatimer": os.environ["HF_CLIPLLaMATimer_Token"],
16
+ "blipqwentimer": os.environ["HF_BLIPQwenTimer_Token"],
17
+ "blipllamatimer": os.environ["HF_BLIPLLaMATimer_Token"],
18
+ "clipqwenchronos": os.environ["HF_CLIPQwenChronos_Token"],
19
+ "clipllamachronos": os.environ["HF_CLIPLLaMAChronos_Token"],
20
+ "blipqwenchronos": os.environ["HF_BLIPQwenChronos_Token"],
21
+ "blipllamachronos": os.environ["HF_BLIPLLaMAChronos_Token"]
22
+ }
23
+
24
+ with open('example/inputs.pkl', 'rb') as f:
25
+ inputs = pickle.load(f)
26
+
27
+ with open('example/targets.pkl', 'rb') as f:
28
+ targets = pickle.load(f)
29
+
30
+ descriptions = {
31
+ "NN5 Daily": "Daily cash withdrawal volumes from automated teller machines (ATMs) in the United Kingdom, originally used in the NN5 forecasting competition.",
32
+ "Australian Electricity": "Half-hourly electricity demand data across five Australian states.",
33
+ "CIF 2016": "Monthly banking time series used in the CIF 2016 forecasting challenge, reflecting customer financial behaviours.",
34
+ "Tourism Monthly": "Monthly tourism-related time series used in the Kaggle Tourism forecasting competition, covering various regions and visitor types.",
35
+ "Custom": "Custom Dataset"
36
+ }
37
+
38
+ context_length = {
39
+ "NN5 Daily": 56,
40
+ "Australian Electricity": 48,
41
+ "CIF 2016": 12,
42
+ "Tourism Monthly": 24
43
+ }
44
+
45
+ def selected_dataset(dataset):
46
+ if dataset == "Custom":
47
+ gallery_items = []
48
+ else:
49
+ gallery_items = [(Image.open(f'example/img/{dataset.replace(" ", "_")}/{i}.png').convert('RGB'), str(i+1)) for i in range(3)]
50
+
51
+ gallery_items.append((Image.open('example/img/custom.png').convert('RGB'), 'Custom Input'))
52
+
53
+ return gr.Gallery(gallery_items, interactive=False, height="350px", object_fit="contain", preview=True), gr.Textbox(value=descriptions[dataset], label="Dataset Description", interactive=False)
54
+
55
+ def selected_example(gallery, evt: gr.SelectData):
56
+ if evt.index == len(gallery) -1:
57
+ return -1
58
+ else:
59
+ return evt.index
60
+
61
+ def update_guide_markdown(dataset, example_index):
62
+ if example_index is None:
63
+ return gr.Markdown(visible=False), gr.File(visible=False)
64
+
65
+ elif dataset == "Custom":
66
+ return gr.Markdown(visible=False), gr.File(visible=False)
67
+
68
+ elif example_index == -1: # Custom Input
69
+ return (
70
+ gr.Markdown(
71
+ value=f"To use custom input, please use the sample csv file below. Do not change the name of columns. Only the first {context_length[dataset]} values will be used as input time series.",
72
+ visible=True
73
+ ),
74
+ gr.File(value="example/sample.csv", label="Sample CSV File", visible=True)
75
+ )
76
+ else:
77
+ df = inputs[dataset][example_index]
78
+ min = df.min()
79
+ max = df.max()
80
+ min_timestamp = pd.Series(min["Timestamp"]).to_string(index=False)
81
+ max_timestamp = pd.Series(max["Timestamp"]).to_string(index=False)
82
+ min_value = min["Value"]
83
+ max_value = max["Value"]
84
+ return (
85
+ gr.Markdown(
86
+ value=f"This time series contains values from {min_timestamp} to {max_timestamp}, with a minimum value of {min_value:.4f} and a maximum value of {max_value:.4f}.",
87
+ visible=True
88
+ ),
89
+ gr.File(visible=False)
90
+ )
91
+
92
+ def update_time_series_dataframe(dataset, example_index):
93
+ if example_index is None:
94
+ return None, None
95
+ elif example_index == -1: # Custom Input
96
+ return gr.File(label="Time Series CSV File", file_types=[".csv"], visible=True), gr.Dataframe(value=None, visible=False)
97
+ elif dataset == "Custom":
98
+ return None, None
99
+ else:
100
+ df = inputs[dataset][example_index]
101
+ return gr.File(value=None, visible=False), gr.Dataframe(value=df, label="Time Series Input", interactive=False, visible=True)
102
+
103
+ def load_csv(example_index, file):
104
+ if example_index == -1:
105
+ if file is not None:
106
+ return gr.Dataframe(value=pd.read_csv(file.name), visible=True)
107
+ else:
108
+ return gr.Dataframe(value=None, visible=False)
109
+ else:
110
+ return gr.skip()
111
+
112
+ def vision_attention_rollout(attentions, start_layer=0, end_layer=12):
113
+ seq_len = attentions.shape[-1]
114
+ result = np.eye(seq_len)
115
+
116
+ for attn in attentions[start_layer:end_layer]:
117
+ attn_heads = attn.mean(axis=0)
118
+ attn_aug = attn_heads + np.eye(seq_len)
119
+ attn_aug = attn_aug / attn_aug.sum(axis=-1, keepdims=True)
120
+ result = attn_aug @ result
121
+
122
+ return result[0, -49:]
123
+
124
+ def plot_vision_heatmap(image, rollout_attention, alpha=0.5, cmap='jet'):
125
+ num_patches = rollout_attention.shape[0]
126
+ grid_size = int(np.sqrt(num_patches))
127
+
128
+ attn_grid = rollout_attention.reshape(grid_size, grid_size)
129
+
130
+ H, W = image.shape[:2]
131
+ attn_map = cv2.resize(attn_grid, (W, H), interpolation=cv2.INTER_CUBIC)
132
+ attn_map = attn_map / attn_map.max()
133
+
134
+ plt.figure(figsize=(6,6))
135
+ plt.imshow(image)
136
+ plt.imshow(attn_map, cmap=cmap, alpha=alpha)
137
+ plt.axis('off')
138
+ buf = io.BytesIO()
139
+ plt.savefig(buf, format='png')
140
+ buf.seek(0)
141
+ plot_img = Image.open(buf).convert('RGB')
142
+ plt.clf()
143
+
144
+ return plot_img
145
+
146
+ def time_series_attention_sum(attentions, context_length, start_layer=0, end_layer=12):
147
+ import math
148
+ seq_len = attentions.shape[-1]
149
+ result = np.zeros(seq_len)
150
+ for attn in attentions[start_layer:end_layer]:
151
+ attn_heads = attn.mean(0).squeeze()
152
+ result += attn_heads
153
+ att_len = math.ceil(context_length/16)
154
+ return result[-att_len:]
155
+
156
+ def plot_time_series_heatmap(context, attention, time_steps):
157
+ plt.figure(figsize=(8, 4))
158
+ plt.plot(context, color="black", linewidth=2)
159
+ attention = attention/attention.max()
160
+ cmap = plt.get_cmap("coolwarm")
161
+ for i, v in enumerate(attention):
162
+ start = i * 16
163
+ end = min((i + 1) * 16, time_steps-1)
164
+ color = cmap(v)[:-1] + (v,)
165
+ plt.axvspan(start, end, color=color)
166
+
167
+ buf = io.BytesIO()
168
+ plt.savefig(buf, format='png')
169
+ buf.seek(0)
170
+ plot_img = Image.open(buf).convert('RGB')
171
+ plt.clf()
172
+
173
+ return plot_img
174
+
175
+ def predict(dataset, text, example_index, file, vision_encoder, text_encoder, tsfm, model_id):
176
+
177
+ if tsfm == "Custom" and model_id == "":
178
+ return (
179
+ gr.Markdown(
180
+ value=f"Please enter the hugging face model repo id.",
181
+ visible=True
182
+ ),
183
+ None,
184
+ None,
185
+ None,
186
+ None
187
+ )
188
+
189
+ if (dataset is None or example_index is None) or (example_index == -1 and file is None):
190
+ return (
191
+ gr.Markdown(
192
+ value=f"Please Select Example or Provide CSV File.",
193
+ visible=True
194
+ ),
195
+ None,
196
+ None,
197
+ None,
198
+ None
199
+ )
200
+ elif (vision_encoder is None or text_encoder is None or tsfm is None):
201
+ return (
202
+ gr.Markdown(
203
+ value=f"Please Select Pretrained Model For UniCast.",
204
+ visible=True
205
+ ),
206
+ None,
207
+ None,
208
+ None,
209
+ None
210
+ )
211
+ else:
212
+ pass
213
+ if example_index == -1:
214
+ df = pd.read_csv(file.name)
215
+ df = df.iloc[:context_length[dataset]]
216
+ else:
217
+ df = inputs[dataset][example_index]
218
+ time_series = np.array(df["Value"])
219
+ mean = np.mean(time_series)
220
+ std = np.std(time_series)
221
+ time_series_normalized = (time_series-mean)/std
222
+
223
+ text = None if text == '' else text
224
+
225
+ unicast_model = f"{vision_encoder.lower()}{text_encoder.lower()}{tsfm.lower()}"
226
+
227
+ if tsfm == "Custom":
228
+ url = f"https://adnlp-multicast-custom.hf.space/predict"
229
+ headers = {"Authorization": f"Bearer {hf_token["multicastcustom"]}"}
230
+ payload = {
231
+ "repo_id": model_id,
232
+ "dataset": dataset,
233
+ "context": time_series_normalized.tolist(),
234
+ "text": text
235
+ }
236
+ else:
237
+ url = f"https://adnlp-unicast-{unicast_model}.hf.space/predict"
238
+ headers = {"Authorization": f"Bearer {hf_token[unicast_model]}"}
239
+ payload = {
240
+ "dataset": dataset,
241
+ "context": time_series_normalized.tolist(),
242
+ "text": text
243
+ }
244
+
245
+ res = requests.post(url, headers=headers, json=payload)
246
+ res_json = res.json()
247
+
248
+ # Forecast Plot
249
+ prediction = np.array(res_json['prediction'])
250
+ cl = context_length[dataset]
251
+ prediction = prediction[:cl]
252
+ prediction = prediction*std+mean
253
+
254
+ input_dates_series = pd.to_datetime(df["Timestamp"])
255
+ time_diff = input_dates_series.diff().mode()[0]
256
+ start_time = input_dates_series.iloc[-1] + time_diff
257
+ forecast_dates_series = pd.date_range(start=start_time, periods=len(input_dates_series), freq=time_diff)
258
+
259
+ plt.close()
260
+ with plt.style.context("seaborn-v0_8"):
261
+ fig, ax = plt.subplots(figsize=(10,4))
262
+ ax.plot(input_dates_series, time_series, color="black", alpha=0.7, linewidth=3, label='Input')
263
+ ax.plot(forecast_dates_series, prediction, color='C2', alpha=0.7, linewidth=3, label='Forecast')
264
+ if example_index == -1: # Custom Input
265
+ true = df["Ground Truth"]
266
+ else:
267
+ true = targets[dataset][example_index].iloc[:, -1]
268
+ if len(true) == context_length[dataset]:
269
+ ax.plot(forecast_dates_series, true, color='C0', alpha=0.7, linewidth=3, label='Ground Truth')
270
+ ax.legend()
271
+
272
+ # Vision Heatmap
273
+ plt.figure(figsize=(384/100, 384/100), dpi=100)
274
+ plt.plot(time_series_normalized, color="black", linestyle="-", linewidth=1, marker="*", markersize=1)
275
+ plt.xticks([])
276
+ plt.yticks([])
277
+ plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0)
278
+ plt.margins(0,0)
279
+
280
+ buf = io.BytesIO()
281
+ plt.savefig(buf, format='png')
282
+ buf.seek(0)
283
+ context_image = np.array(Image.open(buf).convert('RGB'))
284
+
285
+ vision_attentions = np.array(res_json['vision_attentions'])
286
+ vision_heatmap_gallery_items = []
287
+ for i in range(0, 12, 3):
288
+ vis_attn = vision_attention_rollout(vision_attentions, i, i+3)
289
+ vision_heatmap = plot_vision_heatmap(context_image, vis_attn)
290
+ vision_heatmap_gallery_items.append((vision_heatmap, f"Heatmap from Layer{i}:{i+3}"))
291
+
292
+ # Time Series Heatmap
293
+ if tsfm == "Chronos":
294
+ time_series_attentions = np.array(res_json['time_series_attentions'])
295
+ time_series_heatmap_gallery_items = []
296
+ for i in range(0, 12, 3):
297
+ ts_attn = time_series_attention_sum(time_series_attentions, cl, i, i+3)
298
+ time_series_heatmap = plot_time_series_heatmap(time_series, ts_attn, cl)
299
+ time_series_heatmap_gallery_items.append((time_series_heatmap, f"Heatmap from Layer{i}:{i+3}"))
300
+ else:
301
+ time_series_heatmap_gallery_items = None
302
+
303
+ return (
304
+ gr.Markdown(visible=False),
305
+ fig,
306
+ gr.Markdown("# Attention Map", visible=True),
307
+ gr.Gallery(vision_heatmap_gallery_items, interactive=False, height="350px", object_fit="contain", visible=True),
308
+ gr.Gallery(time_series_heatmap_gallery_items, interactive=False, height="350px", object_fit="contain", visible=True if time_series_heatmap_gallery_items else False)
309
+ )
310
+
311
+ def add_example_gallery(dataset, gallery, example_index, file):
312
+ if example_index == -1 and file:
313
+ df = pd.read_csv(file.name)
314
+ custom_input = df[["Timestamp", "Value"]]
315
+ custom_target = df[["Timestamp", "Ground Truth"]]
316
+
317
+
318
+ plt.style.use("seaborn-v0_8")
319
+ ax = custom_input.plot(x="Timestamp", color="black", linewidth=3, legend=False, x_compat=True)
320
+ ax.set_xlabel("")
321
+ # ax.xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m-%d %H:%M"))
322
+ buf = io.BytesIO()
323
+ plt.savefig(buf, format='png')
324
+ buf.seek(0)
325
+ plot_img = Image.open(buf).convert('RGB')
326
+ plt.clf()
327
+ gallery.insert(-1, (plot_img, f"Custom {len(gallery)-3}"))
328
+
329
+ inputs[dataset].append(custom_input)
330
+ targets[dataset].append(custom_target)
331
+ return gallery
332
+
333
+ def on_model_selection(selected):
334
+ return gr.update(visible=selected=="Custom")
335
+
336
+ custom_css = """
337
+ .two-col { display:flex; align-items:flex-end; gap: 16px; }
338
+ .right-col { display:flex; flex-direction:column; } /* optional */
339
+ .push-down { margin-top:auto; } /* optional */
340
+ .footer-fixed{
341
+ position: fixed; left:0; right:0; bottom:0;
342
+ font-size: 16px;
343
+ padding: 10px 16px; border-top: 1px solid var(--border-color);
344
+ background: var(--background-fill-primary); z-index: 1000;
345
+ display: flex; justify-content: flex-end; align-items: center; /* right align */
346
+ }
347
+ """
348
+
349
+ with gr.Blocks(css=custom_css) as demo:
350
+
351
+ gr.HTML("""
352
+ <style>
353
+ #logo {
354
+ display: flex;
355
+ justify-content: flex-start;
356
+ }
357
+ .gallery-container .grid-container {
358
+ display: flex !important;
359
+ }
360
+ </style>
361
+ """)
362
+ gr.Image(
363
+ value="logo.png",
364
+ show_label=False,
365
+ show_download_button=False,
366
+ show_fullscreen_button=False,
367
+ show_share_button=False,
368
+ interactive=False,
369
+ height=128,
370
+ container=False,
371
+ elem_id="logo"
372
+ )
373
+ with gr.Row(elem_classes=["two-col"]):
374
+ with gr.Column(scale=2):
375
+ gr.Markdown("# Choose Dataset")
376
+ dataset_choices = ["NN5 Daily", "Australian Electricity", "Custom"]
377
+ dataset_dropdown = gr.Dropdown(dataset_choices, value=None, label="Datasets", interactive=True)
378
+ dataset_description_textbox = gr.Textbox(label="Dataset Description", interactive=False)
379
+
380
+ gr.Markdown("# Data Selection")
381
+ example_gallery = gr.Gallery(
382
+ None,
383
+ interactive=False
384
+ )
385
+ example_index = gr.State(value=None)
386
+ example_gallery.select(selected_example, inputs=example_gallery, outputs=example_index)
387
+
388
+ guide_text_markdown = gr.Markdown(visible=False)
389
+ sample_csv_file = gr.File(visible=False)
390
+
391
+ gr.Markdown("# Data Viewer")
392
+ time_series_file = gr.File(value=None, visible=False)
393
+ time_series_dataframe = gr.Dataframe(visible=False)
394
+
395
+ dataset_dropdown.change(selected_dataset, inputs=dataset_dropdown, outputs=[example_gallery, dataset_description_textbox])
396
+ dataset_dropdown.change(update_guide_markdown, inputs=[dataset_dropdown, example_index], outputs=[guide_text_markdown, sample_csv_file])
397
+ dataset_dropdown.change(update_time_series_dataframe, inputs=[dataset_dropdown, example_index], outputs=[time_series_file, time_series_dataframe])
398
+ example_index.change(update_guide_markdown, inputs=[dataset_dropdown, example_index], outputs=[guide_text_markdown, sample_csv_file])
399
+ example_index.change(update_time_series_dataframe, inputs=[dataset_dropdown, example_index], outputs=[time_series_file, time_series_dataframe])
400
+
401
+ time_series_file.change(load_csv, inputs=[example_index, time_series_file], outputs=time_series_dataframe)
402
+
403
+ with gr.Column(scale=1):
404
+
405
+ gr.Markdown("# Model Selection")
406
+ model_choices = ["Timer", "Chronos", "Custom"]
407
+ tsfm_radio = gr.Radio(model_choices, label="Time Series Foundation Model")
408
+ md_choices = gr.State(model_choices)
409
+
410
+ model_id_box = gr.Textbox(placeholder="Type and Enter…", label="HF Model ID", interactive=True, visible=False)
411
+ # model_token_box = gr.Textbox(placeholder="Type and Enter…", label="HF Model Token", interactive=True, visible=False)
412
+
413
+ vision_encoder_radio = gr.Radio(["CLIP", "BLIP"], label="Vision Encoder")
414
+ text_encoder_radio = gr.Radio(["Qwen", "LLaMA"], label="Text Encoder")
415
+ warning_markdown = gr.Markdown(visible=False)
416
+ btn = gr.Button("Run")
417
+
418
+ tsfm_radio.change(on_model_selection, [tsfm_radio], model_id_box)
419
+ # tsfm_radio.change(on_model_selection, [tsfm_radio], model_token_box)
420
+
421
+ with gr.Row():
422
+ with gr.Column(scale=2):
423
+ gr.Markdown("# Prediction")
424
+ forecast_plot = gr.Plot(label="Forecast", format="png")
425
+ heatmap_header_html = gr.Markdown("# Attention Map", visible=False)
426
+ vision_heatmap_gallery = gr.Gallery(visible=False)
427
+ time_series_heatmap_gallery = gr.Gallery(visible=False)
428
+
429
+ btn.click(predict, inputs=[dataset_dropdown, dataset_description_textbox, example_index, time_series_file, vision_encoder_radio, text_encoder_radio, tsfm_radio, model_id_box], outputs=[warning_markdown, forecast_plot, heatmap_header_html, vision_heatmap_gallery, time_series_heatmap_gallery])
430
+ btn.click(add_example_gallery, inputs=[dataset_dropdown, example_gallery, example_index, time_series_file], outputs=[example_gallery])
431
+
432
+ gr.HTML("<small>This work is sponsored by Google Research</small>", elem_classes=["footer-fixed"])
433
+
434
+ if __name__ == "__main__":
435
  demo.launch(ssr_mode=False)
logo.png CHANGED

Git LFS Details

  • SHA256: 3e3832413e35a5e972cabea9a4b481dc16b668f1d177a4304c1072e3baf1d379
  • Pointer size: 131 Bytes
  • Size of remote file: 134 kB

Git LFS Details

  • SHA256: 7b0dc88336df2337705808bcfd3b8a1d0f5cfcd02609884c469937cad7c988a0
  • Pointer size: 130 Bytes
  • Size of remote file: 80.7 kB