diff --git a/.docker_rebuild b/.docker_rebuild
deleted file mode 100644
index 5c33b151d250b810160383280bc4f93b8c935f0a..0000000000000000000000000000000000000000
--- a/.docker_rebuild
+++ /dev/null
@@ -1 +0,0 @@
-trigger
diff --git a/.huggingface/rebuild b/.huggingface/rebuild
deleted file mode 100644
index 5b52fc9f825e7d7997f32173b23d1b1f0aed0156..0000000000000000000000000000000000000000
--- a/.huggingface/rebuild
+++ /dev/null
@@ -1 +0,0 @@
-rebuild
\ No newline at end of file
diff --git a/.rebuild b/.rebuild
deleted file mode 100644
index 17835370e2ae5f81ddb51ddf98e462dff821b66b..0000000000000000000000000000000000000000
--- a/.rebuild
+++ /dev/null
@@ -1 +0,0 @@
-Rebuild: vtv_api.py VTVGo proxy (player thật, no XFO) cho tất cả kênh
\ No newline at end of file
diff --git a/.rebuild_flag b/.rebuild_flag
deleted file mode 100644
index fc13f38f3b6fb446eb5419f850e65c767c44cdf2..0000000000000000000000000000000000000000
--- a/.rebuild_flag
+++ /dev/null
@@ -1 +0,0 @@
-rebuild
diff --git a/.rebuild_v2 b/.rebuild_v2
deleted file mode 100644
index fc13f38f3b6fb446eb5419f850e65c767c44cdf2..0000000000000000000000000000000000000000
--- a/.rebuild_v2
+++ /dev/null
@@ -1 +0,0 @@
-rebuild
diff --git a/.restart_trigger b/.restart_trigger
deleted file mode 100644
index fc13f38f3b6fb446eb5419f850e65c767c44cdf2..0000000000000000000000000000000000000000
--- a/.restart_trigger
+++ /dev/null
@@ -1 +0,0 @@
-rebuild
diff --git a/CHANGELOG.md b/CHANGELOG.md
deleted file mode 100644
index 065430a415d73f8c8cf5cdae84396bd20b5e22d8..0000000000000000000000000000000000000000
--- a/CHANGELOG.md
+++ /dev/null
@@ -1,71 +0,0 @@
-# VNEWS v6.5 - Resilient Shorts Auto-Updater
-
-## Changes
-
-### Critical Fix: Shorts timeout and homepage load stability
-**Root cause**: YouTube shorts fetching in `main.py` using `scrape_shorts()` and `_yt_channel_shorts_requests()` could hang indefinitely when YouTube blocks requests or yt-dlp times out, causing:
-- Homepage `/api/shorts` endpoint to time out (30s limit)
-- Space to appear unresponsive on first load
-- No fallback when sources fail
-
-**Fix applied**:
-1. **shorts_updater.py** (NEW) — Resilient background updater:
- - Hard timeout (25s) per channel using subprocess isolation
- - Stale-while-revalidate pattern: returns cached data immediately, updates in background
- - Automatic fallback to hardcoded short URLs when all sources fail
- - Persistent storage in `/data/shorts_cache.json` for cache across restarts
- - Background scheduler runs every 10 minutes automatically
- - No blocking on first homepage load
-
-2. **_run.py** — Integrated resilient shorts endpoint:
- - Overrides `/api/shorts` with non-blocking version
- - Returns cached/fallback data in <100ms guaranteed
- - Triggers background update if cache is stale or empty
- - Never hangs - always returns valid JSON response
-
-3. **FALLBACK_SHORTS** — 6 hardcoded viral shorts as emergency fallback:
- - baodantri7941 (Dân trí) headlines
- - baosuckhoedoisongboyte (Sức khỏe & đời sống) stories
- - vtvnambo (VTV Nam Bộ) news
-
-### Benefits
-- Homepage loads in <2 seconds always
-- Shorts data auto-updates every 10 minutes
-- Never times out - graceful degradation to fallback
-- Persistent cache survives Space restarts
-- Uses bucket `bep40/VNEWS-storage` for cache storage
-
-### Channels monitored
-- baodantri7941 (Dân trí)
-- baosuckhoedoisongboyte (Sức khỏe & đời sống)
-- vtvnambo (VTV Nam Bộ)
-
----
-
-# VNEWS v5.1 - Rewrite Fix
-
-## Changes
-
-### Critical Fix: Rewrite button not creating posts on Tường AI
-**Root cause**: `_run.py` imports from `app_v2_entry.py`, but the `/api/rewrite_share` endpoint was only defined in `ai_runtime_patch_fast.py` (loaded through `app_entry.py` which is NOT used). The frontend called a non-existent endpoint → 404 → silent failure.
-
-**Fix applied**:
-1. **app_v2_entry.py** — Added 3 new endpoints:
- - `POST /api/rewrite_slide` — Fast extractive summary (no AI needed), creates slides from article key points + images, saves to wall
- - `POST /api/rewrite_share` — AI-powered rewrite with extractive fallback, saves to wall
- - `POST /api/url_wall` — URL submission endpoint (alias for rewrite_share)
- - All endpoints use the same `_load_wall_posts()` / `_save_wall_posts()` and `WALL_FILE` path as the existing `/api/wall` endpoint
-
-2. **static/index_v2.html** — Added `` to load the rewrite fix
-
-3. **static/rewrite_fix_v2.js** — New file that overrides `rewriteArticle()` to:
- - Call `/api/rewrite_slide` first (fast, no AI needed)
- - Fallback to `/api/rewrite_share` if slide fails
- - Show slide preview overlay after successful post
- - Use `prependWallPost()` to add the new post to Tường AI
-
-### Previous changes (v5)
-- Rewrote match_detail_v2.py with correct event parsing
-- 2-tab layout for match detail (stats + timeline)
-- Fixed _run.py import
-- Dockerfile cache busting
diff --git a/Dockerfile b/Dockerfile
index ad8359efe8f5b01a42240fae0ba5d17538eaafe6..f6851b59cd237e28dd4aafa5101281c1d20838e3 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,16 +1,14 @@
-FROM python:3.12-slim
+FROM python:3.11-slim
WORKDIR /app
RUN apt-get update && apt-get install -y --no-install-recommends ffmpeg fonts-dejavu-core && rm -rf /var/lib/apt/lists/*
-RUN pip install --no-cache-dir "beautifulsoup4>=4.12" lxml
-RUN pip install --no-cache-dir fastapi uvicorn requests beautifulsoup4 jinja2 yt-dlp huggingface_hub gTTS pillow edge-tts python-dateutil httpx
-
-COPY requirements.txt .
-RUN pip install --no-cache-dir -r requirements.txt || true
+RUN pip install --no-cache-dir fastapi uvicorn requests beautifulsoup4 lxml jinja2 yt-dlp huggingface_hub gTTS pillow edge-tts python-dateutil httpx
COPY . .
+
+RUN echo 'from app_v2_entry import app; import rewrite_slide' > _run.py
+
EXPOSE 7860
CMD ["uvicorn", "_run:app", "--host", "0.0.0.0", "--port", "7860"]
-# v2
\ No newline at end of file
diff --git a/README.md b/README.md
index 8f9526538a80ace404f12e119c3bd2d0e940b957..ebcd71c672947f56bc568d773f5762789310a420 100644
--- a/README.md
+++ b/README.md
@@ -9,25 +9,5 @@ tags:
- ml-intern
---
-# VNEWS - Tin Tức Việt Nam
-
-**v18 - FIXED VTV2/VTV3/VTV6/VTV9 stream hanging**
-
-## 🔧 Changes in v18 (2026-07-06)
-- **VTV2, VTV3, VTV6, VTV9**: Skip expired ssaimh CDN token → immediately fall through to sv2.xemtivitop.com
-- **15+ extraction patterns** for m3u8 URL (up from 5), including: file:, src=, source:, player.src(), hls.loadSource(), href=, ``, url:, window.location, iframe follow (3 levels deep), base64 decode
-- **Backup CDN** `tv.mediacdn.vn` for VTV2/VTV3/VTV6/VTV9
-- **Fast timeout** 5s for CDN, 12s for PHP endpoints (was 15s each = 60s+ total)
-- **sv2.xemtivitop.com** re-prioritized to check BEFORE xemtv.us
-- **Iframe chain following**: if a PHP page returns an iframe → follow it up to 3 levels to find the m3u8
-
-## Features:
-- 📰 News from VnExpress (10 categories) + GenK AI
-- ⚽ Livescore from bongda.com.vn (live, today, upcoming, results, standings)
-- 🎬 Football highlights from xemlaibongda.top (8 leagues)
-- 📺 VTV live channels (VTV1→VTV10, VTV Prime)
- - Priority: ssaimh CDN → sv2.xemtivitop.com → xemtv.us → xemtivitop blogspot → FPTPlay → VTVGo → mediacdn → xemtv.net
-- 🏆 World Cup 2026 (news, fixtures, standings, stats, highlights)
-- 🤖 AI article writing + TTS (multilingual, emotion-aware)
-- 🔍 Topic search (8 news sources)
-- 🎤 TTS: voice selector + emotion selector + speed control
\ No newline at end of file
+# bep40/vnews
+
diff --git a/_run.py b/_run.py
deleted file mode 100644
index de72380c3006dba490ec49c1fe421c87723a0490..0000000000000000000000000000000000000000
--- a/_run.py
+++ /dev/null
@@ -1 +0,0 @@
-from app_v2_entry import app # v5-stable inline bongda proxy
\ No newline at end of file
diff --git a/ai_ext.py b/ai_ext.py
index cf4b1d0ff3215baa8ed3f05887a252c47d56bc90..3e88fa0767207c12dd9e1038bed2c9a306895b2f 100644
--- a/ai_ext.py
+++ b/ai_ext.py
@@ -13,16 +13,7 @@ from bs4 import BeautifulSoup
from fastapi import Request, Query
from fastapi.responses import HTMLResponse, JSONResponse, FileResponse
-# Try to import main app, but don't fail if it doesn't exist
-try:
- from main import app
-except ImportError:
- # Create a minimal FastAPI app for standalone testing
- try:
- from fastapi import FastAPI
- app = FastAPI()
- except Exception:
- app = None
+from main import app
# Import wall store from main.py so we read/write the SAME file
try:
@@ -50,10 +41,6 @@ except ImportError:
def _web_context(topic):
return ""
-# ai_ext alias for backward compatibility
-_load_ai_wall = _load_wall
-_save_ai_wall = _save_wall
-
try:
from huggingface_hub import AsyncInferenceClient
except Exception:
@@ -73,150 +60,21 @@ except Exception:
def _hf_token():
- for k in ("HF_TOKEN", "HUGGINGFACE_HUB_API_TOKEN", "HUGGING_FACE_HUB_TOKEN", "HF_API_TOKEN"):
+ for k in ("HF_TOKEN", "HUGGINGFACEHUB_API_TOKEN", "HUGGING_FACE_HUB_TOKEN", "HF_API_TOKEN"):
v = os.getenv(k, "").strip()
if v:
return v
return ""
-
-def _clean_text(s: str) -> str:
- """Clean text for processing."""
- s = html_lib.unescape(s or "")
- s = re.sub(r"\s+", " ", s)
- return s.strip()
-
-
-def _domain(url: str) -> str:
- """Extract domain from URL."""
- try:
- return urlparse(url or "").netloc.replace("www.", "")
- except Exception:
- return ""
-
-
-async def qwen_generate(prompt: str, image_url: str = None, max_tokens: int = 1200) -> str:
- """Generate text using Qwen models via Hugging Face Inference API.
-
- This function provides a resilient implementation that:
- 1. First tries the SDK-based inference client if available
- 2. Falls back to REST API calls to HF router endpoint
- 3. Returns a fallback summary if all else fails
- """
- token = _hf_token()
- errors = []
-
- # Try HF router API with multiple models
- if token:
- models = [
- 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",
- "Qwen/Qwen2.5-72B-Instruct",
- "meta-llama/Llama-3.3-70B-Instruct",
- ]
- # Deduplicate while preserving order
- seen = set()
- models = [m for m in models if m and m not in seen and not seen.add(m)]
-
- headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
-
- for model in models:
- try:
- is_vl = "VL" in model and image_url
- if is_vl:
- user_content = [
- {"type": "image_url", "image_url": {"url": image_url}},
- {"type": "text", "text": prompt}
- ]
- else:
- user_content = prompt
-
- payload = {
- "model": model,
- "messages": [
- {"role": "system", "content": "Bạn là trợ lý AI tiếng Việt. Trả lời tự nhiên, ngắn gọn, chính xác."},
- {"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}")
- continue
-
- j = r.json()
- txt = (j.get("choices", [{}])[0].get("message", {}).get("content") or "").strip()
-
- if txt:
- return txt
-
- errors.append(f"{model}: empty response")
-
- except Exception as e:
- errors.append(f"{model}: {type(e).__name__}")
-
- # Fallback: extractive summary from prompt
- LAST_QWEN_ERROR = errors[-3:] if errors else "unknown error"
- return _fallback_summary_from_prompt(prompt, max_units=6)
-
-
-def _fallback_summary_from_prompt(prompt: str, max_units: int = 6) -> str:
- """Generate a simple fallback summary when AI is unavailable."""
- 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()
-
- # Split into sentences - extract ALL valid sentences, not just first few
- sentences = re.split(r"(?<=[.!?])\s+(?=[A-ZÀ-Ỹ0-9])", text)
- units = []
- for s in sentences:
- s = _clean_text(s)
- if len(s) >= 30: # Lower threshold to capture more content
- units.append(s)
-
- if units:
- # Take up to max_units valid sentences
- result_units = units[:max_units]
- return "\n".join("• " + u for u in result_units)
- if text:
- # Fallback: take chunks if no sentence boundaries found
- chunks = []
- for i in range(0, min(len(text), max_units * 300), 280):
- chunk = _clean_text(text[i:i+300])
- if chunk and chunk not in chunks:
- chunks.append(chunk)
- if len(chunks) >= max_units:
- break
- if chunks:
- return "\n".join("• " + c for c in chunks)
- return "• Không có đủ nội dung để tóm tắt."
-
-
HF_TOKEN = _hf_token()
QWEN_VL_MODEL = os.getenv("QWEN_VL_MODEL", "Qwen/Qwen2.5-VL-7B-Instruct")
+# Fast TEXT models for summaries that don't need vision (much faster than the VL model).
QWEN_TEXT_MODELS = [m.strip() for m in os.getenv(
"QWEN_TEXT_MODELS",
"Qwen/Qwen2.5-72B-Instruct,meta-llama/Llama-3.3-70B-Instruct,Qwen/Qwen2.5-7B-Instruct"
).split(",") if m.strip()]
-_WORKING_MODEL_TEXT = None
-_WORKING_MODEL_VL = None
+_WORKING_MODEL_TEXT = None # cached last-working text model
+_WORKING_MODEL_VL = None # cached last-working vision model
DATA_DIR = "/data" if os.path.isdir("/data") else "/app/data"
SHORTS_DIR = os.path.join(DATA_DIR, "ai_shorts")
HEADERS = {
@@ -225,350 +83,1082 @@ HEADERS = {
}
LAST_QWEN_ERROR = ""
+# ===== TTS VOICE CONFIG =====
+# Multilingual neural voices grouped by country/language
+# Format: key -> {id, gender, name, country, lang, flag}
+TTS_VOICES = {
+ # === VIETNAM (edge-tts) ===
+ "hoaimy": {"id": "vi-VN-HoaiMyNeural", "gender": "female", "name": "Hoài My", "country": "Việt Nam", "lang": "vi", "flag": "🇻🇳", "engine": "edge"},
+ "namminh": {"id": "vi-VN-NamMinhNeural", "gender": "male", "name": "Nam Minh", "country": "Việt Nam", "lang": "vi", "flag": "🇻🇳", "engine": "edge"},
+ # === gTTS (Google, tiếng Việt cơ bản) ===
+ "gtts_vi": {"id": "gtts", "gender": "female", "name": "gTTS Google", "country": "Việt Nam", "lang": "vi", "flag": "🇻🇳", "engine": "gtts"},
+ # === MULTILINGUAL (đa ngôn ngữ — đọc được tiếng Việt + nhiều thứ tiếng) ===
+ "en_au_william": {"id": "en-AU-WilliamMultilingualNeural", "gender": "male", "name": "William (Đa NN)", "country": "Đa ngôn ngữ", "lang": "multi", "flag": "🌐", "engine": "edge"},
+ "en_us_andrew": {"id": "en-US-AndrewMultilingualNeural", "gender": "male", "name": "Andrew (Đa NN)", "country": "Đa ngôn ngữ", "lang": "multi", "flag": "🌐", "engine": "edge"},
+ "en_us_ava": {"id": "en-US-AvaMultilingualNeural", "gender": "female", "name": "Ava (Đa NN)", "country": "Đa ngôn ngữ", "lang": "multi", "flag": "🌐", "engine": "edge"},
+ "en_us_brian": {"id": "en-US-BrianMultilingualNeural", "gender": "male", "name": "Brian (Đa NN)", "country": "Đa ngôn ngữ", "lang": "multi", "flag": "🌐", "engine": "edge"},
+ "en_us_emma": {"id": "en-US-EmmaMultilingualNeural", "gender": "female", "name": "Emma (Đa NN)", "country": "Đa ngôn ngữ", "lang": "multi", "flag": "🌐", "engine": "edge"},
+ "fr_vivienne": {"id": "fr-FR-VivienneMultilingualNeural","gender": "female", "name": "Vivienne (Đa NN)","country": "Đa ngôn ngữ", "lang": "multi", "flag": "🌐", "engine": "edge"},
+ "fr_remy": {"id": "fr-FR-RemyMultilingualNeural", "gender": "male", "name": "Rémy (Đa NN)", "country": "Đa ngôn ngữ", "lang": "multi", "flag": "🌐", "engine": "edge"},
+ "de_seraphina": {"id": "de-DE-SeraphinaMultilingualNeural","gender": "female","name": "Seraphina (Đa NN)","country": "Đa ngôn ngữ","lang": "multi", "flag": "🌐", "engine": "edge"},
+ "de_florian": {"id": "de-DE-FlorianMultilingualNeural", "gender": "male", "name": "Florian (Đa NN)", "country": "Đa ngôn ngữ", "lang": "multi", "flag": "🌐", "engine": "edge"},
+ "it_giuseppe": {"id": "it-IT-GiuseppeMultilingualNeural","gender": "male", "name": "Giuseppe (Đa NN)","country": "Đa ngôn ngữ", "lang": "multi", "flag": "🌐", "engine": "edge"},
+ "ko_hyunsu": {"id": "ko-KR-HyunsuMultilingualNeural", "gender": "male", "name": "Hyunsu (Đa NN)", "country": "Đa ngôn ngữ", "lang": "multi", "flag": "🌐", "engine": "edge"},
+ "pt_thalita": {"id": "pt-BR-ThalitaMultilingualNeural", "gender": "female", "name": "Thalita (Đa NN)", "country": "Đa ngôn ngữ", "lang": "multi", "flag": "🌐", "engine": "edge"},
+}
+TTS_DEFAULT_VOICE = "hoaimy"
+TTS_DEFAULT_SPEED = 1.2 # 1.2x speed for faster reading
-# ===== MULTILINGUAL VOICES FOR TTS =====
-# Maps voice IDs to edge-tts voice names (only MultilingualNeural voices)
-MULTILINGUAL_VOICES = {
- # Vietnamese - Native voices
- "vi-vn-hoaimyneural": "vi-VN-HoaiMyNeural",
- "vi-vn-namminhneural": "vi-VN-NamMinhNeural",
- "hoaimy": "vi-VN-HoaiMyNeural",
- "namminh": "vi-VN-NamMinhNeural",
- "vi_female": "vi-VN-HoaiMyNeural",
- "vi_male": "vi-VN-NamMinhNeural",
- "nu": "vi-VN-HoaiMyNeural",
- "male": "vi-VN-NamMinhNeural",
- "female": "vi-VN-HoaiMyNeural",
- "mien-nam": "vi-VN-HoaiMyNeural",
- # English - Multilingual
- "en-us-andrewmultilingualneural": "en-US-AndrewMultilingualNeural",
- "en-au-williammultilingualneural": "en-AU-WilliamMultilingualNeural",
- "en_andrew": "en-US-AndrewMultilingualNeural",
- "andrew": "en-US-AndrewMultilingualNeural",
- "en_jenny": "en-US-AndrewMultilingualNeural",
- "jenny": "en-US-AndrewMultilingualNeural",
- # Portuguese - Thalita Multilingual ONLY
- "pt-br-thalitamultilingualneural": "pt-BR-ThalitaMultilingualNeural",
- "pt_thalita": "pt-BR-ThalitaMultilingualNeural",
- "thalita": "pt-BR-ThalitaMultilingualNeural",
- "pt_francisco": "pt-BR-ThalitaMultilingualNeural",
- "pt": "pt-BR-ThalitaMultilingualNeural",
- # French - Multilingual
- "fr-fr-viviennemultilingualneural": "fr-FR-VivienneMultilingualNeural",
- "fr-fr-remymultilingualneural": "fr-FR-RemyMultilingualNeural",
- "fr_denise": "fr-FR-VivienneMultilingualNeural",
- "denise": "fr-FR-VivienneMultilingualNeural",
- "fr": "fr-FR-VivienneMultilingualNeural",
- # German - Multilingual
- "de-de-seraphinamultilingualneural": "de-DE-SeraphinaMultilingualNeural",
- "de-de-florianmultilingualneural": "de-DE-FlorianMultilingualNeural",
- "de_katja": "de-DE-SeraphinaMultilingualNeural",
- "katja": "de-DE-SeraphinaMultilingualNeural",
- "de": "de-DE-SeraphinaMultilingualNeural",
- # Korean - Hyunsu Multilingual (NOT SunHee)
- "ko-kr-hyunsumultilingualneural": "ko-KR-HyunsuMultilingualNeural",
- "ko_sunhee": "ko-KR-HyunsuMultilingualNeural",
- "sunhee": "ko-KR-HyunsuMultilingualNeural",
- "ko": "ko-KR-HyunsuMultilingualNeural",
- # Italian - Multilingual
- "it-it-giuseppemultilingualneural": "it-IT-GiuseppeMultilingualNeural",
- # Spanish (fallback to English multilingual)
- "es_ela": "en-US-AndrewMultilingualNeural",
- "ela": "en-US-AndrewMultilingualNeural",
- "es_carlos": "en-US-AndrewMultilingualNeural",
- "es": "en-US-AndrewMultilingualNeural",
- # Japanese (fallback to English multilingual)
- "ja_nanami": "en-US-AndrewMultilingualNeural",
- "nanami": "en-US-AndrewMultilingualNeural",
- "ja": "en-US-AndrewMultilingualNeural",
- # Chinese (fallback to English multilingual)
- "zh_xiaochen": "en-US-AndrewMultilingualNeural",
- "xiaochen": "en-US-AndrewMultilingualNeural",
- "zh": "en-US-AndrewMultilingualNeural",
+# Topic -> voice mapping (auto-detect based on topic keywords)
+TOPIC_VOICE_MAP = {
+ # Sports -> male voice
+ "bóng đá": "namminh", "thể thao": "namminh", "world cup": "namminh",
+ "premier league": "namminh", "champions league": "namminh", "la liga": "namminh",
+ "serie a": "namminh", "bundesliga": "namminh", "v-league": "namminh",
+ "tennis": "namminh", "olympic": "namminh", "f1": "namminh", "moto": "namminh",
+ # Lifestyle/Health/Entertainment -> female voice
+ "sức khỏe": "hoaimy", "làm đẹp": "hoaimy", "giải trí": "hoaimy",
+ "âm nhạc": "hoaimy", "phim": "hoaimy", "thời trang": "hoaimy",
+ "ẩm thực": "hoaimy", "du lịch": "hoaimy", "gia đình": "hoaimy",
+ "tình yêu": "hoaimy", "hôn nhân": "hoaimy", "mẹ và bé": "hoaimy",
+ # Tech/Science -> male voice
+ "công nghệ": "namminh", "ai": "namminh", "robot": "namminh",
+ "khoa học": "namminh", "vũ trụ": "namminh", "điện thoại": "namminh",
+ "laptop": "namminh", "game": "namminh",
+ # News/Politics/Economy -> male voice
+ "chính trị": "namminh", "kinh tế": "namminh", "tài chính": "namminh",
+ "chứng khoán": "namminh", "ngân hàng": "namminh", "thị trường": "namminh",
+ "xã hội": "namminh", "pháp luật": "namminh", "giáo dục": "namminh",
}
-def _detect_voice_emotion(title, text):
- """Detect appropriate voice and emotion based on content for multilingual TTS."""
- content = ((title or "") + " " + (text or "")).lower()
-
- # World Cup / Football content - use Andrew multilingual
- if any(kw in content for kw in ["world cup", "wc 2026", "fifa", "bóng đá", "trận đấu", "bóng bóng", "đội tuyển", "cầu thủ"]):
- return ("andrew", "excited")
-
- # News categories - choose appropriate voice
- if any(kw in content for kw in ["kinh tế", "tài chính", "thị trường", "economics", "finance"]):
- return ("jenny", "calm")
- if any(kw in content for kw in ["thiên tai", "bão", "lũ lụt", "cháy nổ", "tai nạn", "disaster", "accident"]):
- return ("thalita", "serious")
- if any(kw in content for kw in ["giải trí", "showbiz", "entertainment", "hài hước"]):
- return ("ela", "happy")
- if any(kw in content for kw in ["công nghệ", "tech", "technology", "ai", "trí tuệ nhân tạo"]):
- return ("katja", "excited")
-
- # Default Vietnamese
- return ("hoaimy", "trung_tinh")
+def _detect_voice_for_topic(title: str, text: str) -> str:
+ """Auto-detect the best voice based on topic keywords."""
+ combined = (title + " " + text[:500]).lower()
+ for keyword, voice_id in TOPIC_VOICE_MAP.items():
+ if keyword in combined:
+ return voice_id
+ return TTS_DEFAULT_VOICE
-def _safe_name(s: str) -> str:
- """Create safe filename from string."""
- s = re.sub(r"[^\w\-.]", "_", s)
- return s[:100] if len(s) > 100 else s
+# ===== EMOTION (CẢM XÚC) FOR TTS =====
+# edge-tts does NOT support Azure express-as styles, so emotion is simulated
+# with pitch + rate(speed multiplier) + volume. Each preset is a delta.
+# rate_mul multiplies the user/auto speed; pitch is absolute Hz; volume is percent.
+EMOTION_PRESETS = {
+ "vui": {"label": "Vui tươi", "emoji": "😊", "rate_mul": 1.06, "pitch": "+15Hz", "volume": "+6%"},
+ "hao_hung": {"label": "Hào hứng", "emoji": "🔥", "rate_mul": 1.12, "pitch": "+24Hz", "volume": "+12%"},
+ "nghiem": {"label": "Nghiêm túc", "emoji": "📰", "rate_mul": 1.00, "pitch": "-3Hz", "volume": "+0%"},
+ "tram": {"label": "Trầm ấm", "emoji": "🌙", "rate_mul": 0.94, "pitch": "-10Hz", "volume": "+0%"},
+ "buon": {"label": "Buồn/Xúc động", "emoji": "💧", "rate_mul": 0.88, "pitch": "-18Hz", "volume": "-4%"},
+ "trung_tinh":{"label": "Trung tính", "emoji": "🎙️", "rate_mul": 1.00, "pitch": "+0Hz", "volume": "+0%"},
+}
+EMOTION_DEFAULT = "trung_tinh"
+# Topic keyword -> emotion. Checked in order; first match wins.
+TOPIC_EMOTION_MAP = {
+ # Sports / wins -> excited
+ "chiến thắng": "hao_hung", "vô địch": "hao_hung", "world cup": "hao_hung",
+ "bóng đá": "hao_hung", "thể thao": "hao_hung", "ghi bàn": "hao_hung",
+ "champions league": "hao_hung", "premier league": "hao_hung", "chung kết": "hao_hung",
+ "olympic": "hao_hung", "kỷ lục": "hao_hung",
+ # Entertainment / lifestyle / good news -> cheerful
+ "giải trí": "vui", "âm nhạc": "vui", "phim": "vui", "lễ hội": "vui",
+ "du lịch": "vui", "ẩm thực": "vui", "thời trang": "vui", "ra mắt": "vui",
+ "khai trương": "vui", "tin vui": "vui", "hạnh phúc": "vui",
+ # Sad / accidents / loss -> sad
+ "tai nạn": "buon", "qua đời": "buon", "tử vong": "buon", "thiệt mạng": "buon",
+ "động đất": "buon", "lũ lụt": "buon", "thiên tai": "buon", "cháy": "buon",
+ "tang lễ": "buon", "mất tích": "buon", "thương tâm": "buon",
+ # Health / science / calm -> calm warm
+ "sức khỏe": "tram", "y tế": "tram", "bệnh": "tram", "dinh dưỡng": "tram",
+ "tâm lý": "tram", "thiền": "tram", "giấc ngủ": "tram",
+ # News / politics / economy / law -> serious
+ "chính trị": "nghiem", "kinh tế": "nghiem", "tài chính": "nghiem",
+ "chứng khoán": "nghiem", "pháp luật": "nghiem", "tòa án": "nghiem",
+ "ngân hàng": "nghiem", "thị trường": "nghiem", "lạm phát": "nghiem",
+ "công nghệ": "nghiem", "ai": "nghiem", "khoa học": "nghiem", "giáo dục": "nghiem",
+}
-def _download_image(url: str, fallback_title: str, out_path: str) -> bool:
- """Download image from URL to path."""
- if not url:
- return False
- try:
- r = requests.get(url, headers=HEADERS, timeout=15)
- if r.status_code == 200:
- os.makedirs(os.path.dirname(out_path), exist_ok=True)
- with open(out_path, "wb") as f:
- f.write(r.content)
- return True
- except Exception:
- pass
- return False
+def _detect_emotion_for_topic(title: str, text: str) -> str:
+ """Auto-detect emotion (cảm xúc) from topic/content keywords."""
+ combined = (title + " " + text[:600]).lower()
+ for keyword, emo in TOPIC_EMOTION_MAP.items():
+ if keyword in combined:
+ return emo
+ return EMOTION_DEFAULT
-def pollination_image_url(topic: str) -> str:
- """Generate image URL from Pollinations.ai."""
- return f"https://image.pollinations.ai/prompt/{quote(topic)}?width=1024&height=768&nologo=true&model=flux"
+def _detect_voice_emotion(title: str, text: str) -> tuple:
+ """Return (voice_id, emotion_id) auto-chosen for the article's topic."""
+ return _detect_voice_for_topic(title, text), _detect_emotion_for_topic(title, text)
-# Use the same wall file as app_v2_entry.py for consistency
-WALL_FILE = os.path.join(DATA_DIR, "wall_posts.json")
-def _load_ai_wall():
- """Load AI wall posts from JSON file (uses wall_posts.json for consistency with app_v2_entry)."""
+# ===== TEXT HELPERS =====
+def _clean_text(s: str) -> str:
+ s = html_lib.unescape(s or "")
+ return re.sub(r"\s+", " ", s).strip()
+
+def _domain(u):
try:
- if os.path.exists(WALL_FILE):
- with open(WALL_FILE, "r", encoding="utf-8") as f:
- return json.load(f)
+ return urlparse(u).netloc.replace("www.", "")
except Exception:
- pass
- return []
+ return ""
+def _safe_name(s):
+ return re.sub(r"[^a-zA-Z0-9_-]+", "_", str(s))[:80]
-def _save_ai_wall(posts):
- """Save AI wall posts to JSON file (uses wall_posts.json for consistency with app_v2_entry)."""
- try:
- os.makedirs(os.path.dirname(WALL_FILE), exist_ok=True)
- tmp = WALL_FILE + ".tmp"
- with open(tmp, "w", encoding="utf-8") as f:
- json.dump(posts[:100], f, ensure_ascii=False)
- os.replace(tmp, WALL_FILE)
- except Exception:
- pass
+# ===== CLEAN AI OUTPUT =====
+def _clean_ai_output(text: str) -> str:
+ """Remove markdown artifacts, instruction leakage, and aggressively dedup content."""
+ if not text:
+ return ""
+ # Remove markdown headings, bold, italic, horizontal rules
+ text = re.sub(r'^#{1,6}\s+', '', text, flags=re.MULTILINE)
+ text = re.sub(r'\*\*([^*]+)\*\*', r'\1', text)
+ text = re.sub(r'\*([^*]+)\*', r'\1', text)
+ text = re.sub(r'^---+\s*$', '', text, flags=re.MULTILINE)
+ text = re.sub(r'^[-*_]{3,}\s*$', '', text, flags=re.MULTILINE)
+ # Remove common AI instruction leakage phrases (entire line)
+ leakage = [
+ r'Dưới đây là', r'Theo yêu cầu', r'Tôi sẽ viết', r'Tôi sẽ tóm tắt',
+ r'Đây là bài', r'Đây là nội dung', r'Bài viết sau đây',
+ r'Nội dung (tóm tắt|chính)', r'Nhiệm vụ', r'Vai trò', r'Tôi là',
+ r'Dựa trên.*tôi sẽ', r'Hãy', r'Bạn cần', r'Đọc bài viết',
+ r'Tôi xin', r'Xin chào', r'Trân trọng', r'Kính thưa',
+ r'Dựa trên.*dưới đây', r'Sau đây là', r'Dưới đây là bài',
+ ]
+ for phrase in leakage:
+ text = re.sub(r'^' + phrase + r'[^\n]*\n?', '', text, flags=re.MULTILINE | re.IGNORECASE)
+ text = re.sub(r'\n{3,}', '\n\n', text)
+ # --- Aggressive dedup: split into sentences, remove any that repeats ---
+ # Normalize: collapse whitespace, strip
+ def _norm(s):
+ return re.sub(r'\s+', ' ', s.strip().lower())
+
+ # Split by sentence-ending punctuation (keep delimiters)
+ raw_parts = re.split(r'(?<=[.!?])\s+', text.strip())
+ seen_sentences = set()
+ unique_parts = []
+ for part in raw_parts:
+ n = _norm(part)
+ # Skip near-duplicate: if >70% of an existing seen sentence matches
+ is_dup = False
+ if n:
+ if n in seen_sentences:
+ is_dup = True
+ else:
+ partial = re.sub(r'\W+', '', n)
+ for seen in seen_sentences:
+ seen_clean = re.sub(r'\W+', '', seen)
+ # Check substring match for very similar sentences
+ if partial and seen_clean and (
+ partial in seen_clean or seen_clean in partial
+ ):
+ shorter = min(len(partial), len(seen_clean))
+ longer = max(len(partial), len(seen_clean))
+ if shorter > 20 and shorter / longer > 0.75:
+ is_dup = True
+ break
+ if is_dup:
+ continue
+ if n:
+ seen_sentences.add(n)
+ unique_parts.append(part)
-# Helper functions for wall operations
-def _load_wall_posts():
- """Alias for _load_ai_wall for consistency with app_v2_entry.py."""
- return _load_ai_wall()
+ result = ' '.join(unique_parts).strip()
+ # Final pass: remove any remaining consecutive duplicate lines
+ lines = result.split('\n')
+ final_lines = []
+ prev_line = ""
+ for line in lines:
+ stripped = line.strip()
+ if stripped and stripped == prev_line:
+ continue
+ final_lines.append(line)
+ prev_line = stripped
+ result = '\n'.join(final_lines).strip()
+ return result
-def _save_wall_posts(posts):
- """Alias for _save_ai_wall for consistency with app_v2_entry.py."""
- return _save_ai_wall(posts)
+# ===== EXTRACT ALL IMAGES FROM ARTICLE =====
+def _extract_all_images(soup, base_url: str) -> List[Dict]:
+ """Extract ALL content images from an article page using multi-strategy approach."""
+ images = []
+ seen_urls = set()
+ skip_patterns = [
+ "avatar", "icon", "logo", "button", "banner-ad", "tracking",
+ "beacon", "pixel", "1x1", "spacer", "emoji", "sprite", "placeholder",
+ "advertisement", "ads", "widget", "sidebar", "footer-logo",
+ ]
+ def _add_image(src: str, alt: str = "", source_tag: str = "img"):
+ if not src or src.startswith("data:"):
+ return
+ abs_url = urljoin(base_url, src.strip())
+ if abs_url in seen_urls:
+ return
+ # Skip non-content images by URL pattern
+ if any(p in abs_url.lower() for p in skip_patterns):
+ return
+ # Skip very small images (likely icons)
+ try:
+ parsed = urlparse(abs_url)
+ path = parsed.path.lower()
+ if any(path.endswith(ext) for ext in ['.svg', '.ico', '.gif']):
+ return
+ except Exception:
+ pass
+ seen_urls.add(abs_url)
+ images.append({"url": abs_url, "alt": alt, "source": source_tag})
+
+ # Strategy 1: Standard
tags with all lazy-load attributes
+ for img in soup.find_all("img"):
+ src = (img.get("src") or img.get("data-src") or img.get("data-lazy-src") or
+ img.get("data-original") or img.get("data-srcset", "").split(",")[0].strip().split(" ")[0])
+ _add_image(src, alt=img.get("alt", ""), source_tag="img")
+
+ # Strategy 2: srcset on
+ for img in soup.find_all("img", srcset=True):
+ for part in img["srcset"].split(","):
+ part = part.strip()
+ if part:
+ _add_image(part.split(" ")[0], alt=img.get("alt", ""), source_tag="srcset")
+
+ # Strategy 3: with
+ for picture in soup.find_all("picture"):
+ for source in picture.find_all("source"):
+ srcset = source.get("srcset", "")
+ for part in srcset.split(","):
+ part = part.strip()
+ if part:
+ _add_image(part.split(" ")[0], source_tag="picture/srcset")
+ fallback_img = picture.find("img")
+ if fallback_img:
+ _add_image(
+ fallback_img.get("src") or fallback_img.get("data-src"),
+ alt=fallback_img.get("alt", ""),
+ source_tag="picture/img"
+ )
+
+ # Strategy 4: WordPress CMS patterns
+ for img in soup.find_all("img", class_=re.compile(r"wp-image|size-large|size-full|aligncenter")):
+ _add_image(img.get("data-src") or img.get("src"),
+ alt=img.get("alt", ""), source_tag="wp-image")
+
+ # Strategy 5: Background images in style attributes
+ for tag in soup.find_all(style=re.compile(r"background-image")):
+ for m in re.findall(r'url\(["\']?(.*?)["\']?\)', tag.get("style", "")):
+ _add_image(m, source_tag="background-style")
+
+ # Strategy 6: og:image (featured/hero image)
+ og_image = soup.find("meta", property="og:image")
+ if og_image and og_image.get("content"):
+ _add_image(og_image["content"], source_tag="og:image")
+
+ # Strategy 7: twitter:image
+ tw_image = soup.find("meta", attrs={"name": "twitter:image"})
+ if tw_image and tw_image.get("content"):
+ _add_image(tw_image["content"], source_tag="twitter:image")
+
+ # Strategy 8: with
+ for figure in soup.find_all("figure"):
+ img = figure.find("img")
+ if img:
+ src = img.get("data-src") or img.get("src")
+ figcaption = figure.find("figcaption")
+ alt = figcaption.get_text(strip=True) if figcaption else img.get("alt", "")
+ _add_image(src, alt=alt, source_tag="figure")
+
+ # Strategy 9: tags linking to images
+ for a in soup.find_all("a", href=True):
+ href = a["href"]
+ if any(href.lower().endswith(ext) for ext in [".jpg", ".jpeg", ".png", ".webp", ".gif"]):
+ _add_image(href, alt=a.get_text(strip=True)[:80], source_tag="link")
+
+ return images
+
+
+# ===== JINA READER =====
+def _reader_url(target_url: str) -> str:
+ safe = quote(target_url, safe=":/?#[]@!$&'()*+,;=%")
+ return "https://r.jina.ai/http://" + safe
+
+def jina_reader_markdown(url: str) -> str:
+ jr = _reader_url(url)
+ r = requests.get(jr, headers={"Accept": "text/markdown,text/plain,*/*", "X-Return-Format": "markdown", "User-Agent": "Mozilla/5.0"}, timeout=35)
+ r.raise_for_status()
+ return r.text or ""
+
+def _parse_jina_markdown(md: str, url: str):
+ lines = [x.rstrip() for x in (md or "").splitlines()]
+ title = ""; first_image = ""; all_images = []; content_lines = []; in_content = False
+ for ln in lines:
+ if ln.startswith("Title:") and not title:
+ title = _clean_text(ln.replace("Title:", "", 1)); continue
+ if ln.startswith("URL Source:"):
+ continue
+ if ln.startswith("Markdown Content:"):
+ in_content = True; continue
+ # Extract ALL images from markdown 
+ for mimg in re.finditer(r'!\[[^\]]*\]\((https?://[^)]+)\)', ln):
+ img_url = mimg.group(1)
+ if img_url not in all_images:
+ all_images.append(img_url)
+ if not first_image:
+ first_image = img_url
+ if in_content or (title and not ln.startswith("Title:")):
+ if ln.strip():
+ content_lines.append(ln)
+ text = "\n".join(content_lines)
+ text = re.sub(r'!\[[^\]]*\]\([^)]+\)', '', text)
+ paras = []
+ for part in re.split(r'\n{2,}|\n(?=#{1,3}\s)', text):
+ t = _clean_text(re.sub(r'^#{1,6}\s*', '', part))
+ if len(t) >= 40:
+ paras.append(t)
+ if len(paras) >= 35:
+ break
+ if not title and paras:
+ title = paras[0][:90]
+ return {"url": url, "title": title or url, "summary": paras[0] if paras else "",
+ "text": "\n".join(paras), "image": first_image,
+ "images": all_images, "via": "jina"}
+
+
+# ===== WEB SCRAPE (with full image extraction) =====
+def _best_content_block(soup):
+ best, best_score = None, 0
+ for el in soup.find_all(["article", "main", "section", "div"]):
+ ps = el.find_all("p")
+ txt = " ".join(p.get_text(" ", strip=True) for p in ps)
+ score = len(ps) * 100 + len(txt)
+ cls = " ".join(el.get("class", []))
+ if any(k in cls.lower() for k in ["content", "article", "detail", "body", "post", "entry"]):
+ score += 800
+ if score > best_score:
+ best, best_score = el, score
+ return best
+
+def scrape_any_url_direct(url: str):
+ r = requests.get(url, headers=HEADERS, timeout=18)
+ if r.status_code in {401, 403, 406, 409, 429, 451, 503}:
+ raise RuntimeError(f"blocked status {r.status_code}")
+ r.encoding = "utf-8"
+ soup = BeautifulSoup(r.text, "lxml")
+ for tag in soup.find_all(["script", "style", "nav", "footer", "aside", "form", "noscript"]):
+ tag.decompose()
+
+ # Title
+ title = soup.find("h1").get_text(" ", strip=True) if soup.find("h1") else ""
+ if not title:
+ ogt = soup.find("meta", property="og:title") or soup.find("meta", attrs={"name": "title"})
+ title = ogt.get("content", "") if ogt else (soup.title.get_text(strip=True) if soup.title else "")
+
+ # Summary
+ desc_tag = soup.find("meta", property="og:description") or soup.find("meta", attrs={"name": "description"})
+ summary = desc_tag.get("content", "") if desc_tag else ""
+
+ # Featured image (og:image)
+ img_tag = soup.find("meta", property="og:image") or soup.find("meta", attrs={"name": "twitter:image"})
+ image = img_tag.get("content", "") if img_tag else ""
+ if image and image.startswith("//"):
+ image = "https:" + image
+
+ # Extract ALL images from the article
+ all_images = _extract_all_images(soup, url)
+ image_urls = [img["url"] for img in all_images]
+
+ # Ensure featured image is first
+ if image and image not in image_urls:
+ image_urls.insert(0, image)
+ elif image in image_urls:
+ image_urls.remove(image)
+ image_urls.insert(0, image)
+
+ # Content paragraphs
+ block = _best_content_block(soup) or soup
+ paras, seen_p = [], set()
+ for p in block.find_all("p"):
+ t = _clean_text(p.get_text(" ", strip=True))
+ if len(t) >= 40 and t not in seen_p:
+ seen_p.add(t)
+ paras.append(t)
+ if len(paras) >= 35:
+ break
+
+ if not title and paras:
+ title = paras[0][:90]
-def make_post(title: str, text: str, img: str, url: str, kind: str, sources=None):
- """Create a post dict with standard fields."""
return {
- "id": str(int(time.time() * 1000)),
- "title": title,
- "text": text,
- "img": img,
- "url": url,
- "kind": kind,
- "sources": sources or [],
- "ts": int(time.time())
+ "url": url, "title": title or url, "summary": paras[0] if paras else "",
+ "text": "\n".join(paras), "image": image_urls[0] if image_urls else "",
+ "images": image_urls, "via": _domain(url)
}
+def scrape_any_url(url: str):
+ """Try direct scrape first, fall back to Jina Reader."""
+ data = scrape_any_url_direct(url)
+ raw_text = (data.get("summary", "") + "\n" + data.get("text", "")).strip()
+ if len(raw_text) >= 120:
+ return data
+ try:
+ md = jina_reader_markdown(url)
+ if md:
+ jr = _parse_jina_markdown(md, url)
+ if jr.get("text"):
+ if data.get("title") and data["title"] != url:
+ jr["title"] = data["title"]
+ if data.get("image"):
+ jr["image"] = data["image"]
+ if data.get("images"):
+ jr["images"] = data["images"]
+ jr["via"] = data.get("via", _domain(url)) + " + jina"
+ return jr
+ except Exception:
+ pass
+ return data
-def _short_script(post) -> str:
- """Extract clean text for TTS from post."""
- text = post.get("text", "") or post.get("title", "")
- text = re.sub(r"^[•\-\*]\s*", "", text, flags=re.M)
- text = re.sub(r"\s*\n\s*", ". ", text)
- return _clean_text(text)[:2000] # Increased from 1000 to 2000 for full content
+# ===== POLLINATIONS IMAGE =====
+def pollinations_image_url(topic: str) -> str:
+ prompt = "editorial illustration, Vietnamese news, " + topic
+ return "https://image.pollinations.ai/prompt/" + quote(prompt, safe="") + "?width=1024&height=576&nologo=true"
-# ===== SCRAPER FUNCTIONS (required by ai_patch.py) =====
-def scrape_any_url(url: str) -> dict:
- """Scrape any URL and extract article content.
-
- Returns dict with: title, summary, text, image, og_image, via (domain)
+
+@app.get("/api/ai/probe")
+async def api_ai_probe():
+ """Diagnostic: test which chat models actually work on this token + their latency."""
+ import time as _t
+ tok = _hf_token()
+ out = []
+ extra = os.getenv("PROBE_MODELS", "").split(",")
+ cand = [m.strip() for m in extra if m.strip()] + [
+ "Qwen/Qwen2.5-VL-7B-Instruct", "Qwen/Qwen2.5-VL-3B-Instruct", "Qwen/Qwen2-VL-7B-Instruct",
+ "Qwen/Qwen2.5-VL-72B-Instruct", "Qwen/Qwen3-8B", "Qwen/Qwen3-4B", "Qwen/Qwen3-32B",
+ "Qwen/Qwen2.5-7B-Instruct-1M", "meta-llama/Llama-3.2-3B-Instruct"]
+ seen = set()
+ for m in cand:
+ if not m or m in seen:
+ continue
+ seen.add(m)
+ t0 = _t.time()
+ try:
+ c = AsyncInferenceClient(provider="auto", api_key=tok, timeout=40)
+ r = await c.chat_completion(model=m, messages=[{"role": "user", "content": "Trả lời đúng 1 từ: xin chào"}], max_tokens=10)
+ out.append({"model": m, "ok": True, "sec": round(_t.time() - t0, 1), "txt": (r.choices[0].message.content or "")[:30]})
+ except Exception as e:
+ out.append({"model": m, "ok": False, "sec": round(_t.time() - t0, 1), "err": (type(e).__name__ + ": " + str(e))[-300:]})
+ return JSONResponse({"results": out})
+
+
+# ===== QWEN AI (strict, concise) =====
+async def qwen_generate(prompt: str, image_url: Optional[str] = None, max_tokens: int = 500, image_urls: Optional[List[str]] = None):
+ global LAST_QWEN_ERROR, HF_TOKEN
+ HF_TOKEN = _hf_token()
+ if not HF_TOKEN:
+ LAST_QWEN_ERROR = "Không tìm thấy token"
+ return None
+ if not AsyncInferenceClient:
+ LAST_QWEN_ERROR = "Thiếu huggingface_hub"
+ return None
+ errors = []; models = []
+ has_images = bool(image_urls) or bool(image_url)
+ if has_images:
+ # Vision needed -> VL models
+ candidate = [QWEN_VL_MODEL, "Qwen/Qwen2.5-VL-7B-Instruct", "Qwen/Qwen2.5-VL-3B-Instruct"]
+ else:
+ # Text-only summary -> FAST text models first, VL only as last resort.
+ candidate = QWEN_TEXT_MODELS + [QWEN_VL_MODEL]
+ # Use the last-known-working model first to avoid wasting time on unavailable models.
+ global _WORKING_MODEL_TEXT, _WORKING_MODEL_VL
+ cached_ok = _WORKING_MODEL_VL if has_images else _WORKING_MODEL_TEXT
+ if cached_ok and cached_ok in candidate:
+ candidate = [cached_ok] + [m for m in candidate if m != cached_ok]
+ for m in candidate:
+ if m and m not in models:
+ models.append(m)
+ for model in models:
+ try:
+ client = AsyncInferenceClient(provider="auto", api_key=HF_TOKEN, timeout=60)
+ content = []
+ # Collect all images: image_urls list takes priority, fall back to single image_url
+ all_img_urls = []
+ if image_urls:
+ all_img_urls = image_urls[:6] # max 6 images to avoid context overflow
+ elif image_url:
+ all_img_urls = [image_url]
+ for img_u in all_img_urls:
+ if img_u and img_u.startswith("http"):
+ content.append({"type": "image_url", "image_url": {"url": img_u}})
+ content.append({"type": "text", "text": prompt})
+ messages = [
+ {"role": "system", "content": (
+ "Bạn là biên tập viên báo điện tử tiếng Việt. "
+ "NHIỆM VỤ: Chỉ TÓM TẮT nội dung, KHÔNG viết lại bài đầy đủ. "
+ "QUY TẮC CỨNG: "
+ "(1) KHÔNG lặp lại bất kỳ nội dung nào — mỗi ý chỉ xuất hiện ĐÚNG 1 LẦN. "
+ "(2) Nếu 2 câu diễn đạt cùng 1 ý → bỏ cây thứ 2. "
+ "(3) KHÔNG dùng Markdown (##, **, ---, *). "
+ "(4) KHÔNG viết 'Dưới đây là', 'Tôi sẽ', 'Theo yêu cầu', 'Nhiệm vụ', 'Vai trò', 'Đây là bài tóm tắt'. "
+ "(5) KHÔNG bịa thông tin ngoài nguồn. "
+ "(6) Chỉ viết ĐOẠN VĂN THUẦN, không bullet points. "
+ "(7) Tối đa 200 từ. Ngắn gọn, súc tích."
+ )},
+ {"role": "user", "content": content}
+ ]
+ resp = await client.chat_completion(model=model, messages=messages, max_tokens=max_tokens, temperature=0.3, top_p=0.8)
+ txt = (resp.choices[0].message.content or "").strip()
+ if txt:
+ LAST_QWEN_ERROR = ""
+ if has_images: _WORKING_MODEL_VL = model
+ else: _WORKING_MODEL_TEXT = model
+ return txt
+ except Exception as e:
+ errors.append(f"{model}: {type(e).__name__}: {str(e)[:220]}")
+ LAST_QWEN_ERROR = " | ".join(errors) or "Qwen không trả nội dung."
+ print("[qwen errors]", LAST_QWEN_ERROR)
+ return None
+
+
+# ===== TTS GENERATION =====
+async def _generate_tts_edge(text: str, voice_id: str, speed: float, out_path: str, emotion: str = None):
+ """Generate TTS using edge-tts (or gTTS if engine=gtts) with voice, speed & emotion control.
+
+ Emotion (cảm xúc) is simulated via pitch + rate + volume (edge-tts has no express-as).
"""
- try:
- r = requests.get(url, headers=HEADERS, timeout=15, allow_redirects=True)
- r.encoding = 'utf-8'
- soup = BeautifulSoup(r.text, 'lxml')
-
- # Remove scripts, styles, nav, footer
- for tag in soup.find_all(['script', 'style', 'nav', 'footer', 'aside', 'form']):
- tag.decompose()
-
- # Extract title
- h1 = soup.find('h1')
- ogt = soup.find('meta', property='og:title')
- title = (h1.get_text(strip=True) if h1 else '') or (ogt.get('content', '') if ogt else url)
-
- # Extract OG image
- ogi = soup.find('meta', property='og:image')
- og_image = ogi.get('content', '') if ogi else ''
-
- # Extract article body
- block = None
- for sel in ['article', '.singular-content', '.detail-content', '.fck_detail', '.content-detail', '.knc-content', 'main', '.cms-body', '.article__body']:
- el = soup.select_one(sel)
- if el and len(el.find_all('p')) >= 2:
- block = el
- break
- if not block:
- block = soup.body or soup
-
- # Extract text from paragraphs
- paragraphs = []
- for el in block.find_all(['p', 'h2', 'h3'], recursive=True):
- t = _clean_text(el.get_text(strip=True))
- if t and len(t) > 40:
- paragraphs.append(t)
-
- # Extract images
- images = []
- for el in block.find_all(['figure', 'img'], recursive=True):
- im = el if el.name == 'img' else el.find('img')
- if im:
- src = im.get('data-src') or im.get('src') or im.get('data-original') or ''
- if src and 'base64' not in src:
- if src.startswith('//'):
- src = 'https:' + src
- images.append(src)
-
- # Prefer OG image as main image
- image = og_image or (images[0] if images else '')
-
- return {
- 'title': title,
- 'summary': paragraphs[0] if paragraphs else '',
- 'text': '\n'.join(paragraphs),
- 'image': image,
- 'og_image': og_image,
- 'via': _domain(url),
- 'images': images
- }
- except Exception as e:
- return {'title': url, 'summary': '', 'text': '', 'image': '', 'og_image': '', 'via': _domain(url), 'error': str(e)}
+ vcfg = TTS_VOICES.get(voice_id, TTS_VOICES[TTS_DEFAULT_VOICE])
+ # gTTS engine (no voice/speed/emotion control)
+ if vcfg.get("engine") == "gtts":
+ _generate_tts_gtts(text, out_path)
+ return
+ if edge_tts is None:
+ raise RuntimeError("edge-tts chưa cài đặt")
+ voice = vcfg["id"]
+ emo = EMOTION_PRESETS.get(emotion or EMOTION_DEFAULT, EMOTION_PRESETS[EMOTION_DEFAULT])
+ # Apply emotion rate multiplier on top of base speed
+ eff_speed = speed * emo.get("rate_mul", 1.0)
+ pct = int(round((eff_speed - 1.0) * 100))
+ rate = f"+{pct}%" if pct >= 0 else f"{pct}%"
+ pitch = emo.get("pitch", "+0Hz")
+ volume = emo.get("volume", "+0%")
+ communicate = edge_tts.Communicate(text, voice, rate=rate, pitch=pitch, volume=volume)
+ await communicate.save(out_path)
+
+def _generate_tts_gtts(text: str, out_path: str):
+ """Fallback TTS using gTTS (no voice/speed control)."""
+ if gTTS is None:
+ raise RuntimeError("gTTS chưa cài đặt")
+ gTTS(text, lang="vi").save(out_path)
-def web_context(topic: str, limit: int = 5) -> tuple:
- """Get web context for a topic. Returns (context_text, sources_list)."""
- sources = []
+# ===== SHORT VIDEO GENERATION (multi-segment: each key point with its own image) =====
+def _download_image(url, fallback_topic, out_path):
+ """Download an image (un-proxying our own /api/proxy/img). Falls back to generated image."""
+ if url:
+ u = url
+ m = re.search(r'/api/proxy/img\?url=(.+)$', u)
+ if m:
+ from urllib.parse import unquote
+ u = unquote(m.group(1))
+ try:
+ r = requests.get(u, headers={**HEADERS, "Referer": "https://dantri.com.vn/"}, timeout=15)
+ if r.status_code == 200 and len(r.content) > 1000:
+ with open(out_path, "wb") as f:
+ f.write(r.content)
+ if Image:
+ Image.open(out_path).verify()
+ return out_path
+ except Exception:
+ pass
+ gen = pollinations_image_url(fallback_topic)
try:
- # Try Google News RSS
- rss_url = f"https://news.google.com/rss/search?q={quote_plus(topic)}&hl=vi&gl=VN&ceid=VN:vi"
- r = requests.get(rss_url, headers=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 ''
- if title and link:
- sources.append({'title': title, 'url': link, 'via': _domain(link)})
+ r = requests.get(gen, headers=HEADERS, timeout=25)
+ if r.status_code == 200 and len(r.content) > 1000:
+ with open(out_path, "wb") as f:
+ f.write(r.content)
+ return out_path
except Exception:
pass
-
- context = f'Trên mạng có nhiều bài viết về "{topic}". Một số nguồn: ' + ', '.join([s.get('title', '') for s in sources[:3]])
- return context, sources
+ if Image:
+ Image.new("RGB", (1080, 980), (30, 55, 42)).save(out_path)
+ return out_path
+ raise RuntimeError("Không tạo được ảnh")
-# ===== SHORT FRAME FUNCTION (required by ai_patch.py) =====
-def _make_short_frame(post, img_path, out_path):
- """Create a short video frame from post and image.
-
- Called by ai_patch.py _make_short_frame_full when Image is available.
- """
+def _split_keypoint_sentences(text, max_points=6):
+ """Split summary text into key points: prefer bullet markers, else sentences."""
+ text = _clean_text(text)
+ parts = re.split(r'\s*•\s*', text)
+ good = [p.strip() for p in parts if len(p.strip()) > 20]
+ if len(good) >= 2:
+ # Explicit bullet points: keep each one as-is (never merge).
+ return good[:max_points]
+ # Fallback: split into sentences and merge orphan short fragments.
+ pts = [p.strip() for p in re.split(r'(?<=[.!?])\s+', text) if len(p.strip()) > 20]
+ out = []
+ for p in pts:
+ if out and len(p) < 40:
+ out[-1] = (out[-1] + " " + p).strip()
+ else:
+ out.append(p)
+ return out[:max_points] if out else ([text] if text else [])
+
+
+def _build_keypoints(post, max_points=6):
+ """Return [{text, image}] pairing each key point with its own image."""
+ slides = post.get("slides") or []
+ images = post.get("images") or ([post.get("img")] if post.get("img") else [])
+ images = [i for i in images if i]
+ if slides:
+ kps = []
+ for i, s in enumerate(slides[:max_points]):
+ t = _clean_text(s.get("text", ""))
+ img = s.get("image") or (images[i] if i < len(images) else (images[-1] if images else ""))
+ if t:
+ kps.append({"text": t, "image": img})
+ if kps:
+ return kps
+ points = _split_keypoint_sentences(post.get("text", ""), max_points)
+ kps = []
+ for i, t in enumerate(points):
+ img = images[i] if i < len(images) else (images[-1] if images else "")
+ kps.append({"text": t, "image": img})
+ if not kps:
+ kps = [{"text": _clean_text(post.get("title", "")) or "VNEWS", "image": images[0] if images else ""}]
+ return kps
+
+
+def _wrap_text(draw, text, font, max_w):
+ words = text.split()
+ lines, cur = [], ""
+ for w in words:
+ test = (cur + " " + w).strip()
+ if draw.textlength(test, font=font) <= max_w:
+ cur = test
+ else:
+ if cur:
+ lines.append(cur)
+ cur = w
+ if cur:
+ lines.append(cur)
+ return lines
+
+
+def _make_segment_frame(title, point_text, img_path, idx, total, out_path):
+ """Render a 1080x1920 vertical frame: image on top, key point text below."""
if Image is None:
- # Create a minimal frame without PIL - just return success
- # The caller should handle this case
- return False
-
+ raise RuntimeError("Pillow chưa sẵn sàng")
W, H = 1080, 1920
- bg = Image.new("RGB", (W, H), (14, 14, 14))
-
+ IMG_H = 980
+ bg = Image.new("RGB", (W, H), (12, 14, 18))
try:
im = Image.open(img_path).convert("RGB")
- target = (1080, 760)
- im_ratio = im.width / max(1, im.height)
- target_ratio = target[0] / target[1]
-
- if im_ratio > target_ratio:
- new_h = target[1]
- new_w = int(new_h * im_ratio)
+ tr = W / IMG_H
+ ir = im.width / im.height
+ if ir > tr:
+ nh = IMG_H; nw = int(nh * ir)
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]))
+ nw = W; nh = int(nw / ir)
+ im = im.resize((nw, nh))
+ left = (nw - W) // 2; top = (nh - IMG_H) // 2
+ im = im.crop((left, top, left + W, top + IMG_H))
bg.paste(im, (0, 0))
except Exception:
pass
-
draw = ImageDraw.Draw(bg)
-
+ draw.rectangle((0, IMG_H, W, H), fill=(12, 14, 18))
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)
+ f_label = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 34)
+ f_title = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 46)
+ f_point = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 52)
except Exception:
- font_title = font_body = None
-
- draw.rectangle((0, 720, W, H), fill=(14, 14, 14))
- margin = 48
- maxw = W - margin * 2
-
- y = 830
- for ln in _wrap_text(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(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
-
+ f_label = f_title = f_point = ImageFont.load_default()
+ draw.text((54, IMG_H + 24), "VNEWS · Tường AI", fill=(92, 184, 122), font=f_label)
+ cnt = f"{idx + 1}/{total}"
+ draw.text((W - 54 - draw.textlength(cnt, font=f_label), IMG_H + 24), cnt, fill=(240, 192, 64), font=f_label)
+ y = IMG_H + 86
+ for ln in _wrap_text(draw, _clean_text(title), f_title, W - 108)[:2]:
+ draw.text((54, y), ln, fill=(255, 255, 255), font=f_title)
+ y += 56
+ y += 16
+ for ln in _wrap_text(draw, _clean_text(point_text), f_point, W - 108)[:11]:
+ draw.text((54, y), ln, fill=(225, 230, 235), font=f_point)
+ y += 64
bg.save(out_path, quality=92)
- return True
+ return out_path
-def _wrap_text(draw, text, font, max_width, max_lines):
- """Helper for wrapping text in frames."""
- words = _clean_text(text).split()
- lines, cur = [], ""
- for w in words:
- test = (cur + " " + w).strip()
+def _ffmpeg_bin():
+ return os.environ.get("FFMPEG_BIN", "ffmpeg")
+
+
+def _audio_duration(path):
+ try:
+ out = subprocess.run([_ffmpeg_bin(), "-i", path], capture_output=True, text=True, timeout=30).stderr
+ m = re.search(r"Duration:\s*(\d+):(\d+):(\d+\.\d+)", out)
+ if m:
+ h, mi, s = m.groups()
+ return int(h) * 3600 + int(mi) * 60 + float(s)
+ except Exception:
+ pass
+ return 0.0
+
+
+async def _generate_short_video(post, post_id: str, voice_id: str = None, speed: float = None, emotion: str = None) -> str:
+ """Generate a multi-segment MP4 short: each key point shown with its OWN image + narration."""
+ try:
+ os.makedirs(SHORTS_DIR, exist_ok=True)
+ out_mp4 = os.path.join(SHORTS_DIR, _safe_name(post_id) + ".mp4")
+ if os.path.exists(out_mp4) and voice_id is None and speed is None and emotion is None:
+ return "/api/ai/short-file/" + post_id
+
+ work = os.path.join(SHORTS_DIR, _safe_name(post_id) + "_work")
+ os.makedirs(work, exist_ok=True)
+
+ title = _clean_text(post.get("title", "")) or "VNEWS"
+ kps = _build_keypoints(post)
+
+ # Resolve voice + emotion (auto from topic, or from post, or explicit args)
+ auto_voice, auto_emotion = _detect_voice_emotion(post.get("title", ""), post.get("text", ""))
+ if voice_id is None:
+ voice_id = post.get("voice") or auto_voice
+ if emotion is None:
+ emotion = post.get("emotion") or auto_emotion
+ if speed is None:
+ speed = TTS_DEFAULT_SPEED
+ vcfg = TTS_VOICES.get(voice_id, TTS_VOICES[TTS_DEFAULT_VOICE])
+
+ seg_files = []
+ ff = _ffmpeg_bin()
+ for i, kp in enumerate(kps):
+ img_path = os.path.join(work, f"img{i}.jpg")
+ frame_path = os.path.join(work, f"frame{i}.jpg")
+ audio_path = os.path.join(work, f"voice{i}.mp3")
+ seg_mp4 = os.path.join(work, f"seg{i}.mp4")
+ _download_image(kp.get("image", ""), title, img_path)
+ _make_segment_frame(title, kp["text"], img_path, i, len(kps), frame_path)
+ narration = (title + ". " + kp["text"]) if i == 0 else kp["text"]
+ try:
+ await _generate_tts_edge(narration, voice_id, speed, audio_path, emotion=emotion)
+ except Exception as e:
+ print(f"[TTS edge-tts error] {e}, falling back to gTTS")
+ if gTTS:
+ _generate_tts_gtts(narration, audio_path)
+ else:
+ return ""
+ dur = _audio_duration(audio_path)
+ if dur < 1.0:
+ dur = 2.0
+ cmd = [ff, "-y", "-loop", "1", "-i", frame_path, "-i", audio_path,
+ "-c:v", "libx264", "-tune", "stillimage", "-pix_fmt", "yuv420p",
+ "-t", f"{dur + 0.4:.2f}", "-c:a", "aac", "-b:a", "128k", "-ar", "44100",
+ "-vf", "scale=1080:1920", "-r", "25", seg_mp4]
+ subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=180)
+ seg_files.append(seg_mp4)
+
+ if not seg_files:
+ return ""
+ if len(seg_files) == 1:
+ os.replace(seg_files[0], out_mp4)
+ return "/api/ai/short-file/" + post_id
+
+ listfile = os.path.join(work, "concat.txt")
+ with open(listfile, "w", encoding="utf-8") as f:
+ f.write("\n".join(f"file '{s}'" for s in seg_files))
+ cmd = [ff, "-y", "-f", "concat", "-safe", "0", "-i", listfile,
+ "-c:v", "libx264", "-pix_fmt", "yuv420p", "-c:a", "aac", "-b:a", "128k", out_mp4]
+ subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=300)
+ return "/api/ai/short-file/" + post_id
+ except Exception as e:
+ print(f"[short video error] {e}")
+ return ""
+
+import threading as _threading
+def _spawn_background_video(post):
+ """Generate the short video in a BACKGROUND thread so the rewrite/topic endpoint
+ can return immediately. When done, persist the video URL onto the wall post.
+ This is the main fix for 'rewrite tu cac nguon tin qua lau' — the user gets the
+ text post instantly; the video appears shortly after (or via the 'Tao Video' button)."""
+ pid = post.get("id")
+ if not pid:
+ return
+ def _run():
try:
- width = draw.textbbox((0, 0), test, font=font)[2]
- except Exception:
- width = len(test) * 20
- if width <= max_width:
- cur = test
+ loop = asyncio.new_event_loop()
+ asyncio.set_event_loop(loop)
+ video_url = loop.run_until_complete(_generate_short_video(post, pid))
+ loop.close()
+ if video_url:
+ posts = _load_wall()
+ for i, p in enumerate(posts):
+ if str(p.get("id")) == str(pid):
+ posts[i]["video"] = video_url
+ break
+ _save_wall(posts)
+ print(f"[bg-video] done {pid} -> {video_url}")
+ except Exception as e:
+ print(f"[bg-video] error {pid}: {e}")
+ _threading.Thread(target=_run, daemon=True).start()
+
+# ===== MAKE POST =====
+def make_post(title, text, image, source_url, kind, sources=None, images=None, voice=None, emotion=None):
+ # Auto-pick voice + emotion from the article topic when not provided
+ auto_voice, auto_emotion = _detect_voice_emotion(title or "", text or "")
+ return {
+ "id": str(int(time.time() * 1000)) + str(random.randint(100, 999)),
+ "title": title, "text": text, "img": image, "url": source_url,
+ "kind": kind, "sources": sources or [], "video": "",
+ "images": images or [], "ts": int(time.time()),
+ "voice": voice or auto_voice,
+ "emotion": emotion or auto_emotion,
+ }
+
+
+# ===== SHARED PROMPT BUILDER =====
+def _build_rewrite_prompt(title: str, raw: str, images: List[str] = None) -> str:
+ image_info = ""
+ if images:
+ num = len(images)
+ if num == 1:
+ image_info = "\n\nBài viết có 1 ảnh minh họa. Hãy tham khảo ảnh để hiểu ngữ cảnh (nếu phù hợp)."
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
\ No newline at end of file
+ image_info = f"\n\nBài viết có {num} ảnh minh họa. Hãy tham khảo tất cả ảnh để hiểu ngữ cảnh và bổ sung thông tin cho bài viết (nếu phù hợp)."
+
+ return f"""Tóm tắt bài viết sau thành bài TÓM TẮT đăng Tường AI.
+
+QUY TẮC BẮT BUỘC:
+1. Chỉ viết TÓM TẮT các ý chính. KHÔNG sao chép nguyên văn từ bài gốc.
+2. KHÔNG lặp lại bất kỳ nội dung nào. Mỗi thông tin chỉ xuất hiện ĐÚNG 1 LẦN.
+3. Nếu 2 câu nói cùng 1 ý → chỉ giữ 1 câu, bỏ cây còn lại.
+4. KHÔNG dùng Markdown (##, **, ---, *).
+5. KHÔNG viết "Dưới đây là", "Tôi sẽ", "Theo yêu cầu", "Nhiệm vụ", "Vai trò", "Đây là bài tóm tắt".
+6. Viết thành ĐOẠN VĂN THUẦN, mạch lạc, dễ đọc. Không dùng bullet points.
+7. Giữ sự thật, KHÔNG bịa thông tin.
+8. Tối đa 200 từ. Ngắn gọn, đủ ý.{image_info}
+
+Tiêu đề gốc: {title}
+
+Nội dung gốc:
+{raw[:14000]}"""
+
+
+def _build_topic_prompt(topic: str, ctx: str) -> str:
+ return f"""Viết bài TÓM TẮT NGẮN GỌN về chủ đề: "{topic}".
+
+QUY TẮC BẮT BUỘC:
+1. Chỉ viết TÓM TẮT các ý chính từ nguồn. KHÔNG sao chép nguyên văn.
+2. KHÔNG lặp lại bất kỳ nội dung nào. Mỗi thông tin chỉ xuất hiện ĐÚNG 1 LẦN.
+3. Nếu 2 câu nói cùng 1 ý → chỉ giữ 1 câu.
+4. KHÔNG dùng Markdown (##, **, ---, *).
+5. KHÔNG viết "Dưới đây là", "Tôi sẽ", "Theo yêu cầu", "Nhiệm vụ", "Vai trò".
+6. Viết thành ĐOẠN VĂN THUẦN, mạch lạc. Không dùng bullet points.
+7. Giữ sự thật, KHÔNG bịa.
+8. Tối đa 200 từ. Ngắn gọn, đủ ý.
+
+Nguồn thực tế:
+{ctx[:12000]}"""
+
+
+# ===== WRITE ENDPOINTS =====
+@app.post("/api/rewrite_share")
+async def api_rewrite_share(request: Request):
+ body = await request.json()
+ url = _clean_text(body.get("url", ""))
+ if not url.startswith("http"):
+ return JSONResponse({"error": "missing url"}, status_code=400)
+ try:
+ data = 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) < 60:
+ return JSONResponse({"error": "Bài viết quá ngắn để tóm tắt"}, status_code=422)
+
+ images = data.get("images", [])
+ prompt = _build_rewrite_prompt(data.get("title", ""), raw, images)
+ # Text-only summary for SPEED (images are kept on the post for display + short video).
+ text = await qwen_generate(prompt, max_tokens=500)
+ if not text:
+ return JSONResponse({"error": "Qwen2.5-VL chưa sẵn sàng: " + LAST_QWEN_ERROR}, status_code=503)
+ text = _clean_ai_output(text)
+ post = make_post(data.get("title") or "Bài viết", text,
+ images[0] if images else data.get("image", ""),
+ url, "rewrite", images=images)
+
+ # Save post and return IMMEDIATELY; generate the short video in the background
+ # (so rewrite is fast). Video appears on the wall when ready / via 'Tao Video' button.
+ posts = _load_wall()
+ posts.insert(0, post)
+ _save_wall(posts)
+ _spawn_background_video(post)
+ return JSONResponse({"post": post})
+
+
+@app.post("/api/url_wall")
+async def api_url_wall(request: Request):
+ body = await request.json()
+ url = _clean_text(body.get("url", ""))
+ if not url.startswith("http"):
+ return JSONResponse({"error": "missing url"}, status_code=400)
+ try:
+ data = 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) < 60:
+ return JSONResponse({"error": "URL không có đủ nội dung"}, status_code=422)
+
+ images = data.get("images", [])
+ prompt = _build_rewrite_prompt(data.get("title", ""), raw, images)
+ # Text-only summary for SPEED (images are kept on the post for display + short video).
+ text = await qwen_generate(prompt, max_tokens=500)
+ if not text:
+ return JSONResponse({"error": "Qwen2.5-VL chưa sẵn sàng: " + LAST_QWEN_ERROR}, status_code=503)
+ text = _clean_ai_output(text)
+ post = make_post(data.get("title") or "Bài viết", text,
+ images[0] if images else data.get("image", ""),
+ url, "url", images=images)
+
+ posts = _load_wall()
+ posts.insert(0, post)
+ _save_wall(posts)
+ _spawn_background_video(post)
+ return JSONResponse({"post": post})
+
+
+@app.post("/api/topic_post")
+async def api_topic_post(request: Request):
+ body = await request.json()
+ topic = _clean_text(body.get("topic", ""))
+ if not topic:
+ return JSONResponse({"error": "missing topic"}, status_code=400)
+
+ ctx = _web_context(topic)
+ if not ctx:
+ return JSONResponse({"error": "Không lấy được dữ liệu cho chủ đề này"}, status_code=422)
+
+ image = pollinations_image_url(topic)
+ prompt = _build_topic_prompt(topic, ctx)
+ # NOTE: do NOT pass the decorative pollinations image to the VL model — feeding an
+ # image makes Qwen2.5-VL much slower (it must download+process it) with no benefit for
+ # a text summary. We keep the image only for display on the post. This is a major
+ # speed-up for 'rewrite tong hop'. (Text-only inference is several times faster.)
+ text = await qwen_generate(prompt, max_tokens=500)
+ if not text:
+ return JSONResponse({"error": "Qwen2.5-VL chưa sẵn sàng: " + LAST_QWEN_ERROR}, status_code=503)
+ text = _clean_ai_output(text)
+ post = make_post(topic, text, image, "", "topic")
+
+ posts = _load_wall()
+ posts.insert(0, post)
+ _save_wall(posts)
+ _spawn_background_video(post)
+ return JSONResponse({"post": post})
+
+
+# ===== WALL ENDPOINTS =====
+@app.get("/api/ai_wall")
+def api_ai_wall():
+ return JSONResponse({"posts": _load_wall()[:80]})
+
+@app.get("/api/wall")
+def api_wall():
+ return JSONResponse({"posts": _load_wall()[:80]})
+
+
+# ===== SHORT VIDEO ENDPOINT (with voice + speed params) =====
+@app.post("/api/ai/short/{post_id}")
+async def api_ai_short(post_id: str, voice: str = Query(default=None), speed: float = Query(default=None), emotion: str = Query(default=None)):
+ """Generate (or retrieve cached) short video for a wall post.
+
+ Query params:
+ - voice: 'hoaimy' (female) | 'namminh' (male) | auto-detect if not specified
+ - speed: float (default 1.2), e.g. 1.0=normal, 1.2=fast, 0.8=slow
+ - emotion: 'vui'|'hao_hung'|'nghiem'|'tram'|'buon'|'trung_tinh' | auto by topic
+ """
+ posts = _load_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)
+
+ os.makedirs(SHORTS_DIR, exist_ok=True)
+ out_mp4 = os.path.join(SHORTS_DIR, _safe_name(post_id) + ".mp4")
+
+ # If cached and no custom voice/speed/emotion requested, return cached
+ if os.path.exists(out_mp4) and voice is None and speed is None and emotion is None:
+ video_url = "/api/ai/short-file/" + post_id
+ for i, p in enumerate(posts):
+ if str(p.get("id")) == str(post_id):
+ posts[i]["video"] = video_url
+ break
+ _save_wall(posts)
+ return JSONResponse({"video": video_url})
+
+ # Validate params
+ if voice is not None and voice not in TTS_VOICES:
+ return JSONResponse({"error": f"voice không hợp lệ. Chọn: {list(TTS_VOICES.keys())}"}, status_code=400)
+ if emotion is not None and emotion not in EMOTION_PRESETS:
+ return JSONResponse({"error": f"emotion không hợp lệ. Chọn: {list(EMOTION_PRESETS.keys())}"}, status_code=400)
+
+ video_url = await _generate_short_video(post, post_id, voice_id=voice, speed=speed, emotion=emotion)
+ if video_url:
+ for i, p in enumerate(posts):
+ if str(p.get("id")) == str(post_id):
+ posts[i]["video"] = video_url
+ if voice:
+ posts[i]["voice"] = voice
+ if emotion:
+ posts[i]["emotion"] = emotion
+ break
+ _save_wall(posts)
+ return JSONResponse({"video": video_url})
+ return JSONResponse({"error": "Không tạo được shorts"}, status_code=500)
+
+
+@app.get("/api/ai/short-file/{post_id}")
+def api_ai_short_file(post_id: str):
+ path = os.path.join(SHORTS_DIR, _safe_name(post_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-{post_id}.mp4")
+
+
+@app.get("/api/ai/status")
+def api_ai_status():
+ return JSONResponse({
+ "has_token": bool(_hf_token()),
+ "client_imported": AsyncInferenceClient is not None,
+ "model": QWEN_VL_MODEL,
+ "last_error": LAST_QWEN_ERROR,
+ "tts_ready": gTTS is not None or edge_tts is not None,
+ "tts_engine": "edge-tts" if edge_tts else ("gtts" if gTTS else "none"),
+ "tts_voices": {k: v["flag"] + " " + v["name"] for k, v in TTS_VOICES.items()},
+ "tts_voice_count": len(TTS_VOICES),
+ "tts_default_speed": TTS_DEFAULT_SPEED,
+ })
+
+
+@app.get("/api/ai/voices")
+def api_ai_voices():
+ """Return available TTS voices with country/group info."""
+ voices_out = {}
+ for k, v in TTS_VOICES.items():
+ voices_out[k] = {
+ "name": v["name"],
+ "gender": v["gender"],
+ "country": v["country"],
+ "lang": v["lang"],
+ "flag": v["flag"],
+ "label": f"{v['flag']} {v['name']} ({v['gender']})",
+ }
+ return JSONResponse({
+ "voices": voices_out,
+ "default_voice": TTS_DEFAULT_VOICE,
+ "default_speed": TTS_DEFAULT_SPEED,
+ "topic_voice_map": TOPIC_VOICE_MAP,
+ "emotions": {k: {"label": v["label"], "emoji": v["emoji"]} for k, v in EMOTION_PRESETS.items()},
+ "default_emotion": EMOTION_DEFAULT,
+ })
diff --git a/ai_fix2.py b/ai_fix2.py
index 895be1d7505ed5fedaec5b8f023a52434101989f..b5a201354fe9e23c2294108b52d0ac2e3b06ca8d 100644
--- a/ai_fix2.py
+++ b/ai_fix2.py
@@ -321,7 +321,7 @@ async def ai_short_full(post_id: str, request: Request):
subprocess.run(['ffmpeg','-y','-i',audio,'-filter:a',f'atempo={speed}','-vn',audio_fast], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=220)
duration = 45.0
try:
- pr = subprocess.run(['ffprobe','-v','error','-show_entries','format=duration','-of','default=noprint_wrappers=1:no_key=1',audio_fast], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=20)
+ pr = subprocess.run(['ffprobe','-v','error','-show_entries','format=duration','-of','default=noprint_wrappers=1:nokey=1',audio_fast], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=20)
duration = float((pr.stdout or b'45').decode().strip() or 45)
except Exception:
pass
diff --git a/ai_patch.py b/ai_patch.py
index a1ef29df2d0a94076dabec10f6cfbb90445cf76d..cbced1f1fcc64a98e8b3dd5ca6f69421b048b2a7 100644
--- a/ai_patch.py
+++ b/ai_patch.py
@@ -6,7 +6,6 @@ import json
import html as html_lib
import subprocess
import requests
-import hashlib
import ai_ext as base
from ai_ext import app
from fastapi import Request
@@ -42,17 +41,17 @@ def _similar(a, b):
return len(ta & tb) / max(1, min(len(ta), len(tb))) >= 0.72
-def _dedupe_units(units, max_units=25):
- """Deduplicate units - only skip exact matches to ensure all bullet points are read."""
+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)
- # Only skip exact matches, NOT similar content (to avoid skipping valid bullet points)
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:
@@ -60,7 +59,7 @@ def _dedupe_units(units, max_units=25):
return out
-def _postprocess_ai_text(text, max_units=20):
+def _postprocess_ai_text(text, max_units=7):
text = _clean(text)
if not text:
return text
@@ -79,9 +78,10 @@ def _postprocess_ai_text(text, max_units=20):
raw_lines.append(line)
units = []
for line in raw_lines:
- # KEEP FULL bullet point - don't truncate or split into segments
- if len(line) >= 18:
- units.append(_clean(re.sub(r"^[-•*\d\.\)\s]+", "", line)))
+ 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]
@@ -268,7 +268,7 @@ async def qwen_generate_resilient(prompt: str, image_url=None, max_tokens: int =
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=12)
+ return _fallback_summary_from_prompt(prompt, max_units=6)
if not hasattr(base, "_original_qwen_generate"):
@@ -321,8 +321,8 @@ Yêu cầu bắt buộc:
Nội dung bài:
{art['raw'][:14000]}"""
- text = await base.qwen_generate(prompt, image_url=art.get('image') or None, max_tokens=1500)
- text = _postprocess_ai_text(text, max_units=20)
+ 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)
@@ -347,8 +347,8 @@ async def compat_url_wall(request: Request):
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=1500)
- text = _postprocess_ai_text(text, max_units=20)
+ 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)
@@ -357,116 +357,6 @@ async def compat_url_wall(request: Request):
return JSONResponse({'post': post})
-def _is_relevant_image(img_url, title, text):
- """Check if an image is relevant to the article content."""
- if not img_url:
- return False
- skip_patterns = ['pixel', 'analytics', 'tracking', '1x1.gif', 'spacer.gif',
- 'logo', 'icon', 'avatar', 'emoji', 'smiley', 'sprite',
- 'advertisement', 'ad-banner', 'sponsored', 'banner-ads']
- img_lower = img_url.lower()
- for p in skip_patterns:
- if p in img_lower:
- return False
- if not any(img_lower.endswith(ext) for ext in ['.jpg', '.jpeg', '.png', '.webp', '.gif']):
- return False
- return True
-
-
-def _filter_relevant_images(images, title, text, max_images=8):
- """Filter and rank images by relevance to article content."""
- if not images:
- return []
- seen = set()
- relevant = []
- for img in images:
- if img in seen:
- continue
- seen.add(img)
- if _is_relevant_image(img, title, text):
- relevant.append(img)
- return relevant[:max_images]
-
-
-def _extract_key_points_for_slides(paragraphs, max_points=12):
- """Extract key points from paragraphs for slides - extracts ALL sentences, not just first one."""
- points = []
- for p in paragraphs:
- if len(points) >= max_points:
- break
- p = _clean(p)
- if not p:
- continue
- # Split paragraph into sentences using Vietnamese + English punctuation - GET ALL SENTENCES
- sentences = re.split(r'(?<=[.!?])\s+(?=[A-ZÀ-Ỹ0-9])', p)
- sentences = [s.strip() for s in sentences if s.strip()]
-
- for sentence in sentences:
- if len(points) >= max_points:
- break
- sentence = _clean(sentence)
- if len(sentence) < 30:
- continue
- if any(sentence[:60] in existing for existing in points):
- continue
- if not sentence.endswith(('.', '!', '?')):
- sentence = sentence + '.'
- points.append(sentence)
- return points
-
-
-def _scrape_article_images(url):
- """Scrape article page and return only relevant images."""
- try:
- headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
- "Accept-Language": "vi-VN,vi;q=0.9,en;q=0.8"}
- r = requests.get(url, headers=headers, timeout=15, allow_redirects=True)
- r.encoding = 'utf-8'
- soup = BeautifulSoup(r.text, 'lxml')
- for tag in soup.find_all(['script', 'style', 'nav', 'footer', 'aside', 'form']):
- tag.decompose()
- h1 = soup.find('h1')
- ogt = soup.find('meta', property='og:title')
- title = (h1.get_text(strip=True) if h1 else '') or (ogt.get('content', '') if ogt else '')
- ogi = soup.find('meta', property='og:image')
- og_img = ogi.get('content', '') if ogi else ''
- if og_img and og_img.startswith('//'):
- og_img = 'https:' + og_img
- block = None
- for sel in ['article', '.singular-content', '.detail-content', '.fck_detail', '.content-detail', '.knc-content', 'main', '.cms-body', '.article__body']:
- el = soup.select_one(sel)
- if el and len(el.find_all('p')) >= 2:
- block = el
- break
- if not block:
- block = soup.body or soup
- paragraphs = []
- all_images = []
- seen_imgs = set()
- if og_img and og_img not in seen_imgs:
- all_images.append(og_img)
- seen_imgs.add(og_img)
- for el in block.find_all(['p', 'h2', 'h3', 'figure', 'img'], recursive=True):
- if el.name == 'p':
- t = _clean(el.get_text(strip=True))
- if t and len(t) > 40:
- paragraphs.append(t)
- elif el.name in ('figure', 'img'):
- im = el if el.name == 'img' else el.find('img')
- if im:
- src = im.get('data-src') or im.get('src') or im.get('data-original') or ''
- if src and 'base64' not in src:
- if src.startswith('//'):
- src = 'https:' + src
- if src not in seen_imgs:
- all_images.append(src)
- seen_imgs.add(src)
- relevant_images = _filter_relevant_images(all_images, title, ' '.join(paragraphs[:5]))
- return {'title': _clean(title), 'paragraphs': paragraphs, 'images': relevant_images, 'og_img': og_img}
- except Exception:
- return None
-
-
@app.post('/api/rewrite_share')
async def compat_rewrite_share(request: Request):
body = await request.json()
@@ -481,53 +371,40 @@ async def compat_rewrite_share(request: Request):
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=1500)
- text = _postprocess_ai_text(text, max_units=20)
+ 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)
-
- # Generate slides with relevant images only
- slides = []
- page_data = _scrape_article_images(url)
- if page_data and page_data.get('paragraphs'):
- key_points = _extract_key_points_for_slides(page_data['paragraphs'], max_points=12)
- if key_points:
- relevant_imgs = page_data.get('images', [])
- if not relevant_imgs and page_data.get('og_img'):
- relevant_imgs = [page_data['og_img']]
- for i, point in enumerate(key_points):
- img = relevant_imgs[i] if i < len(relevant_imgs) else (relevant_imgs[-1] if relevant_imgs else '')
- slides.append({'text': point, 'image': img, 'index': i + 1})
-
- return JSONResponse({'post': post, 'slides': slides})
+ return JSONResponse({'post': post})
def _emotion_script(text, emotion):
- """Prepend emotion-appropriate prefix to text based on emotion type.
-
- NOTE: Prefix is NOT added to avoid cluttering Short AI speech.
- The emotion is still used for voice selection but content is read cleanly.
- """
text = _clean(text)
- # REMOVED: No prefix added to keep content clean and natural
+ 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) if hasattr(base, '_short_script') else _clean(post.get('text', '') or post.get('title', ''))
+ 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()
- # REMOVED: _emotion_script call - read content cleanly without prefix
- # INCREASED to 3000 to read full content of all bullet points
- if len(raw) > 3000:
- raw = raw[:3000]
+ raw = _emotion_script(raw, emotion)
+ if len(raw) > 1000:
+ raw = raw[:1000]
cut = max(raw.rfind("."), raw.rfind("!"), raw.rfind("?"))
- if cut > 700:
+ if cut > 350:
raw = raw[:cut + 1]
return raw
@@ -642,7 +519,7 @@ def _make_short_frame_full(post, img_path, out_path):
-def _summary_segments_from_post(post, max_segments=25):
+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()
@@ -653,7 +530,7 @@ def _summary_segments_from_post(post, max_segments=25):
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)<3:
+ if len(lines)<2:
lines=[]
for s in re.split(r'(?<=[\.\!\?])\s+', raw):
s=_clean(s)
@@ -705,8 +582,7 @@ def _make_scene_frame(post, segment, idx, total, img_path, out_path, emotion='ne
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
- # INCREASED from 12 to 18 for full content display - each key point can span multiple lines
- for ln in _wrap_text_px(draw, segment, font_seg, maxw, 18):
+ 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
@@ -718,11 +594,10 @@ def _make_scene_frame(post, segment, idx, total, img_path, out_path, emotion='ne
bg.save(out_path, quality=92)
-def _estimate_audio_duration(path, fallback=15.0):
- """Estimate audio duration with 15s minimum per segment for complete bullet reading."""
+def _estimate_audio_duration(path, fallback=4.0):
try:
- pr=subprocess.run(['ffprobe','-v','error','-show_entries','format=duration','-of','default=noprint_wrappers=1:no_key=1',path], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=20)
- return max(12.0, float((pr.stdout or b'').decode().strip() or fallback))
+ 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
@@ -735,7 +610,7 @@ async def patched_ai_short(post_id: str, request: Request):
body = {}
voice = str(body.get('voice', 'nu')).strip().lower()
emotion = str(body.get('emotion', 'neutral')).strip().lower()
- speed = float(body.get('speed', 1.0) or 1.0)
+ speed = float(body.get('speed', 1.2) or 1.2)
speed = max(0.85, min(1.35, speed))
posts = base._load_ai_wall()
@@ -743,7 +618,7 @@ async def patched_ai_short(post_id: str, request: Request):
if not post:
return JSONResponse({'error': 'post not found'}, status_code=404)
- segments = _summary_segments_from_post(post, max_segments=25)
+ 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"
@@ -766,63 +641,11 @@ async def patched_ai_short(post_id: str, request: Request):
try:
base._download_image(post.get('img'), post.get('title', 'AI news'), img)
edge_voice = {
- # Vietnamese
- 'vi-vn-hoaimyneural': 'vi-VN-HoaiMyNeural',
- 'vi-vn-namminhneural': 'vi-VN-NamMinhNeural',
- 'hoaimy': 'vi-VN-HoaiMyNeural',
- 'namminh': 'vi-VN-NamMinhNeural',
'nam': 'vi-VN-NamMinhNeural',
'male': 'vi-VN-NamMinhNeural',
'nu': 'vi-VN-HoaiMyNeural',
'female': 'vi-VN-HoaiMyNeural',
'mien-nam': 'vi-VN-HoaiMyNeural',
- # English - Multilingual
- 'en-us-andrewmultilingualneural': 'en-US-AndrewMultilingualNeural',
- 'en-au-williammultilingualneural': 'en-AU-WilliamMultilingualNeural',
- 'andrew': 'en-US-AndrewMultilingualNeural',
- 'en_andrew': 'en-US-AndrewMultilingualNeural',
- 'jenny': 'en-US-AndrewMultilingualNeural',
- 'en_jenny': 'en-US-AndrewMultilingualNeural',
- # Portuguese - Multilingual (ONLY Thalita)
- 'pt-br-thalitamultilingualneural': 'pt-BR-ThalitaMultilingualNeural',
- 'thalita': 'pt-BR-ThalitaMultilingualNeural',
- 'pt_thalita': 'pt-BR-ThalitaMultilingualNeural',
- 'pt_br_thalita': 'pt-BR-ThalitaMultilingualNeural',
- 'pt': 'pt-BR-ThalitaMultilingualNeural',
- 'pt_francisco': 'pt-BR-ThalitaMultilingualNeural',
- # French - Multilingual
- 'fr-fr-viviennemultilingualneural': 'fr-FR-VivienneMultilingualNeural',
- 'fr-fr-remymultilingualneural': 'fr-FR-RemyMultilingualNeural',
- 'denise': 'fr-FR-VivienneMultilingualNeural',
- 'fr': 'fr-FR-VivienneMultilingualNeural',
- 'fr_denise': 'fr-FR-VivienneMultilingualNeural',
- # German - Multilingual
- 'de-de-seraphinamultilingualneural': 'de-DE-SeraphinaMultilingualNeural',
- 'de-de-florianmultilingualneural': 'de-DE-FlorianMultilingualNeural',
- 'katja': 'de-DE-SeraphinaMultilingualNeural',
- 'de': 'de-DE-SeraphinaMultilingualNeural',
- 'de_katja': 'de-DE-SeraphinaMultilingualNeural',
- # Korean - Multilingual (Hyunsu, NOT SunHee)
- 'ko-kr-hyusumultilingualneural': 'ko-KR-HyunsuMultilingualNeural',
- 'ko-kr-hyunsuneural': 'ko-KR-HyunsuMultilingualNeural',
- 'sunhee': 'ko-KR-HyunsuMultilingualNeural',
- 'ko': 'ko-KR-HyunsuMultilingualNeural',
- 'ko_sunhee': 'ko-KR-HyunsuMultilingualNeural',
- # Italian - Multilingual
- 'it-it-giuseppemultilingualneural': 'it-IT-GiuseppeMultilingualNeural',
- # Spanish (keep for backward compat)
- 'ela': 'en-US-AndrewMultilingualNeural',
- 'es_ela': 'en-US-AndrewMultilingualNeural',
- 'es': 'en-US-AndrewMultilingualNeural',
- 'es_carlos': 'en-US-AndrewMultilingualNeural',
- # Japanese (keep for backward compat)
- 'nanami': 'en-US-AndrewMultilingualNeural',
- 'ja': 'en-US-AndrewMultilingualNeural',
- 'ja_nanami': 'en-US-AndrewMultilingualNeural',
- # Chinese (keep for backward compat)
- 'xiaochen': 'en-US-AndrewMultilingualNeural',
- 'zh': 'en-US-AndrewMultilingualNeural',
- 'zh_xiaochen': 'en-US-AndrewMultilingualNeural',
}.get(voice, 'vi-VN-HoaiMyNeural')
part_files=[]
for idx, seg in enumerate(segments):
@@ -835,13 +658,13 @@ async def patched_ai_short(post_id: str, request: Request):
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','hoaimy') else 'com'
+ 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=15.0)+0.35
+ 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')
@@ -876,3 +699,53 @@ def api_ai_shorts():
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'''
+
+🎬 Đang tạo Short AI
Đang chuẩn bị...
+
+'''
+
+@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('