bep40 commited on
Commit
92a492b
·
verified ·
1 Parent(s): ac767d5

Summaries only and add AI shorts video generation from wall posts

Browse files
Files changed (1) hide show
  1. ai_ext.py +164 -20
ai_ext.py CHANGED
@@ -1,10 +1,10 @@
1
- import os, re, json, time, random, html as html_lib
2
  from urllib.parse import quote_plus, quote, urlparse
3
  from typing import Optional
4
  import requests
5
  from bs4 import BeautifulSoup
6
  from fastapi import Request
7
- from fastapi.responses import HTMLResponse, JSONResponse
8
 
9
  from main import app
10
 
@@ -12,8 +12,16 @@ try:
12
  from huggingface_hub import AsyncInferenceClient
13
  except Exception:
14
  AsyncInferenceClient = None
 
 
 
 
 
 
 
 
 
15
 
16
- # Accept all common HF token env names used by Spaces/Hub examples.
17
  def _hf_token():
18
  for k in ("HF_TOKEN", "HUGGINGFACEHUB_API_TOKEN", "HUGGING_FACE_HUB_TOKEN", "HF_API_TOKEN"):
19
  v = os.getenv(k, "").strip()
@@ -23,10 +31,13 @@ def _hf_token():
23
 
24
  HF_TOKEN = _hf_token()
25
  QWEN_VL_MODEL = os.getenv("QWEN_VL_MODEL", "Qwen/Qwen2.5-VL-7B-Instruct")
26
- WALL_FILE = "/data/ai_wall_posts.json" if os.path.isdir("/data") else "/app/ai_wall_posts.json"
 
 
27
  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"}
28
  LAST_QWEN_ERROR = ""
29
 
 
30
  def _load_ai_wall():
31
  try:
32
  if os.path.exists(WALL_FILE):
@@ -36,6 +47,7 @@ def _load_ai_wall():
36
  pass
37
  return []
38
 
 
39
  def _save_ai_wall(posts):
40
  try:
41
  os.makedirs(os.path.dirname(WALL_FILE), exist_ok=True)
@@ -46,26 +58,31 @@ def _save_ai_wall(posts):
46
  except Exception:
47
  pass
48
 
 
49
  def _clean_text(s: str) -> str:
50
  s = html_lib.unescape(s or "")
51
  return re.sub(r"\s+", " ", s).strip()
52
 
 
53
  def _domain(u):
54
  try:
55
  return urlparse(u).netloc.replace("www.", "")
56
  except Exception:
57
  return ""
58
 
 
59
  def _reader_url(target_url: str) -> str:
60
  safe = quote(target_url, safe=":/?#[]@!$&'()*+,;=%")
61
  return "https://r.jina.ai/http://" + safe
62
 
 
63
  def jina_reader_markdown(url: str) -> str:
64
  jr = _reader_url(url)
65
  r = requests.get(jr, headers={"Accept":"text/markdown,text/plain,*/*", "X-Return-Format":"markdown", "User-Agent":"Mozilla/5.0"}, timeout=35)
66
  r.raise_for_status()
67
  return r.text or ""
68
 
 
69
  def _parse_jina_markdown(md: str, url: str):
70
  lines = [x.rstrip() for x in (md or "").splitlines()]
71
  title = ""; image = ""; content_lines = []; in_content = False
@@ -92,6 +109,7 @@ def _parse_jina_markdown(md: str, url: str):
92
  if not title and paras: title = paras[0][:90]
93
  return {"url": url, "title": title or url, "summary": paras[0] if paras else "", "text": "\n".join(paras), "image": image, "images": [image] if image else [], "via":"jina"}
94
 
 
95
  def _best_content_block(soup):
96
  best, best_score = None, 0
97
  for el in soup.find_all(["article", "main", "section", "div"]):
@@ -103,6 +121,7 @@ def _best_content_block(soup):
103
  if score > best_score: best, best_score = el, score
104
  return best
105
 
 
106
  def scrape_any_url_direct(url: str):
107
  r = requests.get(url, headers=HEADERS, timeout=18)
108
  if r.status_code in {401,403,406,409,429,451,503}: raise RuntimeError(f"blocked status {r.status_code}")
