|
|
|
"""Fake News Generator & Detector.ipynb |
|
|
|
Automatically generated by Colab. |
|
|
|
Original file is located at |
|
https://colab.research.google.com/drive/1k_GVU6WO9Mggr87RxntJFq2S1M6R4dI0 |
|
|
|
# π° **Fake News Generator & Detector** |
|
|
|
This educational and awareness-focused Generative AI application explores the dual power of AI in creating and detecting fake news. It allows users to generate fake news headlines based on a selected category (e.g., politics, tech), a subject or entity (e.g., Apple, Government), and number of headlines. It also provides a detection system where users can input a news snippet to check whether it is likely to be fake or real using a fine-tuned BERT model. |
|
|
|
**The application is built using:** |
|
|
|
Transformers library |
|
(For GPT-2 based fake news generation and BERT-based fake news classification) |
|
|
|
Gradio |
|
(To create a clean, tabbed web interface for both generation and detection) |
|
|
|
Google Colab / Python |
|
(For backend development, model training/fine-tuning, and deployment) |
|
|
|
**This project aims to:** |
|
|
|
β¨ Demonstrate the capabilities and risks of large language models |
|
|
|
π Raise awareness about misinformation in media |
|
|
|
π§ Promote responsible AI development through practical exploration |
|
|
|
This project showcases how AI can both contribute to and combat misinformation, serving as a tool for learning, experimentation, and ethics in AI.""" |
|
|
|
|
|
|
|
import torch |
|
import torch.nn.functional as F |
|
from transformers import ( |
|
GPT2LMHeadModel, |
|
GPT2Tokenizer, |
|
AutoTokenizer, |
|
AutoModelForSequenceClassification |
|
) |
|
import gradio as gr |
|
|
|
|
|
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
|
|
|
|
|
gpt2_tokenizer = GPT2Tokenizer.from_pretrained("gpt2") |
|
gpt2_model = GPT2LMHeadModel.from_pretrained("gpt2").to(device) |
|
gpt2_model.eval() |
|
|
|
|
|
def generate_fake_headlines(category, subject, num_headlines=1, max_length=20): |
|
headlines = [] |
|
for i in range(num_headlines): |
|
prompt = f"Write a fake {category} news headline about {subject}:\n" |
|
input_ids = gpt2_tokenizer.encode(prompt, return_tensors='pt').to(device) |
|
|
|
output = gpt2_model.generate( |
|
input_ids, |
|
max_length=len(input_ids[0]) + max_length, |
|
temperature=0.9, |
|
top_p=0.9, |
|
do_sample=True, |
|
pad_token_id=gpt2_tokenizer.eos_token_id, |
|
no_repeat_ngram_size=2, |
|
early_stopping=True |
|
) |
|
|
|
generated_text = gpt2_tokenizer.decode(output[0], skip_special_tokens=True) |
|
headline = generated_text.replace(prompt, "").strip().split("\n")[0] |
|
headlines.append(headline) |
|
return headlines |
|
|
|
|
|
def gradio_fake_news_generator(category, subject, num_headlines): |
|
try: |
|
num = int(num_headlines) |
|
if num <= 0 or num > 10: |
|
return "Please enter a number between 1 and 10." |
|
except: |
|
return "Invalid input for number of headlines." |
|
|
|
headlines = generate_fake_headlines(category, subject, num) |
|
return "\n".join([f"{i+1}. {h}" for i, h in enumerate(headlines)]) |
|
|
|
|
|
bert_tokenizer = AutoTokenizer.from_pretrained("Pulk17/Fake-News-Detection") |
|
bert_model = AutoModelForSequenceClassification.from_pretrained("Pulk17/Fake-News-Detection").to(device) |
|
bert_model.eval() |
|
|
|
|
|
def detect_fake_news(text): |
|
inputs = bert_tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512).to(device) |
|
outputs = bert_model(**inputs) |
|
probs = F.softmax(outputs.logits, dim=1) |
|
predicted_class = torch.argmax(probs, dim=1).item() |
|
confidence = torch.max(probs).item() |
|
label = "FAKE π₯" if predicted_class == 0 else "REAL π©" |
|
return f"Prediction: {label} ({confidence*100:.2f}% confidence)" |
|
|
|
|
|
generator_interface = gr.Interface( |
|
fn=gradio_fake_news_generator, |
|
inputs=[ |
|
gr.Textbox(label="News Category (e.g., politics, tech)"), |
|
gr.Textbox(label="Main Subject / Entity (e.g., Apple, Government)"), |
|
gr.Textbox(label="Number of Headlines (1-10)") |
|
], |
|
outputs=gr.Textbox(label="Generated Fake News Headlines"), |
|
title="π° Fake News Generator", |
|
description="Generate fake news headlines using GPT-2" |
|
) |
|
|
|
detector_interface = gr.Interface( |
|
fn=detect_fake_news, |
|
inputs=gr.Textbox(lines=5, label="Enter full news text"), |
|
outputs=gr.Textbox(label="Fake News Detection Result"), |
|
title="π Fake News Detector", |
|
description="Detect whether a news article is FAKE or REAL using BERT" |
|
) |
|
|
|
|
|
gr.TabbedInterface([generator_interface, detector_interface], |
|
tab_names=["π° Generator", "π Detector"]).launch() |
|
|
|
|