Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -136,7 +136,103 @@ except Exception:
|
|
| 136 |
return "\n".join(lines).strip()
|
| 137 |
|
| 138 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 139 |
-
# 2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 141 |
for fp in [
|
| 142 |
"/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",
|
|
@@ -149,7 +245,7 @@ for fp in [
|
|
| 149 |
break
|
| 150 |
|
| 151 |
# โโโโโโโโโโโโโโโโโโโโ๏ฟฝ๏ฟฝโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 152 |
-
#
|
| 153 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 154 |
CHANNEL_ACCESS_TOKEN = os.getenv("CHANNEL_ACCESS_TOKEN")
|
| 155 |
CHANNEL_SECRET = os.getenv("CHANNEL_SECRET")
|
|
@@ -172,7 +268,7 @@ configuration = Configuration(access_token=CHANNEL_ACCESS_TOKEN)
|
|
| 172 |
handler = WebhookHandler(CHANNEL_SECRET)
|
| 173 |
|
| 174 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 175 |
-
#
|
| 176 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 177 |
@app.route("/", methods=["GET"])
|
| 178 |
def home():
|
|
@@ -225,16 +321,17 @@ def home():
|
|
| 225 |
<section class="hero">
|
| 226 |
<div class="badge">็ๆ
๏ผ<span style="color:{'#86efac' if channel_ok=='โ
' else '#fbbf24'}">{channel_ok}</span> LINE ้้ฐใยทใHF Space๏ผ{space_ok}</div>
|
| 227 |
<h1 class="title">ๅฐ้้ ่ญฆ dayichen โ LINE Bot</h1>
|
| 228 |
-
<p class="subtitle"
|
| 229 |
<div class="grid">
|
| 230 |
<div class="card">
|
| 231 |
<h3>๐ ๅฟซ้้ๅง</h3>
|
| 232 |
<ul>
|
| 233 |
-
<li
|
| 234 |
-
<li
|
| 235 |
<li><span class="kbd">่บ็ฃๅฐ้</span>/<span class="kbd">ๅฐ็ฃๅฐ้</span>๏ผไปๅนดๅฐ็ฃๅๅๆธ
ๅฎ๏ผๅซๆฅๆๆ้๏ผ</li>
|
| 236 |
<li><span class="kbd">่บ็ฃๅฐ้็ซๅ</span>/<span class="kbd">ๅฐ็ฃๅฐ้็ซๅ</span>๏ผๅๅณๅฐๅๅ็</li>
|
| 237 |
<li><span class="kbd">ๅฐ้้ ่ญฆ</span>๏ผCWA ๅฐ้้ ่ญฆ๏ผๆๆฐ 5 ็ญ๏ผ</li>
|
|
|
|
| 238 |
</ul>
|
| 239 |
</div>
|
| 240 |
<div class="card">
|
|
@@ -248,9 +345,9 @@ def home():
|
|
| 248 |
<div class="card">
|
| 249 |
<h3>โน๏ธ ๅ่จป</h3>
|
| 250 |
<ul>
|
| 251 |
-
<li
|
|
|
|
| 252 |
<li>ๅฐๅไปฅ Matplotlib ็ข็ๆผ <span class="mono">/tmp/static</span>๏ผ็ฑ <span class="mono">/static/</span> ๆไพใ</li>
|
| 253 |
-
<li>่ฅๅ็ๅจ LINE ้กฏ็คบ็ฉบ็ฝ๏ผ่ซ็ขบ่ชๆฌ้ ใWebhook ่้ๆ
ๆชใ็ถฒๅ่ฝๅพๅค็ถฒๅญๅใ</li>
|
| 254 |
</ul>
|
| 255 |
</div>
|
| 256 |
</div>
|
|
@@ -270,7 +367,7 @@ def serve_static(filename):
|
|
| 270 |
return send_from_directory(STATIC_DIR, filename)
|
| 271 |
|
| 272 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 273 |
-
#
|
| 274 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 275 |
USGS_API_BASE_URL = "https://earthquake.usgs.gov/fdsnws/event/1/query"
|
| 276 |
|
|
@@ -334,7 +431,7 @@ def fetch_taiwan_df_this_year(min_mag=5.0) -> pd.DataFrame | str:
|
|
| 334 |
return f"โ ๆฅ่ฉขๅคฑๆ: {e}"
|
| 335 |
|
| 336 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 337 |
-
#
|
| 338 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 339 |
def create_and_save_map(df: pd.DataFrame) -> str:
|
| 340 |
fig, ax = plt.subplots(figsize=(9, 6), dpi=150)
|
|
@@ -367,7 +464,7 @@ def _base_url_for_images() -> str:
|
|
| 367 |
return HF_SPACE_URL.rstrip("/") if HF_SPACE_URL else request.url_root.rstrip("/")
|
| 368 |
|
| 369 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 370 |
-
#
|
| 371 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 372 |
@app.route("/callback", methods=["POST"])
|
| 373 |
def callback():
|
|
@@ -381,11 +478,13 @@ def callback():
|
|
| 381 |
|
| 382 |
@handler.add(MessageEvent, message=TextMessageContent)
|
| 383 |
def handle_message(event):
|
| 384 |
-
|
|
|
|
|
|
|
| 385 |
with ApiClient(configuration) as api_client:
|
| 386 |
line_bot_api = MessagingApi(api_client)
|
| 387 |
|
| 388 |
-
# ๅฐ้้ ่ญฆ๏ผCWA๏ผ
|
| 389 |
if "ๅฐ้้ ่ญฆ" in user_message:
|
| 390 |
reply_text = fetch_cwa_alarm_list(limit=5)
|
| 391 |
line_bot_api.reply_message_with_http_info(
|
|
@@ -393,7 +492,7 @@ def handle_message(event):
|
|
| 393 |
)
|
| 394 |
return
|
| 395 |
|
| 396 |
-
# ่บ็ฃๅฐ้็ซๅ
|
| 397 |
if ("่บ็ฃๅฐ้็ซๅ" in user_message) or ("ๅฐ็ฃๅฐ้็ซๅ" in user_message):
|
| 398 |
result = fetch_taiwan_df_this_year()
|
| 399 |
if isinstance(result, pd.DataFrame):
|
|
@@ -411,7 +510,7 @@ def handle_message(event):
|
|
| 411 |
line_bot_api.reply_message_with_http_info(reply)
|
| 412 |
return
|
| 413 |
|
| 414 |
-
# ่ชชๆ
|
| 415 |
if user_message == "/help":
|
| 416 |
text = (
|
| 417 |
"๐ ๅฐ้้ ่ญฆ dayichen ๆไปค\n\n"
|
|
@@ -420,6 +519,7 @@ def handle_message(event):
|
|
| 420 |
"โก๏ธ ่บ็ฃๅฐ้ / ๅฐ็ฃๅฐ้๏ผไปๅนดๅฐ็ฃๅๅๆธ
ๅฎ๏ผ\n"
|
| 421 |
"โก๏ธ ่บ็ฃๅฐ้็ซๅ / ๅฐ็ฃๅฐ้็ซๅ๏ผไปๅนดๅฐ็ฃๅๅๅไฝๅ๏ผ\n"
|
| 422 |
"โก๏ธ ๅฐ้้ ่ญฆ๏ผCWA ๆๆฐ 5 ็ญ๏ผ\n"
|
|
|
|
| 423 |
"โก๏ธ ไฝ ๅฅฝ"
|
| 424 |
)
|
| 425 |
line_bot_api.reply_message_with_http_info(
|
|
@@ -427,7 +527,7 @@ def handle_message(event):
|
|
| 427 |
)
|
| 428 |
return
|
| 429 |
|
| 430 |
-
# ๅฐ็ฃๆธ
ๅฎ
|
| 431 |
if ("่บ็ฃๅฐ้" in user_message) or ("ๅฐ็ฃๅฐ้" in user_message):
|
| 432 |
result = fetch_taiwan_df_this_year()
|
| 433 |
if isinstance(result, pd.DataFrame):
|
|
@@ -446,7 +546,7 @@ def handle_message(event):
|
|
| 446 |
)
|
| 447 |
return
|
| 448 |
|
| 449 |
-
# ๅ
จ็ 24 ๅฐๆ
|
| 450 |
if ("ๅฐ้" in user_message) or ("quake" in user_message):
|
| 451 |
reply_text = fetch_global_last24h_text()
|
| 452 |
line_bot_api.reply_message_with_http_info(
|
|
@@ -454,7 +554,18 @@ def handle_message(event):
|
|
| 454 |
)
|
| 455 |
return
|
| 456 |
|
| 457 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 458 |
if ("ไฝ ๅฅฝ" in user_message) or ("hi" in user_message):
|
| 459 |
line_bot_api.reply_message_with_http_info(
|
| 460 |
ReplyMessageRequest(
|
|
@@ -462,4 +573,10 @@ def handle_message(event):
|
|
| 462 |
messages=[TextMessage(text="๐ ไฝ ๅฅฝ๏ผ่ผธๅ
ฅ /help ๆฅ็ๆไปคใ")]
|
| 463 |
)
|
| 464 |
)
|
| 465 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
return "\n".join(lines).strip()
|
| 137 |
|
| 138 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 139 |
+
# 2) AI ๅฐ่ฉฑ๏ผTransformers๏ผโ ๅปถ้ฒ่ผๅ
ฅ่ๅฎน้ฏ
|
| 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 torch # noqa: F401
|
| 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"
|
| 165 |
+
_t.set_num_threads(max(1, int(os.getenv("LLM_THREADS", "1"))))
|
| 166 |
+
except Exception:
|
| 167 |
+
device = "cpu"
|
| 168 |
+
|
| 169 |
+
model_name = os.getenv("LLM_MODEL", "ckiplab/gpt2-base-chinese") # ๅฏๆนๆ "gpt2"
|
| 170 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 171 |
+
tok = AutoTokenizer.from_pretrained(model_name)
|
| 172 |
+
mdl = AutoModelForCausalLM.from_pretrained(model_name)
|
| 173 |
+
try:
|
| 174 |
+
mdl = mdl.to(device)
|
| 175 |
+
except Exception:
|
| 176 |
+
device = "cpu"
|
| 177 |
+
mdl = mdl.to(device)
|
| 178 |
+
|
| 179 |
+
_LLM.update({"ok": True, "model": mdl, "tokenizer": tok, "device": device})
|
| 180 |
+
return True, None
|
| 181 |
+
except Exception as e:
|
| 182 |
+
_LLM["err"] = f"{e}"
|
| 183 |
+
_LLM["ok"] = False
|
| 184 |
+
return False, _LLM["err"]
|
| 185 |
+
|
| 186 |
+
def generate_ai_text(user_prompt: str) -> str:
|
| 187 |
+
ok, err = _ensure_llm()
|
| 188 |
+
if not ok:
|
| 189 |
+
return ("๐ค AI ๅฐๆชๅ็จ๏ผ็ผบๅฐไพ่ณดๆๆจกๅๆชไธ่ผใ\n"
|
| 190 |
+
"่ซๅจ requirements.txt ๅ ๅ
ฅ๏ผtransformersใtorchใaccelerateใsafetensorsใ\n"
|
| 191 |
+
f"่ฉณ็ดฐ้ฏ่ชค๏ผ{err}")
|
| 192 |
+
|
| 193 |
+
tok = _LLM["tokenizer"]
|
| 194 |
+
mdl = _LLM["model"]
|
| 195 |
+
device = _LLM["device"]
|
| 196 |
+
|
| 197 |
+
max_new = int(os.getenv("LLM_MAX_NEW_TOKENS", "120"))
|
| 198 |
+
top_k = int(os.getenv("LLM_TOP_K", "50"))
|
| 199 |
+
temperature = float(os.getenv("LLM_TEMPERATURE", "0.7"))
|
| 200 |
+
|
| 201 |
+
# ๅปบ็ซ็ณป็ตฑๅๆ็คบ๏ผ่ฎๅ็ญๆด่ฒผ่ฟๆฉๅจไบบไธป้ก
|
| 202 |
+
system_prefix = (
|
| 203 |
+
"ไฝ ๆฏไธๅๅฐ้่ณ่จ่ไธ่ฌๅ็ญ็ LINE ๅฉ็ใๅ็ญ่ฆ็ฒพ็ฐกใๆธ
ๆฅ๏ผ"
|
| 204 |
+
"่ฅ่ๅฐ้็ธ้ๅฏๅ ๅ
ฅๆณจๆไบ้
ใ่ฅๅ้ก่ๅฐ้็ก้๏ผๅฐฑไธ่ฌๅ่ฆใ\n\n"
|
| 205 |
+
"ไฝฟ็จ่
๏ผ"
|
| 206 |
+
)
|
| 207 |
+
prompt = system_prefix + user_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(
|
| 214 |
+
input_ids=inputs["input_ids"],
|
| 215 |
+
attention_mask=inputs.get("attention_mask"),
|
| 216 |
+
max_new_tokens=max_new,
|
| 217 |
+
do_sample=True,
|
| 218 |
+
top_k=top_k,
|
| 219 |
+
temperature=temperature,
|
| 220 |
+
pad_token_id=tok.eos_token_id,
|
| 221 |
+
)
|
| 222 |
+
text = tok.decode(output[0], skip_special_tokens=True)
|
| 223 |
+
# ๅ่ฉฆๆ system_prefix ๅปๆ๏ผ็ไธไฝฟ็จ่
ๅพ็ๅๆ
|
| 224 |
+
if system_prefix in text:
|
| 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 |
+
# 3) ไธญๆๅญๅ๏ผ่ฅๅฎนๅจๆ Noto/WenQuanYi ็ญๆ่ชๅๅฅ็จ๏ผ
|
| 236 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 237 |
for fp in [
|
| 238 |
"/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",
|
|
|
|
| 245 |
break
|
| 246 |
|
| 247 |
# โโโโโโโโโโโโโโโโโโโโ๏ฟฝ๏ฟฝโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 248 |
+
# 4) ๅบๆฌ่จญๅฎ
|
| 249 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 250 |
CHANNEL_ACCESS_TOKEN = os.getenv("CHANNEL_ACCESS_TOKEN")
|
| 251 |
CHANNEL_SECRET = os.getenv("CHANNEL_SECRET")
|
|
|
|
| 268 |
handler = WebhookHandler(CHANNEL_SECRET)
|
| 269 |
|
| 270 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 271 |
+
# 5) ้ฆ้ ๏ผ็พ่ง่ชชๆ้ ๏ผ
|
| 272 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 273 |
@app.route("/", methods=["GET"])
|
| 274 |
def home():
|
|
|
|
| 321 |
<section class="hero">
|
| 322 |
<div class="badge">็ๆ
๏ผ<span style="color:{'#86efac' if channel_ok=='โ
' else '#fbbf24'}">{channel_ok}</span> LINE ้้ฐใยทใHF Space๏ผ{space_ok}</div>
|
| 323 |
<h1 class="title">ๅฐ้้ ่ญฆ dayichen โ LINE Bot</h1>
|
| 324 |
+
<p class="subtitle">ๆไปค๏ผ/helpใๅฐ้/quakeใ่บ็ฃๅฐ้/ๅฐ็ฃๅฐ้ใ่บ็ฃๅฐ้็ซๅ/ๅฐ็ฃๅฐ้็ซๅใๅฐ้้ ่ญฆใAI ๅฐ่ฉฑ๏ผai + ๅ้ก๏ผใ</p>
|
| 325 |
<div class="grid">
|
| 326 |
<div class="card">
|
| 327 |
<h3>๐ ๅฟซ้้ๅง</h3>
|
| 328 |
<ul>
|
| 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">
|
|
|
|
| 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>
|
|
|
|
| 367 |
return send_from_directory(STATIC_DIR, filename)
|
| 368 |
|
| 369 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 370 |
+
# 6) ่ณๆๆฅ่ฉข๏ผUSGS๏ผ
|
| 371 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 372 |
USGS_API_BASE_URL = "https://earthquake.usgs.gov/fdsnws/event/1/query"
|
| 373 |
|
|
|
|
| 431 |
return f"โ ๆฅ่ฉขๅคฑๆ: {e}"
|
| 432 |
|
| 433 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 434 |
+
# 7) ่ฃฝๅ
|
| 435 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 436 |
def create_and_save_map(df: pd.DataFrame) -> str:
|
| 437 |
fig, ax = plt.subplots(figsize=(9, 6), dpi=150)
|
|
|
|
| 464 |
return HF_SPACE_URL.rstrip("/") if HF_SPACE_URL else request.url_root.rstrip("/")
|
| 465 |
|
| 466 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 467 |
+
# 8) LINE Webhook
|
| 468 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 469 |
@app.route("/callback", methods=["POST"])
|
| 470 |
def callback():
|
|
|
|
| 478 |
|
| 479 |
@handler.add(MessageEvent, message=TextMessageContent)
|
| 480 |
def handle_message(event):
|
| 481 |
+
user_message_raw = (event.message.text or "").strip()
|
| 482 |
+
user_message = user_message_raw.lower()
|
| 483 |
+
|
| 484 |
with ApiClient(configuration) as api_client:
|
| 485 |
line_bot_api = MessagingApi(api_client)
|
| 486 |
|
| 487 |
+
# 1) ๅฐ้้ ่ญฆ๏ผCWA๏ผ
|
| 488 |
if "ๅฐ้้ ่ญฆ" in user_message:
|
| 489 |
reply_text = fetch_cwa_alarm_list(limit=5)
|
| 490 |
line_bot_api.reply_message_with_http_info(
|
|
|
|
| 492 |
)
|
| 493 |
return
|
| 494 |
|
| 495 |
+
# 2) ่บ็ฃๅฐ้็ซๅ
|
| 496 |
if ("่บ็ฃๅฐ้็ซๅ" in user_message) or ("ๅฐ็ฃๅฐ้็ซๅ" in user_message):
|
| 497 |
result = fetch_taiwan_df_this_year()
|
| 498 |
if isinstance(result, pd.DataFrame):
|
|
|
|
| 510 |
line_bot_api.reply_message_with_http_info(reply)
|
| 511 |
return
|
| 512 |
|
| 513 |
+
# 3) ่ชชๆ
|
| 514 |
if user_message == "/help":
|
| 515 |
text = (
|
| 516 |
"๐ ๅฐ้้ ่ญฆ dayichen ๆไปค\n\n"
|
|
|
|
| 519 |
"โก๏ธ ่บ็ฃๅฐ้ / ๅฐ็ฃๅฐ้๏ผไปๅนดๅฐ็ฃๅๅๆธ
ๅฎ๏ผ\n"
|
| 520 |
"โก๏ธ ่บ็ฃๅฐ้็ซๅ / ๅฐ็ฃๅฐ้็ซๅ๏ผไปๅนดๅฐ็ฃๅๅๅไฝๅ๏ผ\n"
|
| 521 |
"โก๏ธ ๅฐ้้ ่ญฆ๏ผCWA ๆๆฐ 5 ็ญ๏ผ\n"
|
| 522 |
+
"โก๏ธ ai ไฝ ็ๅ้ก๏ผAI ๅฐ่ฉฑ๏ผ\n"
|
| 523 |
"โก๏ธ ไฝ ๅฅฝ"
|
| 524 |
)
|
| 525 |
line_bot_api.reply_message_with_http_info(
|
|
|
|
| 527 |
)
|
| 528 |
return
|
| 529 |
|
| 530 |
+
# 4) ๅฐ็ฃๆธ
ๅฎ
|
| 531 |
if ("่บ็ฃๅฐ้" in user_message) or ("ๅฐ็ฃๅฐ้" in user_message):
|
| 532 |
result = fetch_taiwan_df_this_year()
|
| 533 |
if isinstance(result, pd.DataFrame):
|
|
|
|
| 546 |
)
|
| 547 |
return
|
| 548 |
|
| 549 |
+
# 5) ๅ
จ็ 24 ๅฐๆ
|
| 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 |
)
|
| 555 |
return
|
| 556 |
|
| 557 |
+
# 6) AI ๅฐ่ฉฑ๏ผ่งธ็ผ่ฉ๏ผไปฅ "ai "ใ"ai๏ผ"ใ"ai:" ้้ ญ๏ผๆๆๅพๅ
ๅบ๏ผ
|
| 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 |
+
# 7) ๆๅผ
|
| 569 |
if ("ไฝ ๅฅฝ" in user_message) or ("hi" in user_message):
|
| 570 |
line_bot_api.reply_message_with_http_info(
|
| 571 |
ReplyMessageRequest(
|
|
|
|
| 573 |
messages=[TextMessage(text="๐ ไฝ ๅฅฝ๏ผ่ผธๅ
ฅ /help ๆฅ็ๆไปคใ")]
|
| 574 |
)
|
| 575 |
)
|
| 576 |
+
return
|
| 577 |
+
|
| 578 |
+
# 8) ๅ
ๅบ๏ผ่ฅ้ไธ่ฟฐไปปไฝๆไปค๏ผไบค็ตฆ AI ็ข็ๅ่ฆ๏ผๆดๆบ่ฝ๏ผ
|
| 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)])
|
| 582 |
+
)
|