Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
3 |
+
import gradio as gr
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
|
6 |
+
model = GPT2LMHeadModel.from_pretrained("gpt2")
|
7 |
+
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
8 |
+
|
9 |
+
def predict_fake_news(text):
|
10 |
+
input_ids = tokenizer.encode(text, return_tensors='pt')
|
11 |
+
output = model.generate(input_ids, max_length=50, num_return_sequences=1)
|
12 |
+
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
13 |
+
|
14 |
+
fake_confidence = 1 if "fake" in generated_text.lower() else 0
|
15 |
+
real_confidence = 1 - fake_confidence
|
16 |
+
|
17 |
+
fig, ax = plt.subplots()
|
18 |
+
ax.bar(["Real", "Fake"], [real_confidence, fake_confidence], color=['blue', 'red'])
|
19 |
+
plt.ylim(0, 1)
|
20 |
+
plt.xticks(rotation=45)
|
21 |
+
plt.title("Prediction Confidence")
|
22 |
+
|
23 |
+
return fig
|
24 |
+
|
25 |
+
input_text = gr.Textbox(lines=7, label="Paste the news article here", placeholder="Example: Scientists have discovered a new cure for cancer.")
|
26 |
+
output_graph = gr.Image(label="Prediction Confidence")
|
27 |
+
|
28 |
+
examples = [["New study shows coffee may prevent heart disease.", "Real"],
|
29 |
+
["Aliens have landed in New York City!", "Fake"],
|
30 |
+
["Global warming effects becoming more severe.", "Real"]]
|
31 |
+
|
32 |
+
gr.Interface(predict_fake_news, inputs=input_text, outputs=output_graph, title="Real/Fake News Detector", theme="soft", examples=examples).launch()
|