| import streamlit as st |
| import torch |
| From newspaper import Article |
|
|
| |
| from transformers import AutoTokenizer, AutoModelForSequenceClassification |
|
|
| tokenizer = AutoTokenizer.from_pretrained("sofzcc/distilbert-base-uncased-fake-news-checker") |
| model = AutoModelForSequenceClassification.from_pretrained("sofzcc/distilbert-base-uncased-fake-news-checker") |
|
|
| def newspaper_text_extraction(article_url): |
| article = Article(article_url) |
| article.download() |
| article.parse() |
| return article. title,article.text |
|
|
|
|
| |
| def predict_news(news_text): |
| inputs = tokenizer(news_text, return_tensors="pt", truncation=True, padding=True, max_length=512) |
| with torch.no_grad(): |
| outputs = model(**inputs) |
| logits = outputs.logits |
| predictions = torch.argmax(logits, dim=-1).item() |
| return "Real" if predictions == 1 else "Fake" |
|
|
| |
| st.title("Fake News Detector") |
|
|
| st.write("Enter a news article URL below to check if it's real or fake:") |
|
|
| news_url = st.text_area("News URL", height=100) |
|
|
| if st.button("Evaluate"): |
| if news_url: |
| news_text = newspaper_text_extraction(news_url) |
| prediction = predict_news(news_text) |
| st.write(f"The news article is predicted to be: **{prediction}**") |
| else: |
| st.write("Please enter some news URL to evaluate.") |
|
|