@@ -135,6 +154,7 @@ def scrape_any_url_direct(url: str):
135
  if len(data["text"]) < 160: raise RuntimeError("direct scrape too short")
136
  return data
137
 
 
138
  def scrape_any_url(url: str):
139
  try:
140
  return scrape_any_url_direct(url)
@@ -145,10 +165,12 @@ def scrape_any_url(url: str):
145
  raise RuntimeError(f"Jina Reader also returned too little content; direct={direct_error}")
146
  return data
147
 
 
148
  def pollinations_image_url(topic: str, width=1024, height=576):
149
  prompt = f"editorial illustration, topic: {topic}, modern clean cinematic news image, high quality, no text, no watermark"
150
  return f"https://image.pollinations.ai/prompt/{quote_plus(prompt)}?width={width}&height={height}&seed={random.randint(1,999999)}&nologo=true&private=true"
151
 
 
152
  def jina_search(query: str, limit=6):
153
  for accept in ("application/json", "text/plain"):
154
  try:
@@ -174,6 +196,7 @@ def jina_search(query: str, limit=6):
174
  pass
175
  return []
176
 
 
177
  def duck_context(topic: str, limit=6):
178
  try:
179
  url = "https://duckduckgo.com/html/?q=" + quote_plus(topic + " tin tức phân tích bối cảnh")
@@ -187,6 +210,7 @@ def duck_context(topic: str, limit=6):
187
  return results
188
  except Exception: return []
189
 
 
190
  def web_context(topic: str, limit=6):
191
  queries = [topic + " tin tức phân tích bối cảnh", topic]
192
  results=[]
@@ -206,6 +230,7 @@ def web_context(topic: str, limit=6):
206
  context = "\n".join([f"- {x.get('title') or _domain(x.get('url',''))} ({_domain(x.get('url',''))}): {x.get('excerpt') or x.get('description','')}" for x in enriched if x.get("excerpt") or x.get("title")])
207
  return context, enriched
208
 
 
209
  async def qwen_generate(prompt: str, image_url: Optional[str] = None, max_tokens: int = 1200):
210
  global LAST_QWEN_ERROR, HF_TOKEN
211
  HF_TOKEN = _hf_token()
