File size: 40,402 Bytes
190b31c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
import os
import re
import time
import random
import json
import html as html_lib
import subprocess
import requests
import ai_ext as base
from ai_ext import app
from fastapi import Request
from fastapi.responses import JSONResponse, HTMLResponse, FileResponse
from bs4 import BeautifulSoup
from urllib.parse import quote_plus

try:
    from PIL import Image, ImageDraw, ImageFont
except Exception:
    Image = ImageDraw = ImageFont = None


def _clean(s):
    s = html_lib.unescape(s or "")
    s = re.sub(r"[ \t]+", " ", s)
    s = re.sub(r"\n{3,}", "\n\n", s)
    return s.strip()


def _norm(s):
    s = s.lower()
    s = re.sub(r"[^\wÀ-ỹ\s]", " ", s)
    s = re.sub(r"\s+", " ", s).strip()
    return s


def _similar(a, b):
    ta = set(_norm(a).split())
    tb = set(_norm(b).split())
    if not ta or not tb:
        return False
    return len(ta & tb) / max(1, min(len(ta), len(tb))) >= 0.72


def _dedupe_units(units, max_units=7):
    out, seen = [], set()
    for u in units:
        u = _clean(re.sub(r"^[-•*\d\.\)\s]+", "", u))
        if len(u) < 18:
            continue
        nu = _norm(u)
        if nu in seen:
            continue
        if any(_similar(u, old) for old in out):
            continue
        seen.add(nu)
        out.append(u)
        if len(out) >= max_units:
            break
    return out


def _postprocess_ai_text(text, max_units=7):
    text = _clean(text)
    if not text:
        return text
    drop_prefixes = (
        "dưới đây", "sau đây", "bài viết", "tôi sẽ", "mình sẽ",
        "tóm tắt bài", "tiêu đề:", "sapo:", "nội dung:", "kết luận:"
    )
    raw_lines = []
    for line in re.split(r"\n+", text):
        line = _clean(line)
        if not line:
            continue
        low = line.lower().strip()
        if any(low.startswith(p) and len(line) < 80 for p in drop_prefixes):
            continue
        raw_lines.append(line)
    units = []
    for line in raw_lines:
        if len(line) > 260:
            units.extend(re.split(r"(?<=[\.\!\?])\s+(?=[A-ZÀ-Ỹ0-9])", line))
        else:
            units.append(line)
    units = _dedupe_units(units, max_units=max_units)
    if not units:
        return text[:900]
    title = ""
    if raw_lines and len(raw_lines[0]) <= 90 and not raw_lines[0].startswith(("-", "•", "*")):
        title = raw_lines[0]
        units = [u for u in units if not _similar(u, title)]
    body = "\n".join("• " + u for u in units[:max_units])
    return (title + "\n\n" + body).strip() if title else body


def _fallback_summary_from_prompt(prompt, max_units=6):
    text = prompt or ""
    for marker in ["Nội dung nguồn:", "Nội dung bài:", "Nội dung gốc:", "Nội dung:", "Nguồn/bối cảnh internet:"]:
        if marker in text:
            text = text.split(marker, 1)[1]
            break
    text = re.sub(r"https?://\S+", "", text)
    text = re.sub(r"\s+", " ", text).strip()
    sentences = re.split(r"(?<=[\.\!\?])\s+(?=[A-ZÀ-Ỹ0-9])", text)
    candidates = []
    for s in sentences:
        s = _clean(s)
        if 45 <= len(s) <= 260:
            candidates.append(s)
    units = _dedupe_units(candidates, max_units=max_units)
    if units:
        return "\n".join("• " + u for u in units)
    if text:
        return "• " + text[:700].rsplit(" ", 1)[0]
    return "• Không có đủ nội dung nguồn để tóm tắt."


def _source_line(sources):
    names = []
    for s in (sources or [])[:5]:
        via = s.get("via") or base._domain(s.get("url", "")) or s.get("title", "")
        if via and via not in names:
            names.append(via)
    return "Nguồn tham khảo: " + ", ".join(names[:5]) if names else "Nguồn tham khảo: tổng hợp internet"


def _make_summary_prompt(title, raw, source_hint=""):
    return f"""Bạn là biên tập viên tóm tắt tin tức tiếng Việt.

NHIỆM VỤ BẮT BUỘC:
- Chỉ TÓM TẮT nội dung chính, KHÔNG viết lại toàn bộ bài.
- Không lặp lại cùng một ý, cùng một câu, cùng một chi tiết.
- Không thêm thông tin ngoài nguồn.
- Tối đa 5 gạch đầu dòng, mỗi gạch đầu dòng 1 câu ngắn.
- Nếu bài có số liệu/nhân vật/thời điểm quan trọng thì giữ lại.
- Không viết phần mở bài dài, không viết văn kể lại.

Tiêu đề nguồn: {title}
Nguồn: {source_hint}

Nội dung nguồn:
{raw[:14000]}
"""


def _direct_news_rss(topic, limit=10):
    out = []
    try:
        url = "https://news.google.com/rss/search?q=" + quote_plus(topic) + "&hl=vi&gl=VN&ceid=VN:vi"
        r = requests.get(url, headers=base.HEADERS, timeout=15)
        r.encoding = "utf-8"
        soup = BeautifulSoup(r.text, "xml")
        for it in soup.find_all("item")[:limit]:
            title = it.find("title").get_text(" ", strip=True) if it.find("title") else ""
            link = it.find("link").get_text(strip=True) if it.find("link") else ""
            src = it.find("source").get_text(" ", strip=True) if it.find("source") else base._domain(link)
            if title and link:
                out.append({"title": title, "url": link, "via": src, "excerpt": title})
    except Exception:
        pass
    return out


