Upload 2 files
Browse files- app.py +121 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""Fake_News Generator_Detector.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colab.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1C8ZWvJVZzTCngwpz788EpcT2lrbBWhYa
|
8 |
+
|
9 |
+
### π° **Project Description: Fake News Generator & Detector using Generative AI and NLP**
|
10 |
+
This project is an interactive AI-powered tool that enables users to both generate realistic-looking fake news articles and detect whether a given news text is fake or real using state-of-the-art natural language processing models. It highlights how language models can both create and combat misinformation.
|
11 |
+
|
12 |
+
π§° **The application is built using:**
|
13 |
+
|
14 |
+
1. **Transformers Library (by Hugging Face)**:
|
15 |
+
For loading and using pretrained models β GPT-2 for fake news generation and fine-tuned BERT for fake news detection.
|
16 |
+
|
17 |
+
2. **Gradio:** To create a simple, interactive web-based user interface with tabbed sections for generation and detection.
|
18 |
+
|
19 |
+
3. **Google Colab / Python:** For backend development, prototyping, and running the application in a cloud-based notebook environment.
|
20 |
+
|
21 |
+
π― **Project Objectives**
|
22 |
+
|
23 |
+
**1. Generate Fake News Text**
|
24 |
+
|
25 |
+
Use GPT-2 to simulate fake news articles from user-provided prompts for for awareness and experimentation.
|
26 |
+
|
27 |
+
**2. Detect Fake or Real News**
|
28 |
+
|
29 |
+
Utilize a fine-tuned BERT model (Pulk17/Fake-News-Detection) to accurately classify news content as fake or real.
|
30 |
+
|
31 |
+
**3. Provide an Interactive Interface**
|
32 |
+
|
33 |
+
Use Gradio to build a user-friendly web interface.
|
34 |
+
|
35 |
+
**4. Demonstrate Dual Use of AI in Misinformation**
|
36 |
+
|
37 |
+
Showcase how AI can both create and detect fake news, promoting awareness and responsible AI usage.
|
38 |
+
|
39 |
+
**Step 2: Import Required Modules**
|
40 |
+
|
41 |
+
Import libraries for model handling, tokenization, and interface building.
|
42 |
+
"""
|
43 |
+
|
44 |
+
# app.py
|
45 |
+
|
46 |
+
import torch
|
47 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
48 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
49 |
+
import gradio as gr
|
50 |
+
|
51 |
+
# Device setup
|
52 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
53 |
+
|
54 |
+
# Load GPT-2 for fake news generation
|
55 |
+
gpt2_tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
56 |
+
gpt2_model = GPT2LMHeadModel.from_pretrained("gpt2").to(device)
|
57 |
+
|
58 |
+
# Load fine-tuned BERT model for fake news detection
|
59 |
+
bert_tokenizer = AutoTokenizer.from_pretrained("Pulk17/Fake-News-Detection")
|
60 |
+
bert_model = AutoModelForSequenceClassification.from_pretrained(
|
61 |
+
"Pulk17/Fake-News-Detection"
|
62 |
+
).to(device)
|
63 |
+
|
64 |
+
# Function to generate fake news from a prompt
|
65 |
+
def generate_fake_news(prompt):
|
66 |
+
inputs = gpt2_tokenizer.encode(prompt, return_tensors="pt").to(device)
|
67 |
+
outputs = gpt2_model.generate(
|
68 |
+
inputs,
|
69 |
+
max_length=200,
|
70 |
+
num_return_sequences=1,
|
71 |
+
no_repeat_ngram_size=2,
|
72 |
+
do_sample=True,
|
73 |
+
temperature=0.7,
|
74 |
+
top_k=50,
|
75 |
+
top_p=0.95,
|
76 |
+
early_stopping=True
|
77 |
+
)
|
78 |
+
generated_text = gpt2_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
79 |
+
return generated_text
|
80 |
+
|
81 |
+
# Function to detect if news is fake or real
|
82 |
+
def detect_news(text):
|
83 |
+
inputs = bert_tokenizer(text, return_tensors="pt", truncation=True, padding=True).to(device)
|
84 |
+
with torch.no_grad():
|
85 |
+
outputs = bert_model(**inputs)
|
86 |
+
logits = outputs.logits
|
87 |
+
predicted_class = torch.argmax(logits, dim=1).item()
|
88 |
+
confidence = torch.softmax(logits, dim=1)[0][predicted_class].item()
|
89 |
+
label = "π₯ Fake News" if predicted_class == 0 else "π© Real News"
|
90 |
+
return f"{label} (Confidence: {confidence:.2f})"
|
91 |
+
|
92 |
+
# Gradio Interface
|
93 |
+
with gr.Blocks() as demo:
|
94 |
+
gr.Markdown("## π° Fake News Generator & Detector (GPT-2 + BERT)")
|
95 |
+
|
96 |
+
with gr.Tab("π οΈ Generate Fake News"):
|
97 |
+
with gr.Row():
|
98 |
+
input_text = gr.Textbox(
|
99 |
+
label="Enter a News Headline or Prompt",
|
100 |
+
placeholder="e.g. Scientists discover a talking dolphin species near Japan...",
|
101 |
+
lines=2
|
102 |
+
)
|
103 |
+
generate_btn = gr.Button("Generate")
|
104 |
+
output_text = gr.Textbox(label="Generated News Article")
|
105 |
+
generate_btn.click(generate_fake_news, inputs=input_text, outputs=output_text)
|
106 |
+
|
107 |
+
with gr.Tab("π Detect Fake or Real"):
|
108 |
+
with gr.Row():
|
109 |
+
detect_input = gr.Textbox(
|
110 |
+
label="Enter a News Article or Statement",
|
111 |
+
placeholder="Paste a paragraph to detect if it's fake or real...",
|
112 |
+
lines=5
|
113 |
+
)
|
114 |
+
detect_btn = gr.Button("Detect")
|
115 |
+
detect_output = gr.Textbox(label="Detection Result")
|
116 |
+
detect_btn.click(detect_news, inputs=detect_input, outputs=detect_output)
|
117 |
+
|
118 |
+
# Launch the app
|
119 |
+
demo.launch()
|
120 |
+
|
121 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
transformers
|
3 |
+
gradio
|