cwadayi commited on
Commit
474e96f
·
verified ·
1 Parent(s): 0e7af90
Files changed (1) hide show
  1. app.py +0 -172
app.py DELETED
@@ -1,172 +0,0 @@
1
-
2
- # app.py
3
- # Gradio app for Hugging Face Spaces: CWA 顯著有感地震報告 (E-A0015-001)
4
- import os
5
- import json
6
- import tempfile
7
- from datetime import datetime
8
- from typing import List, Dict, Any, Tuple
9
-
10
- import gradio as gr
11
- import pandas as pd
12
- import requests
13
-
14
- BASE_URL = "https://opendata.cwa.gov.tw/api/v1/rest/datastore/E-A0015-001"
15
- AREAS = ['宜蘭縣', '花蓮縣', '臺東縣', '澎湖縣', '金門縣', '連江縣', '臺北市', '新北市', '桃園市', '臺中市', '臺南市', '高雄市', '基隆市', '新竹縣', '新竹市', '苗栗縣', '彰化縣', '南投縣', '雲林縣', '嘉義縣', '嘉義市', '屏東縣']
16
-
17
- def validate_iso(dt: str) -> str:
18
- if not dt:
19
- return ""
20
- try:
21
- datetime.strptime(dt, "%Y-%m-%dT%H:%M:%S")
22
- return dt
23
- except ValueError:
24
- raise gr.Error("時間格式需為 yyyy-MM-ddThh:mm:ss")
25
-
26
- def build_params(auth: str, limit: int|None, offset: int|None, fmt: str, areas: List[str], stations: List[str], sort: str|None, timeFrom: str|None, timeTo: str|None):
27
- params = []
28
- if auth:
29
- params.append(("Authorization", auth))
30
- elif os.getenv("CWA_API_KEY"):
31
- params.append(("Authorization", os.getenv("CWA_API_KEY")))
32
- else:
33
- raise gr.Error("缺少授權碼:請在左側輸入 Authorization,或於 Spaces Secrets 設定 CWA_API_KEY。")
34
-
35
- if limit is not None: params.append(("limit", str(limit)))
36
- if offset is not None: params.append(("offset", str(offset)))
37
- if fmt: params.append(("format", fmt))
38
- for a in (areas or []):
39
- params.append(("AreaName", a))
40
- for s in (stations or []):
41
- params.append(("StationName", s))
42
- if sort: params.append(("sort", sort))
43
- if timeFrom: params.append(("timeFrom", timeFrom))
44
- if timeTo: params.append(("timeTo", timeTo))
45
- return params
46
-
47
- def http_get(url: str, params: List[Tuple[str,str]]) -> Dict[str, Any]:
48
- sess = requests.Session()
49
- resp = sess.get(url, params=params, timeout=(5, 20))
50
- resp.raise_for_status()
51
- if "application/json" in resp.headers.get("Content-Type","").lower() or resp.text.strip().startswith("{"):
52
- return resp.json()
53
- else:
54
- return {"raw": resp.text}
55
-
56
- def extract_records(payload: Dict[str, Any]):
57
- recs = payload.get("records")
58
- if isinstance(recs, dict):
59
- for k, v in recs.items():
60
- if isinstance(v, list):
61
- return v
62
- result = payload.get("result")
63
- if isinstance(result, dict) and isinstance(result.get("records"), list):
64
- return result["records"]
65
- for key in ("Earthquake","earthquakes","data","items"):
66
- v = payload.get(key)
67
- if isinstance(v, list):
68
- return v
69
- return []
70
-
71
- def flatten_row(row: Dict[str, Any]) -> Dict[str, Any]:
72
- out = {}
73
- for key in ("EarthquakeNo","ReportImageURI","Web","ReportColor","ReportContent"):
74
- if key in row:
75
- out[key] = row.get(key)
76
- eqi = row.get("EarthquakeInfo")
77
- if isinstance(eqi, dict):
78
- out["OriginTime"] = eqi.get("OriginTime")
79
- out["Depth_km"] = eqi.get("FocalDepth")
80
- mag = eqi.get("EarthquakeMagnitude") or {}
81
- if isinstance(mag, dict):
82
- out["Magnitude"] = mag.get("MagnitudeValue")
83
- out["MagnitudeType"] = mag.get("MagnitudeType")
84
- epic = eqi.get("Epicenter") or {}
85
- if isinstance(epic, dict):
86
- out["Epicenter"] = epic.get("Location")
87
- out["EpicenterLon"] = epic.get("EpicenterLongitude")
88
- out["EpicenterLat"] = epic.get("EpicenterLatitude")
89
- for k in ("OriginTime","originTime","Time"):
90
- if k in row and "OriginTime" not in out:
91
- out["OriginTime"] = row.get(k)
92
- for k in ("Depth","depth","FocalDepth"):
93
- if k in row and "Depth_km" not in out:
94
- out["Depth_km"] = row.get(k)
95
- for k in ("Magnitude","mag"):
96
- if k in row and "Magnitude" not in out:
97
- out["Magnitude"] = row.get(k)
98
- maxint = row.get("Intensity") or row.get("ShakingArea")
99
- if isinstance(maxint, dict):
100
- out["MaxIntensity"] = maxint.get("MaxIntensity")
101
- return out
102
-
103
- def fetch(auth, time_from, time_to, limit, offset, fmt, sel_areas, stations, sort):
104
- time_from = validate_iso(time_from) if time_from else None
105
- time_to = validate_iso(time_to) if time_to else None
106
- params = build_params(auth=auth, limit=limit, offset=offset, fmt=fmt, areas=sel_areas, stations=stations, sort=sort, timeFrom=time_from, timeTo=time_to)
107
- payload = http_get(BASE_URL, params)
108
- records = extract_records(payload)
109
- flat = [flatten_row(r) for r in records]
110
-
111
- # dataframe
112
- df = pd.DataFrame(flat)
113
- # Save files to temp and return handles
114
- tmpdir = tempfile.mkdtemp(prefix="cwa_")
115
- csv_path = os.path.join(tmpdir, "cwa_quake.csv")
116
- json_path = os.path.join(tmpdir, "raw.json")
117
- df.to_csv(csv_path, index=False, encoding="utf-8")
118
- with open(json_path, "w", encoding="utf-8") as f:
119
- json.dump(payload, f, ensure_ascii=False, indent=2)
120
-
121
- # quick summary
122
- total = len(df)
123
- earliest = latest = ""
124
- if total and "OriginTime" in df.columns:
125
- try:
126
- s = df.dropna(subset=["OriginTime"]).copy()
127
- s["OriginTime_dt"] = pd.to_datetime(s["OriginTime"], format="%Y-%m-%dT%H:%M:%S", errors="coerce")
128
- s = s.sort_values("OriginTime_dt")
129
- if len(s):
130
- earliest_row = s.iloc[0]
131
- latest_row = s.iloc[-1]
132
- earliest = f"最早: {earliest_row['OriginTime']} | {earliest_row.get('Epicenter','')} | M{earliest_row.get('Magnitude','')}"
133
- latest = f"最新: {latest_row['OriginTime']} | {latest_row.get('Epicenter','')} | M{latest_row.get('Magnitude','')}"
134
- except Exception:
135
- pass
136
-
137
- summary = f"取得筆數: {total}\n{earliest}\n{latest}"
138
- return df, summary, csv_path, json_path
139
-
140
- with gr.Blocks(title="CWA 顯著有感地震報告 E-A0015-001") as demo:
141
- gr.Markdown("# CWA 顯著有感地震報告 (E-A0015-001)")
142
- gr.Markdown("在左側設定授權與查詢條件。Authorization 可留空並改用環境變數 **CWA_API_KEY**(建議在 Spaces Secrets 設定)。")
143
- with gr.Row():
144
- with gr.Column(scale=1):
145
- auth = gr.Textbox(label="Authorization(可留空改用 CWA_API_KEY)", type="password", placeholder="留空則使用環境變數 CWA_API_KEY")
146
- time_from = gr.Textbox(label="timeFrom yyyy-MM-ddThh:mm:ss", placeholder="例如 2025-08-01T00:00:00")
147
- time_to = gr.Textbox(label="timeTo yyyy-MM-ddThh:mm:ss", placeholder="例如 2025-08-10T23:59:59")
148
- areas = gr.CheckboxGroup(choices=AREAS, label="AreaName(可複選)")
149
- stations = gr.Textbox(label="StationName(以逗號分隔,可留空)", placeholder="例如:台北、花蓮...")
150
- sort = gr.Dropdown(choices=[None, "OriginTime"], value=None, label="sort(預設降冪;選 OriginTime 會升冪)")
151
- with gr.Row():
152
- limit = gr.Number(label="limit(筆數上限)", precision=0)
153
- offset = gr.Number(label="offset(起始偏移)", precision=0, value=0)
154
- fmt = gr.Radio(choices=["JSON","XML"], value="JSON", label="回傳格式")
155
- run_btn = gr.Button("查詢", variant="primary")
156
- with gr.Column(scale=2):
157
- out_df = gr.Dataframe(label="查詢結果(扁平化)", interactive=False, wrap=True, datatype="str")
158
- out_summary = gr.Textbox(label="摘要", interactive=False)
159
- out_csv = gr.File(label="下載 CSV")
160
- out_json = gr.File(label="下載原始 JSON")
161
-
162
- def on_click(auth, time_from, time_to, limit, offset, fmt, areas_sel, stations_txt, sort):
163
- stations_list = []
164
- if stations_txt:
165
- stations_list = [s.strip() for s in stations_txt.split(",") if s.strip()]
166
- df, summary, csv_path, json_path = fetch(auth, time_from, time_to, int(limit) if limit is not None else None, int(offset) if offset is not None else None, fmt, areas_sel, stations_list, sort)
167
- return df, summary, csv_path, json_path
168
-
169
- run_btn.click(on_click, inputs=[auth, time_from, time_to, limit, offset, fmt, areas, stations, sort], outputs=[out_df, out_summary, out_csv, out_json])
170
-
171
- if __name__ == "__main__":
172
- demo.launch()