sofzcc commited on
Commit
d4d4d4d
·
verified ·
1 Parent(s): 9ebf913

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -3
app.py CHANGED
@@ -34,17 +34,77 @@ def predict_news(news_text):
34
  predictions = torch.argmax(logits, dim=-1).item()
35
  return "Real" if predictions == 1 else "Fake"
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  # Streamlit App
38
  st.title("Fake News Detector")
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  st.write("Enter a news article URL below to check if it's real or fake:")
41
 
42
  news_url = st.text_area("News URL", height=100)
43
 
44
  if st.button("Evaluate"):
45
  if news_url:
46
- news_text = extract_news_text(news_url)
47
- prediction = predict_news(news_text)
48
- st.write(f"The news article is predicted to be: **{prediction}**")
 
 
 
 
 
 
 
49
  else:
50
  st.write("Please enter some news URL to evaluate.")
 
34
  predictions = torch.argmax(logits, dim=-1).item()
35
  return "Real" if predictions == 1 else "Fake"
36
 
37
+ # Check if a GPU is available and move the model to the appropriate device
38
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
39
+ model.to(device)
40
+
41
+ # Define text classification function
42
+ def classify_text(text):
43
+ # Tokenize the input text
44
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
45
+ inputs = {key: value.to(device) for key, value in inputs.items()}
46
+
47
+ # Ensure the model is in evaluation mode
48
+ model.eval()
49
+
50
+ # Perform the forward pass
51
+ with torch.no_grad():
52
+ outputs = model(**inputs)
53
+
54
+ logits = outputs.logits
55
+
56
+ # Convert logits to probabilities
57
+ probabilities = torch.nn.functional.softmax(logits, dim=-1)
58
+
59
+ # Get the predicted class
60
+ predicted_class = torch.argmax(probabilities, dim=1).item()
61
+
62
+ # Define the label mapping
63
+ labels = ['False', 'True']
64
+
65
+ # Return the predicted label and probabilities
66
+ predicted_label = labels[predicted_class]
67
+ probabilities = probabilities.cpu().numpy()
68
+
69
+ return predicted_label, probabilities
70
+
71
+
72
  # Streamlit App
73
  st.title("Fake News Detector")
74
 
75
+ # Add disclaimer
76
+ st.markdown("""
77
+ ### Disclaimer
78
+
79
+ **Important Notice:**
80
+
81
+ This model was trained exclusively on news articles from Reuters. As a result, the model may be biased towards considering news from Reuters as "True" and may not accurately classify news from other sources.
82
+
83
+ **Usage Warning:**
84
+
85
+ - This model is intended for experimental and educational purposes only.
86
+ - We do not take any responsibility for the outcomes or decisions made based on the results provided by this model.
87
+ - The model should not be used for any critical or real-world applications, especially those that involve significant consequences or decision-making.
88
+ - Users are encouraged to apply their own judgment and consult multiple sources when evaluating the credibility of news.
89
+
90
+ **By using this model, you acknowledge and accept these terms and disclaimers.**
91
+ """)
92
+
93
  st.write("Enter a news article URL below to check if it's real or fake:")
94
 
95
  news_url = st.text_area("News URL", height=100)
96
 
97
  if st.button("Evaluate"):
98
  if news_url:
99
+ try:
100
+ news_text = extract_news_text(news_url)
101
+ predicted_label, probabilities = classify_text(news_text)
102
+ st.write(f"The news article is predicted to be: **{predicted_label}**")
103
+ except:
104
+ st.write("It wasn't possible to fetch the article text. Enter the news article text below to check if it's real or fake:")
105
+ news_text = st.text_area("News text", height=300)
106
+ predicted_label, probabilities = classify_text(news_text)
107
+ st.write(f"The news article is predicted to be: **{predicted_label}**")
108
+
109
  else:
110
  st.write("Please enter some news URL to evaluate.")