Update app.py
Browse files
app.py
CHANGED
|
@@ -2,25 +2,20 @@ from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassifica
|
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
|
| 6 |
-
model_name = "distilbert-base-uncased-finetuned-sst-2-english" # Replace with a fake news-specific model if needed
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 9 |
|
| 10 |
-
# Create a pipeline for text classification
|
| 11 |
classifier = pipeline("text-classification", model=model, tokenizer=tokenizer)
|
| 12 |
|
| 13 |
-
# Function to detect fake or real news
|
| 14 |
def detect_fake_news(text):
|
| 15 |
-
if not text.strip():
|
| 16 |
return "Please enter some text to analyze."
|
| 17 |
|
| 18 |
-
# Get prediction from the model
|
| 19 |
result = classifier(text)
|
| 20 |
label = result[0]['label']
|
| 21 |
score = result[0]['score']
|
| 22 |
|
| 23 |
-
# Custom logic for fake/real classification
|
| 24 |
if label == "POSITIVE" and score > 0.9:
|
| 25 |
return f"Likely Real (Confidence: {score:.4f})"
|
| 26 |
elif label == "NEGATIVE" and score > 0.9:
|
|
@@ -28,20 +23,15 @@ def detect_fake_news(text):
|
|
| 28 |
else:
|
| 29 |
return f"Uncertain (Confidence: {score:.4f})"
|
| 30 |
|
| 31 |
-
# Gradio interface
|
| 32 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
|
| 33 |
gr.Markdown("# Cricket Fake News Detector")
|
| 34 |
gr.Markdown("Enter a cricket-related news snippet to check if it's fake or real.")
|
| 35 |
|
| 36 |
-
# Input and output components
|
| 37 |
input_text = gr.Textbox(label="Enter News Here", placeholder="e.g., India wins World Cup 2025 with one ball!")
|
| 38 |
output_text = gr.Textbox(label="Result", interactive=False)
|
| 39 |
|
| 40 |
-
# Blue button for submission
|
| 41 |
submit_btn = gr.Button("Analyze", variant="primary")
|
| 42 |
|
| 43 |
-
# Link the button to the detection function
|
| 44 |
submit_btn.click(fn=detect_fake_news, inputs=input_text, outputs=output_text)
|
| 45 |
|
| 46 |
-
# Launch the interface
|
| 47 |
demo.launch()
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
model_name = "distilbert-base-uncased-finetuned-sst-2-english"
|
|
|
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 8 |
|
|
|
|
| 9 |
classifier = pipeline("text-classification", model=model, tokenizer=tokenizer)
|
| 10 |
|
|
|
|
| 11 |
def detect_fake_news(text):
|
| 12 |
+
if not text.strip():
|
| 13 |
return "Please enter some text to analyze."
|
| 14 |
|
|
|
|
| 15 |
result = classifier(text)
|
| 16 |
label = result[0]['label']
|
| 17 |
score = result[0]['score']
|
| 18 |
|
|
|
|
| 19 |
if label == "POSITIVE" and score > 0.9:
|
| 20 |
return f"Likely Real (Confidence: {score:.4f})"
|
| 21 |
elif label == "NEGATIVE" and score > 0.9:
|
|
|
|
| 23 |
else:
|
| 24 |
return f"Uncertain (Confidence: {score:.4f})"
|
| 25 |
|
|
|
|
| 26 |
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
|
| 27 |
gr.Markdown("# Cricket Fake News Detector")
|
| 28 |
gr.Markdown("Enter a cricket-related news snippet to check if it's fake or real.")
|
| 29 |
|
|
|
|
| 30 |
input_text = gr.Textbox(label="Enter News Here", placeholder="e.g., India wins World Cup 2025 with one ball!")
|
| 31 |
output_text = gr.Textbox(label="Result", interactive=False)
|
| 32 |
|
|
|
|
| 33 |
submit_btn = gr.Button("Analyze", variant="primary")
|
| 34 |
|
|
|
|
| 35 |
submit_btn.click(fn=detect_fake_news, inputs=input_text, outputs=output_text)
|
| 36 |
|
|
|
|
| 37 |
demo.launch()
|