@@ -215,8 +240,7 @@ async def qwen_generate(prompt: str, image_url: Optional[str] = None, max_tokens
215
  if not AsyncInferenceClient:
216
  LAST_QWEN_ERROR = "Thiếu thư viện huggingface_hub hoặc import lỗi."
217
  return None
218
- errors=[]
219
- models=[]
220
  for m in [QWEN_VL_MODEL, "Qwen/Qwen2.5-VL-7B-Instruct", "Qwen/Qwen2.5-VL-3B-Instruct"]:
221
  if m and m not in models: models.append(m)
222
  for model in models:
@@ -229,38 +253,41 @@ async def qwen_generate(prompt: str, image_url: Optional[str] = None, max_tokens
229
  resp=await client.chat_completion(model=model, messages=messages, max_tokens=max_tokens, temperature=0.45, top_p=0.85)
230
  txt=(resp.choices[0].message.content or "").strip()
231
  if txt:
232
- LAST_QWEN_ERROR=""
233
- return txt
234
  except Exception as e:
235
  errors.append(f"{model}: {type(e).__name__}: {str(e)[:220]}")
236
  LAST_QWEN_ERROR = " | ".join(errors) or "Qwen không trả nội dung."
237
  print("[qwen errors]", LAST_QWEN_ERROR)
238
  return None
239
 
 
240
  def make_post(title, text, image, source_url, kind, sources=None):
241
- 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 [],"ts":int(time.time())}
 
242
 
243
  @app.get("/api/ai_wall")
244
  def api_ai_wall(): return JSONResponse({"posts":_load_ai_wall()[:80]})
245
 
 
246
  @app.get("/api/ai/status")
247
  def api_ai_status():
248
- return JSONResponse({"has_token":bool(_hf_token()),"client_imported":AsyncInferenceClient is not None,"model":QWEN_VL_MODEL,"last_error":LAST_QWEN_ERROR})
 
249
 
250
  @app.post("/api/ai/topic")
251
  async def api_ai_topic(request:Request):
252
- body=await request.json();topic=_clean_text(body.get("topic",""))
253
  if not topic: return JSONResponse({"error":"missing topic"},status_code=400)
254
  ctx,sources=web_context(topic)
255
  if not ctx: return JSONResponse({"error":"Không lấy được dữ liệu internet cho chủ đề này. Hãy thử chủ đề cụ thể hơn."},status_code=422)
256
  image=pollinations_image_url(topic)
257
- prompt=f"""Tạo một bản tóm tắt chủ đề thật ngắn gọn cho Tường AI.
258
 
259
  Chủ đề: {topic}
260
 
261
  Yêu cầu:
262
  - Chỉ tóm tắt các ý chính dựa trên nguồn internet bên dưới.
263
- - Không viết thành bài dài, không triển khai lan man.
264
  - 1 tiêu đề ngắn.
265
  - 3-5 gạch đầu dòng, mỗi dòng tối đa 1 câu.
266
  - 1 câu kết luận ngắn.
@@ -270,12 +297,13 @@ Nguồn/bối cảnh internet:
270
  {ctx}"""
271
  text=await qwen_generate(prompt,image_url=image,max_tokens=900)
272
  if not text: return JSONResponse({"error":"Qwen2.5-VL chưa sẵn sàng: "+LAST_QWEN_ERROR},status_code=503)
273
- post=make_post(topic,text,image,"","topic",sources=sources[:5]);posts=_load_ai_wall();posts.insert(0,post);_save_ai_wall(posts)
274
  return JSONResponse({"post":post})
275
 
 
276
  @app.post("/api/ai/url")
277
  async def api_ai_url(request:Request):
278
- body=await request.json();url=_clean_text(body.get("url",""))
279
  if not url.startswith("http"): return JSONResponse({"error":"missing url"},status_code=400)
280
  try: data=scrape_any_url(url)
281
  except Exception as e: return JSONResponse({"error":"Không scrape được URL: "+str(e)[:180]},status_code=422)
@@ -298,12 +326,13 @@ Nội dung gốc:
298
  text=await qwen_generate(prompt,image_url=data.get("image") or None,max_tokens=900)
299
  if not text: return JSONResponse({"error":"Qwen2.5-VL chưa sẵn sàng: "+LAST_QWEN_ERROR},status_code=503)
300
  post=make_post(data.get("title") or "Bài viết",text,data.get("image") or "",url,"url",sources=[{"title":data.get("title"),"url":url,"excerpt":raw[:500],"via":data.get("via")}])
301
- posts=_load_ai_wall();posts.insert(0,post);_save_ai_wall(posts)
302
  return JSONResponse({"post":post})
303
 
 
304
  @app.post("/api/rewrite_share")
305
  async def api_rewrite_share(request:Request):
306
- body=await request.json();url=_clean_text(body.get("url",""))
307
  if not url.startswith("http"): return JSONResponse({"error":"missing url"},status_code=400)
308
  try: data=scrape_any_url(url)
309
  except Exception as e: return JSONResponse({"error":"Không đọc được bài viết: "+str(e)[:180]},status_code=422)
@@ -322,13 +351,128 @@ Nội dung:
322
  text=await qwen_generate(prompt,image_url=data.get("image") or None,max_tokens=900)
323
  if not text: return JSONResponse({"error":"Qwen2.5-VL chưa sẵn sàng: "+LAST_QWEN_ERROR},status_code=503)
324
  post=make_post(data.get("title") or "Bài viết",text,data.get("image") or "",url,"rewrite")
325
- posts=_load_ai_wall();posts.insert(0,post);_save_ai_wall(posts)
326
  return JSONResponse({"post":post})
327
 
328
- # Override index route to show only Tường AI slide, no redundant compose inputs.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
329
  app.router.routes=[r for r in app.router.routes if not (getattr(r,'path',None)=='/' and 'GET' in getattr(r,'methods',set()))]
330
  AI_INJECT=r'''
331
- <style>.ai-wall-extra{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-card button{margin-top:8px;width:100%;border:1px solid #333;background:#222;color:#ddd;border-radius:14px;padding:6px 8px;font-size:10px}</style><script>(function(){function esc(s){return String(s||'').replace(/[&<>"']/g,m=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[m]));}let aiWall=[];async function refreshAiWall(){try{const r=await fetch('/api/ai_wall');const j=await r.json();aiWall=j.posts||[];renderAiWall();}catch(e){}}function renderAiWall(){const home=document.getElementById('view-home');if(!home)return;let old=document.getElementById('ai-wall-block');if(old)old.remove();if(!aiWall.length)return;let wrap=document.createElement('div');wrap.id='ai-wall-block';wrap.className='ai-wall-extra';let h='<div class="slider-header"><span class="slider-label">🧱 Tường AI</span></div><div class="slider-track">';aiWall.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><button onclick="aiReadWall(${i})">Xem đầy đủ</button></div>`});h+='</div>';wrap.innerHTML=h;home.prepend(wrap)}window.aiReadWall=function(i){const p=aiWall[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 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><div class="article-actions">${p.url?`<button onclick="window.open('${p.url}','_blank')">🔗 Nguồn</button>`:''}</div></div>`;document.getElementById('view-article').innerHTML=h;window.scrollTo(0,0)};const oldLoad=window.loadHome;if(typeof oldLoad==='function'){window.loadHome=async function(){await oldLoad.apply(this,arguments);setTimeout(refreshAiWall,50);};}setTimeout(refreshAiWall,1200);})();</script>'''
332
  @app.get('/')
333
  async def index_ai():
334
  with open('/app/static/index.html','r',encoding='utf-8') as f: html=f.read()
 
1
+ import os, re, json, time, random, html as html_lib, subprocess, uuid
2
  from urllib.parse import quote_plus, quote, urlparse
3
  from typing import Optional
4
  import requests
5
  from bs4 import BeautifulSoup
6
  from fastapi import Request
7
+ from fastapi.responses import HTMLResponse, JSONResponse, FileResponse
8
 
9
  from main import app
10
 
 
12
  from huggingface_hub import AsyncInferenceClient
13
  except Exception:
14
  AsyncInferenceClient = None
15
+ try:
16
+ from gtts import gTTS
17
+ except Exception:
18
+ gTTS = None
19
+ try:
20
+ from PIL import Image, ImageDraw, ImageFont
21
+ except Exception:
22
+ Image = ImageDraw = ImageFont = None
23
+
24
 
 
25
  def _hf_token():
26
  for k in ("HF_TOKEN", "HUGGINGFACEHUB_API_TOKEN", "HUGGING_FACE_HUB_TOKEN", "HF_API_TOKEN"):
27
  v = os.getenv(k, "").strip()
 
31
 
32
  HF_TOKEN = _hf_token()
33
  QWEN_VL_MODEL = os.getenv("QWEN_VL_MODEL", "Qwen/Qwen2.5-VL-7B-Instruct")
34
+ DATA_DIR = "/data" if os.path.isdir("/data") else "/app/data"
35
+ WALL_FILE = os.path.join(DATA_DIR, "ai_wall_posts.json")
36
+ SHORTS_DIR = os.path.join(DATA_DIR, "ai_shorts")
37
  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"}
38
  LAST_QWEN_ERROR = ""
39
 
40
+
41
  def _load_ai_wall():
42
  try:
43
  if os.path.exists(WALL_FILE):
 
47
  pass
48
  return []
49
 
50
+
51
  def _save_ai_wall(posts):
52
  try:
53
  os.makedirs(os.path.dirname(WALL_FILE), exist_ok=True)
 
58
  except Exception:
59
  pass
60
 
61
+
62
  def _clean_text(s: str) -> str:
63
  s = html_lib.unescape(s or "")
64
  return re.sub(r"\s+", " ", s).strip()
65
 
66
+
67
  def _domain(u):
68
  try:
69
  return urlparse(u).netloc.replace("www.", "")
70
  except Exception:
71
  return ""
72
 
73
+
74
  def _reader_url(target_url: str) -> str:
75
  safe = quote(target_url, safe=":/?#[]@!$&'()*+,;=%")
76
  return "https://r.jina.ai/http://" + safe
77
 
78
+
79
  def jina_reader_markdown(url: str) -> str:
80
  jr = _reader_url(url)
81
  r = requests.get(jr, headers={"Accept":"text/markdown,text/plain,*/*", "X-Return-Format":"markdown", "User-Agent":"Mozilla/5.0"}, timeout=35)
82
  r.raise_for_status()
83
  return r.text or ""
84
 
85
+
86
  def _parse_jina_markdown(md: str, url: str):
87
  lines = [x.rstrip() for x in (md or "").splitlines()]
88
  title = ""; image = ""; content_lines = []; in_content = False
 
109
  if not title and paras: title = paras[0][:90]
110
  return {"url": url, "title": title or url, "summary": paras[0] if paras else "", "text": "\n".join(paras), "image": image, "images": [image] if image else [], "via":"jina"}
111
 
112
+
113
  def _best_content_block(soup):
114
  best, best_score = None, 0
115
  for el in soup.find_all(["article", "main", "section", "div"]):
 
121
  if score > best_score: best, best_score = el, score
122
  return best
123
 
124
+
125
  def scrape_any_url_direct(url: str):
126
  r = requests.get(url, headers=HEADERS, timeout=18)
127
  if r.status_code in {401,403,406,409,429,451,503}: raise RuntimeError(f"blocked status {r.status_code}")
 
154
  if len(data["text"]) < 160: raise RuntimeError("direct scrape too short")
155
  return data
156
 
157
+
158
  def scrape_any_url(url: str):
159
  try:
160
  return scrape_any_url_direct(url)
 
165
  raise RuntimeError(f"Jina Reader also returned too little content; direct={direct_error}")
166
  return data
167
 
168
+
169
  def pollinations_image_url(topic: str, width=1024, height=576):
170
  prompt = f"editorial illustration, topic: {topic}, modern clean cinematic news image, high quality, no text, no watermark"
171
  return f"https://image.pollinations.ai/prompt/{quote_plus(prompt)}?width={width}&height={height}&seed={random.randint(1,999999)}&nologo=true&private=true"
172
 
173
+
174
  def jina_search(query: str, limit=6):
175
  for accept in ("application/json", "text/plain"):
176
  try:
 
196
  pass
197
  return []
198
 
199
+
200
  def duck_context(topic: str, limit=6):
201
  try:
202
  url = "https://duckduckgo.com/html/?q=" + quote_plus(topic + " tin tức phân tích bối cảnh")
 
210
  return results
211
  except Exception: return []
212
 
213
+
214
  def web_context(topic: str, limit=6):
215
  queries = [topic + " tin tức phân tích bối cảnh", topic]
216
  results=[]
 
230
  context = "\n".join([f"- {x.get('title') or _domain(x.get('url',''))} ({_domain(x.get('url',''))}): {x.get('excerpt') or x.get('description','')}" for x in enriched if x.get("excerpt") or x.get("title")])
231
  return context, enriched
232
 
233
+
234
  async def qwen_generate(prompt: str, image_url: Optional[str] = None, max_tokens: int = 1200):
235
  global LAST_QWEN_ERROR, HF_TOKEN
236
  HF_TOKEN = _hf_token()
 
240
  if not AsyncInferenceClient:
241
  LAST_QWEN_ERROR = "Thiếu thư viện huggingface_hub hoặc import lỗi."
242
  return None
243
+ errors=[]; models=[]
 
244
  for m in [QWEN_VL_MODEL, "Qwen/Qwen2.5-VL-7B-Instruct", "Qwen/Qwen2.5-VL-3B-Instruct"]:
245
  if m and m not in models: models.append(m)
246
  for model in models:
 
253
  resp=await client.chat_completion(model=model, messages=messages, max_tokens=max_tokens, temperature=0.45, top_p=0.85)
254
  txt=(resp.choices[0].message.content or "").strip()
255
  if txt:
256
+ LAST_QWEN_ERROR=""; return txt
 
257
  except Exception as e:
258
  errors.append(f"{model}: {type(e).__name__}: {str(e)[:220]}")
259
  LAST_QWEN_ERROR = " | ".join(errors) or "Qwen không trả nội dung."
260
  print("[qwen errors]", LAST_QWEN_ERROR)
261
  return None
262
 
263
+
264
  def make_post(title, text, image, source_url, kind, sources=None):
265
+ 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":"","ts":int(time.time())}
266
+
267
 
268
  @app.get("/api/ai_wall")
269
  def api_ai_wall(): return JSONResponse({"posts":_load_ai_wall()[:80]})
270
 
271
+
272
  @app.get("/api/ai/status")
273
  def api_ai_status():
274
+ 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})
275
+
276
 
