carmelog commited on
Commit
ea828c3
·
0 Parent(s):

feat: e2s demo with FCN on HF ZeroGPU

Browse files
Files changed (5) hide show
  1. .gitignore +4 -0
  2. Earth2Studio_P1.ipynb +428 -0
  3. README.md +19 -0
  4. app.py +598 -0
  5. requirements.txt +10 -0
.gitignore ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ .venv/
2
+ __pycache__/
3
+ outputs/
4
+ OLD_DOCKER/
Earth2Studio_P1.ipynb ADDED
@@ -0,0 +1,428 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "# Introduction to Earth2Studio \n",
8
+ "\n",
9
+ "In this notebook, we will introduce the Earth2Studio Python package and run a example that will demonstrate how to run a simple inference workflow to generate a basic determinstic forecast using one of the built in models of Earth-2 Inference Studio.\n",
10
+ "\n",
11
+ "\n",
12
+ "#### Contents of the Notebook\n",
13
+ "\n",
14
+ "- [Earth2Studio](#Earth2Studio)\n",
15
+ "- [Simple Deterministic Inference](#Simple-Deterministic-Inference)\n",
16
+ " - [Set Up](#Set-Up)\n",
17
+ " - [Execute the Workflow](#Execute-the-Workflow)\n",
18
+ " - [Post Processing](#Post-Processing)\n",
19
+ "\n",
20
+ "#### Learning Outcomes\n",
21
+ "\n",
22
+ "- Earth2Studio Key Features\n",
23
+ "- How to instantiate a built in prognostic model\n",
24
+ "- Creating a data source and IO object\n",
25
+ "- Running a simple built in workflow\n",
26
+ "- Post-processing results"
27
+ ]
28
+ },
29
+ {
30
+ "cell_type": "markdown",
31
+ "metadata": {},
32
+ "source": [
33
+ "## Earth2Studio\n",
34
+ "\n",
35
+ "Earth2Studio is a Python package built to empower researchers, scientists, and enthusiasts in the fields of weather and climate science with the latest artificial intelligence models/capabilities. With an intuitive design and comprehensive feature set, this package serves as a robust toolkit for exploring this AI revolution in the weather and climate science domain.\n",
36
+ "\n",
37
+ "### Package Design\n",
38
+ "\n",
39
+ "The goal of this package is to enable the use to extrapolate and build beyond what is implemented in it. The design philosophy of Earth2Studio embodies a modular architecture where the inference workflow acts as a flexible adhesive, seamlessly binding together various specialized software components with well-defined interfaces. Each component within the package serves a distinct purpose in typical inference workflows.\n",
40
+ "\n",
41
+ "<div style=\"display: flex; justify-content: center; gap: 10px;\">\n",
42
+ " <figure style=\"text-align: center;\">\n",
43
+ " <img src=\"https://raw.githubusercontent.com/openhackathons-org/End-to-End-AI-for-Science/main/workspace/python/jupyter_notebook/Earth2Studio/images/arch.png\" style=\"width: 100%; height: auto;\">\n",
44
+ " <figcaption>Model architecture overview.</figcaption>\n",
45
+ " </figure>\n",
46
+ "</div>\n",
47
+ "\n",
48
+ "By viewing the inference workflow as a dynamic connector, Earth2Studio facilitates effortless integration of these components, allowing researchers to easily swap out or augment functionalities to suit their specific needs. We recognize that many users will have their own custom workflow needs, thus encourage users to use the provided features as a starting point to build their own.\n",
49
+ "\n",
50
+ "<div style=\"display: flex; justify-content: center; gap: 10px;\">\n",
51
+ " <figure style=\"text-align: center;\">\n",
52
+ " <img src=\"https://raw.githubusercontent.com/openhackathons-org/End-to-End-AI-for-Science/main/workspace/python/jupyter_notebook/Earth2Studio/images/samples.png\" style=\"width: 100%; height: auto;\">\n",
53
+ " </figure>\n",
54
+ "</div>\n",
55
+ "\n",
56
+ "Significant importance is placed on the interface that enables the connection between the components and the workflow. These are simple python protocols that all variants of a particular component must share. This not only enables a consistent API but also the generalization of workflows.\n",
57
+ "\n",
58
+ "### Key Features\n",
59
+ "\n",
60
+ "While Earth2Studio contains a large collection of general utilities, functions and tooling the following six are considered the core. For more information on these features, see the dedicated documentation for each.\n",
61
+ "\n",
62
+ "- **Built-in Workflows**: Multiple built-in inference workflows to accelerate your development and research.\n",
63
+ "- **Prognostic Models**: Support for the latest AI weather forecast models offered under a coherent interface.\n",
64
+ "- **Diagnostic Models**: Diagnostic models for mapping to other quantities of interest.\n",
65
+ "- **Datasources**: Datasources to connect on-prem and remote data stores to inference workflows.\n",
66
+ "- **IO**: Simple, yet powerful IO utilities to export data for post-processing.\n",
67
+ "- **Statistical Operators**: Statistical methods to fuse directly into your inference workflow for more complex uncertainty analysis.\n"
68
+ ]
69
+ },
70
+ {
71
+ "cell_type": "markdown",
72
+ "metadata": {},
73
+ "source": [
74
+ "## Simple Deterministic Inference\n",
75
+ "\n",
76
+ "<div style=\"display: flex; justify-content: center; gap: 10px;\">\n",
77
+ " <figure style=\"text-align: center;\">\n",
78
+ " <img src=\"https://raw.githubusercontent.com/openhackathons-org/End-to-End-AI-for-Science/main/workspace/python/jupyter_notebook/Earth2Studio/images/deterministic.png\" style=\"width: 100%; height: auto;\">\n",
79
+ " </figure>\n",
80
+ "</div>"
81
+ ]
82
+ },
83
+ {
84
+ "cell_type": "markdown",
85
+ "metadata": {},
86
+ "source": [
87
+ "### **Set Up**\n",
88
+ "All workflows inside Earth2Studio require constructed components to be handed to them. In this example, let's take a look at the most basic: `earth2studio.run.deterministic`.\n",
89
+ "\n"
90
+ ]
91
+ },
92
+ {
93
+ "cell_type": "markdown",
94
+ "metadata": {},
95
+ "source": [
96
+ "Let us look at the built in Models, Datasource and IO Backends that are avaialable with Earth2Studio `0.2.0`release. \n",
97
+ "\n",
98
+ "\n",
99
+ "#### Prognostic Model: \n",
100
+ "\n",
101
+ "Prognostic models are a class of models that perform time-integration. Thus are typically used to generate forecast predictions.\n",
102
+ "\n",
103
+ "The list of Prognostic Models available as of `0.2.0` are: \n",
104
+ "- **models.px.DLWP** : Deep learning weather prediction (DLWP) prognostic model.\n",
105
+ "- **models.px.FCN** : FourCastNet global prognostic model.\n",
106
+ "- **models.px.FengWu** : FengWu (operational) weather model consists of single auto-regressive model with a time-step size of 6 hours.\n",
107
+ "- **models.px.FuXi** : FuXi weather model consists of three auto-regressive U-net transfomer models with a time-step size of 6 hours.\n",
108
+ "- **models.px.Pangu24** : Pangu Weather 24 hour model.\n",
109
+ "- **models.px.Pangu6** : Pangu Weather 6 hour model.\n",
110
+ "- **models.px.Pangu3** : Pangu Weather 3 hour model.\n",
111
+ "- **models.px.Persistence** : Persistence model that generates a forecast by applying the identity operator on the initial condition and indexing the lead time by 6 hours.\n",
112
+ "- **models.px.SFNO** : Spherical Fourier Operator Network global prognostic model.\n",
113
+ "\n",
114
+ "\n",
115
+ "#### Data source : \n",
116
+ "\n",
117
+ "Data sources used for downloading, caching and reading different weather / climate data APIs into Xarray data arrays. Used for fetching initial conditions for inference and validation data for scoring.\n",
118
+ "\n",
119
+ "The list of Datasources available as of `0.2.0` are: \n",
120
+ "- **data.ARCO** : Analysis-Ready, Cloud Optimized (ARCO) is a data store of ERA5 re-analysis data currated by Google.\n",
121
+ "- **data.CDS** : The climate data source (CDS) serving ERA5 re-analysis data.\n",
122
+ "- **data.GFS** : The global forecast service (GFS) initial state data source provided on an equirectangular grid.\n",
123
+ "- **data.HRRR** : High-Resolution Rapid Refresh (HRRR) is a North-American weather forecast model with hourly data-assimilation developed by NOAA.\n",
124
+ "- **data.IFS** : The integrated forecast system (IFS) initial state data source provided on an equirectangular grid.\n",
125
+ "- **data.IMERG** : The Integrated Multi-satellitE Retrievals (IMERG) for GPM.\n",
126
+ "- **data.Random(domain_coords)** : A randomly generated normally distributed data.\n",
127
+ "- **data.WB2ERA5** : ERA5 reanalysis data with several derived variables on a 0.25 degree lat-lon grid from 1959 to 2023 (incl) to 6 hour intervals on 13 pressure levels.\n",
128
+ "- **data.WB2ERA5_121x240** : ERA5 reanalysis data with several derived variables down sampled to a 1.5 degree lat-lon grid from 1959 to 2023 (incl) to 6 hour intervals on 13 pressure levels.\n",
129
+ "- **data.WB2ERA5_32x64** : ERA5 reanalysis data with several derived variables down sampled to a 5.625 degree lat-lon grid from 1959 to 2023 (incl) to 6 hour intervals on 13 pressure levels.\n",
130
+ "- **data.WB2Climatology** : Climatology provided by WeatherBench2,\n",
131
+ "- **data.DataArrayFile** : A local xarray dataarray file data source.\n",
132
+ "- **data.DataSetFile** : A local xarray dataset file data source.\n",
133
+ "\n",
134
+ "#### IO Backend: \n",
135
+ "\n",
136
+ "The IO Backends for used for saving the inference results for further post processing.\n",
137
+ "\n",
138
+ "The list of IO Backends available as of `0.2.0` are: \n",
139
+ "- **io.KVBackend** : A key-value (dict) backend.\n",
140
+ "- **io.NetCDF4Backend** : A backend that supports the NetCDF4 format.\n",
141
+ "- **io.XarrayBackend** : An xarray backed IO object.\n",
142
+ "- **io.ZarrBackend** : A backend that supports the zarr format.\n",
143
+ "\n",
144
+ "\n",
145
+ "For this example, we will be using the following:\n",
146
+ "\n",
147
+ "- **Prognostic Model**: Use the built in FourCastNet Model :`earth2studio.models.px.FCN`.\n",
148
+ "- **Datasource**: Pull data from the GFS data api :`earth2studio.data.GFS`.\n",
149
+ "- **IO Backend**: Let's save the outputs into a Zarr store :`earth2studio.io.ZarrBackend`."
150
+ ]
151
+ },
152
+ {
153
+ "cell_type": "code",
154
+ "execution_count": null,
155
+ "metadata": {
156
+ "collapsed": false,
157
+ "jupyter": {
158
+ "outputs_hidden": false
159
+ }
160
+ },
161
+ "outputs": [],
162
+ "source": [
163
+ "import os\n",
164
+ "\n",
165
+ "os.environ['EARTH2STUDIO_CACHE'] = os.getcwd() + \"/outputs/cache\"\n",
166
+ "os.makedirs(\"outputs\", exist_ok=True)\n",
167
+ "from dotenv import load_dotenv\n",
168
+ "load_dotenv()\n",
169
+ "\n",
170
+ "from earth2studio.data import GFS\n",
171
+ "from earth2studio.io import ZarrBackend\n",
172
+ "from earth2studio.models.px import FCN\n",
173
+ "\n",
174
+ "# Prognostic Model - Load the default model package which downloads the check point from NGC\n",
175
+ "package = FCN.load_default_package()\n",
176
+ "model = FCN.load_model(package)\n",
177
+ "\n",
178
+ "# Data Source - Create the data source\n",
179
+ "data = GFS()\n",
180
+ "\n",
181
+ "# IO Backend - Create the IO handler, store in memory\n",
182
+ "io = ZarrBackend()"
183
+ ]
184
+ },
185
+ {
186
+ "cell_type": "markdown",
187
+ "metadata": {},
188
+ "source": [
189
+ "### **Execute the Workflow**\n",
190
+ "\n",
191
+ "With all components initialized, running the workflow is a single line of Python code. \n",
192
+ "Workflow will return the provided IO object back to the user, which can be used to\n",
193
+ "then post process. Let us look at the API for Determinstic inference\n",
194
+ "\n",
195
+ "```python \n",
196
+ "\n",
197
+ "def deterministic(\n",
198
+ " time: list[str] | list[datetime] | list[np.datetime64],\n",
199
+ " nsteps: int,\n",
200
+ " prognostic: PrognosticModel,\n",
201
+ " data: DataSource,\n",
202
+ " io: IOBackend,\n",
203
+ " output_coords: CoordSystem = OrderedDict({}),\n",
204
+ " device: torch.device | None = None,\n",
205
+ ") -> IOBackend:\n",
206
+ " \"\"\"Built in deterministic workflow.\n",
207
+ " This workflow creates a determinstic inference pipeline to produce a forecast\n",
208
+ " prediction using a prognostic model.\n",
209
+ "\n",
210
+ " Parameters\n",
211
+ " ----------\n",
212
+ " time : list[str] | list[datetime] | list[np.datetime64]\n",
213
+ " List of string, datetimes or np.datetime64\n",
214
+ " nsteps : int\n",
215
+ " Number of forecast steps\n",
216
+ " prognostic : PrognosticModel\n",
217
+ " Prognostic model\n",
218
+ " data : DataSource\n",
219
+ " Data source\n",
220
+ " io : IOBackend\n",
221
+ " IO object\n",
222
+ " output_coords: CoordSystem, optional\n",
223
+ " IO output coordinate system override, by default OrderedDict({})\n",
224
+ " device : torch.device, optional\n",
225
+ " Device to run inference on, by default None\n",
226
+ "\n",
227
+ " Returns\n",
228
+ " -------\n",
229
+ " IOBackend\n",
230
+ " Output IO object\n",
231
+ " \"\"\"\n",
232
+ "```\n",
233
+ "\n",
234
+ "For the forecast we will predict for 20 forecast steps which is 5 days."
235
+ ]
236
+ },
237
+ {
238
+ "cell_type": "code",
239
+ "execution_count": null,
240
+ "metadata": {
241
+ "collapsed": false,
242
+ "jupyter": {
243
+ "outputs_hidden": false
244
+ }
245
+ },
246
+ "outputs": [],
247
+ "source": [
248
+ "import earth2studio.run as run\n",
249
+ "\n",
250
+ "nsteps = 20 # Each step has a lead time of 6 hours. \n",
251
+ "io = run.deterministic([\"2024-01-01\"], nsteps, model, data, io)\n",
252
+ "\n",
253
+ "print(io.root.tree())"
254
+ ]
255
+ },
256
+ {
257
+ "cell_type": "markdown",
258
+ "metadata": {},
259
+ "source": [
260
+ "### **Post Processing**\n",
261
+ "The last step is to post process our results. Cartopy is a great library for plotting\n",
262
+ "fields on projections of a sphere. Here we will just plot the temperature at 2 meters\n",
263
+ "(t2m) 1 day into the forecast.\n",
264
+ "\n",
265
+ "Notice that the Zarr IO function has additional APIs to interact with the stored data.\n",
266
+ "\n"
267
+ ]
268
+ },
269
+ {
270
+ "cell_type": "code",
271
+ "execution_count": null,
272
+ "metadata": {
273
+ "collapsed": false,
274
+ "jupyter": {
275
+ "outputs_hidden": false
276
+ }
277
+ },
278
+ "outputs": [],
279
+ "source": [
280
+ "import cartopy.crs as ccrs\n",
281
+ "import matplotlib.pyplot as plt\n",
282
+ "\n",
283
+ "forecast = \"2024-01-01\"\n",
284
+ "variable = \"t2m\"\n",
285
+ "step = 4 # lead time = 4 x 6 = 24 hrs\n",
286
+ "\n",
287
+ "plt.close(\"all\")\n",
288
+ "# Create a Robinson projection\n",
289
+ "projection = ccrs.Robinson()\n",
290
+ "\n",
291
+ "# Create a figure and axes with the specified projection\n",
292
+ "fig, ax = plt.subplots(subplot_kw={\"projection\": projection}, figsize=(10, 6))\n",
293
+ "\n",
294
+ "# Plot the field using pcolormesh\n",
295
+ "im = ax.pcolormesh(\n",
296
+ " io[\"lon\"][:],\n",
297
+ " io[\"lat\"][:],\n",
298
+ " io[variable][0, step],\n",
299
+ " transform=ccrs.PlateCarree(),\n",
300
+ " cmap=\"Spectral_r\",\n",
301
+ ")\n",
302
+ "\n",
303
+ "# Set title\n",
304
+ "ax.set_title(f\"{forecast} - Lead time: {6*step}hrs\")\n",
305
+ "\n",
306
+ "# Add coastlines and gridlines\n",
307
+ "ax.coastlines()\n",
308
+ "ax.gridlines()\n",
309
+ "plt.savefig(\"outputs/01_t2m_prediction.jpg\")"
310
+ ]
311
+ },
312
+ {
313
+ "cell_type": "markdown",
314
+ "metadata": {},
315
+ "source": [
316
+ "Let us now create a simple GIF that would go through all the steps using the below script.\n",
317
+ "\n",
318
+ "Kindly note, the below script would take approximately 10 minutes to show the output. "
319
+ ]
320
+ },
321
+ {
322
+ "cell_type": "code",
323
+ "execution_count": null,
324
+ "metadata": {},
325
+ "outputs": [],
326
+ "source": [
327
+ "import numpy as np\n",
328
+ "import matplotlib.animation as animation\n",
329
+ "from IPython.display import HTML\n",
330
+ "\n",
331
+ "forecast = \"2024-01-01\"\n",
332
+ "variable = \"t2m\"\n",
333
+ "num_timesteps = 20 # Number of time steps to create GIF\n",
334
+ "\n",
335
+ "# Create a Robinson projection\n",
336
+ "projection = ccrs.Robinson()\n",
337
+ "\n",
338
+ "# Create a figure and axes with the specified projection\n",
339
+ "fig, ax = plt.subplots(subplot_kw={\"projection\": projection}, figsize=(10, 6))\n",
340
+ "\n",
341
+ "# Create a function to update the frame\n",
342
+ "def update(frame):\n",
343
+ " ax.clear() # Clear the axis for new plot\n",
344
+ " im = ax.pcolormesh(\n",
345
+ " io[\"lon\"][:],\n",
346
+ " io[\"lat\"][:],\n",
347
+ " io[variable][0, frame],\n",
348
+ " transform=ccrs.PlateCarree(),\n",
349
+ " cmap=\"Spectral_r\",\n",
350
+ " )\n",
351
+ " ax.set_title(f\"{forecast} - Lead time: {6 * frame } hrs\")\n",
352
+ " ax.coastlines()\n",
353
+ " ax.gridlines()\n",
354
+ " return im,\n",
355
+ "\n",
356
+ "# Create an animation\n",
357
+ "ani = animation.FuncAnimation(fig, update, frames=num_timesteps, blit=False)\n",
358
+ "\n",
359
+ "# Save as GIF & Display them - Kindly note, this cell takes around 10 minutes or more to display the output\n",
360
+ "ani.save('outputs/t2m_prediction_animation.gif')\n",
361
+ "print(\"Animation of 20 Timesteps\")\n",
362
+ "HTML(ani.to_html5_video())"
363
+ ]
364
+ },
365
+ {
366
+ "cell_type": "markdown",
367
+ "metadata": {},
368
+ "source": [
369
+ "# Important: Free up GPU Memory!\n",
370
+ "\n",
371
+ "Run the below cell to free up GPU memory after training the model before moving to the next notebook."
372
+ ]
373
+ },
374
+ {
375
+ "cell_type": "code",
376
+ "execution_count": null,
377
+ "metadata": {},
378
+ "outputs": [],
379
+ "source": [
380
+ "import os\n",
381
+ "os._exit(00)"
382
+ ]
383
+ },
384
+ {
385
+ "cell_type": "markdown",
386
+ "metadata": {},
387
+ "source": [
388
+ "Now we had looked at the plot of t2m at the 4th step ( Each step is 6 hours in FCN Model ). In the Next notebook, let us extend this by adding a Diagnostic Model with FCN."
389
+ ]
390
+ },
391
+ {
392
+ "cell_type": "markdown",
393
+ "metadata": {},
394
+ "source": [
395
+ "--- \n",
396
+ "\n",
397
+ "Don't forget to check out additional [Open Hackathons Resources](https://www.openhackathons.org/s/technical-resources) and join our [OpenACC and Hackathons Slack Channel](https://www.openacc.org/community#slack) to share your experience and get more help from the community.\n",
398
+ "\n",
399
+ "---\n",
400
+ "\n",
401
+ "# Licensing\n",
402
+ "\n",
403
+ "Copyright © 2023 OpenACC-Standard.org. This material is released by OpenACC-Standard.org, in collaboration with NVIDIA Corporation, under the Creative Commons Attribution 4.0 International (CC BY 4.0). These materials may include references to hardware and software developed by other entities; all applicable licensing and copyrights apply.\n"
404
+ ]
405
+ }
406
+ ],
407
+ "metadata": {
408
+ "kernelspec": {
409
+ "display_name": "Python 3 (ipykernel)",
410
+ "language": "python",
411
+ "name": "python3"
412
+ },
413
+ "language_info": {
414
+ "codemirror_mode": {
415
+ "name": "ipython",
416
+ "version": 3
417
+ },
418
+ "file_extension": ".py",
419
+ "mimetype": "text/x-python",
420
+ "name": "python",
421
+ "nbconvert_exporter": "python",
422
+ "pygments_lexer": "ipython3",
423
+ "version": "3.10.2"
424
+ }
425
+ },
426
+ "nbformat": 4,
427
+ "nbformat_minor": 4
428
+ }
README.md ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Earth2 Inference Demo
3
+ colorFrom: green
4
+ colorTo: gray
5
+ sdk: gradio
6
+ sdk_version: 6.3.0
7
+ python_version: "3.12.12"
8
+ app_file: app.py
9
+ app_port: 7860
10
+ pinned: false
11
+ tags:
12
+ - physics
13
+ - cfd
14
+ - machine-learning
15
+ - neural-operators
16
+ - fluid-dynamics
17
+ - scientific-computing
18
+ - earth2
19
+ ---
app.py ADDED
@@ -0,0 +1,598 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+ import os
3
+ from typing import Optional, Tuple
4
+
5
+ # Save original asyncio.run BEFORE any imports that might patch it (nest_asyncio)
6
+ _ORIGINAL_ASYNCIO_RUN = asyncio.run
7
+
8
+ # On ZeroGPU (shared A10G), TF32 matmul paths can occasionally trip cuBLAS errors in
9
+ # some einsum-heavy models. Prefer full FP32 math for stability.
10
+ os.environ.setdefault("NVIDIA_TF32_OVERRIDE", "0")
11
+ # ZeroGPU H200-specific workarounds for cuBLAS strided-batch GEMM issues
12
+ # H200 has 70GB VRAM, so memory isn't the issue - focus on CUDA context stability
13
+ # - Force synchronous CUDA execution to avoid race conditions during dynamic GPU allocation
14
+ # - Use deterministic cuBLAS workspace to ensure consistent behavior across GPU allocations
15
+ os.environ.setdefault("CUDA_LAUNCH_BLOCKING", "1")
16
+ os.environ.setdefault("CUBLAS_WORKSPACE_CONFIG", ":16:8")
17
+
18
+ import gradio as gr
19
+ import numpy as np
20
+ from PIL import Image, ImageDraw, ImageFont
21
+
22
+
23
+ # ZeroGPU decorator - only import on Hugging Face Spaces to avoid asyncio conflicts locally
24
+ def _make_spaces_fallback():
25
+ class _SpacesFallback:
26
+ @staticmethod
27
+ def GPU(*args, **kwargs):
28
+ def _decorator(fn):
29
+ return fn
30
+ return _decorator
31
+ return _SpacesFallback()
32
+
33
+ if os.environ.get("SPACE_ID"):
34
+ # Running on Hugging Face Spaces
35
+ try:
36
+ import spaces # type: ignore
37
+ except Exception:
38
+ spaces = _make_spaces_fallback() # type: ignore
39
+ else:
40
+ # Local development - skip spaces import to avoid asyncio conflicts
41
+ spaces = _make_spaces_fallback() # type: ignore
42
+
43
+
44
+ def _ensure_cache_dirs() -> None:
45
+ os.makedirs("outputs", exist_ok=True)
46
+ os.makedirs(os.path.join("outputs", "cache"), exist_ok=True)
47
+ os.environ.setdefault("EARTH2STUDIO_CACHE", os.path.join(os.getcwd(), "outputs", "cache"))
48
+
49
+
50
+ def _normalize_to_uint8(x: np.ndarray) -> np.ndarray:
51
+ x = np.asarray(x, dtype=np.float32)
52
+ finite = np.isfinite(x)
53
+ if not finite.any():
54
+ return np.zeros_like(x, dtype=np.uint8)
55
+ vmin = float(np.nanpercentile(x[finite], 2.0))
56
+ vmax = float(np.nanpercentile(x[finite], 98.0))
57
+ if vmax <= vmin:
58
+ return np.zeros_like(x, dtype=np.uint8)
59
+ y = (x - vmin) / (vmax - vmin)
60
+ y = np.clip(y, 0.0, 1.0)
61
+ return (y * 255.0).astype(np.uint8)
62
+
63
+
64
+ def _apply_simple_colormap(u8: np.ndarray) -> np.ndarray:
65
+ """
66
+ Lightweight colormap without matplotlib:
67
+ map grayscale -> RGB using a simple blue->cyan->yellow->red ramp.
68
+ """
69
+ u = u8.astype(np.float32) / 255.0
70
+ r = np.clip(1.5 * u, 0.0, 1.0)
71
+ g = np.clip(1.5 * (1.0 - np.abs(u - 0.5) * 2.0), 0.0, 1.0)
72
+ b = np.clip(1.5 * (1.0 - u), 0.0, 1.0)
73
+ rgb = np.stack([r, g, b], axis=-1)
74
+ return (rgb * 255.0).astype(np.uint8)
75
+
76
+
77
+ def _plot_latlon_field(lon: np.ndarray, lat: np.ndarray, field2d: np.ndarray, title: str) -> str:
78
+ """
79
+ Save a quick image to outputs/ and return the file path.
80
+ Avoids matplotlib/cartopy to keep system deps minimal on Spaces.
81
+ """
82
+ _ensure_cache_dirs()
83
+
84
+ out_path = os.path.join("outputs", "t2m.png")
85
+ gray = _normalize_to_uint8(field2d)
86
+ rgb = _apply_simple_colormap(gray)
87
+ img = Image.fromarray(rgb, mode="RGB").resize((1024, 512), resample=Image.BILINEAR)
88
+
89
+ draw = ImageDraw.Draw(img)
90
+ text = title
91
+ try:
92
+ font = ImageFont.load_default()
93
+ except Exception:
94
+ font = None
95
+ # simple text background for readability
96
+ pad = 6
97
+ tw, th = draw.textbbox((0, 0), text, font=font)[2:]
98
+ draw.rectangle((0, 0, tw + 2 * pad, th + 2 * pad), fill=(0, 0, 0))
99
+ draw.text((pad, pad), text, fill=(255, 255, 255), font=font)
100
+
101
+ img.save(out_path)
102
+ return out_path
103
+
104
+
105
+ def _gpu_duration(nsteps: int) -> int:
106
+ """
107
+ Calculate GPU duration for inference only.
108
+ """
109
+ nsteps = max(1, int(nsteps))
110
+ # 30s base (model to GPU) + 15s per step
111
+ return int(min(300, 30 + nsteps * 15))
112
+
113
+
114
+ @spaces.GPU(duration=lambda forecast_date, nsteps: _gpu_duration(int(nsteps)))
115
+ def _run_inference(forecast_date: str, nsteps: int):
116
+ """
117
+ GPU-only function: load model, run inference, return extracted data.
118
+
119
+ ZeroGPU uses multiprocessing so we can't pass unpicklable objects (GFS, model).
120
+ Everything must be created inside this function.
121
+ """
122
+ import torch
123
+ import earth2studio.run as run
124
+ from earth2studio.data import GFS
125
+ from earth2studio.io import ZarrBackend
126
+
127
+ _ensure_cache_dirs()
128
+
129
+ # Memory management for ZeroGPU
130
+ torch.backends.cudnn.benchmark = False # More stable on shared GPU
131
+ # Prefer full FP32 math (avoid TF32) for stability on shared A10G
132
+ try:
133
+ torch.set_float32_matmul_precision("highest")
134
+ except Exception:
135
+ pass
136
+ try:
137
+ torch.backends.cuda.matmul.allow_tf32 = False
138
+ except Exception:
139
+ pass
140
+ try:
141
+ torch.backends.cudnn.allow_tf32 = False
142
+ except Exception:
143
+ pass
144
+ # Avoid reduced-precision reductions (guarded for older torch versions)
145
+ try:
146
+ torch.backends.cuda.matmul.allow_fp16_reduced_precision_reduction = False
147
+ except Exception:
148
+ pass
149
+ try:
150
+ torch.backends.cuda.matmul.allow_bf16_reduced_precision_reduction = False
151
+ except Exception:
152
+ pass
153
+ try:
154
+ torch.cuda.set_device(0)
155
+ except Exception:
156
+ pass
157
+ torch.cuda.empty_cache()
158
+
159
+ # Some cuBLAS "INVALID_VALUE" failures originate from non-contiguous einsum operands
160
+ # producing unsupported strides for batched GEMM. Force contiguity at the einsum boundary.
161
+ _orig_einsum = torch.einsum
162
+
163
+ def _einsum_contiguous(equation, *operands):
164
+ ops = []
165
+ for op in operands:
166
+ if isinstance(op, torch.Tensor) and not op.is_contiguous():
167
+ ops.append(op.contiguous())
168
+ else:
169
+ ops.append(op)
170
+ return _orig_einsum(equation, *ops)
171
+
172
+ torch.einsum = _einsum_contiguous # type: ignore[assignment]
173
+
174
+ # Load model inside GPU function (ZeroGPU requirement)
175
+ try:
176
+ from earth2studio.models.px.fcn import FCN
177
+ except Exception:
178
+ from earth2studio.models.px import FCN
179
+
180
+ package = FCN.load_default_package()
181
+ model = FCN.load_model(package)
182
+
183
+ device = torch.device("cuda")
184
+ # Ensure FP32 weights for cuBLAS stability
185
+ try:
186
+ model = model.float()
187
+ except Exception:
188
+ pass
189
+ model = model.to(device)
190
+ model.eval() # Ensure eval mode
191
+
192
+ # Clear memory after model load
193
+ torch.cuda.empty_cache()
194
+
195
+ # CRITICAL: Warmup CUDA/cuBLAS context on ZeroGPU's H200 before complex ops
196
+ # This ensures cuBLAS is fully initialized and strided-batch GEMM handlers are ready
197
+ try:
198
+ with torch.no_grad():
199
+ # Create dummy tensors matching FCN's expected input shape
200
+ # FCN expects (batch, channels, lat, lon) - use minimal batch/size for warmup
201
+ dummy_input = torch.randn(1, 73, 8, 8, device=device, dtype=torch.float32)
202
+ _ = model(dummy_input)
203
+ torch.cuda.synchronize()
204
+ torch.cuda.empty_cache()
205
+ except Exception as warmup_err:
206
+ # If warmup fails, log but continue - the actual inference might still work
207
+ print(f"[Warning] CUDA warmup failed: {warmup_err}")
208
+
209
+ data = GFS()
210
+ io = ZarrBackend()
211
+
212
+ try:
213
+ with torch.no_grad():
214
+ io = run.deterministic([forecast_date], nsteps, model, data, io, device=device)
215
+
216
+ # Extract ALL timesteps to numpy arrays (picklable) before returning
217
+ lon = np.asarray(io["lon"][:])
218
+ lat = np.asarray(io["lat"][:])
219
+ # Return all timesteps: shape (1, nsteps+1, lat, lon)
220
+ all_fields = np.asarray(io["t2m"][:])
221
+
222
+ return lon, lat, all_fields
223
+ finally:
224
+ # Restore einsum in case this worker is reused
225
+ try:
226
+ torch.einsum = _orig_einsum # type: ignore[assignment]
227
+ except Exception:
228
+ pass
229
+ # Free GPU memory aggressively
230
+ try:
231
+ model.to("cpu")
232
+ except Exception:
233
+ pass
234
+ try:
235
+ del model
236
+ del data
237
+ del io
238
+ except Exception:
239
+ pass
240
+ try:
241
+ torch.cuda.empty_cache()
242
+ torch.cuda.synchronize()
243
+ except Exception:
244
+ pass
245
+
246
+
247
+ def run_forecast(forecast_date: str, nsteps: int):
248
+ """
249
+ Run Earth2Studio deterministic inference and return cached results.
250
+ Returns: (forecast_date, nsteps, lon, lat, all_fields, status_msg)
251
+ """
252
+ _ensure_cache_dirs()
253
+
254
+ # Validate inputs
255
+ if not forecast_date:
256
+ return None, None, None, None, None, "ERROR: forecast_date is required (YYYY-MM-DD)."
257
+
258
+ nsteps = int(nsteps)
259
+ if nsteps < 1:
260
+ return None, None, None, None, None, "ERROR: nsteps must be >= 1"
261
+
262
+ # Run inference on GPU (model loaded inside due to ZeroGPU pickling)
263
+ try:
264
+ lon, lat, all_fields = _run_inference(forecast_date, nsteps)
265
+ except Exception as e:
266
+ return None, None, None, None, None, f"ERROR during inference: {type(e).__name__}: {e}"
267
+
268
+ # Return cached data for dynamic plot_step updates
269
+ status = f"SUCCESS: Computed {nsteps} forecast steps ({(nsteps+1)*6} hours total). Use plot_step slider to explore."
270
+ return forecast_date, nsteps, lon, lat, all_fields, status
271
+
272
+
273
+ def update_plot_from_cache(forecast_date, nsteps, lon, lat, all_fields, plot_step):
274
+ """
275
+ Update the displayed plot from cached inference results (no GPU needed).
276
+ """
277
+ if lon is None or lat is None or all_fields is None:
278
+ return None, "No cached results. Click 'Run Inference' first."
279
+
280
+ plot_step = int(plot_step)
281
+ nsteps = int(nsteps)
282
+
283
+ # Validate plot_step
284
+ if plot_step < 0 or plot_step > nsteps:
285
+ return None, f"Invalid plot_step {plot_step} (must be 0-{nsteps})"
286
+
287
+ # Extract the specific timestep
288
+ field = all_fields[0, plot_step]
289
+
290
+ # Plot
291
+ img_path = _plot_latlon_field(
292
+ lon,
293
+ lat,
294
+ field,
295
+ title=f"{forecast_date} - t2m - lead={6 * plot_step}h",
296
+ )
297
+ return img_path, f"Displaying step {plot_step} (lead time: {6 * plot_step} hours)"
298
+
299
+
300
+ def build_ui() -> gr.Blocks:
301
+ with gr.Blocks(title="Earth2Studio FCN (ZeroGPU)") as demo:
302
+ gr.Markdown(
303
+ """
304
+ # Introduction to Earth2Studio
305
+
306
+ Earth2Studio is a Python package built to empower researchers, scientists, and enthusiasts in the fields of weather and climate science with the latest artificial intelligence models and capabilities. With an intuitive design and a comprehensive feature set, it serves as a robust toolkit for exploring modern AI workflows for weather and climate.
307
+
308
+ #### Learning Outcomes
309
+
310
+ - Earth2Studio key features
311
+ - How to instantiate a built-in prognostic model
312
+ - Creating a data source and IO object
313
+ - Running a simple built-in workflow
314
+ - Post-processing results
315
+
316
+ ---
317
+
318
+ ## Package Design
319
+
320
+ The goal of Earth2Studio is to enable users to extrapolate and build beyond what is implemented in it. The design philosophy embodies a **modular architecture** where the inference workflow acts as a flexible adhesive, seamlessly binding together various specialized software components with well-defined interfaces.
321
+
322
+ <div style="display:flex; justify-content:center; gap: 10px;">
323
+ <figure style="text-align:center; max-width: 900px;">
324
+ <img src="https://raw.githubusercontent.com/openhackathons-org/End-to-End-AI-for-Science/main/workspace/python/jupyter_notebook/Earth2Studio/images/arch.png" style="width:100%; height:auto;">
325
+ <figcaption>Model architecture overview.</figcaption>
326
+ </figure>
327
+ </div>
328
+
329
+ By viewing the inference workflow as a dynamic connector, Earth2Studio facilitates effortless integration of these components, allowing researchers to easily swap out or augment functionalities to suit their specific needs.
330
+
331
+ <div style="display:flex; justify-content:center; gap: 10px;">
332
+ <figure style="text-align:center; max-width: 900px;">
333
+ <img src="https://raw.githubusercontent.com/openhackathons-org/End-to-End-AI-for-Science/main/workspace/python/jupyter_notebook/Earth2Studio/images/samples.png" style="width:100%; height:auto;">
334
+ </figure>
335
+ </div>
336
+
337
+ ### Key Features
338
+
339
+ - **Built-in Workflows**: Multiple built-in inference workflows to accelerate your development and research.
340
+ - **Prognostic Models**: Support for the latest AI weather forecast models offered under a coherent interface.
341
+ - **Diagnostic Models**: Diagnostic models for mapping to other quantities of interest.
342
+ - **Datasources**: Datasources to connect on-prem and remote data stores to inference workflows.
343
+ - **IO**: Simple, yet powerful IO utilities to export data for post-processing.
344
+ - **Statistical Operators**: Statistical methods to fuse directly into your inference workflow for more complex uncertainty analysis.
345
+
346
+ ---
347
+
348
+ ## Simple Deterministic Inference
349
+
350
+ <div style="display:flex; justify-content:center; gap: 10px;">
351
+ <figure style="text-align:center; max-width: 900px;">
352
+ <img src="https://raw.githubusercontent.com/openhackathons-org/End-to-End-AI-for-Science/main/workspace/python/jupyter_notebook/Earth2Studio/images/deterministic.png" style="width:100%; height:auto;">
353
+ </figure>
354
+ </div>
355
+
356
+ All workflows inside Earth2Studio require constructed components to be handed to them. In this example, we use `earth2studio.run.deterministic`.
357
+
358
+ ### Prognostic Models
359
+
360
+ Prognostic models are a class of models that perform time-integration. They are typically used to generate forecast predictions. Examples include:
361
+
362
+ | Model | Description |
363
+ |-------|-------------|
364
+ | `models.px.FCN` | FourCastNet - AFNO-based global weather forecasting model (used in this demo) |
365
+ | `models.px.SFNO` | Spherical Fourier Operator Network global prognostic model |
366
+ | `models.px.Pangu24` | Pangu Weather 24 hour model |
367
+ | `models.px.FuXi` | FuXi weather model with three auto-regressive U-net transformer models |
368
+ | `models.px.Aurora` | Aurora transformer-based weather model |
369
+
370
+ ### Data Sources
371
+
372
+ Data sources are used for downloading, caching and reading different weather/climate data APIs into Xarray data arrays. Used for fetching initial conditions for inference and validation data for scoring:
373
+
374
+ | Data Source | Description |
375
+ |-------------|-------------|
376
+ | `data.GFS` | Global Forecast System initial state data source (used in this demo) |
377
+ | `data.ARCO` | Analysis-Ready, Cloud Optimized ERA5 re-analysis data curated by Google |
378
+ | `data.CDS` | Climate Data Store serving ERA5 re-analysis data |
379
+ | `data.HRRR` | High-Resolution Rapid Refresh North-American weather forecast model |
380
+ | `data.IFS` | Integrated Forecast System initial state data source |
381
+
382
+ ### IO Backends
383
+
384
+ IO Backends are used for saving the inference results for further post-processing:
385
+
386
+ | IO Backend | Description |
387
+ |------------|-------------|
388
+ | `io.ZarrBackend` | Zarr format backend (used in this demo) |
389
+ | `io.NetCDF4Backend` | NetCDF4 format backend |
390
+ | `io.XarrayBackend` | Xarray backed IO object |
391
+ | `io.KVBackend` | Key-value (dict) backend |
392
+
393
+ ---
394
+
395
+ ## Code Overview
396
+
397
+ ### Set Up
398
+
399
+ ```python
400
+ import os
401
+ from earth2studio.data import GFS
402
+ from earth2studio.io import ZarrBackend
403
+ from earth2studio.models.px import FCN
404
+
405
+ # Set cache directory
406
+ os.environ['EARTH2STUDIO_CACHE'] = os.getcwd() + "/outputs/cache"
407
+
408
+ # Prognostic Model - Load from NGC (ngc://models/nvidia/modulus/modulus_fcn@v0.2)
409
+ package = FCN.load_default_package()
410
+ model = FCN.load_model(package)
411
+
412
+ # Data Source - Create the data source
413
+ data = GFS()
414
+
415
+ # IO Backend - Create the IO handler
416
+ io = ZarrBackend()
417
+ ```
418
+
419
+ ### Execute the Workflow
420
+
421
+ The `run.deterministic` function signature:
422
+
423
+ ```python
424
+ def deterministic(
425
+ time: list[str] | list[datetime] | list[np.datetime64],
426
+ nsteps: int,
427
+ prognostic: PrognosticModel,
428
+ data: DataSource,
429
+ io: IOBackend,
430
+ output_coords: CoordSystem = OrderedDict({}),
431
+ device: torch.device | None = None,
432
+ ) -> IOBackend:
433
+ \"\"\"Built in deterministic workflow.
434
+
435
+ This workflow creates a deterministic inference pipeline to produce
436
+ a forecast prediction using a prognostic model.
437
+
438
+ Parameters
439
+ ----------
440
+ time : list[str] | list[datetime] | list[np.datetime64]
441
+ List of string, datetimes or np.datetime64
442
+ nsteps : int
443
+ Number of forecast steps
444
+ prognostic : PrognosticModel
445
+ Prognostic model
446
+ data : DataSource
447
+ Data source
448
+ io : IOBackend
449
+ IO object
450
+ output_coords: CoordSystem, optional
451
+ IO output coordinate system override
452
+ device : torch.device, optional
453
+ Device to run inference on
454
+
455
+ Returns
456
+ -------
457
+ IOBackend
458
+ Output IO object
459
+ \"\"\"
460
+ ```
461
+
462
+ Running the forecast (each step is 6 hours for FCN, ~5-10 seconds/step on GPU):
463
+
464
+ ```python
465
+ import earth2studio.run as run
466
+
467
+ nsteps = 4 # 4 steps = 24 hours
468
+ io = run.deterministic(["2024-01-01"], nsteps, model, data, io)
469
+
470
+ print(io.root.tree())
471
+ ```
472
+
473
+ ### Post Processing
474
+
475
+ ```python
476
+ import matplotlib.pyplot as plt
477
+ import cartopy.crs as ccrs
478
+
479
+ forecast = "2024-01-01"
480
+ variable = "t2m"
481
+ step = 1 # lead time = 1 x 6 = 6 hrs
482
+
483
+ projection = ccrs.Robinson()
484
+ fig, ax = plt.subplots(subplot_kw={"projection": projection}, figsize=(10, 6))
485
+
486
+ im = ax.pcolormesh(
487
+ io["lon"][:],
488
+ io["lat"][:],
489
+ io[variable][0, step],
490
+ transform=ccrs.PlateCarree(),
491
+ cmap="Spectral_r",
492
+ )
493
+
494
+ ax.set_title(f"{forecast} - Lead time: {6*step}hrs")
495
+ ax.coastlines()
496
+ ax.gridlines()
497
+ plt.savefig("outputs/t2m_prediction.jpg")
498
+ ```
499
+
500
+ ---
501
+
502
+ ## Interactive Demo
503
+
504
+ This Space runs the deterministic workflow using **FCN** (FourCastNet, checkpoint from [NVIDIA NGC](https://catalog.ngc.nvidia.com/orgs/nvidia/teams/modulus/models/modulus_fcn)) and plots **t2m** (2-meter temperature) at your chosen lead time.
505
+
506
+ FCN uses the AFNO (Adaptive Fourier Neural Operator) architecture and requires ~8GB VRAM.
507
+ """
508
+ )
509
+
510
+ with gr.Row():
511
+ with gr.Column(scale=1):
512
+ forecast_date = gr.Textbox(
513
+ label="Forecast Date",
514
+ value="2024-01-01",
515
+ placeholder="YYYY-MM-DD",
516
+ info="GFS data available from ~2020-present",
517
+ max_lines=1,
518
+ )
519
+ with gr.Column(scale=1):
520
+ nsteps = gr.Slider(
521
+ minimum=1,
522
+ maximum=5,
523
+ step=1,
524
+ value=5,
525
+ label="Number of Forecast Steps",
526
+ info="Each step = 6 hours (5 steps = 30 hours total)",
527
+ )
528
+
529
+ run_btn = gr.Button("Run Inference on ZeroGPU H200", variant="primary")
530
+
531
+ with gr.Row():
532
+ plot_step = gr.Slider(
533
+ minimum=0,
534
+ maximum=5,
535
+ step=1,
536
+ value=2,
537
+ label="Display Timestep",
538
+ info="0=initial conditions, 1-N=forecast steps (updates instantly from cache)",
539
+ )
540
+
541
+ status = gr.Textbox(label="Status", interactive=False)
542
+ out_img = gr.Image(label="2-meter Temperature (t2m)", type="filepath")
543
+
544
+ # Hidden state to cache inference results
545
+ cached_date = gr.State(value=None)
546
+ cached_nsteps = gr.State(value=None)
547
+ cached_lon = gr.State(value=None)
548
+ cached_lat = gr.State(value=None)
549
+ cached_fields = gr.State(value=None)
550
+
551
+ def _sync_plot_step_max(n: int):
552
+ n = int(n)
553
+ # deterministic outputs n+1 time points, so max plot_step = n
554
+ new_max = max(1, n)
555
+ # Default to middle timestep for more interesting view
556
+ new_val = min(n // 2, new_max)
557
+ return gr.Slider(maximum=new_max, value=new_val)
558
+
559
+ # Update plot_step max when nsteps changes
560
+ nsteps.change(fn=_sync_plot_step_max, inputs=[nsteps], outputs=[plot_step])
561
+
562
+ # Run inference and cache results
563
+ run_btn.click(
564
+ fn=run_forecast,
565
+ inputs=[forecast_date, nsteps],
566
+ outputs=[cached_date, cached_nsteps, cached_lon, cached_lat, cached_fields, status],
567
+ ).then(
568
+ fn=update_plot_from_cache,
569
+ inputs=[cached_date, cached_nsteps, cached_lon, cached_lat, cached_fields, plot_step],
570
+ outputs=[out_img, status],
571
+ )
572
+
573
+ # Update plot when plot_step slider changes (instant, uses cache)
574
+ plot_step.change(
575
+ fn=update_plot_from_cache,
576
+ inputs=[cached_date, cached_nsteps, cached_lon, cached_lat, cached_fields, plot_step],
577
+ outputs=[out_img, status],
578
+ )
579
+
580
+ return demo
581
+
582
+
583
+ # ============================================================
584
+ # STARTUP
585
+ # Note: Model is loaded inside @spaces.GPU function because
586
+ # ZeroGPU uses multiprocessing and can't pickle the model.
587
+ # ============================================================
588
+ print("[App] Building Gradio UI...")
589
+
590
+ # Create demo at module level so HF Spaces can find it
591
+ demo = build_ui()
592
+
593
+ if __name__ == "__main__":
594
+ # Fix for local testing: nest_asyncio patches asyncio.run in a way
595
+ # incompatible with uvicorn's loop_factory. Restore original.
596
+ asyncio.run = _ORIGINAL_ASYNCIO_RUN
597
+
598
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ # Pin torch to version required by earth2studio (>=2.5.0) that's also in ZeroGPU supported list
2
+ # earth2studio 0.9.0+ requires torch>=2.5.0, and ZeroGPU docs list 2.5.1 as supported
3
+ torch==2.5.1
4
+ # Other dependencies
5
+ gradio>=4.0.0
6
+ spaces
7
+ earth2studio[fcn]>=0.9.0
8
+ numpy>=2.0.0
9
+ zarr
10
+ pillow