Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- a.py +101 -0
- requirements.txt +6 -0
a.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 4 |
+
import requests
|
| 5 |
+
import json
|
| 6 |
+
from googletrans import Translator
|
| 7 |
+
|
| 8 |
+
st.set_page_config(page_title="News Prediction", page_icon=":earth_africa:")
|
| 9 |
+
|
| 10 |
+
# Load tokenizer and model
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained("hamzab/roberta-fake-news-classification")
|
| 12 |
+
model = AutoModelForSequenceClassification.from_pretrained("hamzab/roberta-fake-news-classification")
|
| 13 |
+
|
| 14 |
+
translator = Translator()
|
| 15 |
+
|
| 16 |
+
def translate_to_english(text):
|
| 17 |
+
try:
|
| 18 |
+
translated = translator.translate(text, dest='en')
|
| 19 |
+
return translated.text
|
| 20 |
+
except Exception as e:
|
| 21 |
+
return f"Error in translation: {e}"
|
| 22 |
+
|
| 23 |
+
def predict_fake(title, text):
|
| 24 |
+
input_str = "<title>" + title + "<content>" + text + "<end>"
|
| 25 |
+
input_ids = tokenizer.encode_plus(input_str, max_length=512, padding="max_length", truncation=True, return_tensors="pt")
|
| 26 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 27 |
+
model.to(device)
|
| 28 |
+
with torch.no_grad():
|
| 29 |
+
output = model(input_ids["input_ids"].to(device), attention_mask=input_ids["attention_mask"].to(device))
|
| 30 |
+
return dict(zip(["Fake", "Real"], [x.item() for x in list(torch.nn.Softmax()(output.logits)[0])]))
|
| 31 |
+
|
| 32 |
+
def fact_check_with_google(api_key, query):
|
| 33 |
+
url = f"https://factchecktools.googleapis.com/v1alpha1/claims:search"
|
| 34 |
+
params = {
|
| 35 |
+
"query": query,
|
| 36 |
+
"key": api_key
|
| 37 |
+
}
|
| 38 |
+
response = requests.get(url, params=params)
|
| 39 |
+
if response.status_code == 200:
|
| 40 |
+
return response.json()
|
| 41 |
+
else:
|
| 42 |
+
return {"error": f"Unable to fetch results from Google Fact Check API. HTTP {response.status_code}: {response.text}"}
|
| 43 |
+
|
| 44 |
+
def main():
|
| 45 |
+
st.title("Fake News Prediction")
|
| 46 |
+
|
| 47 |
+
# Load Google API key from a secure location or environment variable
|
| 48 |
+
try:
|
| 49 |
+
google_api_key = "AIzaSyAf5v5380xkpo0Rk3kBiSxpxYVBQwcDi2A"
|
| 50 |
+
except KeyError:
|
| 51 |
+
st.error("Google Fact Check API key is missing. Please add it to secrets.")
|
| 52 |
+
return
|
| 53 |
+
|
| 54 |
+
# Create the form for user input
|
| 55 |
+
with st.form("news_form"):
|
| 56 |
+
st.subheader("Enter News Details")
|
| 57 |
+
title = st.text_input("Title")
|
| 58 |
+
text = st.text_area("Text")
|
| 59 |
+
language = st.selectbox("Select Language", options=["English", "Other"])
|
| 60 |
+
submit_button = st.form_submit_button("Submit")
|
| 61 |
+
|
| 62 |
+
# Process form submission and make prediction
|
| 63 |
+
if submit_button:
|
| 64 |
+
if language == "Other":
|
| 65 |
+
title = translate_to_english(title)
|
| 66 |
+
text = translate_to_english(text)
|
| 67 |
+
|
| 68 |
+
prediction = predict_fake(title, text)
|
| 69 |
+
|
| 70 |
+
st.subheader("Prediction:")
|
| 71 |
+
st.write("Prediction: ", prediction)
|
| 72 |
+
|
| 73 |
+
if prediction.get("Real") > 0.5:
|
| 74 |
+
st.write("This news is predicted to be **real** :muscle:")
|
| 75 |
+
else:
|
| 76 |
+
st.write("This news is predicted to be **fake** :shit:")
|
| 77 |
+
|
| 78 |
+
# Fact-check the news using Google Fact Check API
|
| 79 |
+
st.subheader("Fact-Checking Results:")
|
| 80 |
+
query = title if title else text[:100] # Use title or first 100 chars of text as query
|
| 81 |
+
fact_check_results = fact_check_with_google(google_api_key, query)
|
| 82 |
+
|
| 83 |
+
if "error" in fact_check_results:
|
| 84 |
+
st.error(fact_check_results["error"])
|
| 85 |
+
else:
|
| 86 |
+
claims = fact_check_results.get("claims", [])
|
| 87 |
+
if claims:
|
| 88 |
+
for claim in claims:
|
| 89 |
+
st.write(f"**Claim:** {claim.get('text', 'N/A')}")
|
| 90 |
+
claim_review = claim.get("claimReview", [])
|
| 91 |
+
if claim_review:
|
| 92 |
+
for review in claim_review:
|
| 93 |
+
st.write(f"- **Publisher:** {review.get('publisher', {}).get('name', 'N/A')}")
|
| 94 |
+
st.write(f" **Rating:** {review.get('textualRating', 'N/A')}")
|
| 95 |
+
st.write(f" **URL:** [More Info]({review.get('url', '#')})")
|
| 96 |
+
else:
|
| 97 |
+
st.write("No fact-checking information found for this query.")
|
| 98 |
+
|
| 99 |
+
if __name__ == "__main__":
|
| 100 |
+
main()
|
| 101 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
transformers
|
| 3 |
+
gradio
|
| 4 |
+
googletrans==4.0.0-rc1
|
| 5 |
+
requests
|
| 6 |
+
|