svm_model / gradio_app.py
TrBn17
app
e775b41
import gradio as gr
import warnings
from typing import Tuple
from config import MODEL_PATH, REAL_LABEL
import joblib
from helper import _combine
from schemas import PredictIn
# Suppress sklearn version warnings
warnings.filterwarnings("ignore", category=UserWarning, module="sklearn")
warnings.filterwarnings("ignore", message=".*InconsistentVersionWarning.*")
try:
from sklearn.exceptions import InconsistentVersionWarning
warnings.filterwarnings("ignore", category=InconsistentVersionWarning)
except ImportError:
pass
# Load model
print("Loading model from:", MODEL_PATH)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
PIPE = joblib.load(MODEL_PATH)
print("Model loaded successfully")
# Get class indices
try:
classes = list(PIPE.named_steps["clf"].classes_)
except Exception:
classes = list(getattr(PIPE, "classes_", [0, 1]))
print(f"Model classes: {classes}")
IDX_REAL = classes.index(REAL_LABEL)
IDX_FAKE = classes.index(0)
def predict_news(title: str, text: str) -> Tuple[str, float, float, str]:
"""
Predict if news is real or fake
Args:
title: News article title
text: News article content
Returns:
Tuple of (prediction, real_probability, fake_probability, confidence_level)
"""
# Combine title and text
text_all = _combine(title, text)
# Get prediction probabilities
with warnings.catch_warnings():
warnings.simplefilter("ignore")
probs = PIPE.predict_proba([text_all])[0]
prob_real = float(probs[IDX_REAL])
prob_fake = float(probs[IDX_FAKE])
# Determine prediction and confidence
if prob_real >= 0.5:
prediction = "REAL"
confidence = prob_real
else:
prediction = "FAKE"
confidence = prob_fake
# Determine confidence level
if confidence >= 0.8:
confidence_level = "High"
elif confidence >= 0.6:
confidence_level = "Medium"
else:
confidence_level = "Low"
return prediction, prob_real, prob_fake, confidence_level
# Example articles for demonstration
examples = [
[
"Scientists Discover Breakthrough in Cancer Treatment",
"Researchers at leading medical institutions have announced a significant breakthrough in cancer treatment methodology. The new approach shows promising results in early clinical trials, offering hope for millions of patients worldwide. The research, published in a peer-reviewed journal, demonstrates improved survival rates and reduced side effects compared to traditional treatments."
],
[
"SHOCKING: Aliens Found Living Among Us, Government Confirms",
"In a stunning revelation that changes everything we know about humanity, government officials have finally confirmed that extraterrestrial beings have been living among humans for decades. Sources close to the matter reveal that these aliens have been secretly controlling world governments and manipulating global events from the shadows."
],
[
"Local Community Garden Helps Reduce Food Insecurity",
"A grassroots initiative in downtown Springfield has transformed an abandoned lot into a thriving community garden that provides fresh produce to local food banks. The project, started by neighborhood volunteers, has grown to include educational programs and has become a model for similar initiatives in other cities."
]
]
# Create Gradio interface
with gr.Blocks(title="SVM Fake News Classifier", theme=gr.themes.Soft()) as demo:
gr.Markdown(
"""
# πŸ“° SVM Fake News Classifier
This application uses a Support Vector Machine (SVM) with TF-IDF features to classify news articles as **real** or **fake**.
The model has been trained on a large dataset and uses calibrated probabilities for more reliable predictions.
### How to use:
1. Enter a news article title
2. Enter the article content/text
3. Click "Classify News" to get the prediction
The model will return:
- **Prediction**: Whether the article is classified as REAL or FAKE
- **Probabilities**: Confidence scores for both real and fake classifications
- **Confidence Level**: Overall confidence in the prediction (High/Medium/Low)
"""
)
with gr.Row():
with gr.Column():
title_input = gr.Textbox(
label="πŸ“° News Title",
placeholder="Enter the news article title...",
lines=2
)
text_input = gr.Textbox(
label="πŸ“„ News Content",
placeholder="Enter the news article content...",
lines=8
)
classify_btn = gr.Button("πŸ” Classify News", variant="primary", size="lg")
with gr.Column():
with gr.Group():
prediction_output = gr.Textbox(
label="🎯 Prediction",
interactive=False
)
confidence_output = gr.Textbox(
label="πŸ“Š Confidence Level",
interactive=False
)
with gr.Row():
real_prob = gr.Number(
label="βœ… Real Probability",
interactive=False
)
fake_prob = gr.Number(
label="❌ Fake Probability",
interactive=False
)
# Examples section
gr.Markdown("### πŸ“š Try these examples:")
gr.Examples(
examples=examples,
inputs=[title_input, text_input],
label="Example Articles"
)
# Information section
with gr.Accordion("ℹ️ Model Information", open=False):
gr.Markdown(
"""
**Model Details:**
- **Algorithm**: Support Vector Machine (SVM) with TF-IDF vectorization
- **Calibration**: Uses CalibratedClassifierCV for probability estimates
- **Features**: Text preprocessing, TF-IDF feature extraction
- **Training**: Trained on labeled real/fake news dataset
**Interpretation:**
- **Real Probability > 0.5**: Article classified as REAL news
- **Fake Probability > 0.5**: Article classified as FAKE news
- **Confidence Level**: Based on the highest probability score
- High: β‰₯ 80% confidence
- Medium: 60-79% confidence
- Low: < 60% confidence
**Note**: This is a machine learning model and may not be 100% accurate.
Always verify important information through multiple reliable sources.
"""
)
# Set up the prediction function
classify_btn.click(
fn=predict_news,
inputs=[title_input, text_input],
outputs=[prediction_output, real_prob, fake_prob, confidence_output]
)
# Launch the app
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860)