Spaces:
Sleeping
Sleeping
File size: 5,219 Bytes
d98c82e 4edbcc8 d98c82e 4edbcc8 dad9867 861d961 639474b df52cfe 2ac129f 92faed7 d98c82e 4edbcc8 d98c82e be732bd 4edbcc8 d98c82e 3421951 d98c82e 4edbcc8 3421951 d98c82e 4edbcc8 d98c82e 4edbcc8 d98c82e 4edbcc8 d98c82e 4edbcc8 92faed7 5eea208 92faed7 4edbcc8 92faed7 4edbcc8 92faed7 4edbcc8 92faed7 4edbcc8 92faed7 4edbcc8 b192152 4edbcc8 dad9867 4edbcc8 92faed7 b192152 92faed7 4edbcc8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
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>" + title + "<content>" + text + "<end>"
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()
|