File size: 4,835 Bytes
2fa7bd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- 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()