Spaces:
Running
Running
Upload ai_runtime_final3.py with huggingface_hub
Browse files- ai_runtime_final3.py +191 -0
ai_runtime_final3.py
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Final3 runtime: Qwen topic posts, robust YouTube shorts, TikTok-style actions for Shorts and Short AI."""
|
| 2 |
+
import os, re, time, json, hashlib, requests
|
| 3 |
+
from urllib.parse import quote, urlparse
|
| 4 |
+
import ai_runtime_final2 as f2
|
| 5 |
+
from ai_runtime_final2 import app, base, rt, HTMLResponse, JSONResponse, Request, Query
|
| 6 |
+
|
| 7 |
+
SPACE_URL="https://bep40-vnews.hf.space"
|
| 8 |
+
SHORT_CHANNELS=["baodantri7941","baosuckhoedoisongboyte"]
|
| 9 |
+
_SHORTS_CACHE={"t":0,"d":[]}
|
| 10 |
+
AI_INTERACTIONS_FILE="/data/ai_interactions.json" if os.path.isdir('/data') else "/app/data/ai_interactions.json"
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def clean(s):
|
| 14 |
+
import html as html_lib
|
| 15 |
+
return re.sub(r"\s+"," ",html_lib.unescape(s or "")).strip()
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def _domain(u):
|
| 19 |
+
try:return urlparse(u or '').netloc.replace('www.','')
|
| 20 |
+
except Exception:return ''
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def _load_json(path,default):
|
| 24 |
+
try:
|
| 25 |
+
if os.path.exists(path):
|
| 26 |
+
with open(path,'r',encoding='utf-8') as f:return json.load(f)
|
| 27 |
+
except Exception:pass
|
| 28 |
+
return default
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def _save_json(path,data):
|
| 32 |
+
try:
|
| 33 |
+
os.makedirs(os.path.dirname(path),exist_ok=True);tmp=path+'.tmp'
|
| 34 |
+
with open(tmp,'w',encoding='utf-8') as f:json.dump(data,f,ensure_ascii=False)
|
| 35 |
+
os.replace(tmp,path)
|
| 36 |
+
except Exception:pass
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def _youtube_shorts_ytdlp(handle,count=20):
|
| 40 |
+
try:
|
| 41 |
+
import yt_dlp
|
| 42 |
+
url=f"https://www.youtube.com/@{handle}/shorts"
|
| 43 |
+
opts={'quiet':True,'extract_flat':True,'skip_download':True,'playlistend':count,'ignoreerrors':True,'no_warnings':True}
|
| 44 |
+
with yt_dlp.YoutubeDL(opts) as ydl:
|
| 45 |
+
info=ydl.extract_info(url,download=False)
|
| 46 |
+
out=[]
|
| 47 |
+
for e in (info or {}).get('entries') or []:
|
| 48 |
+
vid=e.get('id') or ''
|
| 49 |
+
if not re.match(r'^[A-Za-z0-9_-]{11}$',vid):continue
|
| 50 |
+
title=e.get('title') or 'YouTube Short'
|
| 51 |
+
out.append({'title':title,'link':f'https://www.youtube.com/watch?v={vid}','img':f'https://i.ytimg.com/vi/{vid}/hqdefault.jpg','source':'yt','id':vid,'channel':handle})
|
| 52 |
+
return out
|
| 53 |
+
except Exception:return []
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def _youtube_shorts_html(handle,count=20):
|
| 57 |
+
try:
|
| 58 |
+
html=requests.get(f"https://www.youtube.com/@{handle}/shorts",headers=getattr(base,'HEADERS',{}),timeout=15).text
|
| 59 |
+
ids=[];out=[]
|
| 60 |
+
for m in re.finditer(r'"videoId":"([A-Za-z0-9_-]{11})"',html):
|
| 61 |
+
vid=m.group(1)
|
| 62 |
+
if vid in ids:continue
|
| 63 |
+
ids.append(vid)
|
| 64 |
+
snip=html[max(0,m.start()-1000):m.start()+1800]
|
| 65 |
+
title='YouTube Short'
|
| 66 |
+
mt=re.search(r'"title":\{"runs":\[\{"text":"([^"]+)"',snip) or re.search(r'"accessibilityText":"([^"]+)"',snip)
|
| 67 |
+
if mt:title=clean(mt.group(1).replace('\\n',' '))
|
| 68 |
+
out.append({'title':title,'link':f'https://www.youtube.com/watch?v={vid}','img':f'https://i.ytimg.com/vi/{vid}/hqdefault.jpg','source':'yt','id':vid,'channel':handle})
|
| 69 |
+
if len(out)>=count:break
|
| 70 |
+
return out
|
| 71 |
+
except Exception:return []
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def _fresh_shorts():
|
| 75 |
+
items=[];seen=set()
|
| 76 |
+
for ch in SHORT_CHANNELS:
|
| 77 |
+
got=_youtube_shorts_ytdlp(ch,24) or _youtube_shorts_html(ch,24)
|
| 78 |
+
for v in got:
|
| 79 |
+
if v['id'] not in seen:
|
| 80 |
+
seen.add(v['id']);items.append(v)
|
| 81 |
+
# fallback from main if live scrape fails
|
| 82 |
+
try:
|
| 83 |
+
for v in getattr(rt.old.base if hasattr(rt.old,'base') else rt,'SHORTS_FALLBACK',[]) or []:
|
| 84 |
+
vid=v.get('id')
|
| 85 |
+
if vid and vid not in seen:
|
| 86 |
+
seen.add(vid);items.append(v)
|
| 87 |
+
except Exception:pass
|
| 88 |
+
return items[:50]
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def _topic_image(topic):
|
| 92 |
+
try:return base.pollinations_image_url(topic)
|
| 93 |
+
except Exception:return "https://image.pollinations.ai/prompt/"+quote("Vietnamese news editorial illustration "+topic)+"?width=1024&height=576&nologo=true"
|
| 94 |
+
|
| 95 |
+
# Remove old endpoints/root to override.
|
| 96 |
+
_PATCH={('/api/shorts','GET'),('/api/topic_post','POST'),('/api/ai/interact','POST'),('/','GET')}
|
| 97 |
+
app.router.routes=[r for r in app.router.routes if not any(getattr(r,'path',None)==p and m in getattr(r,'methods',set()) for p,m in _PATCH)]
|
| 98 |
+
|
| 99 |
+
@app.get('/api/shorts')
|
| 100 |
+
def api_shorts_final3(refresh:int=Query(default=0)):
|
| 101 |
+
now=time.time()
|
| 102 |
+
if not refresh and _SHORTS_CACHE['d'] and now-_SHORTS_CACHE['t']<900:
|
| 103 |
+
return JSONResponse(_SHORTS_CACHE['d'])
|
| 104 |
+
data=_fresh_shorts()
|
| 105 |
+
_SHORTS_CACHE.update({'t':now,'d':data})
|
| 106 |
+
return JSONResponse(data)
|
| 107 |
+
|
| 108 |
+
@app.post('/api/topic_post')
|
| 109 |
+
async def topic_post_qwen(request:Request):
|
| 110 |
+
body=await request.json();topic=clean(body.get('topic',''))
|
| 111 |
+
if not topic:return JSONResponse({'error':'missing topic'},status_code=400)
|
| 112 |
+
img=_topic_image(topic)
|
| 113 |
+
prompt=f"""Bạn là biên tập viên VNEWS. Dựa trên kiến thức tổng quát của bạn, hãy tạo một bài đăng Tường AI bằng tiếng Việt về chủ đề: {topic}
|
| 114 |
+
|
| 115 |
+
Yêu cầu:
|
| 116 |
+
- Viết như một bài tin/tạp chí ngắn, có tiêu đề hấp dẫn.
|
| 117 |
+
- 1 đoạn mở đầu 2 câu.
|
| 118 |
+
- 4-6 ý chính rõ ràng, không lan man.
|
| 119 |
+
- Nếu chủ đề là thể thao/c��ng nghệ/xã hội, hãy viết có bối cảnh và nhận định.
|
| 120 |
+
- Không khẳng định số liệu thời sự mới nếu không chắc; dùng cách diễn đạt thận trọng.
|
| 121 |
+
- Cuối bài thêm dòng: Nguồn tham khảo: Qwen2.5-VL / kiến thức tổng hợp.
|
| 122 |
+
"""
|
| 123 |
+
text=await base.qwen_generate(prompt,image_url=img,max_tokens=1100)
|
| 124 |
+
if not text:
|
| 125 |
+
text=f"{topic}\n\nĐây là bài gợi ý do AI tạo dựa trên kiến thức tổng hợp. Nội dung cung cấp bối cảnh, các điểm đáng chú ý và góc nhìn tham khảo về chủ đề này.\n\nNguồn tham khảo: Qwen2.5-VL / kiến thức tổng hợp."
|
| 126 |
+
post=base.make_post(topic,text,img,'','topic_qwen',sources=[{'title':'Qwen2.5-VL / kiến thức tổng hợp','url':'','via':'Qwen2.5-VL'}])
|
| 127 |
+
post['images']=[img]
|
| 128 |
+
posts=base._load_ai_wall();posts.insert(0,post);base._save_ai_wall(posts)
|
| 129 |
+
return JSONResponse({'post':post})
|
| 130 |
+
|
| 131 |
+
@app.post('/api/ai/interact')
|
| 132 |
+
async def ai_interact(request:Request):
|
| 133 |
+
body=await request.json();pid=str(body.get('id','')).strip();kind=str(body.get('kind','wall')).strip();action=str(body.get('action','')).strip();text=clean(body.get('text',''))
|
| 134 |
+
if not pid:return JSONResponse({'error':'missing id'},status_code=400)
|
| 135 |
+
db=_load_json(AI_INTERACTIONS_FILE,{})
|
| 136 |
+
key=kind+':'+pid
|
| 137 |
+
st=db.get(key) or {'views':0,'likes':0,'comments':[],'asks':[]}
|
| 138 |
+
if action=='view':st['views']=int(st.get('views',0))+1
|
| 139 |
+
elif action=='like':st['likes']=int(st.get('likes',0))+1
|
| 140 |
+
elif action=='comment' and text:
|
| 141 |
+
st.setdefault('comments',[]).insert(0,{'text':text[:240],'ts':int(time.time())});st['comments']=st['comments'][:80]
|
| 142 |
+
elif action=='ask' and text:
|
| 143 |
+
posts=base._load_ai_wall();p=next((x for x in posts if str(x.get('id'))==pid),{})
|
| 144 |
+
prompt=f"""Trả lời ngắn bằng tiếng Việt cho câu hỏi của người xem về nội dung này.
|
| 145 |
+
Tiêu đề: {p.get('title','')}
|
| 146 |
+
Nội dung: {(p.get('text') or '')[:4000]}
|
| 147 |
+
Câu hỏi: {text}
|
| 148 |
+
"""
|
| 149 |
+
ans=await base.qwen_generate(prompt,max_tokens=500)
|
| 150 |
+
if not ans:ans='AI chưa trả lời được lúc này. Bạn thử hỏi lại ngắn gọn hơn.'
|
| 151 |
+
st.setdefault('asks',[]).insert(0,{'q':text[:240],'a':ans[:1000],'ts':int(time.time())});st['asks']=st['asks'][:50]
|
| 152 |
+
db[key]=st;_save_json(AI_INTERACTIONS_FILE,db)
|
| 153 |
+
return JSONResponse({'stats':st})
|
| 154 |
+
|
| 155 |
+
FINAL3_INJECT = r'''
|
| 156 |
+
<style>
|
| 157 |
+
.ai-compose-row.topic-final3{display:flex!important;flex-direction:column!important;gap:8px!important;width:100%!important}.ai-compose-row.topic-final3 input,.ai-compose-row.topic-final3 button{width:100%!important;box-sizing:border-box!important}.short-action-panel{position:absolute;right:8px;bottom:92px;display:flex;flex-direction:column;gap:12px;z-index:20}.short-action-btn{background:none;border:0;color:#fff;text-align:center;font-size:10px}.short-action-btn .ico{width:44px;height:44px;border-radius:50%;background:rgba(0,0,0,.45);display:flex;align-items:center;justify-content:center;font-size:21px;margin:auto}.short-modal{position:fixed;inset:auto 0 0 0;max-height:60vh;background:#181818;border-radius:16px 16px 0 0;z-index:99999;padding:14px;display:none;overflow:auto}.short-modal.active{display:block}.short-modal textarea,.short-modal input{width:100%;background:#222;border:1px solid #444;color:#eee;border-radius:10px;padding:9px;margin:6px 0}.short-modal button{background:#2d8659;border:0;color:#fff;border-radius:10px;padding:8px 12px;margin:4px}.ai-short-home{margin:6px 4px;background:#1a1a1a;border:1px solid #2a2a2a;border-radius:8px;overflow:hidden}.ai-short-card-final{flex:0 0 120px}.ai-short-card-final video{width:100%;aspect-ratio:9/16;object-fit:cover;background:#000;border-radius:8px}
|
| 158 |
+
</style>
|
| 159 |
+
<div id="short-modal" class="short-modal"></div>
|
| 160 |
+
<script>
|
| 161 |
+
(function(){
|
| 162 |
+
let finalWall3=[];let currentShortCtx=null;
|
| 163 |
+
function esc(s){return String(s||'').replace(/[&<>"']/g,m=>({'&':'&','<':'<','>':'>','"':'"',"'":'''}[m]));}
|
| 164 |
+
function ensureTopicBox(){let comp=document.querySelector('.ai-compose');if(!comp)return;if(!document.getElementById('ai-topic-input-final3')){let row=document.createElement('div');row.className='ai-compose-row topic-final3';row.innerHTML='<input id="ai-topic-input-final3" placeholder="Nhập chủ đề để Qwen2.5VL gợi ý bài đăng lên Tường AI..."><button onclick="createTopicPostFinal3()">✨ Tạo bài theo chủ đề bằng Qwen</button>';comp.insertBefore(row,comp.firstChild.nextSibling);} }
|
| 165 |
+
window.createTopicPostFinal3=async function(){let inp=document.getElementById('ai-topic-input-final3');let topic=(inp&&inp.value||'').trim();if(!topic)return alert('Nhập chủ đề trước');let btn=event?.target;if(btn){btn.disabled=true;btn.textContent='Đang tạo...'}try{let r=await fetch('/api/topic_post',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({topic})});let j=await r.json();if(!r.ok||j.error)throw new Error(j.error||'Lỗi');finalWall3.unshift(j.post);if(window.finalWall)window.finalWall.unshift(j.post);if(inp)inp.value='';renderAIShortHome();if(window.renderWall)window.renderWall();alert('Đã tạo bài chủ đề và đăng lên Tường AI, không reload.');}catch(e){alert(e.message)}finally{if(btn){btn.disabled=false;btn.textContent='✨ Tạo bài theo chủ đề bằng Qwen'}}};
|
| 166 |
+
async function refreshFinalWall3(){try{finalWall3=(await (await fetch('/api/ai_wall')).json()).posts||[];renderAIShortHome();}catch(e){}}
|
| 167 |
+
function renderAIShortHome(){let home=document.getElementById('view-home');if(!home)return;document.getElementById('ai-short-home')?.remove();let vids=finalWall3.filter(p=>p.video);if(!vids.length)return;let wrap=document.createElement('div');wrap.id='ai-short-home';wrap.className='ai-short-home';let h='<div class="slider-header"><span class="slider-label">🎬 Short AI</span><span class="slider-note">Video đã tạo</span></div><div class="slider-track">';vids.slice(0,30).forEach((p,i)=>{h+=`<div class="ai-short-card-final" onclick="openAIShortFeed(${i})"><video src="${p.video}" muted playsinline preload="metadata"></video><div class="slider-title">${esc(p.title)}</div></div>`});h+='</div>';wrap.innerHTML=h;let after=document.getElementById('ai-wall-final')||document.querySelector('.ai-compose');if(after)after.after(wrap);else home.prepend(wrap);}
|
| 168 |
+
window.openAIShortFeed=function(start){let vids=finalWall3.filter(p=>p.video);if(!vids.length)return;showView('view-tiktok');let h='<button class="back-btn" onclick="switchCat(\'home\')">← Short AI</button><div class="tiktok-container"><div class="tiktok-feed" id="tiktok-feed">';let ordered=start>0?vids.slice(start).concat(vids.slice(0,start)):vids;ordered.forEach((p,i)=>{h+=`<div class="tiktok-slide" data-kind="ai" data-id="${p.id}"><video src="${p.video}" playsinline controls loop></video><div class="tiktok-bottom"><span class="badge badge-ai">AI</span><p class="tiktok-title">${esc(p.title)}</p></div>${actionPanel('ai',p.id,i)}<span class="tiktok-counter">${i+1}/${ordered.length}</span></div>`});h+='</div></div>';document.getElementById('view-tiktok').innerHTML=h;initActionFeed();}
|
| 169 |
+
function actionPanel(kind,id,i){return `<div class="short-action-panel"><button class="short-action-btn" onclick="shortAct('${kind}','${id}','view')"><div class="ico">👁</div><span id="v-${kind}-${id}">0</span></button><button class="short-action-btn" onclick="shortAct('${kind}','${id}','like')"><div class="ico">❤️</div><span id="l-${kind}-${id}">0</span></button><button class="short-action-btn" onclick="openCommentBox('${kind}','${id}')"><div class="ico">💬</div><span>BL</span></button><button class="short-action-btn" onclick="openAskBox('${kind}','${id}')"><div class="ico">🤖</div><span>Hỏi</span></button><button class="short-action-btn" onclick="shareShortCtx('${kind}','${id}')"><div class="ico">📤</div><span>Share</span></button></div>`}
|
| 170 |
+
window.shortAct=async function(kind,id,action,text=''){let url=kind==='yt'?'/api/short-action':'/api/ai/interact';let body=kind==='yt'?{id,action,text}:{id,kind:'short',action,text};let r=await fetch(url,{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(body)});let j=await r.json();let st=j.stats||j;let v=document.getElementById(`v-${kind}-${id}`),l=document.getElementById(`l-${kind}-${id}`);if(v&&st.views!=null)v.textContent=st.views;if(l&&st.likes!=null)l.textContent=st.likes;return st;}
|
| 171 |
+
window.openCommentBox=function(kind,id){let m=document.getElementById('short-modal');m.innerHTML=`<h3>💬 Bình luận</h3><textarea id="short-comment-text" placeholder="Nhập bình luận..."></textarea><button onclick="submitShortComment('${kind}','${id}')">Gửi</button><button onclick="closeShortModal()">Đóng</button>`;m.classList.add('active')}
|
| 172 |
+
window.submitShortComment=async function(kind,id){let t=document.getElementById('short-comment-text').value.trim();if(!t)return;await shortAct(kind,id,'comment',t);alert('Đã gửi bình luận');closeShortModal()}
|
| 173 |
+
window.openAskBox=function(kind,id){let m=document.getElementById('short-modal');m.innerHTML=`<h3>🤖 Hỏi AI</h3><input id="short-ask-text" placeholder="Bạn muốn hỏi gì về nội dung này?"><div id="short-answer"></div><button onclick="submitShortAsk('${kind}','${id}')">Hỏi</button><button onclick="closeShortModal()">Đóng</button>`;m.classList.add('active')}
|
| 174 |
+
window.submitShortAsk=async function(kind,id){let t=document.getElementById('short-ask-text').value.trim();if(!t)return;if(kind==='yt'){document.getElementById('short-answer').innerHTML='AI chỉ hỗ trợ trả lời sâu cho Short AI/Tường AI.';return}let st=await shortAct(kind,id,'ask',t);let a=(st.asks&&st.asks[0]&&st.asks[0].a)||'Chưa có trả lời';document.getElementById('short-answer').innerHTML='<p style="white-space:pre-wrap;color:#ccc">'+esc(a)+'</p>'}
|
| 175 |
+
window.closeShortModal=function(){document.getElementById('short-modal').classList.remove('active')}
|
| 176 |
+
window.shareShortCtx=function(kind,id){if(kind==='ai'){let p=finalWall3.find(x=>x.id===id);if(p){let url=location.origin+'/aw?post='+encodeURIComponent(id)+'&short=1';if(navigator.share)navigator.share({title:'🎬 Short AI: '+p.title,url}).catch(()=>{});else navigator.clipboard.writeText(url).then(()=>alert('Đã sao chép link!'));}}else{let url='https://www.youtube.com/watch?v='+id;if(navigator.share)navigator.share({title:'Shorts VNEWS',url}).catch(()=>{});else navigator.clipboard.writeText(url).then(()=>alert('Đã sao chép link!'));}}
|
| 177 |
+
function initActionFeed(){let feed=document.getElementById('tiktok-feed');if(!feed)return;let slides=feed.querySelectorAll('.tiktok-slide');let cur=-1;function act(i){if(i===cur)return;slides.forEach((sl,idx)=>{let v=sl.querySelector('video');let fr=sl.querySelector('iframe');if(idx===i){if(v)v.play().catch(()=>{});if(fr&&!fr.src&&fr.dataset.ytSrc)fr.src=fr.dataset.ytSrc;let kind=sl.dataset.kind,id=sl.dataset.id;if(kind&&id)shortAct(kind,id,'view').catch(()=>{})}else{if(v)v.pause();if(fr&&fr.src)fr.src=''}});cur=i}let t;feed.addEventListener('scroll',()=>{clearTimeout(t);t=setTimeout(()=>{let rect=feed.getBoundingClientRect(),ctr=rect.top+rect.height/2,b=-1,d=1e9;slides.forEach((sl,i)=>{let dd=Math.abs(sl.getBoundingClientRect().top+sl.getBoundingClientRect().height/2-ctr);if(dd<d){d=dd;b=i}});if(b>=0)act(b)},150)});setTimeout(()=>act(0),300)}
|
| 178 |
+
// Override openTikTok for regular YouTube shorts with same action layout.
|
| 179 |
+
window.openTikTok=async function(type,startIdx){showView('view-tiktok');let arts= type==='shorts'? await fetch('/api/shorts?refresh=1').then(r=>r.json()).catch(()=>[]) : await fetch(type==='highlights'?'/api/highlights':'/api/bdp_videos').then(r=>r.json()).catch(()=>[]);if(type!=='shorts'&&window.buildTikTokPlayer)return window.buildTikTokPlayer(arts,startIdx,type);let ordered=startIdx>0?arts.slice(startIdx).concat(arts.slice(0,startIdx)):arts;let h='<button class="back-btn" onclick="switchCat(\'home\')">← Shorts</button><div class="tiktok-container"><div class="tiktok-feed" id="tiktok-feed">';ordered.forEach((v,i)=>{let id=v.id||((v.link||'').match(/v=([A-Za-z0-9_-]{11})/)||[])[1]||String(i);let src='https://www.youtube.com/embed/'+id+'?autoplay=1&rel=0&playsinline=1';h+=`<div class="tiktok-slide" data-kind="yt" data-id="${id}"><iframe data-yt-src="${src}" allowfullscreen allow="accelerometer;autoplay;clipboard-write;encrypted-media;gyroscope;picture-in-picture"></iframe><div class="tiktok-bottom"><span class="badge badge-fpt">YT</span><p class="tiktok-title">${esc(v.title)}</p></div>${actionPanel('yt',id,i)}<span class="tiktok-counter">${i+1}/${ordered.length}</span></div>`});h+='</div></div>';document.getElementById('view-tiktok').innerHTML=h;initActionFeed();}
|
| 180 |
+
// Patch make short: update home Short AI slide without reload.
|
| 181 |
+
let oldMake=window.makeFinalShort||window.aiMakeShortPatched;
|
| 182 |
+
window.makeFinalShort=window.aiMakeShortPatched=async function(i){let arr=finalWall3.length?finalWall3:(window.finalWall||[]);let p=arr[i];if(!p&&oldMake)return oldMake(i);if(!p)return;let voice=document.getElementById('ai-short-voice')?.value||'nu';let emotion=document.getElementById('ai-short-emotion')?.value||'neutral';let btn=event?.target;if(btn){btn.disabled=true;btn.textContent='Đang tạo...'}try{let r=await fetch('/api/ai/short/'+p.id,{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({voice,emotion,speed:1.2})});let j=await r.json();if(!r.ok||j.error)throw new Error(j.error||'Lỗi tạo short');p.video=j.video;let idx=finalWall3.findIndex(x=>x.id===p.id);if(idx<0)finalWall3.unshift(p);renderAIShortHome();if(window.renderWall)window.renderWall();alert('Đã tạo short và thêm vào slide Short AI, không reload.');}catch(e){alert(e.message)}finally{if(btn){btn.disabled=false;btn.textContent='🎬 Tạo short'}}}
|
| 183 |
+
setTimeout(()=>{ensureTopicBox();refreshFinalWall3();},700);setInterval(ensureTopicBox,1500);
|
| 184 |
+
})();
|
| 185 |
+
</script>
|
| 186 |
+
'''
|
| 187 |
+
|
| 188 |
+
@app.get('/')
|
| 189 |
+
async def index_final3():
|
| 190 |
+
html=f2.f1._load_index_html();body=getattr(rt.old,'PATCH_INJECT','') + f2.f1.FINAL_INJECT + FINAL3_INJECT
|
| 191 |
+
return HTMLResponse(html.replace('</body>',body+'\n</body>') if '</body>' in html else html+body)
|