Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,126 +1,55 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
# Load model
|
| 5 |
classifier = pipeline(
|
| 6 |
"text-classification",
|
| 7 |
-
model="
|
|
|
|
| 8 |
)
|
| 9 |
|
| 10 |
def predict_news(text):
|
| 11 |
if text.strip() == "":
|
| 12 |
-
return "<span style='color:red;'
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
if label == "LABEL_0":
|
| 20 |
-
display_label = "Fake"
|
| 21 |
-
color = "red"
|
| 22 |
-
else:
|
| 23 |
-
display_label = "Real"
|
| 24 |
-
color = "green"
|
| 25 |
-
|
| 26 |
-
html_output = f"""
|
| 27 |
-
<div style="
|
| 28 |
-
font-size:28px;
|
| 29 |
-
font-weight:bold;
|
| 30 |
-
color:{color};
|
| 31 |
-
text-align:center;
|
| 32 |
-
padding:20px;
|
| 33 |
-
">
|
| 34 |
-
{display_label} {confidence}%
|
| 35 |
-
</div>
|
| 36 |
-
"""
|
| 37 |
-
|
| 38 |
-
return html_output, confidence
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
# ================= UI LAYOUT =================
|
| 42 |
-
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 43 |
-
|
| 44 |
-
gr.Markdown(
|
| 45 |
-
"""
|
| 46 |
-
# 📰 Fake News Detection System
|
| 47 |
-
### AI-based system to detect Fake or Real news (English & Bangla)
|
| 48 |
-
"""
|
| 49 |
-
)
|
| 50 |
-
|
| 51 |
-
with gr.Row():
|
| 52 |
-
with gr.Column(scale=2):
|
| 53 |
-
news_input = gr.Textbox(
|
| 54 |
-
label="Enter News Text",
|
| 55 |
-
placeholder="Paste English or Bangla news here...",
|
| 56 |
-
lines=10
|
| 57 |
-
)
|
| 58 |
-
submit_btn = gr.Button("Submit", variant="primary")
|
| 59 |
-
|
| 60 |
-
with gr.Column(scale=1):
|
| 61 |
-
prediction_output = gr.HTML(label="Prediction")
|
| 62 |
-
confidence_bar = gr.Slider(
|
| 63 |
-
minimum=0,
|
| 64 |
-
maximum=100,
|
| 65 |
-
label="Confidence Percentage",
|
| 66 |
-
interactive=False
|
| 67 |
-
)
|
| 68 |
-
|
| 69 |
-
submit_btn.click(
|
| 70 |
-
fn=predict_news,
|
| 71 |
-
inputs=news_input,
|
| 72 |
-
outputs=[prediction_output, confidence_bar]
|
| 73 |
-
)
|
| 74 |
-
|
| 75 |
-
demo.launch()
|
| 76 |
-
|
| 77 |
-
import gradio as gr
|
| 78 |
-
from transformers import pipeline
|
| 79 |
|
| 80 |
-
# Load model
|
| 81 |
-
classifier = pipeline(
|
| 82 |
-
"text-classification",
|
| 83 |
-
model="mrm8488/bert-tiny-finetuned-fake-news-detection"
|
| 84 |
-
)
|
| 85 |
-
|
| 86 |
-
def predict_news(text):
|
| 87 |
-
if text.strip() == "":
|
| 88 |
-
return "<span style='color:red;'>Please enter news text</span>", 0
|
| 89 |
-
|
| 90 |
-
result = classifier(text)[0]
|
| 91 |
label = result["label"]
|
| 92 |
confidence = round(result["score"] * 100, 2)
|
| 93 |
|
| 94 |
-
#
|
| 95 |
-
if label
|
| 96 |
-
display_label = "Fake"
|
| 97 |
color = "red"
|
| 98 |
else:
|
| 99 |
-
display_label = "Real"
|
| 100 |
color = "green"
|
| 101 |
|
| 102 |
html_output = f"""
|
| 103 |
<div style="
|
| 104 |
-
font-size:28px;
|
| 105 |
-
font-weight:bold;
|
| 106 |
-
color:{color};
|
| 107 |
-
text-align:center;
|
| 108 |
-
padding:20px;
|
| 109 |
">
|
| 110 |
-
{display_label} {confidence}%
|
| 111 |
</div>
|
| 112 |
"""
|
| 113 |
|
| 114 |
return html_output, confidence
|
| 115 |
|
| 116 |
-
|
| 117 |
# ================= UI LAYOUT =================
|
| 118 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 119 |
|
| 120 |
gr.Markdown(
|
| 121 |
"""
|
| 122 |
# 📰 Fake News Detection System
|
| 123 |
-
|
| 124 |
"""
|
| 125 |
)
|
| 126 |
|
|
@@ -150,3 +79,4 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 150 |
|
| 151 |
demo.launch()
|
| 152 |
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Load a better model for Bangla & English fake news detection
|
| 5 |
classifier = pipeline(
|
| 6 |
"text-classification",
|
| 7 |
+
model="armansakif/bengali-fake-news",
|
| 8 |
+
tokenizer="armansakif/bengali-fake-news"
|
| 9 |
)
|
| 10 |
|
| 11 |
def predict_news(text):
|
| 12 |
if text.strip() == "":
|
| 13 |
+
return "<span style='color:red;'>⚠️ Please enter news text</span>", 0
|
| 14 |
|
| 15 |
+
# Run classification
|
| 16 |
+
try:
|
| 17 |
+
result = classifier(text)[0]
|
| 18 |
+
except Exception as e:
|
| 19 |
+
return f"<span style='color:red;'>Error: {str(e)}</span>", 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
label = result["label"]
|
| 22 |
confidence = round(result["score"] * 100, 2)
|
| 23 |
|
| 24 |
+
# Map labels to understandable text
|
| 25 |
+
if label.upper() in ["LABEL_0", "fake", "Fake"]:
|
| 26 |
+
display_label = "🟥 Fake News"
|
| 27 |
color = "red"
|
| 28 |
else:
|
| 29 |
+
display_label = "🟩 Real News"
|
| 30 |
color = "green"
|
| 31 |
|
| 32 |
html_output = f"""
|
| 33 |
<div style="
|
| 34 |
+
font-size: 28px;
|
| 35 |
+
font-weight: bold;
|
| 36 |
+
color: {color};
|
| 37 |
+
text-align: center;
|
| 38 |
+
padding: 20px;
|
| 39 |
">
|
| 40 |
+
{display_label} — {confidence}%
|
| 41 |
</div>
|
| 42 |
"""
|
| 43 |
|
| 44 |
return html_output, confidence
|
| 45 |
|
|
|
|
| 46 |
# ================= UI LAYOUT =================
|
| 47 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 48 |
|
| 49 |
gr.Markdown(
|
| 50 |
"""
|
| 51 |
# 📰 Fake News Detection System
|
| 52 |
+
**AI‑powered fake vs real news classifier (English & Bangla)**
|
| 53 |
"""
|
| 54 |
)
|
| 55 |
|
|
|
|
| 79 |
|
| 80 |
demo.launch()
|
| 81 |
|
| 82 |
+
|