Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,12 @@
|
|
| 1 |
"""
|
| 2 |
app.py β Video Verifier & Sentiment Analyzer
|
| 3 |
-
|
|
|
|
| 4 |
"""
|
| 5 |
|
| 6 |
import os
|
| 7 |
-
import time
|
| 8 |
-
import streamlit as st
|
| 9 |
import pandas as pd
|
|
|
|
| 10 |
|
| 11 |
from fetcher import (
|
| 12 |
extract_video_id,
|
|
@@ -32,773 +32,591 @@ from charts import (
|
|
| 32 |
)
|
| 33 |
|
| 34 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 35 |
-
#
|
| 36 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 37 |
|
| 38 |
-
|
| 39 |
-
page_title="VideoVerifier β MHMisinfo",
|
| 40 |
-
page_icon="π¬",
|
| 41 |
-
layout="wide",
|
| 42 |
-
initial_sidebar_state="expanded",
|
| 43 |
-
)
|
| 44 |
-
|
| 45 |
-
st.markdown("""
|
| 46 |
-
<style>
|
| 47 |
-
/* ββ Google Fonts ββ */
|
| 48 |
@import url('https://fonts.googleapis.com/css2?family=DM+Mono:wght@400;500&family=Syne:wght@400;600;700;800&family=IBM+Plex+Sans:wght@300;400;500&display=swap');
|
| 49 |
|
| 50 |
-
/* ββ Root palette ββ */
|
| 51 |
:root {
|
| 52 |
-
--bg:
|
| 53 |
-
--card:
|
| 54 |
-
--border:
|
| 55 |
-
--text:
|
| 56 |
-
--dim:
|
| 57 |
-
--cyan:
|
| 58 |
-
--green:
|
| 59 |
-
--red:
|
| 60 |
-
--amber:
|
| 61 |
-
--
|
| 62 |
-
--blue: #4a8eff;
|
| 63 |
-
}
|
| 64 |
-
|
| 65 |
-
/* ββ App shell ββ */
|
| 66 |
-
html, body, [class*="css"] {
|
| 67 |
-
background-color: var(--bg) !important;
|
| 68 |
-
color: var(--text) !important;
|
| 69 |
-
font-family: 'IBM Plex Sans', sans-serif !important;
|
| 70 |
-
}
|
| 71 |
-
|
| 72 |
-
.stApp { background: var(--bg) !important; }
|
| 73 |
-
|
| 74 |
-
/* ββ Hide Streamlit chrome ββ */
|
| 75 |
-
#MainMenu, footer, header { visibility: hidden; }
|
| 76 |
-
.block-container { padding: 1.5rem 2rem !important; max-width: 1400px; }
|
| 77 |
-
|
| 78 |
-
/* ββ Sidebar ββ */
|
| 79 |
-
section[data-testid="stSidebar"] {
|
| 80 |
-
background: var(--card) !important;
|
| 81 |
-
border-right: 1px solid var(--border) !important;
|
| 82 |
-
}
|
| 83 |
-
section[data-testid="stSidebar"] * { color: var(--text) !important; }
|
| 84 |
-
|
| 85 |
-
/* ββ Inputs ββ */
|
| 86 |
-
input, textarea, select, .stTextInput input {
|
| 87 |
-
background: #1a1d27 !important;
|
| 88 |
-
border: 1px solid var(--border) !important;
|
| 89 |
-
color: var(--text) !important;
|
| 90 |
-
border-radius: 8px !important;
|
| 91 |
-
font-family: 'DM Mono', monospace !important;
|
| 92 |
-
font-size: 0.88rem !important;
|
| 93 |
-
}
|
| 94 |
-
input:focus, textarea:focus {
|
| 95 |
-
border-color: var(--cyan) !important;
|
| 96 |
-
box-shadow: 0 0 0 2px rgba(0,212,255,0.15) !important;
|
| 97 |
-
}
|
| 98 |
-
|
| 99 |
-
/* ββ Buttons ββ */
|
| 100 |
-
.stButton > button {
|
| 101 |
-
background: linear-gradient(135deg, #00d4ff22, #4a8eff22) !important;
|
| 102 |
-
border: 1px solid var(--cyan) !important;
|
| 103 |
-
color: var(--cyan) !important;
|
| 104 |
-
border-radius: 8px !important;
|
| 105 |
-
font-family: 'DM Mono', monospace !important;
|
| 106 |
-
font-size: 0.85rem !important;
|
| 107 |
-
letter-spacing: 0.05em !important;
|
| 108 |
-
padding: 0.45rem 1.2rem !important;
|
| 109 |
-
transition: all 0.2s ease !important;
|
| 110 |
-
}
|
| 111 |
-
.stButton > button:hover {
|
| 112 |
-
background: linear-gradient(135deg, #00d4ff44, #4a8eff33) !important;
|
| 113 |
-
box-shadow: 0 0 16px rgba(0,212,255,0.25) !important;
|
| 114 |
-
transform: translateY(-1px) !important;
|
| 115 |
-
}
|
| 116 |
-
.stButton > button[kind="primary"] {
|
| 117 |
-
background: linear-gradient(135deg, var(--cyan), var(--blue)) !important;
|
| 118 |
-
border: none !important;
|
| 119 |
-
color: var(--bg) !important;
|
| 120 |
-
font-weight: 600 !important;
|
| 121 |
-
}
|
| 122 |
-
|
| 123 |
-
/* ββ Cards ββ */
|
| 124 |
-
.vv-card {
|
| 125 |
-
background: var(--card);
|
| 126 |
-
border: 1px solid var(--border);
|
| 127 |
-
border-radius: 12px;
|
| 128 |
-
padding: 1.2rem 1.4rem;
|
| 129 |
-
margin-bottom: 1rem;
|
| 130 |
-
}
|
| 131 |
-
.vv-card-accent {
|
| 132 |
-
background: var(--card);
|
| 133 |
-
border-top: 2px solid var(--cyan);
|
| 134 |
-
border-left: 1px solid var(--border);
|
| 135 |
-
border-right: 1px solid var(--border);
|
| 136 |
-
border-bottom: 1px solid var(--border);
|
| 137 |
-
border-radius: 0 0 12px 12px;
|
| 138 |
-
padding: 1.2rem 1.4rem;
|
| 139 |
-
margin-bottom: 1rem;
|
| 140 |
-
}
|
| 141 |
-
|
| 142 |
-
/* ββ Section headers ββ */
|
| 143 |
-
.vv-section-title {
|
| 144 |
-
font-family: 'Syne', sans-serif;
|
| 145 |
-
font-size: 0.7rem;
|
| 146 |
-
font-weight: 700;
|
| 147 |
-
letter-spacing: 0.18em;
|
| 148 |
-
text-transform: uppercase;
|
| 149 |
-
color: var(--dim);
|
| 150 |
-
margin-bottom: 0.6rem;
|
| 151 |
-
}
|
| 152 |
-
|
| 153 |
-
/* ββ Hero title ββ */
|
| 154 |
-
.vv-hero {
|
| 155 |
-
font-family: 'Syne', sans-serif;
|
| 156 |
-
font-size: 1.6rem;
|
| 157 |
-
font-weight: 800;
|
| 158 |
-
background: linear-gradient(135deg, var(--cyan), var(--blue));
|
| 159 |
-
-webkit-background-clip: text;
|
| 160 |
-
-webkit-text-fill-color: transparent;
|
| 161 |
-
background-clip: text;
|
| 162 |
-
letter-spacing: -0.02em;
|
| 163 |
-
line-height: 1.2;
|
| 164 |
-
margin: 0 0 0.2rem;
|
| 165 |
-
}
|
| 166 |
-
|
| 167 |
-
/* ββ Stat chips ββ */
|
| 168 |
-
.vv-stat {
|
| 169 |
-
display: inline-block;
|
| 170 |
-
background: #1a1d27;
|
| 171 |
-
border: 1px solid var(--border);
|
| 172 |
-
border-radius: 6px;
|
| 173 |
-
padding: 0.25rem 0.7rem;
|
| 174 |
-
font-family: 'DM Mono', monospace;
|
| 175 |
-
font-size: 0.78rem;
|
| 176 |
-
color: var(--cyan);
|
| 177 |
-
margin: 0.15rem 0.2rem 0.15rem 0;
|
| 178 |
-
}
|
| 179 |
-
|
| 180 |
-
/* ββ Badge ββ */
|
| 181 |
-
.vv-badge-green {
|
| 182 |
-
display: inline-block;
|
| 183 |
-
background: rgba(0,229,160,0.12);
|
| 184 |
-
border: 1px solid var(--green);
|
| 185 |
-
color: var(--green);
|
| 186 |
-
border-radius: 20px;
|
| 187 |
-
padding: 0.2rem 0.8rem;
|
| 188 |
-
font-size: 0.78rem;
|
| 189 |
-
font-family: 'DM Mono', monospace;
|
| 190 |
-
}
|
| 191 |
-
.vv-badge-red {
|
| 192 |
-
display: inline-block;
|
| 193 |
-
background: rgba(255,71,87,0.12);
|
| 194 |
-
border: 1px solid var(--red);
|
| 195 |
-
color: var(--red);
|
| 196 |
-
border-radius: 20px;
|
| 197 |
-
padding: 0.2rem 0.8rem;
|
| 198 |
-
font-size: 0.78rem;
|
| 199 |
-
font-family: 'DM Mono', monospace;
|
| 200 |
-
}
|
| 201 |
-
.vv-badge-amber {
|
| 202 |
-
display: inline-block;
|
| 203 |
-
background: rgba(255,179,71,0.12);
|
| 204 |
-
border: 1px solid var(--amber);
|
| 205 |
-
color: var(--amber);
|
| 206 |
-
border-radius: 20px;
|
| 207 |
-
padding: 0.2rem 0.8rem;
|
| 208 |
-
font-size: 0.78rem;
|
| 209 |
-
font-family: 'DM Mono', monospace;
|
| 210 |
-
}
|
| 211 |
-
|
| 212 |
-
/* ββ Reasoning box ββ */
|
| 213 |
-
.vv-reasoning {
|
| 214 |
-
background: #0d1119;
|
| 215 |
-
border-left: 3px solid var(--amber);
|
| 216 |
-
padding: 0.7rem 1rem;
|
| 217 |
-
border-radius: 0 8px 8px 0;
|
| 218 |
-
font-size: 0.83rem;
|
| 219 |
-
color: #c0c4cc;
|
| 220 |
-
line-height: 1.6;
|
| 221 |
-
font-family: 'IBM Plex Sans', sans-serif;
|
| 222 |
-
margin-top: 0.6rem;
|
| 223 |
-
}
|
| 224 |
-
|
| 225 |
-
/* ββ Dataframe ββ */
|
| 226 |
-
.stDataFrame {
|
| 227 |
-
background: var(--card) !important;
|
| 228 |
-
border: 1px solid var(--border) !important;
|
| 229 |
-
border-radius: 8px !important;
|
| 230 |
-
}
|
| 231 |
-
.stDataFrame th {
|
| 232 |
-
background: #1a1d27 !important;
|
| 233 |
-
color: var(--cyan) !important;
|
| 234 |
-
font-family: 'DM Mono', monospace !important;
|
| 235 |
-
font-size: 0.78rem !important;
|
| 236 |
-
}
|
| 237 |
-
.stDataFrame td {
|
| 238 |
-
color: var(--text) !important;
|
| 239 |
-
font-size: 0.8rem !important;
|
| 240 |
-
border-color: var(--border) !important;
|
| 241 |
-
}
|
| 242 |
-
|
| 243 |
-
/* ββ Tabs ββ */
|
| 244 |
-
.stTabs [data-baseweb="tab-list"] {
|
| 245 |
-
background: transparent !important;
|
| 246 |
-
border-bottom: 1px solid var(--border) !important;
|
| 247 |
-
gap: 0 !important;
|
| 248 |
-
}
|
| 249 |
-
.stTabs [data-baseweb="tab"] {
|
| 250 |
-
background: transparent !important;
|
| 251 |
-
color: var(--dim) !important;
|
| 252 |
-
font-family: 'DM Mono', monospace !important;
|
| 253 |
-
font-size: 0.82rem !important;
|
| 254 |
-
letter-spacing: 0.05em !important;
|
| 255 |
-
border: none !important;
|
| 256 |
-
padding: 0.5rem 1.2rem !important;
|
| 257 |
-
}
|
| 258 |
-
.stTabs [aria-selected="true"] {
|
| 259 |
-
color: var(--cyan) !important;
|
| 260 |
-
border-bottom: 2px solid var(--cyan) !important;
|
| 261 |
-
}
|
| 262 |
-
|
| 263 |
-
/* ββ Spinner ββ */
|
| 264 |
-
.stSpinner > div { border-top-color: var(--cyan) !important; }
|
| 265 |
-
|
| 266 |
-
/* ββ Alerts ββ */
|
| 267 |
-
.stAlert { border-radius: 8px !important; font-size: 0.85rem !important; }
|
| 268 |
-
|
| 269 |
-
/* ββ Divider ββ */
|
| 270 |
-
hr { border-color: var(--border) !important; }
|
| 271 |
-
|
| 272 |
-
/* ββ Select box ββ */
|
| 273 |
-
.stSelectbox > div > div {
|
| 274 |
-
background: #1a1d27 !important;
|
| 275 |
-
border-color: var(--border) !important;
|
| 276 |
-
color: var(--text) !important;
|
| 277 |
-
}
|
| 278 |
-
|
| 279 |
-
/* ββ File uploader ββ */
|
| 280 |
-
.stFileUploader {
|
| 281 |
-
background: #1a1d27 !important;
|
| 282 |
-
border: 1px dashed var(--border) !important;
|
| 283 |
-
border-radius: 8px !important;
|
| 284 |
}
|
| 285 |
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
}
|
| 296 |
-
|
| 297 |
-
/*
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
}
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
|
| 309 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 310 |
-
#
|
| 311 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 312 |
|
| 313 |
-
def
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
"
|
| 318 |
-
"
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
|
| 334 |
|
| 335 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 336 |
-
#
|
| 337 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 338 |
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 343 |
|
| 344 |
-
|
|
|
|
|
|
|
|
|
|
| 345 |
|
| 346 |
-
api_key
|
| 347 |
-
"YouTube API
|
| 348 |
-
|
| 349 |
-
type="password",
|
| 350 |
-
placeholder="AIza...",
|
| 351 |
-
help="Get a free key at console.cloud.google.com",
|
| 352 |
-
)
|
| 353 |
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 365 |
)
|
| 366 |
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
'4-stream SeTa-Attention model for mental health misinformation detection. '
|
| 372 |
-
'Plug your <code style="background:#1a1d27;padding:1px 4px;border-radius:3px;color:#00d4ff">detect_misinformation()</code> '
|
| 373 |
-
'function in <b>analyzer.py</b> to connect your trained checkpoint.'
|
| 374 |
-
'</p>',
|
| 375 |
-
unsafe_allow_html=True,
|
| 376 |
)
|
| 377 |
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
st.markdown('<p class="vv-section-title">π Log</p>', unsafe_allow_html=True)
|
| 381 |
-
for msg in st.session_state.status_log[-6:]:
|
| 382 |
-
st.markdown(f'<p style="font-size:0.72rem;color:#5a6070;font-family:\'DM Mono\',monospace;margin:2px 0">{msg}</p>', unsafe_allow_html=True)
|
| 383 |
-
|
| 384 |
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
)
|
| 396 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 397 |
|
| 398 |
|
| 399 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 400 |
-
#
|
| 401 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββοΏ½οΏ½οΏ½ββββββββββββββββββββββββ
|
| 402 |
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 406 |
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
with col_url:
|
| 410 |
-
yt_url = st.text_input(
|
| 411 |
-
"YouTube URL",
|
| 412 |
-
placeholder="https://www.youtube.com/watch?v=... or youtu.be/...",
|
| 413 |
-
label_visibility="collapsed",
|
| 414 |
-
)
|
| 415 |
-
with col_btn:
|
| 416 |
-
analyze_url_btn = st.button("π Analyze", type="primary", use_container_width=True)
|
| 417 |
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
else
|
| 423 |
-
st.error("β Could not extract a valid YouTube video ID. Check the URL format.")
|
| 424 |
-
|
| 425 |
-
with input_tab2:
|
| 426 |
-
st.markdown(
|
| 427 |
-
'<div class="vv-card">'
|
| 428 |
-
'<p class="vv-section-title">Upload a video file</p>'
|
| 429 |
-
'<p style="font-size:0.82rem;color:#5a6070;line-height:1.6">'
|
| 430 |
-
'β οΈ <b>Important:</b> The YouTube Data API cannot search by raw video bytes. '
|
| 431 |
-
'After uploading, enter the video title or a keyword to find the matching YouTube entry. '
|
| 432 |
-
'For local-only analysis, the system will run misinformation detection on the filename metadata.'
|
| 433 |
-
'</p></div>',
|
| 434 |
-
unsafe_allow_html=True,
|
| 435 |
-
)
|
| 436 |
-
uploaded = st.file_uploader(
|
| 437 |
-
"Drop a video file",
|
| 438 |
-
type=["mp4", "mov", "avi", "mkv", "webm"],
|
| 439 |
-
label_visibility="collapsed",
|
| 440 |
)
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 475 |
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
|
|
|
|
|
|
| 479 |
|
| 480 |
-
|
| 481 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 482 |
|
| 483 |
-
#
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
# 3. Comments
|
| 499 |
-
with st.spinner(f"Fetching up to {max_comments} commentsβ¦"):
|
| 500 |
-
comments_df, c_status = fetch_comments(video_id, api_key, max_comments=int(max_comments))
|
| 501 |
-
log.append(c_status)
|
| 502 |
-
st.session_state.comments_df = comments_df
|
| 503 |
-
|
| 504 |
-
# 4. Misinformation
|
| 505 |
-
with st.spinner("Running misinformation detectionβ¦"):
|
| 506 |
-
misinfo = detect_misinformation(
|
| 507 |
-
text=f"{meta['title']} {meta['description']}",
|
| 508 |
-
tags=meta["tags"],
|
| 509 |
-
audio_transcript=transcript,
|
| 510 |
-
video_transcript=transcript,
|
| 511 |
-
)
|
| 512 |
-
log.append(f"π¬ Misinfo score: {misinfo['confidence_pct']}%")
|
| 513 |
-
st.session_state.misinfo = misinfo
|
| 514 |
|
| 515 |
-
#
|
| 516 |
-
|
| 517 |
-
|
| 518 |
|
| 519 |
-
# 6. Sentiment
|
| 520 |
if not comments_df.empty:
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
results += analyze_sentiment_batch(chunk, method=sentiment_method, batch_size=batch_size)
|
| 529 |
-
progress.progress(min((i + batch_size) / len(texts), 1.0),
|
| 530 |
-
text=f"Analyzed {min(i+batch_size, len(texts))}/{len(texts)} commentsβ¦")
|
| 531 |
-
progress.empty()
|
| 532 |
-
st.session_state.sentiments = results
|
| 533 |
-
st.session_state.sent_summary = sentiment_summary(results)
|
| 534 |
-
pos_kw, neg_kw = sentiment_weighted_keywords(comments_df, results)
|
| 535 |
-
st.session_state.pos_kw = pos_kw
|
| 536 |
-
st.session_state.neg_kw = neg_kw
|
| 537 |
-
log.append(f"π¬ Sentiment: {st.session_state.sent_summary['pos_pct']}% pos / {st.session_state.sent_summary['neg_pct']}% neg")
|
| 538 |
-
else:
|
| 539 |
-
st.session_state.sentiments = []
|
| 540 |
-
st.session_state.sent_summary = {}
|
| 541 |
-
log.append("π¬ Skipped (no comments)")
|
| 542 |
-
|
| 543 |
-
st.session_state.video_id = video_id
|
| 544 |
-
st.session_state.analysed = True
|
| 545 |
-
st.session_state.status_log = log
|
| 546 |
-
st.rerun()
|
| 547 |
|
|
|
|
|
|
|
| 548 |
|
| 549 |
-
if
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 553 |
|
| 554 |
|
| 555 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 556 |
-
#
|
| 557 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 558 |
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
'<p style="font-family:\'Syne\',sans-serif;font-size:1.1rem;color:#5a6070">'
|
| 565 |
-
'Paste a YouTube URL above and click <b style="color:#00d4ff">Analyze</b> to begin</p>'
|
| 566 |
-
'<p style="font-size:0.8rem;color:#3a3f50;font-family:\'DM Mono\',monospace">'
|
| 567 |
-
'Misinformation detection Β· Sentiment analysis Β· Comment insights</p>'
|
| 568 |
-
'</div>',
|
| 569 |
-
unsafe_allow_html=True,
|
| 570 |
-
)
|
| 571 |
-
st.stop()
|
| 572 |
-
|
| 573 |
-
meta = st.session_state.metadata
|
| 574 |
-
transcript = st.session_state.transcript
|
| 575 |
-
comments_df= st.session_state.comments_df
|
| 576 |
-
misinfo = st.session_state.misinfo
|
| 577 |
-
keywords = st.session_state.keywords
|
| 578 |
-
sentiments = st.session_state.sentiments
|
| 579 |
-
sent_sum = st.session_state.sent_summary
|
| 580 |
-
pos_kw = st.session_state.pos_kw
|
| 581 |
-
neg_kw = st.session_state.neg_kw
|
| 582 |
-
video_id = st.session_state.video_id
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
# ββ Layout: left (info) / right (analytics) βββββββββββββββββββββββββββββββββββ
|
| 586 |
-
|
| 587 |
-
left_col, right_col = st.columns([2, 3], gap="large")
|
| 588 |
-
|
| 589 |
-
# ββββββββββββββββββββββββββββββββ
|
| 590 |
-
# β LEFT COLUMN β Video Info β
|
| 591 |
-
# ββββββββββββββββββββββββββββββββ
|
| 592 |
-
with left_col:
|
| 593 |
-
|
| 594 |
-
# Thumbnail + embed
|
| 595 |
-
if meta.get("thumbnail_url"):
|
| 596 |
-
st.image(meta["thumbnail_url"], use_column_width=True)
|
| 597 |
-
|
| 598 |
-
st.markdown(
|
| 599 |
-
f'<a href="https://www.youtube.com/watch?v={video_id}" target="_blank" '
|
| 600 |
-
f'style="display:block;text-align:center;font-family:\'DM Mono\',monospace;'
|
| 601 |
-
f'font-size:0.78rem;color:#5a6070;text-decoration:none;margin:4px 0 12px">βΆ Open on YouTube</a>',
|
| 602 |
-
unsafe_allow_html=True,
|
| 603 |
-
)
|
| 604 |
-
|
| 605 |
-
# Title & channel
|
| 606 |
-
st.markdown(
|
| 607 |
-
f'<div class="vv-card">'
|
| 608 |
-
f'<p class="vv-section-title">Video</p>'
|
| 609 |
-
f'<p style="font-family:\'Syne\',sans-serif;font-size:1.05rem;font-weight:700;margin:0 0 4px">{meta["title"]}</p>'
|
| 610 |
-
f'<p style="font-size:0.82rem;color:#5a6070;margin:0">by <b style="color:#b0b4c0">{meta["channel_title"]}</b> Β· {meta["published_at"]}</p>'
|
| 611 |
-
f'</div>',
|
| 612 |
-
unsafe_allow_html=True,
|
| 613 |
-
)
|
| 614 |
-
|
| 615 |
-
# Stats
|
| 616 |
-
st.markdown('<p class="vv-section-title">Metrics</p>', unsafe_allow_html=True)
|
| 617 |
-
s1, s2 = st.columns(2)
|
| 618 |
-
with s1:
|
| 619 |
-
st.markdown(f'<span class="vv-stat">π {meta["view_count"]:,}</span>', unsafe_allow_html=True)
|
| 620 |
-
st.markdown(f'<span class="vv-stat">π {meta["like_count"]:,}</span>', unsafe_allow_html=True)
|
| 621 |
-
with s2:
|
| 622 |
-
st.markdown(f'<span class="vv-stat">π¬ {meta["comment_count"]:,}</span>', unsafe_allow_html=True)
|
| 623 |
-
st.markdown(f'<span class="vv-stat">β± {meta["duration"]}</span>', unsafe_allow_html=True)
|
| 624 |
-
|
| 625 |
-
# Tags
|
| 626 |
-
if meta.get("tags"):
|
| 627 |
-
st.markdown('<p class="vv-section-title" style="margin-top:1rem">Tags</p>', unsafe_allow_html=True)
|
| 628 |
-
tag_html = "".join(
|
| 629 |
-
f'<span style="display:inline-block;background:#1a1d27;border:1px solid #1e2330;border-radius:4px;'
|
| 630 |
-
f'padding:2px 8px;font-family:\'DM Mono\',monospace;font-size:0.7rem;color:#8090a0;margin:2px">'
|
| 631 |
-
f'#{t}</span>'
|
| 632 |
-
for t in meta["tags"][:20]
|
| 633 |
)
|
| 634 |
-
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
|
| 638 |
-
|
| 639 |
-
st.markdown(
|
| 640 |
-
f'<p style="font-size:0.8rem;color:#8090a0;line-height:1.65;white-space:pre-wrap">'
|
| 641 |
-
f'{meta["description"][:1200]}{"β¦" if len(meta["description"])>1200 else ""}</p>',
|
| 642 |
-
unsafe_allow_html=True,
|
| 643 |
-
)
|
| 644 |
-
|
| 645 |
-
# Transcript (collapsed)
|
| 646 |
-
with st.expander(f"π Transcript ({len(transcript.split()) if transcript else 0} words)", expanded=False):
|
| 647 |
-
if transcript:
|
| 648 |
-
st.markdown(
|
| 649 |
-
f'<p style="font-size:0.78rem;color:#8090a0;line-height:1.65">'
|
| 650 |
-
f'{transcript[:2500]}{"β¦" if len(transcript)>2500 else ""}</p>',
|
| 651 |
-
unsafe_allow_html=True,
|
| 652 |
-
)
|
| 653 |
-
else:
|
| 654 |
-
st.info("No transcript available for this video.")
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
# ββββββββββββββββββββββββββββββββ
|
| 658 |
-
# β RIGHT COLUMN β Analytics β
|
| 659 |
-
# ββββββββββββββββββββββββββββββββ
|
| 660 |
-
with right_col:
|
| 661 |
-
|
| 662 |
-
# ββ Misinfo verdict ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 663 |
-
st.markdown('<p class="vv-section-title">π¬ Misinformation Analysis</p>', unsafe_allow_html=True)
|
| 664 |
-
|
| 665 |
-
score = misinfo["score"]
|
| 666 |
-
if score < 0.35:
|
| 667 |
-
badge = '<span class="vv-badge-green">β
Appears Credible</span>'
|
| 668 |
-
elif score < 0.65:
|
| 669 |
-
badge = '<span class="vv-badge-amber">β οΈ Uncertain / Mixed Signals</span>'
|
| 670 |
-
else:
|
| 671 |
-
badge = '<span class="vv-badge-red">π¨ Likely Misinformation</span>'
|
| 672 |
-
|
| 673 |
-
st.markdown(badge, unsafe_allow_html=True)
|
| 674 |
-
|
| 675 |
-
ga_col, detail_col = st.columns([1, 1])
|
| 676 |
-
with ga_col:
|
| 677 |
-
st.plotly_chart(
|
| 678 |
-
misinfo_gauge(score, "Misinfo Confidence"),
|
| 679 |
-
use_container_width=True,
|
| 680 |
-
config={"displayModeBar": False},
|
| 681 |
)
|
| 682 |
-
|
| 683 |
-
|
| 684 |
-
|
| 685 |
-
|
| 686 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 687 |
)
|
|
|
|
| 688 |
|
| 689 |
-
st.markdown(
|
| 690 |
-
f'<div class="vv-reasoning">π§ <b>Reasoning:</b> {misinfo["reasoning"]}</div>',
|
| 691 |
-
unsafe_allow_html=True,
|
| 692 |
-
)
|
| 693 |
|
| 694 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 695 |
|
| 696 |
-
# ββ Sentiment analytics ββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 697 |
-
st.markdown('<p class="vv-section-title">π¬ Comment Sentiment</p>', unsafe_allow_html=True)
|
| 698 |
|
| 699 |
-
|
| 700 |
-
|
| 701 |
-
|
| 702 |
-
st.markdown(
|
| 703 |
-
f'<div class="vv-card" style="text-align:center">'
|
| 704 |
-
f'<p style="color:#00e5a0;font-family:\'DM Mono\',monospace;font-size:1.6rem;font-weight:700;margin:0">{sent_sum["pos_pct"]}%</p>'
|
| 705 |
-
f'<p style="color:#5a6070;font-size:0.75rem;margin:0">Positive</p></div>',
|
| 706 |
-
unsafe_allow_html=True,
|
| 707 |
-
)
|
| 708 |
-
with s_col2:
|
| 709 |
-
st.markdown(
|
| 710 |
-
f'<div class="vv-card" style="text-align:center">'
|
| 711 |
-
f'<p style="color:#ff4757;font-family:\'DM Mono\',monospace;font-size:1.6rem;font-weight:700;margin:0">{sent_sum["neg_pct"]}%</p>'
|
| 712 |
-
f'<p style="color:#5a6070;font-size:0.75rem;margin:0">Negative</p></div>',
|
| 713 |
-
unsafe_allow_html=True,
|
| 714 |
-
)
|
| 715 |
-
with s_col3:
|
| 716 |
-
st.markdown(
|
| 717 |
-
f'<div class="vv-card" style="text-align:center">'
|
| 718 |
-
f'<p style="color:#5a6070;font-family:\'DM Mono\',monospace;font-size:1.6rem;font-weight:700;margin:0">{sent_sum["neu_pct"]}%</p>'
|
| 719 |
-
f'<p style="color:#5a6070;font-size:0.75rem;margin:0">Neutral</p></div>',
|
| 720 |
-
unsafe_allow_html=True,
|
| 721 |
-
)
|
| 722 |
|
| 723 |
-
|
| 724 |
-
|
| 725 |
-
|
| 726 |
-
|
| 727 |
-
|
| 728 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 729 |
)
|
| 730 |
-
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
|
| 734 |
-
|
|
|
|
|
|
|
|
|
|
| 735 |
)
|
| 736 |
-
|
| 737 |
-
|
| 738 |
-
|
| 739 |
-
|
| 740 |
-
|
| 741 |
-
keyword_bar(keywords, title="Top Video Keywords", color="#00d4ff"),
|
| 742 |
-
use_container_width=True,
|
| 743 |
-
config={"displayModeBar": False},
|
| 744 |
-
)
|
| 745 |
-
with kw_col2:
|
| 746 |
-
st.plotly_chart(
|
| 747 |
-
keyword_comparison(pos_kw, neg_kw),
|
| 748 |
-
use_container_width=True,
|
| 749 |
-
config={"displayModeBar": False},
|
| 750 |
)
|
| 751 |
|
| 752 |
-
|
| 753 |
-
|
| 754 |
-
|
| 755 |
-
|
| 756 |
-
|
| 757 |
-
|
| 758 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 759 |
)
|
| 760 |
|
| 761 |
-
# ββ
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
|
| 766 |
-
display_df = comments_df.copy()
|
| 767 |
-
if sentiments:
|
| 768 |
-
display_df["sentiment"] = [s["label"] for s in sentiments]
|
| 769 |
-
display_df["compound"] = [round(s.get("compound", 0), 3) for s in sentiments]
|
| 770 |
-
|
| 771 |
-
tab_all, tab_pos, tab_neg, tab_top = st.tabs([
|
| 772 |
-
f"All ({len(display_df)})",
|
| 773 |
-
f"Positive ({sent_sum.get('POSITIVE',0)})",
|
| 774 |
-
f"Negative ({sent_sum.get('NEGATIVE',0)})",
|
| 775 |
-
"Most Liked",
|
| 776 |
-
])
|
| 777 |
|
| 778 |
-
|
| 779 |
-
|
| 780 |
-
show_cols += ["sentiment", "compound"]
|
| 781 |
|
| 782 |
-
|
| 783 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 784 |
|
| 785 |
-
|
| 786 |
-
|
| 787 |
-
|
| 788 |
-
|
| 789 |
-
|
| 790 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 791 |
|
| 792 |
-
|
| 793 |
-
|
| 794 |
-
|
| 795 |
-
|
| 796 |
-
|
| 797 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 798 |
|
| 799 |
-
|
| 800 |
-
|
| 801 |
-
|
|
|
|
|
|
|
| 802 |
|
| 803 |
-
|
| 804 |
-
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
app.py β Video Verifier & Sentiment Analyzer
|
| 3 |
+
Gradio dark-mode application.
|
| 4 |
+
fetcher.py, analyzer.py, charts.py are UNCHANGED β only this file replaces the Streamlit version.
|
| 5 |
"""
|
| 6 |
|
| 7 |
import os
|
|
|
|
|
|
|
| 8 |
import pandas as pd
|
| 9 |
+
import gradio as gr
|
| 10 |
|
| 11 |
from fetcher import (
|
| 12 |
extract_video_id,
|
|
|
|
| 32 |
)
|
| 33 |
|
| 34 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 35 |
+
# CSS β same dark palette as the Streamlit version
|
| 36 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 37 |
|
| 38 |
+
CSS = """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
@import url('https://fonts.googleapis.com/css2?family=DM+Mono:wght@400;500&family=Syne:wght@400;600;700;800&family=IBM+Plex+Sans:wght@300;400;500&display=swap');
|
| 40 |
|
|
|
|
| 41 |
:root {
|
| 42 |
+
--bg: #0d0f14;
|
| 43 |
+
--card: #13161e;
|
| 44 |
+
--border: #1e2330;
|
| 45 |
+
--text: #e8eaf0;
|
| 46 |
+
--dim: #5a6070;
|
| 47 |
+
--cyan: #00d4ff;
|
| 48 |
+
--green: #00e5a0;
|
| 49 |
+
--red: #ff4757;
|
| 50 |
+
--amber: #ffb347;
|
| 51 |
+
--blue: #4a8eff;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
}
|
| 53 |
|
| 54 |
+
body, .gradio-container { background: var(--bg) !important; font-family: 'IBM Plex Sans', sans-serif !important; color: var(--text) !important; max-width: 1400px !important; margin: 0 auto; }
|
| 55 |
+
footer { display: none !important; }
|
| 56 |
+
|
| 57 |
+
/* Panels */
|
| 58 |
+
.gr-group, .gr-box, .gr-panel, div[class*="block"] { background: var(--card) !important; border: 1px solid var(--border) !important; border-radius: 12px !important; }
|
| 59 |
+
|
| 60 |
+
/* Tabs */
|
| 61 |
+
.tab-nav button { background: transparent !important; border: none !important; color: var(--dim) !important; font-family: 'DM Mono', monospace !important; font-size: 0.82rem !important; letter-spacing: 0.05em !important; border-bottom: 2px solid transparent !important; padding: 0.5rem 1.2rem !important; transition: color 0.2s; }
|
| 62 |
+
.tab-nav button.selected { color: var(--cyan) !important; border-bottom-color: var(--cyan) !important; }
|
| 63 |
+
.tab-nav { border-bottom: 1px solid var(--border) !important; }
|
| 64 |
+
|
| 65 |
+
/* Inputs */
|
| 66 |
+
input[type="text"], input[type="password"], textarea { background: #1a1d27 !important; border: 1px solid var(--border) !important; color: var(--text) !important; border-radius: 8px !important; font-family: 'DM Mono', monospace !important; font-size: 0.88rem !important; }
|
| 67 |
+
input:focus, textarea:focus { border-color: var(--cyan) !important; box-shadow: 0 0 0 2px rgba(0,212,255,0.15) !important; outline: none !important; }
|
| 68 |
+
label, .gr-label { color: var(--dim) !important; font-family: 'DM Mono', monospace !important; font-size: 0.75rem !important; letter-spacing: 0.08em !important; text-transform: uppercase; }
|
| 69 |
+
|
| 70 |
+
/* Buttons */
|
| 71 |
+
button.primary, button[variant="primary"] { background: linear-gradient(135deg, var(--cyan), var(--blue)) !important; border: none !important; color: var(--bg) !important; font-weight: 700 !important; font-family: 'DM Mono', monospace !important; border-radius: 8px !important; letter-spacing: 0.06em !important; }
|
| 72 |
+
button.secondary { background: rgba(0,212,255,0.08) !important; border: 1px solid var(--cyan) !important; color: var(--cyan) !important; border-radius: 8px !important; font-family: 'DM Mono', monospace !important; }
|
| 73 |
+
button:hover { opacity: 0.88; transform: translateY(-1px); transition: all 0.15s; }
|
| 74 |
+
|
| 75 |
+
/* Dropdown */
|
| 76 |
+
select { background: #1a1d27 !important; border: 1px solid var(--border) !important; color: var(--text) !important; border-radius: 8px !important; }
|
| 77 |
+
|
| 78 |
+
/* Slider */
|
| 79 |
+
input[type="range"] { accent-color: var(--cyan); }
|
| 80 |
+
|
| 81 |
+
/* Dataframe */
|
| 82 |
+
.gr-dataframe table { background: var(--card) !important; border-collapse: collapse; width: 100%; }
|
| 83 |
+
.gr-dataframe th { background: #1a1d27 !important; color: var(--cyan) !important; font-family: 'DM Mono', monospace !important; font-size: 0.75rem !important; padding: 6px 10px; border-bottom: 1px solid var(--border); }
|
| 84 |
+
.gr-dataframe td { color: var(--text) !important; font-size: 0.78rem !important; padding: 5px 10px; border-bottom: 1px solid var(--border); }
|
| 85 |
+
.gr-dataframe tr:hover td { background: rgba(0,212,255,0.04) !important; }
|
| 86 |
+
|
| 87 |
+
/* Shared HTML helpers */
|
| 88 |
+
.vv-hero { font-family: 'Syne', sans-serif; font-size: 1.6rem; font-weight: 800; background: linear-gradient(135deg, #00d4ff, #4a8eff); -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text; letter-spacing: -0.02em; line-height: 1.2; }
|
| 89 |
+
.vv-section-title { font-family: 'Syne', sans-serif; font-size: 0.68rem; font-weight: 700; letter-spacing: 0.18em; text-transform: uppercase; color: #5a6070; margin-bottom: 0.5rem; }
|
| 90 |
+
.vv-card { background: #13161e; border: 1px solid #1e2330; border-radius: 12px; padding: 1.2rem 1.4rem; margin-bottom: 0.8rem; }
|
| 91 |
+
.vv-stat { display: inline-block; background: #1a1d27; border: 1px solid #1e2330; border-radius: 6px; padding: 0.25rem 0.75rem; font-family: 'DM Mono', monospace; font-size: 0.78rem; color: #00d4ff; margin: 0.15rem 0.2rem; }
|
| 92 |
+
.vv-badge-green { display: inline-block; background: rgba(0,229,160,0.12); border: 1px solid #00e5a0; color: #00e5a0; border-radius: 20px; padding: 0.3rem 1rem; font-size: 0.82rem; font-family: 'DM Mono', monospace; }
|
| 93 |
+
.vv-badge-red { display: inline-block; background: rgba(255,71,87,0.12); border: 1px solid #ff4757; color: #ff4757; border-radius: 20px; padding: 0.3rem 1rem; font-size: 0.82rem; font-family: 'DM Mono', monospace; }
|
| 94 |
+
.vv-badge-amber { display: inline-block; background: rgba(255,179,71,0.12); border: 1px solid #ffb347; color: #ffb347; border-radius: 20px; padding: 0.3rem 1rem; font-size: 0.82rem; font-family: 'DM Mono', monospace; }
|
| 95 |
+
.vv-reasoning { background: #0d1119; border-left: 3px solid #ffb347; padding: 0.75rem 1rem; border-radius: 0 8px 8px 0; font-size: 0.83rem; color: #c0c4cc; line-height: 1.65; font-family: 'IBM Plex Sans', sans-serif; }
|
| 96 |
+
.vv-tag { display: inline-block; background: #1a1d27; border: 1px solid #1e2330; border-radius: 4px; padding: 2px 8px; font-family: 'DM Mono', monospace; font-size: 0.7rem; color: #8090a0; margin: 2px; }
|
| 97 |
+
.vv-stat-big-green { font-family: 'DM Mono', monospace; font-size: 1.6rem; font-weight: 700; color: #00e5a0; margin: 0; }
|
| 98 |
+
.vv-stat-big-red { font-family: 'DM Mono', monospace; font-size: 1.6rem; font-weight: 700; color: #ff4757; margin: 0; }
|
| 99 |
+
.vv-stat-big-dim { font-family: 'DM Mono', monospace; font-size: 1.6rem; font-weight: 700; color: #5a6070; margin: 0; }
|
| 100 |
+
.vv-log-line { font-size: 0.72rem; color: #5a6070; font-family: 'DM Mono', monospace; margin: 2px 0; }
|
| 101 |
+
"""
|
| 102 |
|
| 103 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 104 |
+
# SHARED HELPERS
|
| 105 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 106 |
|
| 107 |
+
def _empty_plotly():
|
| 108 |
+
import plotly.graph_objects as go
|
| 109 |
+
fig = go.Figure()
|
| 110 |
+
fig.update_layout(
|
| 111 |
+
paper_bgcolor="rgba(0,0,0,0)", plot_bgcolor="rgba(0,0,0,0)",
|
| 112 |
+
font=dict(color="#5a6070"), margin=dict(l=10, r=10, t=10, b=10), height=200,
|
| 113 |
+
)
|
| 114 |
+
fig.add_annotation(text="Run analysis to see data", x=0.5, y=0.5,
|
| 115 |
+
showarrow=False, font=dict(size=13, color="#5a6070"))
|
| 116 |
+
return fig
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
def _blank_outputs(status_msg: str):
|
| 120 |
+
"""18-element tuple matching ALL_OUTPUTS when nothing has run yet."""
|
| 121 |
+
ep = _empty_plotly()
|
| 122 |
+
return (
|
| 123 |
+
f'<p style="color:#ff4757;font-family:DM Mono,monospace;padding:8px">{status_msg}</p>', # status
|
| 124 |
+
"<p class='vv-log-line'>β</p>", # log
|
| 125 |
+
"<div style='padding:3rem;text-align:center;color:#5a6070'>No data yet.</div>", # left panel
|
| 126 |
+
"", "", # badge, reasoning
|
| 127 |
+
ep, ep, ep, ep, ep, ep, # 6 charts
|
| 128 |
+
"", "", "", # 3 stat boxes
|
| 129 |
+
pd.DataFrame(), pd.DataFrame(), pd.DataFrame(), pd.DataFrame(), # 4 dataframes
|
| 130 |
+
)
|
| 131 |
|
| 132 |
|
| 133 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 134 |
+
# PIPELINE (replaces Streamlit's run_full_pipeline + session_state writes)
|
| 135 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 136 |
|
| 137 |
+
def run_pipeline(
|
| 138 |
+
url_or_id: str,
|
| 139 |
+
api_key: str,
|
| 140 |
+
sentiment_method: str,
|
| 141 |
+
max_comments: int,
|
| 142 |
+
progress=gr.Progress(track_tqdm=False),
|
| 143 |
+
):
|
| 144 |
+
"""
|
| 145 |
+
Generator function β yields one final tuple when all work is done.
|
| 146 |
+
gr.Progress() gives the user an animated progress bar while waiting.
|
| 147 |
+
All logic is identical to the Streamlit version; we just return values
|
| 148 |
+
instead of writing to st.session_state.
|
| 149 |
+
"""
|
| 150 |
+
# ββ Input guards ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 151 |
+
if not (url_or_id or "").strip():
|
| 152 |
+
yield _blank_outputs("β οΈ Please enter a YouTube URL or video ID.")
|
| 153 |
+
return
|
| 154 |
|
| 155 |
+
video_id = extract_video_id(url_or_id.strip())
|
| 156 |
+
if not video_id:
|
| 157 |
+
yield _blank_outputs("β Could not parse a valid YouTube video ID from that input.")
|
| 158 |
+
return
|
| 159 |
|
| 160 |
+
if not (api_key or "").strip():
|
| 161 |
+
yield _blank_outputs("β οΈ YouTube API key is required. Set it in the βοΈ Settings tab.")
|
| 162 |
+
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
+
# 1 ββ Metadata βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 165 |
+
progress(0.05, desc="Fetching video metadataβ¦")
|
| 166 |
+
meta, err_msg = fetch_video_metadata(video_id, api_key)
|
| 167 |
+
if err_msg:
|
| 168 |
+
yield _blank_outputs(f"β {err_msg}")
|
| 169 |
+
return
|
| 170 |
|
| 171 |
+
# 2 ββ Transcript ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 172 |
+
progress(0.20, desc="Fetching transcriptβ¦")
|
| 173 |
+
transcript, t_status = fetch_transcript(video_id)
|
| 174 |
+
|
| 175 |
+
# 3 ββ Comments ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 176 |
+
progress(0.35, desc=f"Fetching up to {max_comments} commentsβ¦")
|
| 177 |
+
comments_df, c_status = fetch_comments(video_id, api_key, max_comments=int(max_comments))
|
| 178 |
+
|
| 179 |
+
# 4 ββ Misinformation detection ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 180 |
+
progress(0.50, desc="Running misinformation detectionβ¦")
|
| 181 |
+
misinfo = detect_misinformation(
|
| 182 |
+
text=f"{meta['title']} {meta['description']}",
|
| 183 |
+
tags=meta["tags"],
|
| 184 |
+
audio_transcript=transcript,
|
| 185 |
+
video_transcript=transcript,
|
| 186 |
)
|
| 187 |
|
| 188 |
+
# 5 ββ Keywords ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 189 |
+
keywords = extract_keywords(
|
| 190 |
+
f"{meta['title']} {meta['description']} {transcript}",
|
| 191 |
+
meta["tags"],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
)
|
| 193 |
|
| 194 |
+
# 6 ββ Sentiment (batched, same logic as Streamlit version) βββββββββββββββββ
|
| 195 |
+
sentiments, sent_sum, pos_kw, neg_kw = [], {}, [], []
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
+
if not comments_df.empty:
|
| 198 |
+
texts = comments_df["text"].fillna("").tolist()
|
| 199 |
+
batch_size = 64
|
| 200 |
+
for i in range(0, len(texts), batch_size):
|
| 201 |
+
chunk = texts[i: i + batch_size]
|
| 202 |
+
sentiments += analyze_sentiment_batch(chunk, method=sentiment_method, batch_size=batch_size)
|
| 203 |
+
frac = 0.60 + 0.30 * min((i + batch_size) / max(len(texts), 1), 1.0)
|
| 204 |
+
progress(frac, desc=f"Sentiment: {min(i+batch_size, len(texts))}/{len(texts)}β¦")
|
| 205 |
+
|
| 206 |
+
sent_sum = sentiment_summary(sentiments)
|
| 207 |
+
pos_kw, neg_kw = sentiment_weighted_keywords(comments_df, sentiments)
|
| 208 |
+
|
| 209 |
+
# 7 ββ Assemble and yield ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 210 |
+
progress(0.97, desc="Building chartsβ¦")
|
| 211 |
+
yield _build_outputs(
|
| 212 |
+
meta=meta, video_id=video_id, transcript=transcript,
|
| 213 |
+
comments_df=comments_df, misinfo=misinfo, keywords=keywords,
|
| 214 |
+
sentiments=sentiments, sent_sum=sent_sum,
|
| 215 |
+
pos_kw=pos_kw, neg_kw=neg_kw,
|
| 216 |
+
status_log=[
|
| 217 |
+
f"β
Metadata: {meta['title'][:55]}",
|
| 218 |
+
t_status, c_status,
|
| 219 |
+
f"π¬ Misinfo score: {misinfo['confidence_pct']}%",
|
| 220 |
+
*(
|
| 221 |
+
[f"π¬ Sentiment: {sent_sum['pos_pct']}% pos / {sent_sum['neg_pct']}% neg"]
|
| 222 |
+
if sent_sum else ["π¬ Skipped (no comments available)"]
|
| 223 |
+
),
|
| 224 |
+
],
|
| 225 |
+
)
|
| 226 |
|
| 227 |
|
| 228 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 229 |
+
# OUTPUT BUILDER (assembles the 18-element tuple from all computed data)
|
| 230 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββοΏ½οΏ½οΏ½ββββββββββββββββββββββββ
|
| 231 |
|
| 232 |
+
def _build_outputs(
|
| 233 |
+
meta, video_id, transcript, comments_df,
|
| 234 |
+
misinfo, keywords, sentiments, sent_sum, pos_kw, neg_kw, status_log,
|
| 235 |
+
):
|
| 236 |
+
# ββ Status ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 237 |
+
status_html = (
|
| 238 |
+
'<p style="color:#00e5a0;font-family:DM Mono,monospace;font-size:0.82rem;padding:6px 0">'
|
| 239 |
+
'β
Analysis complete</p>'
|
| 240 |
+
)
|
| 241 |
|
| 242 |
+
# ββ Log βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 243 |
+
log_html = "".join(f'<p class="vv-log-line">{line}</p>' for line in status_log)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
|
| 245 |
+
# ββ Left panel (video info) ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 246 |
+
thumb_html = (
|
| 247 |
+
f'<img src="{meta["thumbnail_url"]}" '
|
| 248 |
+
f'style="width:100%;border-radius:8px;margin-bottom:8px">'
|
| 249 |
+
if meta.get("thumbnail_url") else ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
)
|
| 251 |
+
tag_html = "".join(f'<span class="vv-tag">#{t}</span>' for t in meta.get("tags", [])[:20])
|
| 252 |
+
desc_text = meta.get("description", "")
|
| 253 |
+
desc_short = desc_text[:1200] + ("β¦" if len(desc_text) > 1200 else "")
|
| 254 |
+
word_count = len(transcript.split()) if transcript else 0
|
| 255 |
+
transcript_short = (transcript[:2500] + ("β¦" if len(transcript) > 2500 else "")) if transcript else "(not available)"
|
| 256 |
+
|
| 257 |
+
left_html = f"""
|
| 258 |
+
{thumb_html}
|
| 259 |
+
<a href="https://www.youtube.com/watch?v={video_id}" target="_blank"
|
| 260 |
+
style="display:block;text-align:center;font-family:'DM Mono',monospace;
|
| 261 |
+
font-size:0.76rem;color:#5a6070;text-decoration:none;margin:4px 0 10px">
|
| 262 |
+
βΆ Open on YouTube
|
| 263 |
+
</a>
|
| 264 |
+
<div class="vv-card">
|
| 265 |
+
<p class="vv-section-title">Video</p>
|
| 266 |
+
<p style="font-family:'Syne',sans-serif;font-size:1.05rem;font-weight:700;margin:0 0 4px;color:#e8eaf0">
|
| 267 |
+
{meta['title']}
|
| 268 |
+
</p>
|
| 269 |
+
<p style="font-size:0.82rem;color:#5a6070;margin:0">
|
| 270 |
+
by <b style="color:#b0b4c0">{meta['channel_title']}</b> Β· {meta['published_at']}
|
| 271 |
+
</p>
|
| 272 |
+
</div>
|
| 273 |
+
<p class="vv-section-title">Metrics</p>
|
| 274 |
+
<span class="vv-stat">π {meta['view_count']:,}</span>
|
| 275 |
+
<span class="vv-stat">π {meta['like_count']:,}</span>
|
| 276 |
+
<span class="vv-stat">π¬ {meta['comment_count']:,}</span>
|
| 277 |
+
<span class="vv-stat">β± {meta['duration']}</span>
|
| 278 |
+
<p class="vv-section-title" style="margin-top:1rem">Tags</p>
|
| 279 |
+
{tag_html or '<span style="color:#5a6070;font-size:0.78rem">(none)</span>'}
|
| 280 |
+
<details style="margin-top:1rem">
|
| 281 |
+
<summary style="cursor:pointer;font-family:'DM Mono',monospace;font-size:0.78rem;color:#5a6070">
|
| 282 |
+
π Description
|
| 283 |
+
</summary>
|
| 284 |
+
<p style="font-size:0.78rem;color:#8090a0;line-height:1.65;white-space:pre-wrap;margin-top:6px">
|
| 285 |
+
{desc_short}
|
| 286 |
+
</p>
|
| 287 |
+
</details>
|
| 288 |
+
<details style="margin-top:0.5rem">
|
| 289 |
+
<summary style="cursor:pointer;font-family:'DM Mono',monospace;font-size:0.78rem;color:#5a6070">
|
| 290 |
+
π Transcript ({word_count} words)
|
| 291 |
+
</summary>
|
| 292 |
+
<p style="font-size:0.75rem;color:#8090a0;line-height:1.65;margin-top:6px">
|
| 293 |
+
{transcript_short}
|
| 294 |
+
</p>
|
| 295 |
+
</details>
|
| 296 |
+
"""
|
| 297 |
+
|
| 298 |
+
# ββ Misinfo badge + reasoning ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 299 |
+
score = misinfo["score"]
|
| 300 |
+
if score < 0.35:
|
| 301 |
+
badge_html = '<span class="vv-badge-green">β
Appears Credible</span>'
|
| 302 |
+
elif score < 0.65:
|
| 303 |
+
badge_html = '<span class="vv-badge-amber">β οΈ Uncertain / Mixed Signals</span>'
|
| 304 |
+
else:
|
| 305 |
+
badge_html = '<span class="vv-badge-red">π¨ Likely Misinformation</span>'
|
| 306 |
|
| 307 |
+
reasoning_html = (
|
| 308 |
+
f'<div class="vv-reasoning" style="margin-top:8px">'
|
| 309 |
+
f'π§ <b>Reasoning:</b> {misinfo["reasoning"]}'
|
| 310 |
+
f'</div>'
|
| 311 |
+
)
|
| 312 |
|
| 313 |
+
# ββ Plotly charts ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 314 |
+
fig_gauge = misinfo_gauge(score, "Misinfo Confidence")
|
| 315 |
+
fig_streams = stream_trust_bars(misinfo["stream_details"])
|
| 316 |
+
fig_donut = sentiment_donut(sent_sum) if sent_sum else _empty_plotly()
|
| 317 |
+
fig_timeline = sentiment_timeline(comments_df, sentiments) if (sent_sum and not comments_df.empty) else _empty_plotly()
|
| 318 |
+
fig_kw = keyword_bar(keywords, title="Top Video Keywords", color="#00d4ff")
|
| 319 |
+
fig_kw_comp = keyword_comparison(pos_kw, neg_kw) if (pos_kw or neg_kw) else _empty_plotly()
|
| 320 |
|
| 321 |
+
# ββ Sentiment stat boxes βββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 322 |
+
if sent_sum:
|
| 323 |
+
stat_pos = (f'<div class="vv-card" style="text-align:center">'
|
| 324 |
+
f'<p class="vv-stat-big-green">{sent_sum["pos_pct"]}%</p>'
|
| 325 |
+
f'<p style="color:#5a6070;font-size:0.75rem;margin:0">Positive</p></div>')
|
| 326 |
+
stat_neg = (f'<div class="vv-card" style="text-align:center">'
|
| 327 |
+
f'<p class="vv-stat-big-red">{sent_sum["neg_pct"]}%</p>'
|
| 328 |
+
f'<p style="color:#5a6070;font-size:0.75rem;margin:0">Negative</p></div>')
|
| 329 |
+
stat_neu = (f'<div class="vv-card" style="text-align:center">'
|
| 330 |
+
f'<p class="vv-stat-big-dim">{sent_sum["neu_pct"]}%</p>'
|
| 331 |
+
f'<p style="color:#5a6070;font-size:0.75rem;margin:0">Neutral</p></div>')
|
| 332 |
+
else:
|
| 333 |
+
placeholder = '<div class="vv-card" style="text-align:center;color:#5a6070;font-size:0.8rem">N/A</div>'
|
| 334 |
+
stat_pos = stat_neg = stat_neu = placeholder
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
|
| 336 |
+
# ββ Comment DataFrames for the 4 tabs βββββββββββββββββββββββββββββββββββββ
|
| 337 |
+
show_cols = ["author", "text", "likes", "published_at"]
|
| 338 |
+
df_all = df_pos = df_neg = df_top = pd.DataFrame()
|
| 339 |
|
|
|
|
| 340 |
if not comments_df.empty:
|
| 341 |
+
display_df = comments_df.copy()
|
| 342 |
+
if sentiments:
|
| 343 |
+
display_df["sentiment"] = [s["label"] for s in sentiments]
|
| 344 |
+
display_df["compound"] = [round(s.get("compound", 0), 3) for s in sentiments]
|
| 345 |
+
cols = show_cols + ["sentiment", "compound"]
|
| 346 |
+
else:
|
| 347 |
+
cols = show_cols
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 348 |
|
| 349 |
+
df_all = display_df[cols].head(100).reset_index(drop=True)
|
| 350 |
+
df_top = display_df.sort_values("likes", ascending=False).head(20)[cols].reset_index(drop=True)
|
| 351 |
|
| 352 |
+
if "sentiment" in display_df.columns:
|
| 353 |
+
df_pos = display_df[display_df["sentiment"] == "POSITIVE"][cols].head(50).reset_index(drop=True)
|
| 354 |
+
df_neg = display_df[display_df["sentiment"] == "NEGATIVE"][cols].head(50).reset_index(drop=True)
|
| 355 |
+
|
| 356 |
+
return (
|
| 357 |
+
status_html, # 0 status_box
|
| 358 |
+
log_html, # 1 log_html_out
|
| 359 |
+
left_html, # 2 left_panel_html
|
| 360 |
+
badge_html, # 3 misinfo_badge_html
|
| 361 |
+
reasoning_html, # 4 misinfo_reasoning_html
|
| 362 |
+
fig_gauge, # 5 misinfo_gauge_plot
|
| 363 |
+
fig_streams, # 6 stream_bars_plot
|
| 364 |
+
fig_donut, # 7 donut_plot
|
| 365 |
+
fig_timeline, # 8 timeline_plot
|
| 366 |
+
fig_kw, # 9 kw_bar_plot
|
| 367 |
+
fig_kw_comp, # 10 kw_comp_plot
|
| 368 |
+
stat_pos, # 11 stat_pos_html
|
| 369 |
+
stat_neg, # 12 stat_neg_html
|
| 370 |
+
stat_neu, # 13 stat_neu_html
|
| 371 |
+
df_all, # 14 df_all_out
|
| 372 |
+
df_pos, # 15 df_pos_out
|
| 373 |
+
df_neg, # 16 df_neg_out
|
| 374 |
+
df_top, # 17 df_top_out
|
| 375 |
+
)
|
| 376 |
|
| 377 |
|
| 378 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 379 |
+
# UPLOAD TAB β search by keyword helper
|
| 380 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 381 |
|
| 382 |
+
def do_search(keyword: str, api_key: str):
|
| 383 |
+
if not (api_key or "").strip():
|
| 384 |
+
return (
|
| 385 |
+
"<p style='color:#ff4757;font-family:DM Mono,monospace'>β οΈ API key required.</p>",
|
| 386 |
+
gr.update(choices=[], value=None, visible=False),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 387 |
)
|
| 388 |
+
results = search_videos_by_title(keyword, api_key, max_results=5)
|
| 389 |
+
if not results:
|
| 390 |
+
return (
|
| 391 |
+
"<p style='color:#ffb347;font-family:DM Mono,monospace'>No results found. Try a different keyword.</p>",
|
| 392 |
+
gr.update(choices=[], value=None, visible=False),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 393 |
)
|
| 394 |
+
|
| 395 |
+
html = ""
|
| 396 |
+
choices = []
|
| 397 |
+
for r in results:
|
| 398 |
+
vid = r["video_id"]
|
| 399 |
+
url = f"https://www.youtube.com/watch?v={vid}"
|
| 400 |
+
choices.append((r["title"][:70], url))
|
| 401 |
+
html += (
|
| 402 |
+
f'<div class="vv-card" style="display:flex;align-items:center;gap:12px;margin-bottom:6px">'
|
| 403 |
+
f'<img src="{r["thumbnail_url"]}" style="width:72px;height:54px;object-fit:cover;border-radius:6px;flex-shrink:0">'
|
| 404 |
+
f'<div><p style="margin:0;font-size:0.85rem;font-weight:600;color:#e8eaf0">{r["title"][:80]}</p>'
|
| 405 |
+
f'<p style="margin:0;font-size:0.75rem;color:#5a6070">{r["channel_title"]} Β· {r["published_at"]} Β· '
|
| 406 |
+
f'<code style="color:#00d4ff">v={vid}</code></p></div></div>'
|
| 407 |
)
|
| 408 |
+
return html, gr.update(choices=choices, value=None, visible=True)
|
| 409 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 410 |
|
| 411 |
+
def pick_and_analyze(selected_url, api_key, sentiment_method, max_comments):
|
| 412 |
+
"""When user picks a search result, run the full pipeline on it."""
|
| 413 |
+
if not selected_url:
|
| 414 |
+
yield _blank_outputs("Select a video from the search results above.")
|
| 415 |
+
return
|
| 416 |
+
yield from run_pipeline(selected_url, api_key, sentiment_method, max_comments)
|
| 417 |
|
|
|
|
|
|
|
| 418 |
|
| 419 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 420 |
+
# GRADIO BLOCKS UI
|
| 421 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 422 |
|
| 423 |
+
with gr.Blocks(css=CSS, title="VideoVerifier β MHMisinfo", theme=gr.themes.Base()) as demo:
|
| 424 |
+
|
| 425 |
+
# ββ Header ββββββββββββββββββββββββοΏ½οΏ½οΏ½βββββββββββββββββββββββββββββββββββββββ
|
| 426 |
+
gr.HTML("""
|
| 427 |
+
<div style="padding:1.5rem 0 0.8rem;border-bottom:1px solid #1e2330;margin-bottom:1.2rem">
|
| 428 |
+
<h1 class="vv-hero">π¬ Video Verifier & Sentiment Analyzer</h1>
|
| 429 |
+
<p style="color:#5a6070;font-size:0.85rem;margin-top:4px;font-family:'DM Mono',monospace">
|
| 430 |
+
Detect mental health misinformation Β· Analyze public sentiment Β· Understand video content at a glance
|
| 431 |
+
</p>
|
| 432 |
+
</div>
|
| 433 |
+
""")
|
| 434 |
+
|
| 435 |
+
# ββ Settings row (always visible at top) ββββββββββββββββββββββββββββββββββ
|
| 436 |
+
with gr.Accordion("βοΈ Settings", open=False):
|
| 437 |
+
with gr.Row():
|
| 438 |
+
api_key_input = gr.Textbox(
|
| 439 |
+
value=os.environ.get("YT_API_KEY", ""),
|
| 440 |
+
placeholder="AIzaβ¦",
|
| 441 |
+
label="YouTube Data API v3 Key",
|
| 442 |
+
type="password",
|
| 443 |
+
scale=3,
|
| 444 |
+
info="Get a free key at console.cloud.google.com β Enable YouTube Data API v3",
|
| 445 |
)
|
| 446 |
+
sentiment_selector = gr.Dropdown(
|
| 447 |
+
choices=[
|
| 448 |
+
("VADER β fast, CPU-only (~5 000 comments/sec)", "vader"),
|
| 449 |
+
("DistilBERT β accurate, downloads ~500 MB on first run", "hf"),
|
| 450 |
+
],
|
| 451 |
+
value="vader",
|
| 452 |
+
label="Sentiment Engine",
|
| 453 |
+
scale=2,
|
| 454 |
)
|
| 455 |
+
max_comments_slider = gr.Slider(
|
| 456 |
+
minimum=10, maximum=500, value=150, step=10,
|
| 457 |
+
label="Max comments to fetch",
|
| 458 |
+
scale=2,
|
| 459 |
+
info="YouTube API quota: ~1 unit per comment request",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 460 |
)
|
| 461 |
|
| 462 |
+
# ββ Input tabs βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 463 |
+
with gr.Tabs():
|
| 464 |
+
|
| 465 |
+
# Tab 1 β URL
|
| 466 |
+
with gr.TabItem("π YouTube URL"):
|
| 467 |
+
with gr.Row():
|
| 468 |
+
url_input = gr.Textbox(
|
| 469 |
+
placeholder="https://www.youtube.com/watch?v=... or youtu.be/... or raw 11-char ID",
|
| 470 |
+
label="YouTube URL / Video ID",
|
| 471 |
+
scale=5,
|
| 472 |
+
)
|
| 473 |
+
analyze_btn = gr.Button("π Analyze", variant="primary", scale=1, min_width=130)
|
| 474 |
+
|
| 475 |
+
# Tab 2 β Upload / search
|
| 476 |
+
with gr.TabItem("π Upload / Search by Title"):
|
| 477 |
+
gr.HTML("""
|
| 478 |
+
<div class="vv-card" style="margin-bottom:8px">
|
| 479 |
+
<p class="vv-section-title">Upload a video file β find matching YouTube metadata</p>
|
| 480 |
+
<p style="font-size:0.82rem;color:#5a6070;line-height:1.6;margin:0">
|
| 481 |
+
β οΈ The YouTube Data API cannot search by raw video bytes.
|
| 482 |
+
Upload your file, then type the title or a keyword below to find the matching YouTube entry.
|
| 483 |
+
</p>
|
| 484 |
+
</div>
|
| 485 |
+
""")
|
| 486 |
+
upload_file = gr.File(
|
| 487 |
+
label="Drop a video file (mp4, mov, avi, mkv, webm)",
|
| 488 |
+
file_types=[".mp4", ".mov", ".avi", ".mkv", ".webm"],
|
| 489 |
+
)
|
| 490 |
+
with gr.Row():
|
| 491 |
+
kw_input = gr.Textbox(placeholder="Enter video title or keywordβ¦", label="Search keyword", scale=4)
|
| 492 |
+
search_btn = gr.Button("π Find on YouTube", scale=1)
|
| 493 |
+
search_results_html = gr.HTML()
|
| 494 |
+
search_radio = gr.Radio(
|
| 495 |
+
label="Select a video to analyze",
|
| 496 |
+
choices=[],
|
| 497 |
+
visible=False,
|
| 498 |
)
|
| 499 |
|
| 500 |
+
# ββ Status bar βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 501 |
+
status_box = gr.HTML(
|
| 502 |
+
'<p style="color:#5a6070;font-family:DM Mono,monospace;font-size:0.8rem;padding:6px 0">'
|
| 503 |
+
'Enter a URL above and click Analyze.</p>'
|
| 504 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 505 |
|
| 506 |
+
# ββ Main results layout ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 507 |
+
with gr.Row(equal_height=False):
|
|
|
|
| 508 |
|
| 509 |
+
# LEFT β video info
|
| 510 |
+
with gr.Column(scale=2):
|
| 511 |
+
left_panel_html = gr.HTML(
|
| 512 |
+
'<div style="padding:3rem;text-align:center;color:#5a6070;'
|
| 513 |
+
'font-family:DM Mono,monospace">No data yet.</div>'
|
| 514 |
+
)
|
| 515 |
|
| 516 |
+
# RIGHT β analytics
|
| 517 |
+
with gr.Column(scale=3):
|
| 518 |
+
|
| 519 |
+
# Misinfo block
|
| 520 |
+
gr.HTML('<p class="vv-section-title" style="margin-top:0">π¬ Misinformation Analysis</p>')
|
| 521 |
+
misinfo_badge_html = gr.HTML()
|
| 522 |
+
with gr.Row():
|
| 523 |
+
misinfo_gauge_plot = gr.Plot(label="", show_label=False)
|
| 524 |
+
stream_bars_plot = gr.Plot(label="", show_label=False)
|
| 525 |
+
misinfo_reasoning_html = gr.HTML()
|
| 526 |
+
|
| 527 |
+
gr.HTML('<hr style="border-color:#1e2330;margin:1rem 0">')
|
| 528 |
+
|
| 529 |
+
# Sentiment block
|
| 530 |
+
gr.HTML('<p class="vv-section-title">π¬ Comment Sentiment</p>')
|
| 531 |
+
with gr.Row():
|
| 532 |
+
stat_pos_html = gr.HTML()
|
| 533 |
+
stat_neg_html = gr.HTML()
|
| 534 |
+
stat_neu_html = gr.HTML()
|
| 535 |
+
with gr.Row():
|
| 536 |
+
donut_plot = gr.Plot(label="", show_label=False)
|
| 537 |
+
timeline_plot = gr.Plot(label="", show_label=False)
|
| 538 |
+
with gr.Row():
|
| 539 |
+
kw_bar_plot = gr.Plot(label="", show_label=False)
|
| 540 |
+
kw_comp_plot = gr.Plot(label="", show_label=False)
|
| 541 |
+
|
| 542 |
+
gr.HTML('<hr style="border-color:#1e2330;margin:1rem 0">')
|
| 543 |
+
|
| 544 |
+
# Comments deep-dive
|
| 545 |
+
gr.HTML('<p class="vv-section-title">π Comments Deep-Dive</p>')
|
| 546 |
+
with gr.Tabs():
|
| 547 |
+
with gr.TabItem("All"):
|
| 548 |
+
df_all_out = gr.Dataframe(
|
| 549 |
+
headers=["author","text","likes","published_at","sentiment","compound"],
|
| 550 |
+
datatype=["str","str","number","str","str","number"],
|
| 551 |
+
wrap=True, height=320,
|
| 552 |
+
)
|
| 553 |
+
with gr.TabItem("Positive"):
|
| 554 |
+
df_pos_out = gr.Dataframe(wrap=True, height=320)
|
| 555 |
+
with gr.TabItem("Negative"):
|
| 556 |
+
df_neg_out = gr.Dataframe(wrap=True, height=320)
|
| 557 |
+
with gr.TabItem("Most Liked"):
|
| 558 |
+
df_top_out = gr.Dataframe(wrap=True, height=320)
|
| 559 |
+
|
| 560 |
+
# ββ Activity log ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 561 |
+
with gr.Accordion("π Activity Log", open=False):
|
| 562 |
+
log_html_out = gr.HTML(
|
| 563 |
+
'<p class="vv-log-line">β</p>'
|
| 564 |
+
)
|
| 565 |
|
| 566 |
+
# ββ Footer βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 567 |
+
gr.HTML("""
|
| 568 |
+
<div style="margin-top:2rem;padding-top:1rem;border-top:1px solid #1e2330;
|
| 569 |
+
text-align:center;font-family:'DM Mono',monospace;font-size:0.72rem;color:#3a3f50">
|
| 570 |
+
4-stream SeTa-Attention BiGRU Β· CCM / DMTE / Uncertainty Fusion Β·
|
| 571 |
+
Plug your checkpoint into
|
| 572 |
+
<code style="color:#00d4ff">detect_misinformation()</code> in analyzer.py Β·
|
| 573 |
+
Test ROC-AUC 0.967
|
| 574 |
+
</div>
|
| 575 |
+
""")
|
| 576 |
+
|
| 577 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 578 |
+
# OUTPUT LIST β order must exactly match _build_outputs / _blank_outputs
|
| 579 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 580 |
+
ALL_OUTPUTS = [
|
| 581 |
+
status_box, # 0
|
| 582 |
+
log_html_out, # 1
|
| 583 |
+
left_panel_html, # 2
|
| 584 |
+
misinfo_badge_html, # 3
|
| 585 |
+
misinfo_reasoning_html,# 4
|
| 586 |
+
misinfo_gauge_plot, # 5
|
| 587 |
+
stream_bars_plot, # 6
|
| 588 |
+
donut_plot, # 7
|
| 589 |
+
timeline_plot, # 8
|
| 590 |
+
kw_bar_plot, # 9
|
| 591 |
+
kw_comp_plot, # 10
|
| 592 |
+
stat_pos_html, # 11
|
| 593 |
+
stat_neg_html, # 12
|
| 594 |
+
stat_neu_html, # 13
|
| 595 |
+
df_all_out, # 14
|
| 596 |
+
df_pos_out, # 15
|
| 597 |
+
df_neg_out, # 16
|
| 598 |
+
df_top_out, # 17
|
| 599 |
+
]
|
| 600 |
+
|
| 601 |
+
# ββ Events: URL tab βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 602 |
+
_pipeline_inputs = [url_input, api_key_input, sentiment_selector, max_comments_slider]
|
| 603 |
+
|
| 604 |
+
analyze_btn.click(fn=run_pipeline, inputs=_pipeline_inputs, outputs=ALL_OUTPUTS)
|
| 605 |
+
url_input.submit(fn=run_pipeline, inputs=_pipeline_inputs, outputs=ALL_OUTPUTS)
|
| 606 |
+
|
| 607 |
+
# ββ Events: Upload/Search tab βββββββββββββββββββββββββββββββββββββββββββββ
|
| 608 |
+
search_btn.click(
|
| 609 |
+
fn=do_search,
|
| 610 |
+
inputs=[kw_input, api_key_input],
|
| 611 |
+
outputs=[search_results_html, search_radio],
|
| 612 |
+
)
|
| 613 |
|
| 614 |
+
search_radio.change(
|
| 615 |
+
fn=pick_and_analyze,
|
| 616 |
+
inputs=[search_radio, api_key_input, sentiment_selector, max_comments_slider],
|
| 617 |
+
outputs=ALL_OUTPUTS,
|
| 618 |
+
)
|
| 619 |
|
| 620 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 621 |
+
if __name__ == "__main__":
|
| 622 |
+
demo.launch(show_api=False)
|