app.py
Browse files
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
|
3 |
+
# Install necessary libraries (This line is for local setup; Gradio Spaces should have dependencies set in the config)
|
4 |
+
# !pip install transformers datasets scikit-learn accelerate gradio
|
5 |
+
|
6 |
+
# Importing necessary libraries
|
7 |
+
import gradio as gr
|
8 |
+
from datasets import load_dataset
|
9 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, TextClassificationPipeline
|
10 |
+
|
11 |
+
# Load the dataset
|
12 |
+
ds = load_dataset("GonzaloA/fake_news")
|
13 |
+
|
14 |
+
# Load pre-trained tokenizer and model
|
15 |
+
tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
|
16 |
+
model = AutoModelForSequenceClassification.from_pretrained('TeamQuad-fine-tuned-bert')
|
17 |
+
|
18 |
+
# Create a classification pipeline
|
19 |
+
classifier = TextClassificationPipeline(model=model, tokenizer=tokenizer)
|
20 |
+
|
21 |
+
# Define label mapping for fake news detection
|
22 |
+
label_mapping = {0: 'fake', 1: 'true'}
|
23 |
+
|
24 |
+
# Function to classify input text
|
25 |
+
def classify_news(text):
|
26 |
+
result = classifier(text)
|
27 |
+
label = result[0]['label'].split('_')[1] # Extract label from the model's output
|
28 |
+
score = result[0]['score'] # Confidence score
|
29 |
+
mapped_result = {'label': label_mapping[int(label)], 'score': score}
|
30 |
+
return f"Label: {mapped_result['label']}, Score: {mapped_result['score']:.4f}"
|
31 |
+
|
32 |
+
# Create a Gradio interface
|
33 |
+
iface = gr.Interface(
|
34 |
+
fn=classify_news, # The function to process the input
|
35 |
+
inputs=gr.Textbox(lines=10, placeholder="Enter a news headline or article to classify..."),
|
36 |
+
outputs="text", # Output will be displayed as text
|
37 |
+
title="Fake News Detection",
|
38 |
+
description="Enter a news headline or article and see whether the model classifies it as 'Fake News' or 'True News'."
|
39 |
+
)
|
40 |
+
|
41 |
+
# Launch the interface
|
42 |
+
if __name__ == "__main__":
|
43 |
+
iface.launch(share=True)
|