def _topic_source_articles(topic, limit=5):
    """Return actual scraped article bodies for a topic. Each source becomes one Wall AI post."""
    try:
        _ctx, sources = base.web_context(topic, limit=limit)
    except Exception:
        sources = []
    if not sources:
        sources = _direct_news_rss(topic, limit=10)
    out, seen = [], set()
    for s in (sources or [])[:limit * 3]:
        url = s.get("url") or ""
        if not url.startswith("http") or url in seen:
            continue
        seen.add(url)
        try:
            page = base.scrape_any_url(url)
            raw = (page.get("summary", "") + "\n" + page.get("text", "")).strip()
            if len(raw) < 180:
                continue
            title = page.get("title") or s.get("title") or url
            via = page.get("via") or s.get("via") or base._domain(url)
            out.append({
                "title": title,
                "url": url,
                "raw": raw,
                "image": page.get("image") or "",
                "via": via,
                "source": {"title": title, "url": url, "excerpt": raw[:700], "via": via}
            })
            if len(out) >= limit:
                break
        except Exception:
            continue
    if not out:
        for s in (sources or _direct_news_rss(topic, 6))[:limit]:
            title = s.get("title") or topic
            excerpt = s.get("excerpt") or s.get("description") or s.get("content") or title
            url = s.get("url", "")
            via = s.get("via") or base._domain(url)
            out.append({
                "title": title,
                "url": url,
                "raw": excerpt,
                "image": base.pollinations_image_url(title),
                "via": via,
                "source": {"title": title, "url": url, "excerpt": excerpt[:700], "via": via}
            })
    return out[:limit]


async def qwen_generate_resilient(prompt: str, image_url=None, max_tokens: int = 1200):
    errors = []
    token = base._hf_token()
    try:
        original = getattr(base, "_original_qwen_generate", None)
        if original:
            txt = await original(prompt, image_url=image_url, max_tokens=max_tokens)
            if txt:
                base.LAST_QWEN_ERROR = ""
                return txt
            if getattr(base, "LAST_QWEN_ERROR", ""):
                errors.append("sdk: " + str(base.LAST_QWEN_ERROR)[:260])
    except Exception as e:
        errors.append(f"sdk: {type(e).__name__}: {str(e)[:260]}")
    if token:
        models = []
        for m in [
            os.getenv("QWEN_VL_MODEL", ""),
            "Qwen/Qwen2.5-VL-7B-Instruct",
            "Qwen/Qwen2.5-VL-3B-Instruct",
            "Qwen/Qwen2.5-7B-Instruct",
            "Qwen/Qwen2.5-3B-Instruct",
            "Qwen/Qwen2.5-1.5B-Instruct",
        ]:
            if m and m not in models:
                models.append(m)
        headers = {"Authorization": "Bearer " + token, "Content-Type": "application/json"}
        for model in models:
            try:
                is_vl = "VL" in model and bool(image_url)
                user_content = ([{"type": "image_url", "image_url": {"url": image_url}}, {"type": "text", "text": prompt}] if is_vl else prompt)
                payload = {
                    "model": model,
                    "messages": [
                        {"role": "system", "content": "Bạn là biên tập viên AI tiếng Việt. Chỉ tóm tắt súc tích nội dung nguồn, không viết lại toàn bài, không lặp ý, không bịa chi tiết."},
                        {"role": "user", "content": user_content},
                    ],
                    "max_tokens": min(int(max_tokens or 900), 1400),
                    "temperature": 0.35,
                    "top_p": 0.85,
                }
                r = requests.post("https://router.huggingface.co/v1/chat/completions", headers=headers, json=payload, timeout=95)
                if r.status_code >= 300:
                    errors.append(f"{model}: HTTP {r.status_code} {r.text[:180]}")
                    continue
                j = r.json()
                txt = (j.get("choices", [{}])[0].get("message", {}).get("content") or "").strip()
                if txt:
                    base.LAST_QWEN_ERROR = ""
                    return txt
                errors.append(f"{model}: empty response")
            except Exception as e:
                errors.append(f"{model}: {type(e).__name__}: {str(e)[:220]}")
    else:
        errors.append("missing HF_TOKEN")
    base.LAST_QWEN_ERROR = " | ".join(errors[-6:]) or "Qwen unavailable; used extractive fallback"
    print("[qwen resilient fallback]", base.LAST_QWEN_ERROR)
    return _fallback_summary_from_prompt(prompt, max_units=6)


if not hasattr(base, "_original_qwen_generate"):
    base._original_qwen_generate = base.qwen_generate
base.qwen_generate = qwen_generate_resilient


@app.get('/api/wall')
def compat_wall():
    return JSONResponse({'posts': base._load_ai_wall()[:80]})


_PATCHED_PATHS = {
    ('/api/topic_post', 'POST'),
    ('/api/url_wall', 'POST'),
    ('/api/rewrite_share', 'POST'),
    ('/api/ai/short/{post_id}', 'POST'),
}
app.router.routes = [
    r for r in app.router.routes
    if not any(getattr(r, 'path', None) == p and m in getattr(r, 'methods', set()) for p, m in _PATCHED_PATHS)
]


