nakas commited on
Commit
a30162c
·
1 Parent(s): ed3bee2
README.md CHANGED
@@ -4,9 +4,11 @@ emoji: 👁
4
  colorFrom: blue
5
  colorTo: blue
6
  sdk: gradio
 
 
7
 
8
 
9
  pinned: false
10
  license: mit
11
 
12
- ---
 
4
  colorFrom: blue
5
  colorTo: blue
6
  sdk: gradio
7
+ app_file: app.py
8
+ python_version: 3.10
9
 
10
 
11
  pinned: false
12
  license: mit
13
 
14
+ ---
apps/canada_radar_gradio.py CHANGED
@@ -87,7 +87,10 @@ def fetch_and_recolor(
87
 
88
  with gr.Blocks(title="Canada Radar Recolor → US NWS") as demo:
89
  gr.Markdown("# 🇨🇦→🇺🇸 Radar Recolor (MSC → NWS)")
90
- gr.Markdown("Fetch MSC composite, quantize to dBZ, and recolor to NWS scale.")
 
 
 
91
  with gr.Row():
92
  with gr.Column(scale=1):
93
  layer = gr.Dropdown(
 
87
 
88
  with gr.Blocks(title="Canada Radar Recolor → US NWS") as demo:
89
  gr.Markdown("# 🇨🇦→🇺🇸 Radar Recolor (MSC → NWS)")
90
+ gr.Markdown(
91
+ "Fetch MSC composite, quantize to dBZ, and recolor to NWS scale.\n"
92
+ "Note: On Hugging Face Spaces, enable ‘Allow internet’ in the Space Settings to fetch WMS/legend."
93
+ )
94
  with gr.Row():
95
  with gr.Column(scale=1):
96
  layer = gr.Dropdown(
pydardraw/sources/msc_geomet.py CHANGED
@@ -9,6 +9,37 @@ from PIL import Image
9
 
10
  WMS_URL = "https://geo.weather.gc.ca/geomet"
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  def fetch_wms_png(
14
  layer: str,
@@ -38,12 +69,9 @@ def fetch_wms_png(
38
  }
39
  if time:
40
  params["time"] = time
41
- r = requests.get(wms_url, params=params, timeout=30)
42
  r.raise_for_status()
43
- img = Image.open(BytesIO(r.content))
44
- if img.mode != "RGBA":
45
- img = img.convert("RGBA")
46
- return img
47
 
48
 
49
  def fetch_legend_png(
@@ -60,10 +88,6 @@ def fetch_legend_png(
60
  "format": "image/png",
61
  "scale": str(scale),
62
  }
63
- r = requests.get(wms_url, params=params, timeout=30)
64
  r.raise_for_status()
65
- img = Image.open(BytesIO(r.content))
66
- if img.mode != "RGBA":
67
- img = img.convert("RGBA")
68
- return img
69
-
 
9
 
10
  WMS_URL = "https://geo.weather.gc.ca/geomet"
11
 
12
+ _DEFAULT_HEADERS = {
13
+ "User-Agent": "pydardraw/0.1 (+https://huggingface.co/spaces)",
14
+ "Accept": "image/png,image/*;q=0.9,*/*;q=0.8",
15
+ }
16
+
17
+
18
+ def _open_response_image(r: requests.Response) -> Image.Image:
19
+ """Open a requests response as an image, raising a clear error if not an image.
20
+
21
+ This improves diagnostics on Hugging Face Spaces when external internet is
22
+ disabled or the WMS returns a ServiceException (XML/HTML).
23
+ """
24
+ ctype = r.headers.get("Content-Type", "")
25
+ data = r.content
26
+ try:
27
+ img = Image.open(BytesIO(data))
28
+ img.load() # validate
29
+ if img.mode != "RGBA":
30
+ img = img.convert("RGBA")
31
+ return img
32
+ except Exception:
33
+ try:
34
+ snippet = r.text[:500]
35
+ except Exception:
36
+ snippet = "<binary>"
37
+ raise ValueError(
38
+ "WMS did not return an image. "
39
+ f"Status={r.status_code}, Content-Type={ctype}, URL={r.url}. "
40
+ f"Body snippet: {snippet!r}"
41
+ )
42
+
43
 
44
  def fetch_wms_png(
45
  layer: str,
 
69
  }
70
  if time:
71
  params["time"] = time
72
+ r = requests.get(wms_url, params=params, timeout=30, headers=_DEFAULT_HEADERS)
73
  r.raise_for_status()
74
+ return _open_response_image(r)
 
 
 
75
 
76
 
77
  def fetch_legend_png(
 
88
  "format": "image/png",
89
  "scale": str(scale),
90
  }
91
+ r = requests.get(wms_url, params=params, timeout=30, headers=_DEFAULT_HEADERS)
92
  r.raise_for_status()
93
+ return _open_response_image(r)