app.py
Browse files
app.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer, TextClassificationPipeline
|
3 |
+
import gradio as gr
|
4 |
+
# Load the fine-tuned model and tokenizer
|
5 |
+
new_model = AutoModelForSequenceClassification.from_pretrained('TeamQuad-fine-tuned-bert')
|
6 |
+
new_tokenizer = AutoTokenizer.from_pretrained('TeamQuad-fine-tuned-bert')
|
7 |
+
|
8 |
+
# Create a classification pipeline
|
9 |
+
classifier = TextClassificationPipeline(model=new_model, tokenizer=new_tokenizer)
|
10 |
+
|
11 |
+
# Add label mapping for fake news detection (assuming LABEL_0 = 'fake' and LABEL_1 = 'true')
|
12 |
+
label_mapping = {'LABEL_0': 'fake', 'LABEL_1': 'true'}
|
13 |
+
|
14 |
+
# Function to classify input text
|
15 |
+
def classify_news(text):
|
16 |
+
result = classifier(text)
|
17 |
+
# Extract the label and score
|
18 |
+
label = result[0]['label'] # 'LABEL_0' or 'LABEL_1'
|
19 |
+
score = result[0]['score'] # Confidence score
|
20 |
+
mapped_result = {'label': label_mapping[label], 'score': score}
|
21 |
+
return f"Label: {mapped_result['label']}, Score: {mapped_result['score']:.4f}"
|
22 |
+
|
23 |
+
# Create a Gradio interface
|
24 |
+
iface = gr.Interface(
|
25 |
+
fn=classify_news, # The function to process the input
|
26 |
+
inputs=gr.Textbox(lines=10, placeholder="Enter a news headline or article to classify..."),
|
27 |
+
outputs="text", # Output will be displayed as text
|
28 |
+
title="Fake News Detection",
|
29 |
+
description="Enter a news headline or article and see whether the model classifies it as 'Fake News' or 'True News'.",
|
30 |
+
|
31 |
+
)
|
32 |
+
|
33 |
+
# Launch the interface
|
34 |
+
iface.launch(share=True)
|