Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import DistilBertTokenizer, DistilBertForSequenceClassification
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Load pre-trained DistilBERT model and tokenizer
|
6 |
+
model_name = "distilbert-base-uncased"
|
7 |
+
tokenizer = DistilBertTokenizer.from_pretrained(model_name)
|
8 |
+
model = DistilBertForSequenceClassification.from_pretrained(model_name, num_labels=2)
|
9 |
+
|
10 |
+
# Function to predict if news is real or fake
|
11 |
+
def predict_news(news_text):
|
12 |
+
inputs = tokenizer(news_text, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
13 |
+
with torch.no_grad():
|
14 |
+
outputs = model(**inputs)
|
15 |
+
logits = outputs.logits
|
16 |
+
predictions = torch.argmax(logits, dim=-1).item()
|
17 |
+
return "Real" if predictions == 1 else "Fake"
|
18 |
+
|
19 |
+
# Streamlit App
|
20 |
+
st.title("Fake News Detector")
|
21 |
+
|
22 |
+
st.write("Enter a news article below to check if it's real or fake:")
|
23 |
+
|
24 |
+
news_text = st.text_area("News Article", height=300)
|
25 |
+
|
26 |
+
if st.button("Evaluate"):
|
27 |
+
if news_text:
|
28 |
+
prediction = predict_news(news_text)
|
29 |
+
st.write(f"The news article is predicted to be: **{prediction}**")
|
30 |
+
else:
|
31 |
+
st.write("Please enter some news text to evaluate.")
|
32 |
+
|