@app.post('/api/topic_post')
async def compat_topic_post(request: Request):
    body = await request.json()
    topic = base._clean_text(body.get('topic', ''))
    if not topic:
        return JSONResponse({'error': 'missing topic'}, status_code=400)
    articles = _topic_source_articles(topic, limit=4)
    if not articles:
        return JSONResponse({'error': 'Không lấy được bài viết nguồn cho chủ đề này.'}, status_code=422)
    new_posts = []
    posts = base._load_ai_wall()
    for art in articles:
        prompt = f"""Tóm tắt RIÊNG bài viết nguồn sau để đăng Tường AI.

Chủ đề lọc: {topic}
Tiêu đề bài nguồn: {art['title']}
Nguồn: {art['via']}

Yêu cầu bắt buộc:
- Tóm tắt nội dung trong BÀI VIẾT này, không chỉ tiêu đề.
- Không trộn với bài khác.
- Không viết lại toàn bộ bài.
- Không lặp ý.
- 4-6 gạch đầu dòng, mỗi dòng 1 câu rõ ràng.
- Giữ số liệu/nhân vật/thời điểm quan trọng nếu có.

Nội dung bài:
{art['raw'][:14000]}"""
        text = await base.qwen_generate(prompt, image_url=art.get('image') or None, max_tokens=900)
        text = _postprocess_ai_text(text, max_units=6)
        src = [art['source']]
        if 'Nguồn tham khảo:' not in text:
            text += "\n\n" + _source_line(src)
        post = base.make_post(art['title'], text, art.get('image') or base.pollinations_image_url(art['title']), art.get('url') or '', 'topic_article', sources=src)
        new_posts.append(post)
    posts = new_posts + posts
    base._save_ai_wall(posts)
    return JSONResponse({'post': new_posts[0], 'posts': new_posts, 'count': len(new_posts)})


@app.post('/api/url_wall')
async def compat_url_wall(request: Request):
    body = await request.json()
    url = base._clean_text(body.get('url', ''))
    if not url.startswith('http'):
        return JSONResponse({'error': 'missing url'}, status_code=400)
    try:
        data = base.scrape_any_url(url)
    except Exception as e:
        return JSONResponse({'error': 'Không scrape được URL: ' + str(e)[:180]}, status_code=422)
    raw = (data.get('summary', '') + '\n' + data.get('text', '')).strip()
    if len(raw) < 120:
        return JSONResponse({'error': 'URL không có đủ nội dung để tóm tắt'}, status_code=422)
    prompt = _make_summary_prompt(data.get('title', ''), raw, data.get('via', '') or base._domain(url))
    text = await base.qwen_generate(prompt, image_url=data.get('image') or None, max_tokens=850)
    text = _postprocess_ai_text(text, max_units=6)
    src = [{'title': data.get('title'), 'url': url, 'excerpt': raw[:500], 'via': data.get('via') or base._domain(url)}]
    if 'Nguồn tham khảo:' not in text:
        text += "\n\n" + _source_line(src)
    post = base.make_post(data.get('title') or 'Bài viết', text, data.get('image') or '', url, 'url', sources=src)
    posts = base._load_ai_wall(); posts.insert(0, post); base._save_ai_wall(posts)
    return JSONResponse({'post': post})


@app.post('/api/rewrite_share')
async def compat_rewrite_share(request: Request):
    body = await request.json()
    url = base._clean_text(body.get('url', ''))
    if not url.startswith('http'):
        return JSONResponse({'error': 'missing url'}, status_code=400)
    try:
        data = base.scrape_any_url(url)
    except Exception as e:
        return JSONResponse({'error': 'Không đọc được bài viết: ' + str(e)[:180]}, status_code=422)
    raw = (data.get('summary', '') + '\n' + data.get('text', '')).strip()
    if len(raw) < 120:
        return JSONResponse({'error': 'Bài viết không đủ nội dung để tóm tắt'}, status_code=422)
    prompt = _make_summary_prompt(data.get('title', ''), raw, data.get('via', '') or base._domain(url))
    text = await base.qwen_generate(prompt, image_url=data.get('image') or None, max_tokens=850)
    text = _postprocess_ai_text(text, max_units=6)
    src = [{'title': data.get('title'), 'url': url, 'excerpt': raw[:500], 'via': data.get('via') or base._domain(url)}]
    if 'Nguồn tham khảo:' not in text:
        text += "\n\n" + _source_line(src)
    post = base.make_post(data.get('title') or 'Bài viết', text, data.get('image') or '', url, 'summary', sources=src)
    posts = base._load_ai_wall(); posts.insert(0, post); base._save_ai_wall(posts)
    return JSONResponse({'post': post})


def _emotion_script(text, emotion):
    text = _clean(text)
    if emotion == 'urgent':
        return 'Tin nhanh. ' + text
    if emotion == 'warm':
        return 'Câu chuyện đáng chú ý. ' + text
    if emotion == 'serious':
        return 'Bản tin nghiêm túc. ' + text
    if emotion == 'energetic':
        return 'Cập nhật nổi bật. ' + text
    return text


def _tts_script_smart(post, emotion):
    raw = base._short_script(post)
    raw = re.sub(r"^[•\-\*]\s*", "", raw, flags=re.M)
    raw = re.sub(r"\s*\n\s*", ". ", raw)
    raw = re.sub(r"([\.\!\?])\s*", r"\1\n", raw)
    raw = re.sub(r"\n{2,}", "\n", raw).strip()
    raw = _emotion_script(raw, emotion)
    if len(raw) > 1000:
        raw = raw[:1000]
        cut = max(raw.rfind("."), raw.rfind("!"), raw.rfind("?"))
        if cut > 350:
            raw = raw[:cut + 1]
    return raw


