import streamlit as st
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification,pipeline
import requests
import json
import os
GEMINI_API_KEY = "AIzaSyBO3-HG-WcITn58PdpK7mMyvFQitoH00qA" # Replace with your Gemini API key
GOOGLE_API_KEY = "AIzaSyAf5v5380xkpo0Rk3kBiSxpxYVBQwcDi2A"
st.set_page_config(page_title="News Prediction", page_icon=":earth_africa:")
# Load tokenizer and model
tokenizer = AutoTokenizer.from_pretrained("hamzab/roberta-fake-news-classification")
model = AutoModelForSequenceClassification.from_pretrained("hamzab/roberta-fake-news-classification")
from deep_translator import GoogleTranslator
def translate_to_english(text):
try:
return GoogleTranslator(source='auto', target='en').translate(text)
except Exception as e:
return f"Error in translation: {e}"
def predict_fake(title, text):
input_str = "
" + title + "" + text + ""
input_ids = tokenizer.encode_plus(input_str, max_length=512, padding="max_length", truncation=True, return_tensors="pt")
device = 'cuda' if torch.cuda.is_available() else 'cpu'
model.to(device)
with torch.no_grad():
output = model(input_ids["input_ids"].to(device), attention_mask=input_ids["attention_mask"].to(device))
return dict(zip(["Fake", "Real"], [x.item() for x in list(torch.nn.Softmax()(output.logits)[0])]))
def fact_check_with_google(api_key, query):
url = f"https://factchecktools.googleapis.com/v1alpha1/claims:search"
params = {
"query": query,
"key": api_key
}
response = requests.get(url, params=params)
if response.status_code == 200:
return response.json()
else:
return {"error": f"Unable to fetch results from Google Fact Check API. HTTP {response.status_code}: {response.text}"}
# Load summarizer
@st.cache_resource
def load_summarizer():
return pipeline("summarization", model="facebook/bart-large-cnn")
summarizer = load_summarizer()
import google.generativeai as genai
# Initialize Gemini with your API Key
def configure_gemini(api_key):
genai.configure(api_key=api_key)
model = genai.GenerativeModel('gemini-2.0-flash')
return model
# Function to extract fact-check query using Gemini
def generate_fact_check_query(model, title, text):
prompt = f"""
You are a helpful assistant that extracts the **core claim** from news articles to be used for fact checking.
Given the **title** and **content**, provide a **single-line, specific, fact-checkable statement** that can be searched in fact-checking databases like Google's Fact Check API.
Return only the optimized query.
---
Title: {title}
Content: {text}
"""
try:
response = model.generate_content(prompt)
return response.text.strip()
except Exception as e:
return f"Error generating query: {e}"
def main():
st.title("Fake News Prediction")
# Store your API key here or load from environment variable
# ๐ Replace this!
with st.form("news_form"):
st.subheader("Enter News Details")
title = st.text_input("Title")
text = st.text_area("Text")
language = st.selectbox("Select Language", options=["English", "Other"])
check_fact = st.checkbox("Also check with Google Fact Check API")
submit_button = st.form_submit_button("Submit")
if submit_button:
if language == "Other":
title = translate_to_english(title)
text = translate_to_english(text)
prediction = predict_fake(title, text)
st.subheader("Prediction:")
st.write("Prediction: ", prediction)
if prediction.get("Real") > 0.5:
st.write("This news is predicted to be **real** :muscle:")
else:
st.write("This news is predicted to be **fake** :shit:")
# Use Gemini to generate a better fact-check
if GEMINI_API_KEY:
gemini_model = configure_gemini(GEMINI_API_KEY)
# โ
Use Gemini to generate better fact-check query
fact_query = generate_fact_check_query(gemini_model, title, text)
st.markdown("#### ๐ Optimized Query for Fact Check")
st.write(fact_query)
if check_fact:
# โ
Perform fact check with optimized query
fact_check_data = fact_check_with_google(GOOGLE_API_KEY, fact_query)
st.subheader("๐งพ Google Fact Check Results")
if "claims" in fact_check_data:
for claim in fact_check_data["claims"]:
st.markdown(f"**Claim:** {claim.get('text', 'N/A')}")
for review in claim.get("claimReview", []):
st.write(f"- **Publisher**: {review.get('publisher', {}).get('name', 'N/A')}")
st.write(f"- **Rating**: {review.get('textualRating', 'N/A')}")
st.write(f"- **URL**: {review.get('url', 'N/A')}")
st.write("---")
else:
st.write("No fact-check results found. Try modifying the title or content.")
if __name__ == "__main__":
main()