Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,9 @@
|
|
| 1 |
# app.py
|
| 2 |
import os
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
import uuid
|
| 6 |
import tempfile
|
|
@@ -29,16 +32,14 @@ import matplotlib.cm as cm
|
|
| 29 |
from matplotlib import font_manager as fm
|
| 30 |
|
| 31 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 32 |
-
#
|
| 33 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 34 |
try:
|
| 35 |
-
#
|
| 36 |
-
from fetch_cwa_alarm_list import fetch_cwa_alarm_list # noqa: F401
|
| 37 |
except Exception:
|
| 38 |
CWA_ALARM_API = "https://app-2.cwa.gov.tw/api/v1/earthquake/alarm/list"
|
| 39 |
|
| 40 |
def _parse_cwa_time(s: str):
|
| 41 |
-
"""ๅๅณ(ๅฐ็ฃๆ้, UTC)ใ่ฅ็กๆๅ๏ผ่ฆ็บๅฐ็ฃๆ้(UTC+8)ใ"""
|
| 42 |
if not s:
|
| 43 |
return ("ๆช็ฅ", "ๆช็ฅ")
|
| 44 |
try:
|
|
@@ -54,7 +55,6 @@ except Exception:
|
|
| 54 |
return (s, "ๆช็ฅ")
|
| 55 |
|
| 56 |
def fetch_cwa_alarm_list(limit: int = 5) -> str:
|
| 57 |
-
"""CWA ๅฐ้้ ่ญฆ๏ผๅๆด็๏ผใ"""
|
| 58 |
try:
|
| 59 |
r = requests.get(CWA_ALARM_API, timeout=10)
|
| 60 |
r.raise_for_status()
|
|
@@ -76,9 +76,7 @@ except Exception:
|
|
| 76 |
if "T" in s or s.endswith("Z") or "+" in s:
|
| 77 |
dt = datetime.fromisoformat(s.replace("Z", "+00:00"))
|
| 78 |
else:
|
| 79 |
-
dt = datetime.strptime(s, "%Y-%m-%d %H:%M:%S").replace(
|
| 80 |
-
tzinfo=timezone(timedelta(hours=8))
|
| 81 |
-
)
|
| 82 |
return dt.astimezone(timezone.utc)
|
| 83 |
except Exception:
|
| 84 |
return datetime.min.replace(tzinfo=timezone.utc)
|
|
@@ -93,7 +91,7 @@ except Exception:
|
|
| 93 |
ok = xs.replace(".", "", 1).replace("-", "", 1).isdigit()
|
| 94 |
return float(xs) if ok else None
|
| 95 |
|
| 96 |
-
|
| 97 |
for it in items[:limit]:
|
| 98 |
identifier = it.get("identifier") or it.get("eventId") or it.get("id") or "โ"
|
| 99 |
status = it.get("status") or "โ"
|
|
@@ -102,7 +100,6 @@ except Exception:
|
|
| 102 |
|
| 103 |
mag = _num(it.get("magnitudeValue") or it.get("magnitude") or it.get("ml") or it.get("mw"))
|
| 104 |
mag_str = f"{mag:.1f}" if mag is not None else "โ"
|
| 105 |
-
|
| 106 |
depth = _num(it.get("depth"))
|
| 107 |
depth_str = f"{depth:.0f}" if depth is not None else "โ"
|
| 108 |
|
|
@@ -111,8 +108,7 @@ except Exception:
|
|
| 111 |
lat_str = f"{lat:.2f}" if lat is not None else "โ"
|
| 112 |
lon_str = f"{lon:.2f}" if lon is not None else "โ"
|
| 113 |
|
| 114 |
-
|
| 115 |
-
tw_str, utc_str = _parse_cwa_time(origin)
|
| 116 |
|
| 117 |
areas = it.get("locationDesc") or it.get("areas") or it.get("alertAreas")
|
| 118 |
if isinstance(areas, list):
|
|
@@ -122,43 +118,34 @@ except Exception:
|
|
| 122 |
else:
|
| 123 |
areas_txt = "โ"
|
| 124 |
|
| 125 |
-
|
| 126 |
-
f"
|
| 127 |
f"้็ด/ๆทฑๅบฆ: M{mag_str} / {depth_str} km\n"
|
| 128 |
f"้ไธญ: lat {lat_str}, lon {lon_str}\n"
|
| 129 |
f"ๆ้: {tw_str}๏ผๅฐ็ฃ๏ผ / {utc_str}๏ผUTC๏ผ\n"
|
| 130 |
-
f"้ ่ญฆๅฐๅ: {areas_txt}"
|
|
|
|
| 131 |
)
|
| 132 |
-
|
| 133 |
-
|
| 134 |
if len(items) > limit:
|
| 135 |
-
|
| 136 |
-
return "\n".join(
|
| 137 |
|
| 138 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 139 |
-
#
|
| 140 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 141 |
_LLM = {"loaded": False, "ok": False, "err": None, "model": None, "tokenizer": None, "device": "cpu"}
|
| 142 |
|
| 143 |
def _ensure_llm():
|
| 144 |
-
"""
|
| 145 |
-
ๆถ่ผๅ
ฅ LLMใ้ ่จญไธญๆๆจกๅ ckiplab/gpt2-base-chinese๏ผๅฏ็จ็ฐๅข่ฎๆธ่ฆๅฏซ๏ผ
|
| 146 |
-
- LLM_MODEL (้ ่จญ ckiplab/gpt2-base-chinese)
|
| 147 |
-
- LLM_DEVICE (cuda / cpu)
|
| 148 |
-
ๅๅณ (ok: bool, err: str|None)
|
| 149 |
-
"""
|
| 150 |
if _LLM["loaded"]:
|
| 151 |
return _LLM["ok"], _LLM["err"]
|
| 152 |
-
|
| 153 |
_LLM["loaded"] = True
|
| 154 |
try:
|
| 155 |
-
#
|
| 156 |
-
import
|
| 157 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM # noqa: F401
|
| 158 |
|
| 159 |
device = os.getenv("LLM_DEVICE")
|
| 160 |
if device not in ("cuda", "cpu"):
|
| 161 |
-
# ่ชๅๅตๆธฌ
|
| 162 |
try:
|
| 163 |
import torch as _t
|
| 164 |
device = "cuda" if _t.cuda.is_available() else "cpu"
|
|
@@ -166,10 +153,10 @@ def _ensure_llm():
|
|
| 166 |
except Exception:
|
| 167 |
device = "cpu"
|
| 168 |
|
| 169 |
-
model_name = os.getenv("LLM_MODEL", "ckiplab/gpt2-base-chinese")
|
| 170 |
-
|
| 171 |
-
tok = AutoTokenizer.from_pretrained(model_name)
|
| 172 |
-
mdl = AutoModelForCausalLM.from_pretrained(model_name)
|
| 173 |
try:
|
| 174 |
mdl = mdl.to(device)
|
| 175 |
except Exception:
|
|
@@ -187,9 +174,10 @@ def generate_ai_text(user_prompt: str) -> str:
|
|
| 187 |
ok, err = _ensure_llm()
|
| 188 |
if not ok:
|
| 189 |
return ("๐ค AI ๅฐๆชๅ็จ๏ผ็ผบๅฐไพ่ณดๆๆจกๅๆชไธ่ผใ\n"
|
| 190 |
-
"่ซๅจ requirements.txt
|
| 191 |
f"่ฉณ็ดฐ้ฏ่ชค๏ผ{err}")
|
| 192 |
|
|
|
|
| 193 |
tok = _LLM["tokenizer"]
|
| 194 |
mdl = _LLM["model"]
|
| 195 |
device = _LLM["device"]
|
|
@@ -198,16 +186,13 @@ def generate_ai_text(user_prompt: str) -> str:
|
|
| 198 |
top_k = int(os.getenv("LLM_TOP_K", "50"))
|
| 199 |
temperature = float(os.getenv("LLM_TEMPERATURE", "0.7"))
|
| 200 |
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
"
|
| 204 |
-
"่ฅ่ๅฐ้็ธ้ๅฏๅ ๅ
ฅๆณจๆไบ้
ใ่ฅๅ้ก่ๅฐ้็ก้๏ผๅฐฑไธ่ฌๅ่ฆใ\n\n"
|
| 205 |
-
"ไฝฟ็จ่
๏ผ"
|
| 206 |
)
|
| 207 |
-
prompt =
|
| 208 |
|
| 209 |
try:
|
| 210 |
-
import torch
|
| 211 |
inputs = tok(prompt, return_tensors="pt").to(device)
|
| 212 |
with torch.no_grad():
|
| 213 |
output = mdl.generate(
|
|
@@ -220,19 +205,16 @@ def generate_ai_text(user_prompt: str) -> str:
|
|
| 220 |
pad_token_id=tok.eos_token_id,
|
| 221 |
)
|
| 222 |
text = tok.decode(output[0], skip_special_tokens=True)
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
text = text.split(system_prefix, 1)[-1]
|
| 226 |
-
# GPT-2 ๅฏ่ฝๆๆ prompt ไนๅธถๅ๏ผ็ฐกๅฎ่ฃๅช
|
| 227 |
if user_prompt in text:
|
| 228 |
text = text.split(user_prompt, 1)[-1].strip()
|
| 229 |
-
|
| 230 |
-
return text.strip()[:1200] if text else "๏ผๆฒๆ็ข็ๅ
งๅฎน๏ผ"
|
| 231 |
except Exception as e:
|
| 232 |
return f"AI ็ข็็ผ็้ฏ่ชค๏ผ{e}"
|
| 233 |
|
| 234 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 235 |
-
#
|
| 236 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 237 |
for fp in [
|
| 238 |
"/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",
|
|
@@ -245,15 +227,14 @@ for fp in [
|
|
| 245 |
break
|
| 246 |
|
| 247 |
# โโโโ๏ฟฝ๏ฟฝโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 248 |
-
#
|
| 249 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 250 |
CHANNEL_ACCESS_TOKEN = os.getenv("CHANNEL_ACCESS_TOKEN")
|
| 251 |
CHANNEL_SECRET = os.getenv("CHANNEL_SECRET")
|
| 252 |
|
| 253 |
-
# HF Space ๅ
ฌ็ถฒไฝๅ๏ผSPACEURL > SPACE_ID ๆจ่ซ๏ผ
|
| 254 |
HF_SPACE_URL = os.getenv("SPACEURL")
|
| 255 |
if not HF_SPACE_URL:
|
| 256 |
-
sid = os.getenv("SPACE_ID")
|
| 257 |
if sid and "/" in sid:
|
| 258 |
a, n = sid.split("/", 1)
|
| 259 |
HF_SPACE_URL = f"https://{a.replace('_','-')}-{n.replace('_','-')}.hf.space"
|
|
@@ -268,7 +249,7 @@ configuration = Configuration(access_token=CHANNEL_ACCESS_TOKEN)
|
|
| 268 |
handler = WebhookHandler(CHANNEL_SECRET)
|
| 269 |
|
| 270 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 271 |
-
#
|
| 272 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 273 |
@app.route("/", methods=["GET"])
|
| 274 |
def home():
|
|
@@ -280,83 +261,50 @@ def home():
|
|
| 280 |
|
| 281 |
return f"""
|
| 282 |
<!doctype html>
|
| 283 |
-
<html lang="zh-Hant">
|
| 284 |
-
<
|
| 285 |
-
<meta charset="utf-8" />
|
| 286 |
-
<meta name="viewport" content="width=device-width,initial-scale=1" />
|
| 287 |
<title>ๅฐ้้ ่ญฆ dayichen โ LINE Bot Server</title>
|
| 288 |
<style>
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
</
|
| 319 |
-
<
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
<li><span class="kbd">/help</span>๏ผ้กฏ็คบๆๆๆไปค</li>
|
| 330 |
-
<li><span class="kbd">ๅฐ้</span>/<span class="kbd">quake</span>๏ผๅ
จ็่ฟ 24 ๅฐๆ Mโฅ5.0</li>
|
| 331 |
-
<li><span class="kbd">่บ็ฃๅฐ้</span>/<span class="kbd">ๅฐ็ฃๅฐ้</span>๏ผไปๅนดๅฐ็ฃๅๅๆธ
ๅฎ๏ผๅซๆฅๆๆ้๏ผ</li>
|
| 332 |
-
<li><span class="kbd">่บ็ฃๅฐ้็ซๅ</span>/<span class="kbd">ๅฐ็ฃๅฐ้็ซๅ</span>๏ผๅๅณๅฐๅๅ็</li>
|
| 333 |
-
<li><span class="kbd">ๅฐ้้ ่ญฆ</span>๏ผCWA ๅฐ้้ ่ญฆ๏ผๆๆฐ 5 ็ญ๏ผ</li>
|
| 334 |
-
<li><span class="kbd">ai ไฝ ็ๅ้ก</span>๏ผๅ็จ AI ๅฐ่ฉฑ๏ผไพๅฆ <span class="kbd">ai ไปๆฅๅฐ็ฃๅฐ้้ขจ้ช้่ฆๆณจๆไป้บผ๏ผ</span></li>
|
| 335 |
-
</ul>
|
| 336 |
-
</div>
|
| 337 |
-
<div class="card">
|
| 338 |
-
<h3>๐ ๏ธ Webhook ่้ๆ
ๆช</h3>
|
| 339 |
-
<ul>
|
| 340 |
-
<li>Webhook๏ผ<span class="mono"><a href="{webhook_url}">{webhook_url}</a></span></li>
|
| 341 |
-
<li>้ๆ
ๅ็๏ผ<span class="mono">{static_hint}</span></li>
|
| 342 |
-
<li>ๅฅๅบทๆชขๆฅ๏ผ<span class="mono"><a href="{base}/healthz">{base}/healthz</a></span></li>
|
| 343 |
-
</ul>
|
| 344 |
-
</div>
|
| 345 |
-
<div class="card">
|
| 346 |
-
<h3>โน๏ธ ๅ่จป</h3>
|
| 347 |
-
<ul>
|
| 348 |
-
<li>AI ้่ฆๅฎ่ฃ <span class="mono">transformers / torch</span> ็ญไพ่ณด๏ผๆชๅฎ่ฃๆๆๅๆ็ตฆๅบๆ็คบใ</li>
|
| 349 |
-
<li>ๆจกๅ๏ผ<span class="mono">{os.getenv("LLM_MODEL","ckiplab/gpt2-base-chinese")}</span> ยท ่ฃ็ฝฎ๏ผ<span class="mono">{os.getenv("LLM_DEVICE","auto")}</span></li>
|
| 350 |
-
<li>ๅฐๅไปฅ Matplotlib ็ข็ๆผ <span class="mono">/tmp/static</span>๏ผ็ฑ <span class="mono">/static/</span> ๆไพใ</li>
|
| 351 |
-
</ul>
|
| 352 |
-
</div>
|
| 353 |
-
</div>
|
| 354 |
-
<p class="foot">ยฉ {datetime.now().year} dayichen ยท server: {base}</p>
|
| 355 |
-
</section>
|
| 356 |
-
</div>
|
| 357 |
-
</body>
|
| 358 |
-
</html>
|
| 359 |
-
"""
|
| 360 |
|
| 361 |
@app.route("/healthz")
|
| 362 |
def healthz():
|
|
@@ -367,7 +315,7 @@ def serve_static(filename):
|
|
| 367 |
return send_from_directory(STATIC_DIR, filename)
|
| 368 |
|
| 369 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 370 |
-
#
|
| 371 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 372 |
USGS_API_BASE_URL = "https://earthquake.usgs.gov/fdsnws/event/1/query"
|
| 373 |
|
|
@@ -395,9 +343,7 @@ def fetch_global_last24h_text(min_mag=5.0, limit=10) -> str:
|
|
| 395 |
for f in features:
|
| 396 |
p = f["properties"]
|
| 397 |
t_utc = datetime.fromtimestamp(p["time"] / 1000, tz=timezone.utc)
|
| 398 |
-
lines.append(
|
| 399 |
-
f"้็ด: {p['mag']:.1f} | ๆฅๆๆ้: {t_utc.strftime('%Y-%m-%d %H:%M')} (UTC)\nๅฐ้ป: {p.get('place','')}"
|
| 400 |
-
)
|
| 401 |
return "\n\n".join(lines)
|
| 402 |
except Exception as e:
|
| 403 |
return f"โ ๆฅ่ฉขๅคฑๆ: {e}"
|
|
@@ -431,7 +377,7 @@ def fetch_taiwan_df_this_year(min_mag=5.0) -> pd.DataFrame | str:
|
|
| 431 |
return f"โ ๆฅ่ฉขๅคฑๆ: {e}"
|
| 432 |
|
| 433 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 434 |
-
#
|
| 435 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 436 |
def create_and_save_map(df: pd.DataFrame) -> str:
|
| 437 |
fig, ax = plt.subplots(figsize=(9, 6), dpi=150)
|
|
@@ -464,7 +410,7 @@ def _base_url_for_images() -> str:
|
|
| 464 |
return HF_SPACE_URL.rstrip("/") if HF_SPACE_URL else request.url_root.rstrip("/")
|
| 465 |
|
| 466 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 467 |
-
#
|
| 468 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 469 |
@app.route("/callback", methods=["POST"])
|
| 470 |
def callback():
|
|
@@ -484,7 +430,7 @@ def handle_message(event):
|
|
| 484 |
with ApiClient(configuration) as api_client:
|
| 485 |
line_bot_api = MessagingApi(api_client)
|
| 486 |
|
| 487 |
-
#
|
| 488 |
if "ๅฐ้้ ่ญฆ" in user_message:
|
| 489 |
reply_text = fetch_cwa_alarm_list(limit=5)
|
| 490 |
line_bot_api.reply_message_with_http_info(
|
|
@@ -492,7 +438,7 @@ def handle_message(event):
|
|
| 492 |
)
|
| 493 |
return
|
| 494 |
|
| 495 |
-
#
|
| 496 |
if ("่บ็ฃๅฐ้็ซๅ" in user_message) or ("ๅฐ็ฃๅฐ้็ซๅ" in user_message):
|
| 497 |
result = fetch_taiwan_df_this_year()
|
| 498 |
if isinstance(result, pd.DataFrame):
|
|
@@ -510,24 +456,24 @@ def handle_message(event):
|
|
| 510 |
line_bot_api.reply_message_with_http_info(reply)
|
| 511 |
return
|
| 512 |
|
| 513 |
-
#
|
| 514 |
if user_message == "/help":
|
| 515 |
text = (
|
| 516 |
-
"๐
|
| 517 |
-
"
|
| 518 |
-
"
|
| 519 |
-
"
|
| 520 |
-
"
|
| 521 |
-
"
|
| 522 |
-
"
|
| 523 |
-
"
|
| 524 |
)
|
| 525 |
line_bot_api.reply_message_with_http_info(
|
| 526 |
ReplyMessageRequest(reply_token=event.reply_token, messages=[TextMessage(text=text)])
|
| 527 |
)
|
| 528 |
return
|
| 529 |
|
| 530 |
-
#
|
| 531 |
if ("่บ็ฃๅฐ้" in user_message) or ("ๅฐ็ฃๅฐ้" in user_message):
|
| 532 |
result = fetch_taiwan_df_this_year()
|
| 533 |
if isinstance(result, pd.DataFrame):
|
|
@@ -546,7 +492,7 @@ def handle_message(event):
|
|
| 546 |
)
|
| 547 |
return
|
| 548 |
|
| 549 |
-
#
|
| 550 |
if ("ๅฐ้" in user_message) or ("quake" in user_message):
|
| 551 |
reply_text = fetch_global_last24h_text()
|
| 552 |
line_bot_api.reply_message_with_http_info(
|
|
@@ -554,28 +500,23 @@ def handle_message(event):
|
|
| 554 |
)
|
| 555 |
return
|
| 556 |
|
| 557 |
-
#
|
| 558 |
if user_message.startswith("ai ") or user_message.startswith("ai:") or user_message.startswith("ai๏ผ"):
|
| 559 |
-
prompt = user_message_raw[2:].lstrip(" ๏ผ:").strip()
|
| 560 |
-
if not prompt:
|
| 561 |
-
prompt = "่ซไฝ็บไธๅๅฐ้่ณ่จๅฉ็๏ผ็ฐก่ฆไป็ดนไฝ ็ๅ่ฝใ"
|
| 562 |
ai_text = generate_ai_text(prompt)
|
| 563 |
line_bot_api.reply_message_with_http_info(
|
| 564 |
ReplyMessageRequest(reply_token=event.reply_token, messages=[TextMessage(text=ai_text)])
|
| 565 |
)
|
| 566 |
return
|
| 567 |
|
| 568 |
-
#
|
| 569 |
if ("ไฝ ๅฅฝ" in user_message) or ("hi" in user_message):
|
| 570 |
line_bot_api.reply_message_with_http_info(
|
| 571 |
-
ReplyMessageRequest(
|
| 572 |
-
reply_token=event.reply_token,
|
| 573 |
-
messages=[TextMessage(text="๐ ไฝ ๅฅฝ๏ผ่ผธๅ
ฅ /help ๆฅ็ๆไปคใ")]
|
| 574 |
-
)
|
| 575 |
)
|
| 576 |
return
|
| 577 |
|
| 578 |
-
#
|
| 579 |
fallback = generate_ai_text(user_message_raw)
|
| 580 |
line_bot_api.reply_message_with_http_info(
|
| 581 |
ReplyMessageRequest(reply_token=event.reply_token, messages=[TextMessage(text=fallback)])
|
|
|
|
| 1 |
# app.py
|
| 2 |
import os
|
| 3 |
+
# ๅฐ Matplotlib ่ Hugging Face ๆจกๅๅฟซๅๅฐๅฏๅฏซ็ฎ้๏ผHF Spaces ๅธธ่ฆๅๆณ๏ผ
|
| 4 |
+
os.environ["MPLCONFIGDIR"] = "/tmp/matplotlib"
|
| 5 |
+
os.environ.setdefault("TRANSFORMERS_CACHE", "/tmp/huggingface")
|
| 6 |
+
os.makedirs(os.environ["TRANSFORMERS_CACHE"], exist_ok=True)
|
| 7 |
|
| 8 |
import uuid
|
| 9 |
import tempfile
|
|
|
|
| 32 |
from matplotlib import font_manager as fm
|
| 33 |
|
| 34 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 35 |
+
# CWA ๅฐ้้ ่ญฆ๏ผ่ฅๅค้จๆจก็ตๅญๅจๅฐฑ็จ๏ผๅฆๅไฝฟ็จๅๆดๅฏฆไฝ
|
| 36 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 37 |
try:
|
| 38 |
+
from fetch_cwa_alarm_list import fetch_cwa_alarm_list # type: ignore
|
|
|
|
| 39 |
except Exception:
|
| 40 |
CWA_ALARM_API = "https://app-2.cwa.gov.tw/api/v1/earthquake/alarm/list"
|
| 41 |
|
| 42 |
def _parse_cwa_time(s: str):
|
|
|
|
| 43 |
if not s:
|
| 44 |
return ("ๆช็ฅ", "ๆช็ฅ")
|
| 45 |
try:
|
|
|
|
| 55 |
return (s, "ๆช็ฅ")
|
| 56 |
|
| 57 |
def fetch_cwa_alarm_list(limit: int = 5) -> str:
|
|
|
|
| 58 |
try:
|
| 59 |
r = requests.get(CWA_ALARM_API, timeout=10)
|
| 60 |
r.raise_for_status()
|
|
|
|
| 76 |
if "T" in s or s.endswith("Z") or "+" in s:
|
| 77 |
dt = datetime.fromisoformat(s.replace("Z", "+00:00"))
|
| 78 |
else:
|
| 79 |
+
dt = datetime.strptime(s, "%Y-%m-%d %H:%M:%S").replace(tzinfo=timezone(timedelta(hours=8)))
|
|
|
|
|
|
|
| 80 |
return dt.astimezone(timezone.utc)
|
| 81 |
except Exception:
|
| 82 |
return datetime.min.replace(tzinfo=timezone.utc)
|
|
|
|
| 91 |
ok = xs.replace(".", "", 1).replace("-", "", 1).isdigit()
|
| 92 |
return float(xs) if ok else None
|
| 93 |
|
| 94 |
+
out = ["๐จ ๅฐ้้ ่ญฆ๏ผๆๆฐ๏ผ:", "-" * 20]
|
| 95 |
for it in items[:limit]:
|
| 96 |
identifier = it.get("identifier") or it.get("eventId") or it.get("id") or "โ"
|
| 97 |
status = it.get("status") or "โ"
|
|
|
|
| 100 |
|
| 101 |
mag = _num(it.get("magnitudeValue") or it.get("magnitude") or it.get("ml") or it.get("mw"))
|
| 102 |
mag_str = f"{mag:.1f}" if mag is not None else "โ"
|
|
|
|
| 103 |
depth = _num(it.get("depth"))
|
| 104 |
depth_str = f"{depth:.0f}" if depth is not None else "โ"
|
| 105 |
|
|
|
|
| 108 |
lat_str = f"{lat:.2f}" if lat is not None else "โ"
|
| 109 |
lon_str = f"{lon:.2f}" if lon is not None else "โ"
|
| 110 |
|
| 111 |
+
tw_str, utc_str = _parse_cwa_time(it.get("originTime") or "")
|
|
|
|
| 112 |
|
| 113 |
areas = it.get("locationDesc") or it.get("areas") or it.get("alertAreas")
|
| 114 |
if isinstance(areas, list):
|
|
|
|
| 118 |
else:
|
| 119 |
areas_txt = "โ"
|
| 120 |
|
| 121 |
+
out.append(
|
| 122 |
+
f"{status} | ้กๅ: {msg_type}#{msg_no}\n"
|
| 123 |
f"้็ด/ๆทฑๅบฆ: M{mag_str} / {depth_str} km\n"
|
| 124 |
f"้ไธญ: lat {lat_str}, lon {lon_str}\n"
|
| 125 |
f"ๆ้: {tw_str}๏ผๅฐ็ฃ๏ผ / {utc_str}๏ผUTC๏ผ\n"
|
| 126 |
+
f"้ ่ญฆๅฐๅ: {areas_txt}\n"
|
| 127 |
+
f"ไบไปถ: {identifier}"
|
| 128 |
)
|
| 129 |
+
out.append("")
|
|
|
|
| 130 |
if len(items) > limit:
|
| 131 |
+
out.append(f"... ๅฆๆ {len(items) - limit} ็ญใ")
|
| 132 |
+
return "\n".join(out).strip()
|
| 133 |
|
| 134 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 135 |
+
# AI ็ๆๅ่ฝ๏ผTransformers๏ผ๏ผๆถ่ผๅ
ฅ + ๆฌ้ๅฎๅ
จๅฟซๅ
|
| 136 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 137 |
_LLM = {"loaded": False, "ok": False, "err": None, "model": None, "tokenizer": None, "device": "cpu"}
|
| 138 |
|
| 139 |
def _ensure_llm():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
if _LLM["loaded"]:
|
| 141 |
return _LLM["ok"], _LLM["err"]
|
|
|
|
| 142 |
_LLM["loaded"] = True
|
| 143 |
try:
|
| 144 |
+
import torch # noqa
|
| 145 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
|
| 146 |
|
| 147 |
device = os.getenv("LLM_DEVICE")
|
| 148 |
if device not in ("cuda", "cpu"):
|
|
|
|
| 149 |
try:
|
| 150 |
import torch as _t
|
| 151 |
device = "cuda" if _t.cuda.is_available() else "cpu"
|
|
|
|
| 153 |
except Exception:
|
| 154 |
device = "cpu"
|
| 155 |
|
| 156 |
+
model_name = os.getenv("LLM_MODEL", "ckiplab/gpt2-base-chinese")
|
| 157 |
+
cache_dir = os.environ["TRANSFORMERS_CACHE"]
|
| 158 |
+
tok = AutoTokenizer.from_pretrained(model_name, cache_dir=cache_dir)
|
| 159 |
+
mdl = AutoModelForCausalLM.from_pretrained(model_name, cache_dir=cache_dir)
|
| 160 |
try:
|
| 161 |
mdl = mdl.to(device)
|
| 162 |
except Exception:
|
|
|
|
| 174 |
ok, err = _ensure_llm()
|
| 175 |
if not ok:
|
| 176 |
return ("๐ค AI ๅฐๆชๅ็จ๏ผ็ผบๅฐไพ่ณดๆๆจกๅๆชไธ่ผใ\n"
|
| 177 |
+
"่ซๅจ requirements.txt ๅ ๅ
ฅ transformersใtorchใaccelerateใsafetensorsใsentencepieceใ\n"
|
| 178 |
f"่ฉณ็ดฐ้ฏ่ชค๏ผ{err}")
|
| 179 |
|
| 180 |
+
import torch # type: ignore
|
| 181 |
tok = _LLM["tokenizer"]
|
| 182 |
mdl = _LLM["model"]
|
| 183 |
device = _LLM["device"]
|
|
|
|
| 186 |
top_k = int(os.getenv("LLM_TOP_K", "50"))
|
| 187 |
temperature = float(os.getenv("LLM_TEMPERATURE", "0.7"))
|
| 188 |
|
| 189 |
+
sys_prefix = (
|
| 190 |
+
"ไฝ ๆฏไธๅๅฐ้่ณ่จ่ไธ่ฌๅ็ญ็ LINE ๅฉ็ใๅ็ญ่ฆ็ฒพ็ฐกใๆธ
ๆฅ๏ผ"
|
| 191 |
+
"่ฅ่ๅฐ้็ธ้ๅฏๅ ๅ
ฅๆณจๆไบ้
๏ผ่ฅ็ก้ๅไธ่ฌๅ่ฆใ\n\nไฝฟ็จ่
๏ผ"
|
|
|
|
|
|
|
| 192 |
)
|
| 193 |
+
prompt = sys_prefix + user_prompt
|
| 194 |
|
| 195 |
try:
|
|
|
|
| 196 |
inputs = tok(prompt, return_tensors="pt").to(device)
|
| 197 |
with torch.no_grad():
|
| 198 |
output = mdl.generate(
|
|
|
|
| 205 |
pad_token_id=tok.eos_token_id,
|
| 206 |
)
|
| 207 |
text = tok.decode(output[0], skip_special_tokens=True)
|
| 208 |
+
if sys_prefix in text:
|
| 209 |
+
text = text.split(sys_prefix, 1)[-1]
|
|
|
|
|
|
|
| 210 |
if user_prompt in text:
|
| 211 |
text = text.split(user_prompt, 1)[-1].strip()
|
| 212 |
+
return (text or "๏ผๆฒๆ็ข็ๅ
งๅฎน๏ผ")[:1200]
|
|
|
|
| 213 |
except Exception as e:
|
| 214 |
return f"AI ็ข็็ผ็้ฏ่ชค๏ผ{e}"
|
| 215 |
|
| 216 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 217 |
+
# ไธญๆๅญๅ๏ผๆๅฐฑๅฅ็จ๏ผ
|
| 218 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 219 |
for fp in [
|
| 220 |
"/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",
|
|
|
|
| 227 |
break
|
| 228 |
|
| 229 |
# โโโโ๏ฟฝ๏ฟฝโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 230 |
+
# ๅบๆฌ่จญๅฎ
|
| 231 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 232 |
CHANNEL_ACCESS_TOKEN = os.getenv("CHANNEL_ACCESS_TOKEN")
|
| 233 |
CHANNEL_SECRET = os.getenv("CHANNEL_SECRET")
|
| 234 |
|
|
|
|
| 235 |
HF_SPACE_URL = os.getenv("SPACEURL")
|
| 236 |
if not HF_SPACE_URL:
|
| 237 |
+
sid = os.getenv("SPACE_ID")
|
| 238 |
if sid and "/" in sid:
|
| 239 |
a, n = sid.split("/", 1)
|
| 240 |
HF_SPACE_URL = f"https://{a.replace('_','-')}-{n.replace('_','-')}.hf.space"
|
|
|
|
| 249 |
handler = WebhookHandler(CHANNEL_SECRET)
|
| 250 |
|
| 251 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 252 |
+
# ้ฆ้ ๏ผๆไปค/Webhook/้ๆ
ๆช่ชชๆ๏ผ
|
| 253 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 254 |
@app.route("/", methods=["GET"])
|
| 255 |
def home():
|
|
|
|
| 261 |
|
| 262 |
return f"""
|
| 263 |
<!doctype html>
|
| 264 |
+
<html lang="zh-Hant"><head>
|
| 265 |
+
<meta charset="utf-8"/><meta name="viewport" content="width=device-width,initial-scale=1"/>
|
|
|
|
|
|
|
| 266 |
<title>ๅฐ้้ ่ญฆ dayichen โ LINE Bot Server</title>
|
| 267 |
<style>
|
| 268 |
+
:root{{--bg:#0f1115;--card:#151821;--text:#e6e8ef;--muted:#9aa4b2;--border:rgba(255,255,255,.08)}}
|
| 269 |
+
*{{box-sizing:border-box}} body{{margin:0;background:#0f1115;color:#e6e8ef;
|
| 270 |
+
font:16px/1.6 -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Noto Sans TC","PingFang TC",sans-serif;
|
| 271 |
+
padding:32px 16px;display:flex;justify-content:center}}
|
| 272 |
+
.wrap{{width:100%;max-width:980px}} .hero{{background:linear-gradient(135deg,#1f2937,#0f172a);
|
| 273 |
+
border:1px solid var(--border);border-radius:16px;padding:28px;margin-bottom:20px;box-shadow:0 8px 30px rgba(0,0,0,.25)}}
|
| 274 |
+
.title{{margin:0 0 6px;font-size:28px;font-weight:800}} .subtitle{{margin:0;color:#9aa4b2}}
|
| 275 |
+
.grid{{display:grid;gap:16px;grid-template-columns:repeat(auto-fit,minmax(260px,1fr));margin-top:18px}}
|
| 276 |
+
.card{{background:#151821;border:1px solid var(--border);border-radius:14px;padding:16px 18px}}
|
| 277 |
+
h3{{margin:0 0 8px;font-size:18px}} .kbd{{padding:2px 6px;border:1px solid var(--border);border-radius:6px;background:#0b0e14}}
|
| 278 |
+
.mono,code{{font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace;color:#e6e8ef}}
|
| 279 |
+
a{{color:#60a5fa;text-decoration:none}} a:hover{{text-decoration:underline}}
|
| 280 |
+
.badge{{display:inline-block;padding:2px 8px;border-radius:999px;background:#1f2937;border:1px solid var(--border);font-size:12px;color:#9aa4b2}}
|
| 281 |
+
.foot{{color:#9aa4b2;font-size:13px;margin-top:18px;text-align:center}}
|
| 282 |
+
</style></head>
|
| 283 |
+
<body><div class="wrap"><section class="hero">
|
| 284 |
+
<div class="badge">็ๆ
๏ผ<span style="color:{'#86efac' if channel_ok=='โ
' else '#fbbf24'}">{channel_ok}</span> LINE ้้ฐใยทใHF Space๏ผ{space_ok}</div>
|
| 285 |
+
<h1 class="title">ๅฐ้้ ่ญฆ dayichen โ LINE Bot</h1>
|
| 286 |
+
<p class="subtitle">ๆไปค๏ผ/helpใๅฐ้/quakeใ่บ็ฃๅฐ้/ๅฐ็ฃๅฐ้ใ่บ็ฃๅฐ้็ซๅ/ๅฐ็ฃๅฐ้็ซๅใๅฐ้้ ่ญฆใAI๏ผai + ๅ้ก๏ผใ</p>
|
| 287 |
+
<div class="grid">
|
| 288 |
+
<div class="card"><h3>๐ ๅฟซ้้ๅง</h3><ul>
|
| 289 |
+
<li><span class="kbd">/help</span>๏ผ้กฏ็คบๆๆๆไปค</li>
|
| 290 |
+
<li><span class="kbd">ๅฐ้</span>/<span class="kbd">quake</span>๏ผๅ
จ็่ฟ 24 ๅฐๆ Mโฅ5.0</li>
|
| 291 |
+
<li><span class="kbd">่บ็ฃๅฐ้</span>/<span class="kbd">ๅฐ็ฃๅฐ้</span>๏ผไปๅนดๅฐ็ฃๅๅๆธ
ๅฎ๏ผๅซๆฅๆๆ้๏ผ</li>
|
| 292 |
+
<li><span class="kbd">่บ็ฃๅฐ้็ซๅ</span>/<span class="kbd">ๅฐ็ฃๅฐ้็ซๅ</span>๏ผๅๅณๅฐๅๅ็</li>
|
| 293 |
+
<li><span class="kbd">ๅฐ้้ ่ญฆ</span>๏ผCWA ๅฐ้้ ่ญฆ๏ผๆๆฐ 5 ็ญ๏ผ</li>
|
| 294 |
+
<li><span class="kbd">ai ไฝ ็ๅ้ก</span>๏ผAI ๅฐ่ฉฑ๏ผๆจกๅ๏ผ<span class="mono">{os.getenv("LLM_MODEL","ckiplab/gpt2-base-chinese")}</span>๏ผ</li>
|
| 295 |
+
</ul></div>
|
| 296 |
+
<div class="card"><h3>๐ ๏ธ Webhook / ้ๆ
ๆช</h3><ul>
|
| 297 |
+
<li>Webhook๏ผ<span class="mono"><a href="{webhook_url}">{webhook_url}</a></span></li>
|
| 298 |
+
<li>้ๆ
ๅ็๏ผ<span class="mono">{static_hint}</span></li>
|
| 299 |
+
<li>ๅฅๅบทๆชขๆฅ๏ผ<span class="mono"><a href="{base}/healthz">{base}/healthz</a></span></li>
|
| 300 |
+
</ul></div>
|
| 301 |
+
<div class="card"><h3>โน๏ธ ๅ่จป</h3><ul>
|
| 302 |
+
<li>AI ๅฟซๅไฝ็ฝฎ๏ผ<span class="mono">{os.environ["TRANSFORMERS_CACHE"]}</span></li>
|
| 303 |
+
<li>่ฅ AI ๆชๅฎ่ฃไพ่ณด๏ผๆฉๅจไบบๆๆ็คบๅฎ่ฃ๏ผไธๆๅฝฑ้ฟๅ
ถไปๅ่ฝใ</li>
|
| 304 |
+
</ul></div>
|
| 305 |
+
</div>
|
| 306 |
+
<p class="foot">ยฉ {datetime.now().year} dayichen ยท server: {base}</p>
|
| 307 |
+
</section></div></body></html>"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
|
| 309 |
@app.route("/healthz")
|
| 310 |
def healthz():
|
|
|
|
| 315 |
return send_from_directory(STATIC_DIR, filename)
|
| 316 |
|
| 317 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 318 |
+
# USGS ๆฅ่ฉข
|
| 319 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 320 |
USGS_API_BASE_URL = "https://earthquake.usgs.gov/fdsnws/event/1/query"
|
| 321 |
|
|
|
|
| 343 |
for f in features:
|
| 344 |
p = f["properties"]
|
| 345 |
t_utc = datetime.fromtimestamp(p["time"] / 1000, tz=timezone.utc)
|
| 346 |
+
lines.append(f"้็ด: {p['mag']:.1f} | ๆฅๆๆ้: {t_utc.strftime('%Y-%m-%d %H:%M')} (UTC)\nๅฐ้ป: {p.get('place','')}")
|
|
|
|
|
|
|
| 347 |
return "\n\n".join(lines)
|
| 348 |
except Exception as e:
|
| 349 |
return f"โ ๆฅ่ฉขๅคฑๆ: {e}"
|
|
|
|
| 377 |
return f"โ ๆฅ่ฉขๅคฑๆ: {e}"
|
| 378 |
|
| 379 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 380 |
+
# ่ฃฝๅ
|
| 381 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 382 |
def create_and_save_map(df: pd.DataFrame) -> str:
|
| 383 |
fig, ax = plt.subplots(figsize=(9, 6), dpi=150)
|
|
|
|
| 410 |
return HF_SPACE_URL.rstrip("/") if HF_SPACE_URL else request.url_root.rstrip("/")
|
| 411 |
|
| 412 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 413 |
+
# LINE Webhook
|
| 414 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 415 |
@app.route("/callback", methods=["POST"])
|
| 416 |
def callback():
|
|
|
|
| 430 |
with ApiClient(configuration) as api_client:
|
| 431 |
line_bot_api = MessagingApi(api_client)
|
| 432 |
|
| 433 |
+
# ๅฐ้้ ่ญฆ
|
| 434 |
if "ๅฐ้้ ่ญฆ" in user_message:
|
| 435 |
reply_text = fetch_cwa_alarm_list(limit=5)
|
| 436 |
line_bot_api.reply_message_with_http_info(
|
|
|
|
| 438 |
)
|
| 439 |
return
|
| 440 |
|
| 441 |
+
# ่บ็ฃๅฐ้็ซๅ
|
| 442 |
if ("่บ็ฃๅฐ้็ซๅ" in user_message) or ("ๅฐ็ฃๅฐ้็ซๅ" in user_message):
|
| 443 |
result = fetch_taiwan_df_this_year()
|
| 444 |
if isinstance(result, pd.DataFrame):
|
|
|
|
| 456 |
line_bot_api.reply_message_with_http_info(reply)
|
| 457 |
return
|
| 458 |
|
| 459 |
+
# ่ชชๆ
|
| 460 |
if user_message == "/help":
|
| 461 |
text = (
|
| 462 |
+
"๐ ๆไปค\n\n"
|
| 463 |
+
"โข /help\n"
|
| 464 |
+
"โข ๅฐ้ / quake๏ผๅ
จ็่ฟ24ๅฐๆ๏ผๅซๆฅๆๆ้๏ผ\n"
|
| 465 |
+
"โข ่บ็ฃๅฐ้ / ๅฐ็ฃๅฐ้๏ผไปๅนดๅฐ็ฃๅๅๆธ
ๅฎ๏ผ\n"
|
| 466 |
+
"โข ่บ็ฃๅฐ้็ซๅ / ๅฐ็ฃๅฐ้็ซๅ๏ผไปๅนดๅฐ็ฃๅๅๅไฝๅ๏ผ\n"
|
| 467 |
+
"โข ๅฐ้้ ่ญฆ๏ผCWA ๆๆฐ 5 ็ญ๏ผ\n"
|
| 468 |
+
"โข ai ไฝ ็ๅ้ก๏ผAI ๅฐ่ฉฑ๏ผ\n"
|
| 469 |
+
"โข ไฝ ๅฅฝ"
|
| 470 |
)
|
| 471 |
line_bot_api.reply_message_with_http_info(
|
| 472 |
ReplyMessageRequest(reply_token=event.reply_token, messages=[TextMessage(text=text)])
|
| 473 |
)
|
| 474 |
return
|
| 475 |
|
| 476 |
+
# ๅฐ็ฃๆธ
ๅฎ
|
| 477 |
if ("่บ็ฃๅฐ้" in user_message) or ("ๅฐ็ฃๅฐ้" in user_message):
|
| 478 |
result = fetch_taiwan_df_this_year()
|
| 479 |
if isinstance(result, pd.DataFrame):
|
|
|
|
| 492 |
)
|
| 493 |
return
|
| 494 |
|
| 495 |
+
# ๅ
จ็ 24 ๅฐๆ
|
| 496 |
if ("ๅฐ้" in user_message) or ("quake" in user_message):
|
| 497 |
reply_text = fetch_global_last24h_text()
|
| 498 |
line_bot_api.reply_message_with_http_info(
|
|
|
|
| 500 |
)
|
| 501 |
return
|
| 502 |
|
| 503 |
+
# AI ๅฐ่ฉฑ๏ผๆ็ขบ่งธ็ผ่ฉ๏ผ
|
| 504 |
if user_message.startswith("ai ") or user_message.startswith("ai:") or user_message.startswith("ai๏ผ"):
|
| 505 |
+
prompt = user_message_raw[2:].lstrip(" ๏ผ:").strip() or "่ซ็ฐก่ฆไป็ดนไฝ ็ๅ่ฝใ"
|
|
|
|
|
|
|
| 506 |
ai_text = generate_ai_text(prompt)
|
| 507 |
line_bot_api.reply_message_with_http_info(
|
| 508 |
ReplyMessageRequest(reply_token=event.reply_token, messages=[TextMessage(text=ai_text)])
|
| 509 |
)
|
| 510 |
return
|
| 511 |
|
| 512 |
+
# ๆๅผ
|
| 513 |
if ("ไฝ ๅฅฝ" in user_message) or ("hi" in user_message):
|
| 514 |
line_bot_api.reply_message_with_http_info(
|
| 515 |
+
ReplyMessageRequest(reply_token=event.reply_token, messages=[TextMessage(text="๐ ไฝ ๅฅฝ๏ผ่ผธๅ
ฅ /help ๆฅ็ๆไปคใ")])
|
|
|
|
|
|
|
|
|
|
| 516 |
)
|
| 517 |
return
|
| 518 |
|
| 519 |
+
# ๅ
ๅบ๏ผไบค็ตฆ AI ๅ่ฆ๏ผ่ฎ็ณป็ตฑๆดๆบ่ฝ
|
| 520 |
fallback = generate_ai_text(user_message_raw)
|
| 521 |
line_bot_api.reply_message_with_http_info(
|
| 522 |
ReplyMessageRequest(reply_token=event.reply_token, messages=[TextMessage(text=fallback)])
|