277
  @app.post("/api/ai/topic")
278
  async def api_ai_topic(request:Request):
279
+ body=await request.json(); topic=_clean_text(body.get("topic",""))
280
  if not topic: return JSONResponse({"error":"missing topic"},status_code=400)
281
  ctx,sources=web_context(topic)
282
  if not ctx: return JSONResponse({"error":"Không lấy được dữ liệu internet cho chủ đề này. Hãy thử chủ đề cụ thể hơn."},status_code=422)
283
  image=pollinations_image_url(topic)
284
+ prompt=f"""Tóm tắt chủ đề để đăng Tường AI.
285
 
286
  Chủ đề: {topic}
287
 
288
  Yêu cầu:
289
  - Chỉ tóm tắt các ý chính dựa trên nguồn internet bên dưới.
290
+ - Không viết lại thành bài dài, không lan man, không lặp lại.
291
  - 1 tiêu đề ngắn.
292
  - 3-5 gạch đầu dòng, mỗi dòng tối đa 1 câu.
293
  - 1 câu kết luận ngắn.
 
297
  {ctx}"""
298
  text=await qwen_generate(prompt,image_url=image,max_tokens=900)
299
  if not text: return JSONResponse({"error":"Qwen2.5-VL chưa sẵn sàng: "+LAST_QWEN_ERROR},status_code=503)
