Aishani03's picture
Upload app.py
2fa7bd3 verified
raw
history blame
4.84 kB
# -*- coding: utf-8 -*-
"""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."""
# app.py
import torch
import torch.nn.functional as F
from transformers import (
GPT2LMHeadModel,
GPT2Tokenizer,
AutoTokenizer,
AutoModelForSequenceClassification
)
import gradio as gr
# Device setup
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Load GPT-2 for Fake News Generation
gpt2_tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
gpt2_model = GPT2LMHeadModel.from_pretrained("gpt2").to(device)
gpt2_model.eval()
# Headline generation function
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
# Gradio function wrapper
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)])
# Load BERT model for fake news detection (no token required)
bert_tokenizer = AutoTokenizer.from_pretrained("Pulk17/Fake-News-Detection")
bert_model = AutoModelForSequenceClassification.from_pretrained("Pulk17/Fake-News-Detection").to(device)
bert_model.eval()
# Detection function
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)"
# Gradio interfaces
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"
)
# Combine both interfaces
gr.TabbedInterface([generator_interface, detector_interface],
tab_names=["πŸ“° Generator", "πŸ” Detector"]).launch()