def _split_subtitle_sentences(script):
    parts = []
    for line in script.splitlines():
        line = _clean(line)
        if not line:
            continue
        for s in re.split(r"(?<=[\.\!\?])\s+", line):
            s = _clean(s)
            if 8 <= len(s) <= 140:
                parts.append(s)
    return parts[:12]


def _srt_time(sec):
    ms = int((sec - int(sec)) * 1000)
    sec = int(sec)
    h = sec // 3600
    m = (sec % 3600) // 60
    s = sec % 60
    return f"{h:02d}:{m:02d}:{s:02d},{ms:03d}"


def _write_srt(script, path, total_duration=30):
    subs = _split_subtitle_sentences(script)
    if not subs:
        subs = [script[:120]]
    dur = max(2.2, min(5.0, total_duration / max(1, len(subs))))
    cur = 0.3
    with open(path, 'w', encoding='utf-8') as f:
        for i, s in enumerate(subs, 1):
            start = cur
            end = cur + dur
            cur = end + 0.15
            f.write(f"{i}\n{_srt_time(start)} --> {_srt_time(end)}\n{s}\n\n")


def _wrap_text_px(draw, text, font, max_width, max_lines):
    words = _clean(text).split()
    lines, cur = [], ""
    for w in words:
        test = (cur + " " + w).strip()
        try:
            width = draw.textbbox((0, 0), test, font=font)[2]
        except Exception:
            width = len(test) * 20
        if width <= max_width:
            cur = test
        else:
            if cur:
                lines.append(cur)
            cur = w
        if len(lines) >= max_lines:
            break
    if cur and len(lines) < max_lines:
        lines.append(cur)
    return lines


def _make_short_frame_full(post, img_path, out_path):
    if Image is None:
        return base._make_short_frame(post, img_path, out_path)
    W, H = 1080, 1920
    bg = Image.new("RGB", (W, H), (14, 14, 14))
    try:
        im = Image.open(img_path).convert("RGB")
        target = (1080, 760)
        im_ratio = im.width / im.height
        target_ratio = target[0] / target[1]
        if im_ratio > target_ratio:
            new_h = target[1]
            new_w = int(new_h * im_ratio)
        else:
            new_w = target[0]
            new_h = int(new_w / im_ratio)
        im = im.resize((new_w, new_h))
        left = (new_w - target[0]) // 2
        top = (new_h - target[1]) // 2
        im = im.crop((left, top, left + target[0], top + target[1]))
        bg.paste(im, (0, 0))
    except Exception:
        pass
    draw = ImageDraw.Draw(bg)
    try:
        font_title = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 54)
        font_body = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 38)
        font_label = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 30)
    except Exception:
        font_title = font_body = font_label = None
    draw.rectangle((0, 720, W, H), fill=(14, 14, 14))
    margin = 48
    maxw = W - margin * 2
    draw.text((margin, 770), "VNEWS · Tường AI", fill=(92, 184, 122), font=font_label)
    y = 830
    for ln in _wrap_text_px(draw, post.get("title", ""), font_title, maxw, 4):
        draw.text((margin, y), ln, fill=(255, 255, 255), font=font_title)
        y += 66
    y += 18
    text = post.get("text", "")
    text = re.sub(r"Nguồn tham khảo:.*", "", text, flags=re.S).strip()
    body_lines = _wrap_text_px(draw, text, font_body, maxw, 14)
    for ln in body_lines:
        draw.text((margin, y), ln, fill=(220, 220, 220), font=font_body)
        y += 50
        if y > 1640:
            break
    bg.save(out_path, quality=92)




def _summary_segments_from_post(post, max_segments=7):
    raw = _clean(post.get('text') or post.get('title') or '')
    raw = re.sub(r'^Bản tin AI viết lại:\s*', '', raw, flags=re.I)
    raw = re.sub(r'Nguồn tham khảo:.*$', '', raw, flags=re.I|re.S).strip()
    lines=[]
    for ln in raw.splitlines():
        ln=_clean(re.sub(r'^[•\-\*\d\.\)\s]+','',ln))
        if not ln: continue
        low=ln.lower()
        if low.startswith(('điểm chính','tiêu đề','sapo','nguồn tham khảo')): continue
        if len(ln)>=18: lines.append(ln)
    if len(lines)<2:
        lines=[]
        for s in re.split(r'(?<=[\.\!\?])\s+', raw):
            s=_clean(s)
            if len(s)>=25: lines.append(s)
    segs=_dedupe_units(lines, max_units=max_segments)
    return segs[:max_segments] if segs else [post.get('title','Bản tin VNEWS')]


