Spaces:
Running
Running
Update src/app.py
#12
by
KhaqanNasir
- opened
- src/app.py +441 -58
src/app.py
CHANGED
|
@@ -240,7 +240,6 @@
|
|
| 240 |
# if __name__ == "__main__":
|
| 241 |
# main()
|
| 242 |
|
| 243 |
-
|
| 244 |
import streamlit as st
|
| 245 |
import torch
|
| 246 |
import pandas as pd
|
|
@@ -278,6 +277,281 @@ from src.models.hybrid_model import HybridFakeNewsDetector
|
|
| 278 |
from src.config.config import *
|
| 279 |
from src.data.preprocessor import TextPreprocessor
|
| 280 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
@st.cache_resource
|
| 282 |
def load_model_and_tokenizer():
|
| 283 |
"""Load the model and tokenizer (cached)."""
|
|
@@ -342,15 +616,25 @@ def plot_confidence(probabilities):
|
|
| 342 |
y=list(probabilities.values()),
|
| 343 |
text=[f'{p:.2%}' for p in probabilities.values()],
|
| 344 |
textposition='auto',
|
| 345 |
-
marker_color=['#
|
|
|
|
|
|
|
| 346 |
)
|
| 347 |
])
|
| 348 |
fig.update_layout(
|
| 349 |
-
title=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 350 |
xaxis_title='Class',
|
| 351 |
yaxis_title='Probability',
|
| 352 |
yaxis_range=[0, 1],
|
| 353 |
-
template='plotly_white'
|
|
|
|
|
|
|
|
|
|
| 354 |
)
|
| 355 |
return fig
|
| 356 |
|
|
@@ -361,114 +645,213 @@ def plot_attention(text, attention_weights):
|
|
| 361 |
if isinstance(attention_weights, (list, np.ndarray)):
|
| 362 |
attention_weights = np.array(attention_weights).flatten()
|
| 363 |
formatted_weights = [f'{float(w):.2f}' for w in attention_weights]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 364 |
fig = go.Figure(data=[
|
| 365 |
go.Bar(
|
| 366 |
x=tokens,
|
| 367 |
y=attention_weights,
|
| 368 |
text=formatted_weights,
|
| 369 |
textposition='auto',
|
| 370 |
-
marker_color=
|
|
|
|
|
|
|
| 371 |
)
|
| 372 |
])
|
| 373 |
fig.update_layout(
|
| 374 |
-
title=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 375 |
xaxis_title='Tokens',
|
| 376 |
yaxis_title='Attention Weight',
|
| 377 |
xaxis_tickangle=45,
|
| 378 |
-
template='plotly_white'
|
|
|
|
|
|
|
|
|
|
| 379 |
)
|
| 380 |
return fig
|
| 381 |
|
| 382 |
def main():
|
| 383 |
-
# Hero
|
| 384 |
st.markdown("""
|
| 385 |
-
<div class="hero-
|
| 386 |
-
<
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 391 |
</p>
|
| 392 |
</div>
|
| 393 |
-
<div
|
| 394 |
-
<
|
|
|
|
|
|
|
|
|
|
|
|
|
| 395 |
</div>
|
| 396 |
</div>
|
| 397 |
</div>
|
| 398 |
""", unsafe_allow_html=True)
|
| 399 |
|
| 400 |
-
#
|
| 401 |
-
st.
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
<li>BiLSTM for sequence modeling</li>
|
| 409 |
-
<li>Attention mechanism for interpretability</li>
|
| 410 |
-
</ul>
|
| 411 |
</div>
|
| 412 |
""", unsafe_allow_html=True)
|
| 413 |
|
| 414 |
-
#
|
| 415 |
-
st.
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 423 |
if news_text:
|
| 424 |
-
with st.spinner("Analyzing the news article..."):
|
| 425 |
result = predict_news(news_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 426 |
col1, col2 = st.columns([1, 1], gap="large")
|
| 427 |
|
| 428 |
with col1:
|
| 429 |
-
st.markdown("### Prediction")
|
| 430 |
if result['label'] == 'FAKE':
|
| 431 |
-
st.markdown(f'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 432 |
else:
|
| 433 |
-
st.markdown(f'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 434 |
|
| 435 |
with col2:
|
| 436 |
-
st.markdown("### Confidence
|
| 437 |
st.plotly_chart(plot_confidence(result['probabilities']), use_container_width=True)
|
| 438 |
|
| 439 |
-
st.markdown("### Attention Analysis")
|
| 440 |
st.markdown("""
|
| 441 |
-
<p style="color: #
|
| 442 |
-
The
|
|
|
|
| 443 |
</p>
|
| 444 |
""", unsafe_allow_html=True)
|
| 445 |
st.plotly_chart(plot_attention(news_text, result['attention_weights']), use_container_width=True)
|
| 446 |
|
| 447 |
-
st.markdown("###
|
| 448 |
if result['label'] == 'FAKE':
|
| 449 |
st.markdown("""
|
| 450 |
-
<div
|
| 451 |
-
<
|
| 452 |
-
<ul>
|
| 453 |
-
<li>Linguistic patterns
|
| 454 |
-
<li>Inconsistencies
|
| 455 |
-
<li>Attention weights on suspicious phrases</li>
|
|
|
|
| 456 |
</ul>
|
|
|
|
|
|
|
|
|
|
| 457 |
</div>
|
| 458 |
""", unsafe_allow_html=True)
|
| 459 |
else:
|
| 460 |
st.markdown("""
|
| 461 |
-
<div
|
| 462 |
-
<
|
| 463 |
-
<ul>
|
| 464 |
-
<li>Credible
|
| 465 |
-
<li>
|
| 466 |
-
<li>Attention
|
|
|
|
| 467 |
</ul>
|
|
|
|
|
|
|
|
|
|
| 468 |
</div>
|
| 469 |
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
| 470 |
else:
|
| 471 |
-
st.markdown('
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 472 |
|
| 473 |
if __name__ == "__main__":
|
| 474 |
main()
|
|
|
|
| 240 |
# if __name__ == "__main__":
|
| 241 |
# main()
|
| 242 |
|
|
|
|
| 243 |
import streamlit as st
|
| 244 |
import torch
|
| 245 |
import pandas as pd
|
|
|
|
| 277 |
from src.config.config import *
|
| 278 |
from src.data.preprocessor import TextPreprocessor
|
| 279 |
|
| 280 |
+
# Set page config
|
| 281 |
+
st.set_page_config(
|
| 282 |
+
page_title="TrueCheck - AI Fake News Detector",
|
| 283 |
+
page_icon="π",
|
| 284 |
+
layout="wide",
|
| 285 |
+
initial_sidebar_state="collapsed"
|
| 286 |
+
)
|
| 287 |
+
|
| 288 |
+
# Custom CSS for modern styling
|
| 289 |
+
st.markdown("""
|
| 290 |
+
<style>
|
| 291 |
+
/* Import Google Fonts */
|
| 292 |
+
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');
|
| 293 |
+
|
| 294 |
+
/* Global Styles */
|
| 295 |
+
.main {
|
| 296 |
+
padding: 0;
|
| 297 |
+
}
|
| 298 |
+
|
| 299 |
+
.stApp {
|
| 300 |
+
font-family: 'Inter', sans-serif;
|
| 301 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 302 |
+
min-height: 100vh;
|
| 303 |
+
}
|
| 304 |
+
|
| 305 |
+
/* Hide Streamlit elements */
|
| 306 |
+
#MainMenu {visibility: hidden;}
|
| 307 |
+
footer {visibility: hidden;}
|
| 308 |
+
.stDeployButton {display: none;}
|
| 309 |
+
header {visibility: hidden;}
|
| 310 |
+
|
| 311 |
+
/* Hero Section */
|
| 312 |
+
.hero-container {
|
| 313 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 314 |
+
padding: 4rem 2rem;
|
| 315 |
+
text-align: center;
|
| 316 |
+
color: white;
|
| 317 |
+
margin-bottom: 2rem;
|
| 318 |
+
}
|
| 319 |
+
|
| 320 |
+
.hero-title {
|
| 321 |
+
font-size: 4rem;
|
| 322 |
+
font-weight: 700;
|
| 323 |
+
margin-bottom: 1rem;
|
| 324 |
+
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
|
| 325 |
+
background: linear-gradient(45deg, #fff, #e0e7ff);
|
| 326 |
+
-webkit-background-clip: text;
|
| 327 |
+
-webkit-text-fill-color: transparent;
|
| 328 |
+
background-clip: text;
|
| 329 |
+
}
|
| 330 |
+
|
| 331 |
+
.hero-subtitle {
|
| 332 |
+
font-size: 1.3rem;
|
| 333 |
+
font-weight: 400;
|
| 334 |
+
margin-bottom: 2rem;
|
| 335 |
+
opacity: 0.9;
|
| 336 |
+
max-width: 600px;
|
| 337 |
+
margin-left: auto;
|
| 338 |
+
margin-right: auto;
|
| 339 |
+
line-height: 1.6;
|
| 340 |
+
}
|
| 341 |
+
|
| 342 |
+
/* Features Section */
|
| 343 |
+
.features-container {
|
| 344 |
+
background: white;
|
| 345 |
+
padding: 3rem 2rem;
|
| 346 |
+
margin: 2rem 0;
|
| 347 |
+
border-radius: 20px;
|
| 348 |
+
box-shadow: 0 20px 40px rgba(0,0,0,0.1);
|
| 349 |
+
}
|
| 350 |
+
|
| 351 |
+
.features-grid {
|
| 352 |
+
display: grid;
|
| 353 |
+
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
|
| 354 |
+
gap: 2rem;
|
| 355 |
+
margin-top: 2rem;
|
| 356 |
+
}
|
| 357 |
+
|
| 358 |
+
.feature-card {
|
| 359 |
+
background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
|
| 360 |
+
padding: 2rem;
|
| 361 |
+
border-radius: 16px;
|
| 362 |
+
text-align: center;
|
| 363 |
+
transition: transform 0.3s ease, box-shadow 0.3s ease;
|
| 364 |
+
border: 1px solid #e2e8f0;
|
| 365 |
+
}
|
| 366 |
+
|
| 367 |
+
.feature-card:hover {
|
| 368 |
+
transform: translateY(-10px);
|
| 369 |
+
box-shadow: 0 20px 40px rgba(0,0,0,0.15);
|
| 370 |
+
}
|
| 371 |
+
|
| 372 |
+
.feature-icon {
|
| 373 |
+
font-size: 3rem;
|
| 374 |
+
margin-bottom: 1rem;
|
| 375 |
+
display: block;
|
| 376 |
+
}
|
| 377 |
+
|
| 378 |
+
.feature-title {
|
| 379 |
+
font-size: 1.2rem;
|
| 380 |
+
font-weight: 600;
|
| 381 |
+
color: #1e293b;
|
| 382 |
+
margin-bottom: 0.5rem;
|
| 383 |
+
}
|
| 384 |
+
|
| 385 |
+
.feature-description {
|
| 386 |
+
color: #64748b;
|
| 387 |
+
line-height: 1.5;
|
| 388 |
+
font-size: 0.95rem;
|
| 389 |
+
}
|
| 390 |
+
|
| 391 |
+
/* Main Content Section */
|
| 392 |
+
.main-content {
|
| 393 |
+
background: white;
|
| 394 |
+
padding: 3rem;
|
| 395 |
+
border-radius: 20px;
|
| 396 |
+
box-shadow: 0 20px 40px rgba(0,0,0,0.1);
|
| 397 |
+
margin: 2rem 0;
|
| 398 |
+
}
|
| 399 |
+
|
| 400 |
+
.section-title {
|
| 401 |
+
font-size: 2.5rem;
|
| 402 |
+
font-weight: 700;
|
| 403 |
+
text-align: center;
|
| 404 |
+
color: #1e293b;
|
| 405 |
+
margin-bottom: 1rem;
|
| 406 |
+
}
|
| 407 |
+
|
| 408 |
+
.section-description {
|
| 409 |
+
text-align: center;
|
| 410 |
+
color: #64748b;
|
| 411 |
+
font-size: 1.1rem;
|
| 412 |
+
margin-bottom: 2rem;
|
| 413 |
+
max-width: 600px;
|
| 414 |
+
margin-left: auto;
|
| 415 |
+
margin-right: auto;
|
| 416 |
+
line-height: 1.6;
|
| 417 |
+
}
|
| 418 |
+
|
| 419 |
+
/* Input Section */
|
| 420 |
+
.stTextArea > div > div > textarea {
|
| 421 |
+
border-radius: 12px;
|
| 422 |
+
border: 2px solid #e2e8f0;
|
| 423 |
+
padding: 1rem;
|
| 424 |
+
font-size: 1rem;
|
| 425 |
+
transition: border-color 0.3s ease;
|
| 426 |
+
font-family: 'Inter', sans-serif;
|
| 427 |
+
}
|
| 428 |
+
|
| 429 |
+
.stTextArea > div > div > textarea:focus {
|
| 430 |
+
border-color: #667eea;
|
| 431 |
+
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
|
| 432 |
+
}
|
| 433 |
+
|
| 434 |
+
/* Button Styling */
|
| 435 |
+
.stButton > button {
|
| 436 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 437 |
+
color: white;
|
| 438 |
+
border: none;
|
| 439 |
+
border-radius: 12px;
|
| 440 |
+
padding: 0.75rem 2rem;
|
| 441 |
+
font-size: 1.1rem;
|
| 442 |
+
font-weight: 600;
|
| 443 |
+
font-family: 'Inter', sans-serif;
|
| 444 |
+
transition: all 0.3s ease;
|
| 445 |
+
box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
|
| 446 |
+
width: 100%;
|
| 447 |
+
}
|
| 448 |
+
|
| 449 |
+
.stButton > button:hover {
|
| 450 |
+
transform: translateY(-2px);
|
| 451 |
+
box-shadow: 0 8px 25px rgba(102, 126, 234, 0.6);
|
| 452 |
+
}
|
| 453 |
+
|
| 454 |
+
/* Results Section */
|
| 455 |
+
.result-card {
|
| 456 |
+
background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
|
| 457 |
+
padding: 2rem;
|
| 458 |
+
border-radius: 16px;
|
| 459 |
+
margin: 1rem 0;
|
| 460 |
+
box-shadow: 0 4px 15px rgba(0,0,0,0.1);
|
| 461 |
+
}
|
| 462 |
+
|
| 463 |
+
.success-message {
|
| 464 |
+
background: linear-gradient(135deg, #dcfce7 0%, #bbf7d0 100%);
|
| 465 |
+
color: #166534;
|
| 466 |
+
padding: 1rem 1.5rem;
|
| 467 |
+
border-radius: 12px;
|
| 468 |
+
border-left: 4px solid #22c55e;
|
| 469 |
+
font-weight: 500;
|
| 470 |
+
margin: 1rem 0;
|
| 471 |
+
}
|
| 472 |
+
|
| 473 |
+
.error-message {
|
| 474 |
+
background: linear-gradient(135deg, #fef2f2 0%, #fecaca 100%);
|
| 475 |
+
color: #991b1b;
|
| 476 |
+
padding: 1rem 1.5rem;
|
| 477 |
+
border-radius: 12px;
|
| 478 |
+
border-left: 4px solid #ef4444;
|
| 479 |
+
font-weight: 500;
|
| 480 |
+
margin: 1rem 0;
|
| 481 |
+
}
|
| 482 |
+
|
| 483 |
+
/* Footer */
|
| 484 |
+
.footer {
|
| 485 |
+
background: linear-gradient(135deg, #1e293b 0%, #334155 100%);
|
| 486 |
+
color: white;
|
| 487 |
+
padding: 3rem 2rem 2rem;
|
| 488 |
+
text-align: center;
|
| 489 |
+
margin-top: 4rem;
|
| 490 |
+
}
|
| 491 |
+
|
| 492 |
+
.footer-content {
|
| 493 |
+
max-width: 1200px;
|
| 494 |
+
margin: 0 auto;
|
| 495 |
+
}
|
| 496 |
+
|
| 497 |
+
.footer-title {
|
| 498 |
+
font-size: 1.5rem;
|
| 499 |
+
font-weight: 600;
|
| 500 |
+
margin-bottom: 1rem;
|
| 501 |
+
}
|
| 502 |
+
|
| 503 |
+
.footer-text {
|
| 504 |
+
color: #94a3b8;
|
| 505 |
+
margin-bottom: 2rem;
|
| 506 |
+
line-height: 1.6;
|
| 507 |
+
}
|
| 508 |
+
|
| 509 |
+
.footer-links {
|
| 510 |
+
display: flex;
|
| 511 |
+
justify-content: center;
|
| 512 |
+
gap: 2rem;
|
| 513 |
+
margin-bottom: 2rem;
|
| 514 |
+
}
|
| 515 |
+
|
| 516 |
+
.footer-link {
|
| 517 |
+
color: #94a3b8;
|
| 518 |
+
text-decoration: none;
|
| 519 |
+
transition: color 0.3s ease;
|
| 520 |
+
}
|
| 521 |
+
|
| 522 |
+
.footer-link:hover {
|
| 523 |
+
color: white;
|
| 524 |
+
}
|
| 525 |
+
|
| 526 |
+
.footer-bottom {
|
| 527 |
+
border-top: 1px solid #475569;
|
| 528 |
+
padding-top: 2rem;
|
| 529 |
+
color: #94a3b8;
|
| 530 |
+
font-size: 0.9rem;
|
| 531 |
+
}
|
| 532 |
+
|
| 533 |
+
/* Responsive Design */
|
| 534 |
+
@media (max-width: 768px) {
|
| 535 |
+
.hero-title {
|
| 536 |
+
font-size: 3rem;
|
| 537 |
+
}
|
| 538 |
+
|
| 539 |
+
.features-grid {
|
| 540 |
+
grid-template-columns: 1fr;
|
| 541 |
+
}
|
| 542 |
+
|
| 543 |
+
.main-content {
|
| 544 |
+
padding: 2rem;
|
| 545 |
+
}
|
| 546 |
+
|
| 547 |
+
.footer-links {
|
| 548 |
+
flex-direction: column;
|
| 549 |
+
gap: 1rem;
|
| 550 |
+
}
|
| 551 |
+
}
|
| 552 |
+
</style>
|
| 553 |
+
""", unsafe_allow_html=True)
|
| 554 |
+
|
| 555 |
@st.cache_resource
|
| 556 |
def load_model_and_tokenizer():
|
| 557 |
"""Load the model and tokenizer (cached)."""
|
|
|
|
| 616 |
y=list(probabilities.values()),
|
| 617 |
text=[f'{p:.2%}' for p in probabilities.values()],
|
| 618 |
textposition='auto',
|
| 619 |
+
marker_color=['#22c55e', '#ef4444'],
|
| 620 |
+
marker_line_color='rgba(0,0,0,0.1)',
|
| 621 |
+
marker_line_width=1
|
| 622 |
)
|
| 623 |
])
|
| 624 |
fig.update_layout(
|
| 625 |
+
title={
|
| 626 |
+
'text': 'Prediction Confidence',
|
| 627 |
+
'x': 0.5,
|
| 628 |
+
'xanchor': 'center',
|
| 629 |
+
'font': {'size': 18, 'family': 'Inter'}
|
| 630 |
+
},
|
| 631 |
xaxis_title='Class',
|
| 632 |
yaxis_title='Probability',
|
| 633 |
yaxis_range=[0, 1],
|
| 634 |
+
template='plotly_white',
|
| 635 |
+
plot_bgcolor='rgba(0,0,0,0)',
|
| 636 |
+
paper_bgcolor='rgba(0,0,0,0)',
|
| 637 |
+
font={'family': 'Inter'}
|
| 638 |
)
|
| 639 |
return fig
|
| 640 |
|
|
|
|
| 645 |
if isinstance(attention_weights, (list, np.ndarray)):
|
| 646 |
attention_weights = np.array(attention_weights).flatten()
|
| 647 |
formatted_weights = [f'{float(w):.2f}' for w in attention_weights]
|
| 648 |
+
|
| 649 |
+
# Create color scale based on attention weights
|
| 650 |
+
colors = ['rgba(102, 126, 234, ' + str(0.3 + 0.7 * (w / max(attention_weights))) + ')'
|
| 651 |
+
for w in attention_weights]
|
| 652 |
+
|
| 653 |
fig = go.Figure(data=[
|
| 654 |
go.Bar(
|
| 655 |
x=tokens,
|
| 656 |
y=attention_weights,
|
| 657 |
text=formatted_weights,
|
| 658 |
textposition='auto',
|
| 659 |
+
marker_color=colors,
|
| 660 |
+
marker_line_color='rgba(102, 126, 234, 0.8)',
|
| 661 |
+
marker_line_width=1
|
| 662 |
)
|
| 663 |
])
|
| 664 |
fig.update_layout(
|
| 665 |
+
title={
|
| 666 |
+
'text': 'Attention Weights Analysis',
|
| 667 |
+
'x': 0.5,
|
| 668 |
+
'xanchor': 'center',
|
| 669 |
+
'font': {'size': 18, 'family': 'Inter'}
|
| 670 |
+
},
|
| 671 |
xaxis_title='Tokens',
|
| 672 |
yaxis_title='Attention Weight',
|
| 673 |
xaxis_tickangle=45,
|
| 674 |
+
template='plotly_white',
|
| 675 |
+
plot_bgcolor='rgba(0,0,0,0)',
|
| 676 |
+
paper_bgcolor='rgba(0,0,0,0)',
|
| 677 |
+
font={'family': 'Inter'}
|
| 678 |
)
|
| 679 |
return fig
|
| 680 |
|
| 681 |
def main():
|
| 682 |
+
# Hero Section
|
| 683 |
st.markdown("""
|
| 684 |
+
<div class="hero-container">
|
| 685 |
+
<h1 class="hero-title">π TrueCheck</h1>
|
| 686 |
+
<p class="hero-subtitle">
|
| 687 |
+
Advanced AI-powered fake news detection using cutting-edge deep learning technology.
|
| 688 |
+
Get instant, accurate analysis of news articles with our hybrid BERT-BiLSTM model.
|
| 689 |
+
</p>
|
| 690 |
+
</div>
|
| 691 |
+
""", unsafe_allow_html=True)
|
| 692 |
+
|
| 693 |
+
# Features Section
|
| 694 |
+
st.markdown("""
|
| 695 |
+
<div class="features-container">
|
| 696 |
+
<h2 style="text-align: center; font-size: 2rem; font-weight: 700; color: #1e293b; margin-bottom: 1rem;">
|
| 697 |
+
Why Choose TrueCheck?
|
| 698 |
+
</h2>
|
| 699 |
+
<p style="text-align: center; color: #64748b; font-size: 1.1rem; margin-bottom: 2rem;">
|
| 700 |
+
Our advanced AI model combines multiple technologies for superior accuracy
|
| 701 |
+
</p>
|
| 702 |
+
<div class="features-grid">
|
| 703 |
+
<div class="feature-card">
|
| 704 |
+
<span class="feature-icon">π€</span>
|
| 705 |
+
<h3 class="feature-title">BERT Technology</h3>
|
| 706 |
+
<p class="feature-description">
|
| 707 |
+
Utilizes state-of-the-art BERT transformer for deep contextual understanding of news content
|
| 708 |
+
</p>
|
| 709 |
+
</div>
|
| 710 |
+
<div class="feature-card">
|
| 711 |
+
<span class="feature-icon">π§ </span>
|
| 712 |
+
<h3 class="feature-title">BiLSTM Processing</h3>
|
| 713 |
+
<p class="feature-description">
|
| 714 |
+
Bidirectional LSTM networks capture sequential patterns and dependencies in text structure
|
| 715 |
</p>
|
| 716 |
</div>
|
| 717 |
+
<div class="feature-card">
|
| 718 |
+
<span class="feature-icon">ποΈ</span>
|
| 719 |
+
<h3 class="feature-title">Attention Mechanism</h3>
|
| 720 |
+
<p class="feature-description">
|
| 721 |
+
Advanced attention layers provide interpretable insights into model decision-making process
|
| 722 |
+
</p>
|
| 723 |
</div>
|
| 724 |
</div>
|
| 725 |
</div>
|
| 726 |
""", unsafe_allow_html=True)
|
| 727 |
|
| 728 |
+
# Main Content Section
|
| 729 |
+
st.markdown("""
|
| 730 |
+
<div class="main-content">
|
| 731 |
+
<h2 class="section-title">Analyze News Article</h2>
|
| 732 |
+
<p class="section-description">
|
| 733 |
+
Paste any news article below and our AI will analyze it for authenticity.
|
| 734 |
+
Get detailed insights including confidence scores and attention analysis.
|
| 735 |
+
</p>
|
|
|
|
|
|
|
|
|
|
| 736 |
</div>
|
| 737 |
""", unsafe_allow_html=True)
|
| 738 |
|
| 739 |
+
# Input Section
|
| 740 |
+
col1, col2, col3 = st.columns([1, 3, 1])
|
| 741 |
+
with col2:
|
| 742 |
+
news_text = st.text_area(
|
| 743 |
+
"",
|
| 744 |
+
height=200,
|
| 745 |
+
placeholder="π° Paste your news article here for analysis...",
|
| 746 |
+
key="news_input"
|
| 747 |
+
)
|
| 748 |
+
|
| 749 |
+
analyze_button = st.button("π Analyze Article", key="analyze_button")
|
| 750 |
+
|
| 751 |
+
if analyze_button:
|
| 752 |
if news_text:
|
| 753 |
+
with st.spinner("π€ Analyzing the news article..."):
|
| 754 |
result = predict_news(news_text)
|
| 755 |
+
|
| 756 |
+
# Results Section
|
| 757 |
+
st.markdown('<div class="main-content">', unsafe_allow_html=True)
|
| 758 |
+
|
| 759 |
col1, col2 = st.columns([1, 1], gap="large")
|
| 760 |
|
| 761 |
with col1:
|
| 762 |
+
st.markdown("### π Prediction Result")
|
| 763 |
if result['label'] == 'FAKE':
|
| 764 |
+
st.markdown(f'''
|
| 765 |
+
<div class="error-message">
|
| 766 |
+
π΄ <strong>FAKE NEWS DETECTED</strong><br>
|
| 767 |
+
Confidence: {result["confidence"]:.2%}
|
| 768 |
+
</div>
|
| 769 |
+
''', unsafe_allow_html=True)
|
| 770 |
else:
|
| 771 |
+
st.markdown(f'''
|
| 772 |
+
<div class="success-message">
|
| 773 |
+
π’ <strong>AUTHENTIC NEWS</strong><br>
|
| 774 |
+
Confidence: {result["confidence"]:.2%}
|
| 775 |
+
</div>
|
| 776 |
+
''', unsafe_allow_html=True)
|
| 777 |
|
| 778 |
with col2:
|
| 779 |
+
st.markdown("### π Confidence Breakdown")
|
| 780 |
st.plotly_chart(plot_confidence(result['probabilities']), use_container_width=True)
|
| 781 |
|
| 782 |
+
st.markdown("### π― Attention Analysis")
|
| 783 |
st.markdown("""
|
| 784 |
+
<p style="color: #64748b; text-align: center; margin-bottom: 2rem;">
|
| 785 |
+
The visualization below shows which words our AI model focused on while making its prediction.
|
| 786 |
+
Darker colors indicate higher attention weights.
|
| 787 |
</p>
|
| 788 |
""", unsafe_allow_html=True)
|
| 789 |
st.plotly_chart(plot_attention(news_text, result['attention_weights']), use_container_width=True)
|
| 790 |
|
| 791 |
+
st.markdown("### π Detailed Analysis")
|
| 792 |
if result['label'] == 'FAKE':
|
| 793 |
st.markdown("""
|
| 794 |
+
<div class="result-card">
|
| 795 |
+
<h4 style="color: #ef4444; margin-bottom: 1rem;">β οΈ Fake News Indicators</h4>
|
| 796 |
+
<ul style="color: #64748b; line-height: 1.8;">
|
| 797 |
+
<li><strong>Linguistic Patterns:</strong> The model detected language patterns commonly associated with misinformation</li>
|
| 798 |
+
<li><strong>Content Inconsistencies:</strong> Identified potential factual inconsistencies or misleading statements</li>
|
| 799 |
+
<li><strong>Attention Analysis:</strong> High attention weights on suspicious phrases and emotionally charged language</li>
|
| 800 |
+
<li><strong>Structural Analysis:</strong> Text structure and flow patterns typical of fabricated content</li>
|
| 801 |
</ul>
|
| 802 |
+
<p style="color: #7c3aed; font-weight: 500; margin-top: 1rem;">
|
| 803 |
+
π‘ <strong>Recommendation:</strong> Verify this information through multiple reliable sources before sharing.
|
| 804 |
+
</p>
|
| 805 |
</div>
|
| 806 |
""", unsafe_allow_html=True)
|
| 807 |
else:
|
| 808 |
st.markdown("""
|
| 809 |
+
<div class="result-card">
|
| 810 |
+
<h4 style="color: #22c55e; margin-bottom: 1rem;">β
Authentic News Indicators</h4>
|
| 811 |
+
<ul style="color: #64748b; line-height: 1.8;">
|
| 812 |
+
<li><strong>Credible Language:</strong> Professional journalistic writing style and balanced reporting tone</li>
|
| 813 |
+
<li><strong>Factual Consistency:</strong> Information appears coherent and factually consistent</li>
|
| 814 |
+
<li><strong>Attention Analysis:</strong> Model focused on factual statements and objective reporting</li>
|
| 815 |
+
<li><strong>Structural Integrity:</strong> Well-structured content following standard news article format</li>
|
| 816 |
</ul>
|
| 817 |
+
<p style="color: #7c3aed; font-weight: 500; margin-top: 1rem;">
|
| 818 |
+
π‘ <strong>Note:</strong> While likely authentic, always cross-reference important news from multiple sources.
|
| 819 |
+
</p>
|
| 820 |
</div>
|
| 821 |
""", unsafe_allow_html=True)
|
| 822 |
+
|
| 823 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 824 |
else:
|
| 825 |
+
st.markdown('''
|
| 826 |
+
<div class="main-content">
|
| 827 |
+
<div class="error-message" style="text-align: center;">
|
| 828 |
+
β οΈ Please enter a news article to analyze
|
| 829 |
+
</div>
|
| 830 |
+
</div>
|
| 831 |
+
''', unsafe_allow_html=True)
|
| 832 |
+
|
| 833 |
+
# Footer
|
| 834 |
+
st.markdown("""
|
| 835 |
+
<div class="footer">
|
| 836 |
+
<div class="footer-content">
|
| 837 |
+
<h3 class="footer-title">TrueCheck AI</h3>
|
| 838 |
+
<p class="footer-text">
|
| 839 |
+
Empowering users with AI-driven news verification technology.
|
| 840 |
+
Built with advanced deep learning models for accurate fake news detection.
|
| 841 |
+
</p>
|
| 842 |
+
<div class="footer-links">
|
| 843 |
+
<a href="#" class="footer-link">About</a>
|
| 844 |
+
<a href="#" class="footer-link">How It Works</a>
|
| 845 |
+
<a href="#" class="footer-link">Privacy Policy</a>
|
| 846 |
+
<a href="#" class="footer-link">Contact</a>
|
| 847 |
+
</div>
|
| 848 |
+
<div class="footer-bottom">
|
| 849 |
+
<p>© 2025 TrueCheck AI. Built with β€οΈ using Streamlit, BERT, and PyTorch.</p>
|
| 850 |
+
<p>Disclaimer: This tool provides AI-based analysis. Always verify important information through multiple sources.</p>
|
| 851 |
+
</div>
|
| 852 |
+
</div>
|
| 853 |
+
</div>
|
| 854 |
+
""", unsafe_allow_html=True)
|
| 855 |
|
| 856 |
if __name__ == "__main__":
|
| 857 |
main()
|