300
+ post=make_post(topic,text,image,"","topic",sources=sources[:5]); posts=_load_ai_wall(); posts.insert(0,post); _save_ai_wall(posts)
301
  return JSONResponse({"post":post})
302
 
303
+
304
  @app.post("/api/ai/url")
305
  async def api_ai_url(request:Request):
306
+ body=await request.json(); url=_clean_text(body.get("url",""))
307
  if not url.startswith("http"): return JSONResponse({"error":"missing url"},status_code=400)
308
  try: data=scrape_any_url(url)
309
  except Exception as e: return JSONResponse({"error":"Không scrape được URL: "+str(e)[:180]},status_code=422)
 
326
  text=await qwen_generate(prompt,image_url=data.get("image") or None,max_tokens=900)
327
  if not text: return JSONResponse({"error":"Qwen2.5-VL chưa sẵn sàng: "+LAST_QWEN_ERROR},status_code=503)
328
  post=make_post(data.get("title") or "Bài viết",text,data.get("image") or "",url,"url",sources=[{"title":data.get("title"),"url":url,"excerpt":raw[:500],"via":data.get("via")}])
329
+ posts=_load_ai_wall(); posts.insert(0,post); _save_ai_wall(posts)
330
  return JSONResponse({"post":post})
331
 
332
+
333
  @app.post("/api/rewrite_share")
