|
|
|
|
|
|
|
|
|
|
|
|
|
import gradio as gr |
|
from datasets import load_dataset |
|
from transformers import AutoTokenizer, AutoModelForSequenceClassification, TextClassificationPipeline |
|
|
|
|
|
ds = load_dataset("GonzaloA/fake_news") |
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased') |
|
model = AutoModelForSequenceClassification.from_pretrained('TeamQuad-fine-tuned-bert') |
|
|
|
|
|
classifier = TextClassificationPipeline(model=model, tokenizer=tokenizer) |
|
|
|
|
|
label_mapping = {0: 'fake', 1: 'true'} |
|
|
|
|
|
def classify_news(text): |
|
result = classifier(text) |
|
label = result[0]['label'].split('_')[1] |
|
score = result[0]['score'] |
|
mapped_result = {'label': label_mapping[int(label)], 'score': score} |
|
return f"Label: {mapped_result['label']}, Score: {mapped_result['score']:.4f}" |
|
|
|
|
|
iface = gr.Interface( |
|
fn=classify_news, |
|
inputs=gr.Textbox(lines=10, placeholder="Enter a news headline or article to classify..."), |
|
outputs="text", |
|
title="Fake News Detection", |
|
description="Enter a news headline or article and see whether the model classifies it as 'Fake News' or 'True News'." |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
iface.launch(share=True) |
|
|