def _make_scene_frame(post, segment, idx, total, img_path, out_path, emotion='neutral'):
    if Image is None:
        return _make_short_frame_full(post, img_path, out_path)
    W,H=1080,1920
    bg=Image.new('RGB',(W,H),(10,10,10))
    try:
        im=Image.open(img_path).convert('RGB')
        ratio=im.width/max(1,im.height); target=W/H
        if ratio>target:
            nh=H; nw=int(nh*ratio)
        else:
            nw=W; nh=int(nw/ratio)
        cover=im.resize((nw,nh)); left=(nw-W)//2; top=(nh-H)//2
        cover=cover.crop((left,top,left+W,top+H))
        bg.paste(cover,(0,0))
        bg=Image.blend(bg, Image.new('RGB',(W,H),(0,0,0)), 0.50)
        hero_h=720; target=W/hero_h
        if ratio>target:
            nh=hero_h; nw=int(nh*ratio)
        else:
            nw=W; nh=int(nw/ratio)
        hero=im.resize((nw,nh)); left=(nw-W)//2; top=(nh-hero_h)//2
        hero=hero.crop((left,top,left+W,top+hero_h))
        bg.paste(hero,(0,0))
    except Exception:
        pass
    draw=ImageDraw.Draw(bg)
    try:
        font_brand=ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf',34)
        font_small=ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf',28)
        font_seg=ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf',58)
        font_title=ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf',34)
    except Exception:
        font_brand=font_small=font_seg=font_title=None
    draw.rectangle((0,680,W,H), fill=(12,12,12))
    dot_x=48; dot_y=742
    for i in range(total):
        fill=(92,184,122) if i==idx else (70,70,70)
        draw.rounded_rectangle((dot_x+i*38,dot_y,dot_x+i*38+24,dot_y+10), radius=5, fill=fill)
    draw.text((48,780),'VNEWS AI SHORT',fill=(110,231,143),font=font_brand)
    draw.rounded_rectangle((48,834,260,880), radius=20, fill=(28,70,45))
    draw.text((66,842),f'Đoạn {idx+1}/{total}',fill=(235,235,235),font=font_small)
    y=940; maxw=W-96
    for ln in _wrap_text_px(draw, segment, font_seg, maxw, 8):
        draw.text((48,y),ln,fill=(255,255,255),font=font_seg)
        y+=74
        if y>1500: break
    y2=1640
    draw.line((48,y2-22,W-48,y2-22),fill=(70,70,70),width=2)
    for ln in _wrap_text_px(draw, post.get('title',''), font_title, maxw, 3):
        draw.text((48,y2),ln,fill=(220,220,220),font=font_title)
        y2+=46
    bg.save(out_path, quality=92)


def _estimate_audio_duration(path, fallback=4.0):
    try:
        pr=subprocess.run(['ffprobe','-v','error','-show_entries','format=duration','-of','default=noprint_wrappers=1:nokey=1',path], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=20)
        return max(1.5, float((pr.stdout or b'').decode().strip() or fallback))
    except Exception:
        return fallback


@app.post('/api/ai/short/{post_id}')
async def patched_ai_short(post_id: str, request: Request):
    try:
        body = await request.json()
    except Exception:
        body = {}
    voice = str(body.get('voice', 'nu')).strip().lower()
    emotion = str(body.get('emotion', 'neutral')).strip().lower()
    speed = float(body.get('speed', 1.2) or 1.2)
    speed = max(0.85, min(1.35, speed))

    posts = base._load_ai_wall()
    post = next((p for p in posts if str(p.get('id')) == str(post_id)), None)
    if not post:
        return JSONResponse({'error': 'post not found'}, status_code=404)

    segments = _summary_segments_from_post(post, max_segments=7)
    seg_hash = hashlib.md5(('|'.join(segments)+voice+emotion+str(speed)).encode('utf-8')).hexdigest()[:8]
    os.makedirs(base.SHORTS_DIR, exist_ok=True)
    suffix = f"_{voice}_{emotion}_{str(speed).replace('.', 'p')}_{seg_hash}_scenes_nosub"
    out_mp4 = os.path.join(base.SHORTS_DIR, base._safe_name(post_id + suffix) + '.mp4')
    if os.path.exists(out_mp4):
        post['video'] = '/api/ai/short-file/' + post_id + suffix
        post['short_voice'] = voice
        post['short_emotion'] = emotion
        post['short_speed'] = speed
        post['short_segments'] = segments
        post['short_subtitles'] = False
        base._save_ai_wall(posts)
        return JSONResponse({'video': post['video'], 'voice': voice, 'emotion': emotion, 'speed': speed, 'subtitles': False, 'segments': segments})
    if base.gTTS is None:
        return JSONResponse({'error': 'gTTS chưa sẵn sàng'}, status_code=503)

    work = os.path.join(base.SHORTS_DIR, base._safe_name(post_id + suffix))
    os.makedirs(work, exist_ok=True)
    img = os.path.join(work, 'image.jpg')
    try:
        base._download_image(post.get('img'), post.get('title', 'AI news'), img)
        edge_voice = {
            'nam': 'vi-VN-NamMinhNeural',
            'male': 'vi-VN-NamMinhNeural',
            'nu': 'vi-VN-HoaiMyNeural',
            'female': 'vi-VN-HoaiMyNeural',
            'mien-nam': 'vi-VN-HoaiMyNeural',
        }.get(voice, 'vi-VN-HoaiMyNeural')
        part_files=[]
        for idx, seg in enumerate(segments):
            frame=os.path.join(work,f'frame_{idx:02d}.jpg')
            aud=os.path.join(work,f'voice_{idx:02d}.mp3')
            aud_fast=os.path.join(work,f'voice_{idx:02d}_fast.mp3')
            part=os.path.join(work,f'part_{idx:02d}.mp4')
            _make_scene_frame(post, seg, idx, len(segments), img, frame, emotion=emotion)
            spoken=_emotion_script(seg, emotion)
            try:
                subprocess.run(['python','-m','edge_tts','--voice',edge_voice,'--text',spoken,'--write-media',aud], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=120)
            except Exception:
                tld='com.vn' if voice in ('nu','female','mien-nam') else 'com'
                try:
                    base.gTTS(spoken, lang='vi', tld=tld, slow=False).save(aud)
                except TypeError:
                    base.gTTS(spoken, lang='vi', slow=False).save(aud)
            subprocess.run(['ffmpeg','-y','-i',aud,'-filter:a',f'atempo={speed}','-vn',aud_fast], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=90)
            dur=_estimate_audio_duration(aud_fast, fallback=4.0)+0.35
            subprocess.run(['ffmpeg','-y','-loop','1','-t',str(dur),'-i',frame,'-i',aud_fast,'-shortest','-c:v','libx264','-tune','stillimage','-pix_fmt','yuv420p','-c:a','aac','-b:a','128k',part], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=150)
            part_files.append(part)
        concat=os.path.join(work,'concat.txt')
        with open(concat,'w',encoding='utf-8') as f:
            for p in part_files:
                f.write("file '" + p.replace("'", "'\\''") + "'\n")
        subprocess.run(['ffmpeg','-y','-f','concat','-safe','0','-i',concat,'-c','copy',out_mp4], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=180)
        post['video'] = '/api/ai/short-file/' + post_id + suffix
        post['short_voice'] = voice
        post['short_emotion'] = emotion
        post['short_speed'] = speed
        post['short_segments'] = segments
        post['short_subtitles'] = False
        base._save_ai_wall(posts)
        return JSONResponse({'video': post['video'], 'voice': voice, 'emotion': emotion, 'speed': speed, 'subtitles': False, 'segments': segments})
    except Exception as e:
        return JSONResponse({'error': 'Không tạo được shorts: ' + str(e)[:220]}, status_code=500)