334
  async def api_rewrite_share(request:Request):
335
+ body=await request.json(); url=_clean_text(body.get("url",""))
336
  if not url.startswith("http"): return JSONResponse({"error":"missing url"},status_code=400)
337
  try: data=scrape_any_url(url)
338
  except Exception as e: return JSONResponse({"error":"Không đọc được bài viết: "+str(e)[:180]},status_code=422)
 
351
  text=await qwen_generate(prompt,image_url=data.get("image") or None,max_tokens=900)
352
  if not text: return JSONResponse({"error":"Qwen2.5-VL chưa sẵn sàng: "+LAST_QWEN_ERROR},status_code=503)
353
  post=make_post(data.get("title") or "Bài viết",text,data.get("image") or "",url,"rewrite")
354
+ posts=_load_ai_wall(); posts.insert(0,post); _save_ai_wall(posts)
355
  return JSONResponse({"post":post})
356
 
357
+
358
+ def _safe_name(s): return re.sub(r"[^a-zA-Z0-9_-]+", "_", s)[:80]
359
+
360
+
361
+ def _wrap_text(text, width=32, max_lines=10):
362
+ words=_clean_text(text).split(); lines=[]; cur=""
363
+ for w in words:
364
+ if len(cur)+len(w)+1<=width: cur=(cur+" "+w).strip()
365
+ else:
366
+ if cur: lines.append(cur)
367
+ cur=w
368
+ if len(lines)>=max_lines: break
369
+ if cur and len(lines)<max_lines: lines.append(cur)
370
+ return "\n".join(lines)
371
+
372
+
373
+ def _make_short_frame(post, img_path, out_path):
374
+ if Image is None: raise RuntimeError("Pillow chưa sẵn sàng")
375
+ W,H=1080,1920
376
+ bg=Image.new("RGB",(W,H),(14,14,14))
377
+ try:
378
+ im=Image.open(img_path).convert("RGB")
379
+ # cover top area 1080x860
380
+ target=(1080,860)
381
+ im_ratio=im.width/im.height; target_ratio=target[0]/target[1]
382
+ if im_ratio>target_ratio:
383
+ new_h=target[1]; new_w=int(new_h*im_ratio)
384
+ else:
385
+ new_w=target[0]; new_h=int(new_w/im_ratio)
386
+ im=im.resize((new_w,new_h))
387
+ left=(new_w-target[0])//2; top=(new_h-target[1])//2
388
+ im=im.crop((left,top,left+target[0],top+target[1]))
389
+ bg.paste(im,(0,0))
390
+ except Exception:
391
+ pass
392
+ draw=ImageDraw.Draw(bg)
393
+ try:
394
+ font_title=ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",52)
395
+ font_body=ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",40)
396
+ font_label=ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",32)
397
+ except Exception:
398
+ font_title=font_body=font_label=None
399
+ draw.rectangle((0,780,W,H),fill=(14,14,14))
400
+ draw.text((54,830),"VNEWS · Tường AI",fill=(92,184,122),font=font_label)
401
+ title=_wrap_text(post.get("title",""),24,3)
402
+ draw.multiline_text((54,900),title,fill=(255,255,255),font=font_title,spacing=10)
403
+ body=_wrap_text(post.get("text",""),34,10)
404
+ draw.multiline_text((54,1120),body,fill=(220,220,220),font=font_body,spacing=12)
405
+ bg.save(out_path,quality=92)
406
+
407
+
408
+ def _download_image(url, fallback_topic, out_path):
409
+ if url:
410
+ try:
411
+ r=requests.get(url,headers=HEADERS,timeout=15)
412
+ if r.status_code==200 and len(r.content)>1000:
413
+ with open(out_path,"wb") as f:f.write(r.content)
414
+ return out_path
415
+ except Exception: pass
416
+ # fallback generated image
417
+ gen=pollinations_image_url(fallback_topic)
418
+ try:
419
+ r=requests.get(gen,headers=HEADERS,timeout=25)
420
+ if r.status_code==200 and len(r.content)>1000:
421
+ with open(out_path,"wb") as f:f.write(r.content)
422
+ return out_path
423
+ except Exception: pass
424
+ if Image:
425
+ Image.new("RGB",(1080,860),(30,55,42)).save(out_path)
426
+ return out_path
427
+ raise RuntimeError("Không tạo được ảnh")
428
+
429
+
430
+ def _short_script(post):
431
+ txt=_clean_text(post.get("text",""))
432
+ # TTS should read concise summary, not entire huge post.
433
+ if len(txt)>900: txt=txt[:900].rsplit(" ",1)[0]+"."
434
+ return f"{post.get('title','')}. {txt}"
435
+
436
+
437
+ @app.post("/api/ai/short/{post_id}")
438
+ def api_ai_short(post_id:str):
439
+ posts=_load_ai_wall(); post=next((p for p in posts if str(p.get("id"))==str(post_id)),None)
440
+ if not post: return JSONResponse({"error":"post not found"},status_code=404)
441
+ os.makedirs(SHORTS_DIR,exist_ok=True)
442
+ out_mp4=os.path.join(SHORTS_DIR,_safe_name(post_id)+".mp4")
443
+ if os.path.exists(out_mp4):
444
+ post["video"]="/api/ai/short-file/"+post_id
445
+ _save_ai_wall(posts)
446
+ return JSONResponse({"video":post["video"]})
447
+ if gTTS is None: return JSONResponse({"error":"gTTS chưa sẵn sàng"},status_code=503)
448
+ work=os.path.join(SHORTS_DIR,_safe_name(post_id))
449
+ os.makedirs(work,exist_ok=True)
450
+ img=os.path.join(work,"image.jpg"); frame=os.path.join(work,"frame.jpg"); audio=os.path.join(work,"voice.mp3")
451
+ try:
452
+ _download_image(post.get("img"), post.get("title","AI news"), img)
453
+ _make_short_frame(post,img,frame)
454
+ script=_short_script(post)
455
+ gTTS(script,lang="vi").save(audio)
456
+ cmd=["ffmpeg","-y","-loop","1","-i",frame,"-i",audio,"-shortest","-c:v","libx264","-tune","stillimage","-pix_fmt","yuv420p","-c:a","aac","-b:a","128k","-vf","scale=1080:1920",out_mp4]
457
+ subprocess.run(cmd,check=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,timeout=180)
458
+ post["video"]="/api/ai/short-file/"+post_id
459
+ _save_ai_wall(posts)
460
+ return JSONResponse({"video":post["video"]})
461
+ except Exception as e:
462
+ return JSONResponse({"error":"Không tạo được shorts: "+str(e)[:180]},status_code=500)
463
+
464
+
465
+ @app.get("/api/ai/short-file/{post_id}")
466
+ def api_ai_short_file(post_id:str):
467
+ path=os.path.join(SHORTS_DIR,_safe_name(post_id)+".mp4")
468
+ if not os.path.exists(path): return JSONResponse({"error":"not found"},status_code=404)
469
+ return FileResponse(path,media_type="video/mp4",filename=f"vnews-ai-{post_id}.mp4")
470
+
471
+
472
+ # Override index route to show Tường AI slide + shorts generation.
473
  app.router.routes=[r for r in app.router.routes if not (getattr(r,'path',None)=='/' and 'GET' in getattr(r,'methods',set()))]
474
  AI_INJECT=r'''
475
+ <style>.ai-wall-extra{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{flex:1;border:1px solid #333;background:#222;color:#ddd;border-radius:14px;padding:6px 8px;font-size:10px}.ai-wall-actions button.primary{background:#2d8659;border-color:#2d8659;color:#fff}</style><script>(function(){function esc(s){return String(s||'').replace(/[&<>"']/g,m=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[m]));}let aiWall=[];async function refreshAiWall(){try{const r=await fetch('/api/ai_wall');const j=await r.json();aiWall=j.posts||[];renderAiWall();}catch(e){}}function renderAiWall(){const home=document.getElementById('view-home');if(!home)return;let old=document.getElementById('ai-wall-block');if(old)old.remove();if(!aiWall.length)return;let wrap=document.createElement('div');wrap.id='ai-wall-block';wrap.className='ai-wall-extra';let h='<div class="slider-header"><span class="slider-label">🧱 Tường AI</span></div><div class="slider-track">';aiWall.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="aiReadWall(${i})">Xem</button><button class="primary" onclick="aiMakeShort(${i})">Shorts</button></div></div>`});h+='</div>';wrap.innerHTML=h;home.prepend(wrap)}window.aiReadWall=function(i){const p=aiWall[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 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>`:''}<button onclick="aiMakeShort(${i})">🎬 Tạo video shorts</button></div></div>`;document.getElementById('view-article').innerHTML=h;window.scrollTo(0,0)};window.aiMakeShort=async function(i){const p=aiWall[i];if(!p)return;try{alert('Đang tạo shorts AI, vui lòng chờ...');const r=await fetch('/api/ai/short/'+p.id,{method:'POST'});const j=await r.json();if(!r.ok||j.error)throw new Error(j.error||'Lỗi');p.video=j.video;aiReadWall(i);refreshAiWall();}catch(e){alert(e.message)}};const oldLoad=window.loadHome;if(typeof oldLoad==='function'){window.loadHome=async function(){await oldLoad.apply(this,arguments);setTimeout(refreshAiWall,50);};}setTimeout(refreshAiWall,1200);})();</script>'''
476
  @app.get('/')
477
  async def index_ai():
478
  with open('/app/static/index.html','r',encoding='utf-8') as f: html=f.read()