@app.get('/api/ai/short-file/{file_id}')
def patched_ai_short_file(file_id: str):
    path = os.path.join(base.SHORTS_DIR, base._safe_name(file_id) + '.mp4')
    if not os.path.exists(path):
        return JSONResponse({'error': 'not found'}, status_code=404)
    return FileResponse(path, media_type='video/mp4', filename=f'vnews-ai-{file_id}.mp4')


@app.get('/api/ai_shorts')
def api_ai_shorts():
    posts = [p for p in base._load_ai_wall() if p.get('video')]
    return JSONResponse({'posts': posts[:80]})


app.router.routes = [r for r in app.router.routes if not (getattr(r, 'path', None) == '/' and 'GET' in getattr(r, 'methods', set()))]

PATCH_INJECT = r'''
<style>
.ai-wall-patched{margin:6px 4px;background:#1a1a1a;border:1px solid #2a2a2a;border-radius:8px;overflow:hidden}
.ai-wall-card{flex:0 0 250px;background:#141414;border:1px solid #2b2b2b;border-radius:10px;padding:8px}
.ai-wall-img{width:100%;aspect-ratio:16/9;background:#222;border-radius:8px;overflow:hidden;margin-bottom:6px}
.ai-wall-img img{width:100%;height:100%;object-fit:cover}
.ai-wall-title{font-size:12px;color:#5cb87a;font-weight:800;line-height:1.3;margin-bottom:4px}
.ai-wall-text{font-size:11px;color:#bbb;line-height:1.45;white-space:pre-wrap;display:-webkit-box;-webkit-line-clamp:5;-webkit-box-orient:vertical;overflow:hidden}
.ai-wall-actions{display:flex;gap:6px;margin-top:8px}
.ai-wall-actions button,.ai-wall-actions select{flex:1;border:1px solid #333;background:#222;color:#ddd;border-radius:14px;padding:6px 8px;font-size:10px;min-width:0}
.ai-wall-actions button.primary{background:#2d8659;border-color:#2d8659;color:#fff}
.ai-short-card{flex:0 0 145px}
.ai-short-video{width:100%;aspect-ratio:9/16;background:#000;border-radius:8px;overflow:hidden}
.ai-short-video video{width:100%;height:100%;object-fit:cover}
.ai-short-progress{position:fixed;inset:0;background:rgba(0,0,0,.78);z-index:99999;display:none;align-items:center;justify-content:center;padding:20px}
.ai-short-progress.active{display:flex}
.ai-short-box{max-width:420px;width:100%;background:#141414;border:2px solid #2d8659;border-radius:14px;padding:18px;color:#eee;box-shadow:0 0 30px rgba(45,134,89,.35)}
.ai-short-box h3{color:#5cb87a;margin-bottom:10px}
.ai-short-step{font-size:13px;line-height:1.55;color:#ccc}
.ai-short-spinner{width:34px;height:34px;border:4px solid #333;border-top-color:#5cb87a;border-radius:50%;animation:spin 1s linear infinite;margin:10px auto}
@keyframes spin{to{transform:rotate(360deg)}}
</style>
<div id="ai-short-progress" class="ai-short-progress"><div class="ai-short-box"><h3>🎬 Đang tạo Short AI</h3><div class="ai-short-spinner"></div><div class="ai-short-step" id="ai-short-step">Đang chuẩn bị...</div></div></div>
<script>
(function(){
function esc(s){return String(s||'').replace(/[&<>"']/g,m=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[m]));}
let patchedWall=[];let aiShorts=[];
function showProgress(msg){let box=document.getElementById('ai-short-progress');let st=document.getElementById('ai-short-step');if(st)st.innerHTML=msg;if(box)box.classList.add('active');}
function hideProgress(){document.getElementById('ai-short-progress')?.classList.remove('active');}
function updateAiLabels(){document.querySelectorAll('.ai-compose-title').forEach(e=>e.textContent='🤖 Tường AI: lọc từng bài theo chủ đề, tóm tắt nội dung bài');document.querySelectorAll('button').forEach(b=>{if((b.textContent||'').includes('AI viết lại'))b.textContent='🤖 Tóm tắt AI & đăng tường';});}
async function loadPatchedWall(){try{const r=await fetch('/api/ai_wall');const j=await r.json();patchedWall=j.posts||[];renderPatchedWall();updateAiLabels();}catch(e){}try{const r2=await fetch('/api/ai_shorts');const j2=await r2.json();aiShorts=j2.posts||[];renderAiShorts();}catch(e){}}
function renderAiShorts(){const home=document.getElementById('view-home');if(!home)return;document.getElementById('ai-shorts-patched')?.remove();if(!aiShorts.length)return;let wrap=document.createElement('div');wrap.id='ai-shorts-patched';wrap.className='ai-wall-patched';let h='<div class="slider-header"><span class="slider-label">🎬 Short AI</span><span class="slider-note">Video đã tạo</span></div><div class="slider-track">';aiShorts.slice(0,30).forEach((p,i)=>{h+=`<div class="ai-short-card" onclick="aiReadShortPatched(${i})"><div class="ai-short-video"><video src="${p.video}" muted playsinline preload="metadata"></video></div><div class="slider-title">${esc(p.title)}</div></div>`});h+='</div>';wrap.innerHTML=h;let wall=document.getElementById('ai-wall-patched');if(wall)wall.after(wrap);else home.prepend(wrap);}
function renderPatchedWall(){const home=document.getElementById('view-home');if(!home)return;document.getElementById('ai-wall-patched')?.remove();if(!patchedWall.length)return;let wrap=document.createElement('div');wrap.id='ai-wall-patched';wrap.className='ai-wall-patched';let h='<div class="slider-header"><span class="slider-label">🧱 Tường AI</span><span class="slider-note">Mỗi nguồn = một bài tóm tắt</span></div><div class="slider-track">';patchedWall.slice(0,30).forEach((p,i)=>{h+=`<div class="ai-wall-card"><div class="ai-wall-img">${p.img?`<img src="${p.img}">`:''}</div><div class="ai-wall-title">${esc(p.title)}</div><div class="ai-wall-text">${esc(p.text)}</div><div class="ai-wall-actions"><button onclick="aiReadWallPatched(${i})">Xem</button><button class="primary" onclick="aiMakeShortPatched(${i})">Shorts</button></div></div>`});h+='</div>';wrap.innerHTML=h;let after=document.querySelector('.ai-compose');if(after)after.after(wrap);else home.prepend(wrap);}
window.aiReadShortPatched=function(i){const p=aiShorts[i];if(!p)return;showView('view-article');let h=`<button class="back-btn" onclick="switchCat('home')">← Quay lại</button><div class="article-view"><span class="badge badge-ai">Short AI</span><h1 class="article-title">${esc(p.title)}</h1><video class="article-img" src="${p.video}" controls playsinline autoplay></video><p class="article-p" style="white-space:pre-wrap">${esc(p.text||'')}</p><div class="article-actions"><button onclick="window.open('${p.video}','_blank')">⬇ Mở video</button>${p.url?`<button onclick="window.open('${p.url}','_blank')">🔗 Nguồn</button>`:''}</div></div>`;document.getElementById('view-article').innerHTML=h;window.scrollTo(0,0)};
window.aiReadWallPatched=function(i){const p=patchedWall[i];if(!p)return;showView('view-article');let sources='';if(p.sources&&p.sources.length){sources='<div class="article-summary"><b>Nguồn tham khảo:</b><br>'+p.sources.slice(0,5).map(s=>`• ${esc(s.title||s.url||'Nguồn')} ${s.url?`(${esc(new URL(s.url).hostname.replace('www.',''))})`:''}`).join('<br>')+'</div>'}let voiceBox=`<div class="article-actions"><select id="ai-short-voice"><option value="nu">Giọng nữ Việt</option><option value="nam">Giọng nam Việt</option><option value="mien-nam">Giọng miền Nam</option></select><select id="ai-short-emotion"><option value="neutral">Trung tính</option><option value="urgent">Tin nhanh</option><option value="warm">Ấm áp</option><option value="serious">Nghiêm túc</option><option value="energetic">Sôi nổi</option></select><button onclick="aiMakeShortPatched(${i})">🎬 Tạo video shorts</button></div>`;let h=`<button class="back-btn" onclick="switchCat('home')">← Quay lại</button><div class="article-view"><span class="badge badge-ai">AI</span><h1 class="article-title">${esc(p.title)}</h1>${p.img?`<img class="article-img" src="${p.img}">`:''}${sources}<p class="article-p" style="white-space:pre-wrap">${esc(p.text)}</p>${p.video?`<video class="article-img" src="${p.video}" controls playsinline></video>`:''}<div class="article-actions">${p.url?`<button onclick="window.open('${p.url}','_blank')">🔗 Nguồn</button>`:''}</div>${voiceBox}</div>`;document.getElementById('view-article').innerHTML=h;window.scrollTo(0,0)};
window.aiMakeShortPatched=async function(i){const p=patchedWall[i];if(!p)return;let voice=document.getElementById('ai-short-voice')?.value||'nu';let emotion=document.getElementById('ai-short-emotion')?.value||'neutral';let voiceName={nu:'Giọng nữ Việt',nam:'Giọng nam Việt','mien-nam':'Giọng miền Nam'}[voice]||voice;let emotionName={neutral:'Trung tính',urgent:'Tin nhanh',warm:'Ấm áp',serious:'Nghiêm túc',energetic:'Sôi nổi'}[emotion]||emotion;let ok=confirm(`Quy trình tạo short AI:\n\n1) Dùng ảnh đại diện của bài hoặc tạo ảnh minh họa nếu thiếu.\n2) Rút gọn nội dung tóm tắt thành kịch bản đọc ngắn.\n3) Tự ngắt câu theo dấu câu và xuống dòng hợp lý.\n4) Tạo giọng đọc tiếng Việt: ${voiceName}.\n5) Áp dụng cảm xúc/kịch bản: ${emotionName}.\n6) Tăng tốc giọng đọc 1.2 lần.\n7) Mỗi đoạn tóm tắt sẽ là một cảnh riêng theo thời lượng đọc.\n8) Không thêm phụ đề; video chỉ có chữ cảnh và giọng đọc.\n9) Sau khi xong, video xuất hiện ở slide "Short AI".\n\nQuá trình có thể mất 1-3 phút. Bạn muốn bắt đầu?`);if(!ok)return;try{showProgress(`Bước 1/5: Chuẩn bị ảnh và căn chữ full width...<br>Bước 2/5: Tạo kịch bản, tự ngắt câu/xuống dòng...<br>Bước 3/5: Tạo giọng đọc ${voiceName}, cảm xúc ${emotionName}.<br>Bước 4/5: Tăng tốc 1.2x và ghép từng cảnh riêng, không phụ đề.<br>Bước 5/5: Lưu vào slide "Short AI".`);const r=await fetch('/api/ai/short/'+p.id,{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({voice,emotion,speed:1.2})});const j=await r.json();if(!r.ok||j.error)throw new Error(j.error||'Lỗi');p.video=j.video;hideProgress();alert('Hoàn tất: video shorts đã được tạo và thêm vào slide "Short AI".');aiReadWallPatched(i);loadPatchedWall();}catch(e){hideProgress();alert('Không tạo được shorts: '+e.message)}};
window.createTopicPost=function(){let inp=document.getElementById('ai-topic-input');let topic=(inp&&inp.value||'').trim();if(!topic)return alert('Nhập chủ đề trước');fetch('/api/topic_post',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({topic})}).then(r=>r.json().then(j=>({ok:r.ok,j}))).then(({ok,j})=>{if(ok&&(j.posts||j.post)){let arr=j.posts||[j.post];patchedWall=arr.concat(patchedWall.filter(x=>!arr.find(y=>y.id===x.id)));renderPatchedWall();if(inp)inp.value='';alert(`Đã lọc và tóm tắt ${arr.length} bài viết theo chủ đề lên Tường AI`);}else alert(j.error||'Lỗi tạo bài')}).catch(e=>alert(e.message||'Lỗi tạo bài'));};
window.createUrlPost=function(){let inp=document.getElementById('ai-url-input');let url=(inp&&inp.value||'').trim();if(!url)return alert('Dán URL trước');if(!/^https?:\/\//i.test(url))return alert('URL cần bắt đầu bằng http:// hoặc https://');fetch('/api/url_wall',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({url})}).then(r=>r.json().then(j=>({ok:r.ok,j}))).then(({ok,j})=>{if(ok&&j.post){patchedWall=[j.post].concat(patchedWall.filter(x=>x.id!==j.post.id));renderPatchedWall();if(inp)inp.value='';alert('Đã tóm tắt URL và đăng lên Tường AI');}else alert(j.error||'Lỗi URL')}).catch(e=>alert(e.message||'Lỗi URL'));};
window.rewriteCurrentArticle=function(){if(!window._currentArticle&&typeof _currentArticle!=='undefined')window._currentArticle=_currentArticle;let cur=window._currentArticle||_currentArticle;if(!cur)return;let btn=document.querySelector('.article-actions button.primary');if(btn){btn.textContent='Đang tóm tắt...';btn.disabled=true}fetch('/api/rewrite_share',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({url:cur.url})}).then(r=>r.json().then(j=>({ok:r.ok,j}))).then(({ok,j})=>{if(ok&&j.post){document.getElementById('rewrite-result').innerHTML=`<div class="rewrite-box"><div class="rewrite-title">Đã tóm tắt và đăng Tường AI</div><div class="rewrite-text">${esc(j.post.text||'')}</div></div>`;patchedWall=[j.post].concat(patchedWall.filter(x=>x.id!==j.post.id));renderPatchedWall();alert('Đã tóm tắt lên Tường AI');}else alert(j.error||'Không tóm tắt được')}).catch(e=>alert(e.message||'Lỗi tóm tắt')).finally(()=>{if(btn){btn.textContent='🤖 Tóm tắt AI & đăng tường';btn.disabled=false}})};
setTimeout(loadPatchedWall,1500);setInterval(updateAiLabels,2000);
})();
</script>
'''

@app.get('/')
async def index_patched():
    with open('/app/static/index.html','r',encoding='utf-8') as f:
        html=f.read()
    return HTMLResponse(html.replace('</body>', PATCH_INJECT+'\n</body>'))