aryadytm commited on
Commit
2e5179b
1 Parent(s): 4c236d0

Add application file

Browse files
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import (
2
+ AlbertTokenizerFast, DistilBertTokenizerFast, RobertaTokenizerFast,
3
+ AlbertForSequenceClassification, DistilBertForSequenceClassification, RobertaForSequenceClassification
4
+ )
5
+ import torch.nn.functional as F
6
+ import torch
7
+ import gradio as gr
8
+
9
+ from gradio.components import Textbox
10
+
11
+ albert_tokenizer = AlbertTokenizerFast.from_pretrained('albert-base-v2')
12
+ bert_tokenizer = DistilBertTokenizerFast.from_pretrained('distilbert-base-uncased')
13
+ roberta_tokenizer = RobertaTokenizerFast.from_pretrained('distilroberta-base')
14
+
15
+ albert_model = AlbertForSequenceClassification.from_pretrained("./model_albert")
16
+ bert_model = DistilBertForSequenceClassification.from_pretrained("./model_bert")
17
+ roberta_model = RobertaForSequenceClassification.from_pretrained("./model_roberta")
18
+
19
+ def majority_voting(votes):
20
+ vote_result = max(set(votes), key=votes.count)
21
+ return vote_result
22
+
23
+ def predict_news(news_title, news_text):
24
+ combined_text = f"{news_title} - {news_text}"
25
+
26
+ albert_input = albert_tokenizer(combined_text, return_tensors='pt', truncation=True, padding=True)
27
+ bert_input = bert_tokenizer(combined_text, return_tensors='pt', truncation=True, padding=True)
28
+ roberta_input = roberta_tokenizer(combined_text, return_tensors='pt', truncation=True, padding=True)
29
+
30
+ with torch.no_grad():
31
+ albert_logits = albert_model(**albert_input).logits
32
+ bert_logits = bert_model(**bert_input).logits
33
+ roberta_logits = roberta_model(**roberta_input).logits
34
+
35
+ albert_probs = F.softmax(albert_logits, dim=-1)
36
+ bert_probs = F.softmax(bert_logits, dim=-1)
37
+ roberta_probs = F.softmax(roberta_logits, dim=-1)
38
+
39
+ albert_pred_class = albert_probs.argmax(-1).item()
40
+ bert_pred_class = bert_probs.argmax(-1).item()
41
+ roberta_pred_class = roberta_probs.argmax(-1).item()
42
+
43
+ albert_pred_prob = albert_probs[0, albert_pred_class].item()
44
+ bert_pred_prob = bert_probs[0, bert_pred_class].item()
45
+ roberta_pred_prob = roberta_probs[0, roberta_pred_class].item()
46
+
47
+ average_confidence = (albert_pred_prob + bert_pred_prob + roberta_pred_prob) / 3
48
+
49
+ final_pred_class = majority_voting([albert_pred_class, bert_pred_class, roberta_pred_class])
50
+
51
+ prediction = 'REAL' if final_pred_class == 1 else 'FAKE'
52
+ return prediction, f"{average_confidence:.2f}", f"{albert_pred_prob:.2f}", f"{bert_pred_prob:.2f}", f"{roberta_pred_prob:.2f}"
53
+
54
+ iface = gr.Interface(
55
+ fn=predict_news,
56
+ inputs=[
57
+ Textbox(lines=2, label="News Title", placeholder="Enter News Title Here..."),
58
+ Textbox(lines=7, label="News Text", placeholder="Enter News Text Here...")
59
+ ],
60
+ outputs=[
61
+ Textbox(label="Prediction"),
62
+ Textbox(label="Average Confidence"),
63
+ Textbox(label="ALBERT Confidence"),
64
+ Textbox(label="BERT Confidence"),
65
+ Textbox(label="RoBERTa Confidence")
66
+ ],
67
+ title="Fake News Classification with Ensemble Learning Transformer Models",
68
+ description="UAS Deep Learning - 2501985836 - Arya Adyatma"
69
+ )
70
+
71
+ iface.launch()
model_albert/config.json ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "albert-base-v2",
3
+ "architectures": [
4
+ "AlbertForSequenceClassification"
5
+ ],
6
+ "attention_probs_dropout_prob": 0,
7
+ "bos_token_id": 2,
8
+ "classifier_dropout_prob": 0.1,
9
+ "down_scale_factor": 1,
10
+ "embedding_size": 128,
11
+ "eos_token_id": 3,
12
+ "gap_size": 0,
13
+ "hidden_act": "gelu_new",
14
+ "hidden_dropout_prob": 0,
15
+ "hidden_size": 768,
16
+ "initializer_range": 0.02,
17
+ "inner_group_num": 1,
18
+ "intermediate_size": 3072,
19
+ "layer_norm_eps": 1e-12,
20
+ "max_position_embeddings": 512,
21
+ "model_type": "albert",
22
+ "net_structure_type": 0,
23
+ "num_attention_heads": 12,
24
+ "num_hidden_groups": 1,
25
+ "num_hidden_layers": 12,
26
+ "num_memory_blocks": 0,
27
+ "pad_token_id": 0,
28
+ "position_embedding_type": "absolute",
29
+ "problem_type": "single_label_classification",
30
+ "torch_dtype": "float32",
31
+ "transformers_version": "4.36.0",
32
+ "type_vocab_size": 2,
33
+ "vocab_size": 30000
34
+ }
model_albert/model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d3801acd748ab99ea2ff11f1097fd594e3f672de21ebc2fb12a82c6ff7c88051
3
+ size 46743912
model_albert/training_args.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f7255591348fe6bb850b47d3d540f13261b2ef94508a8f005cadee19b780f4cd
3
+ size 4347
model_bert/config.json ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "distilbert-base-uncased",
3
+ "activation": "gelu",
4
+ "architectures": [
5
+ "DistilBertForSequenceClassification"
6
+ ],
7
+ "attention_dropout": 0.1,
8
+ "dim": 768,
9
+ "dropout": 0.1,
10
+ "hidden_dim": 3072,
11
+ "initializer_range": 0.02,
12
+ "max_position_embeddings": 512,
13
+ "model_type": "distilbert",
14
+ "n_heads": 12,
15
+ "n_layers": 6,
16
+ "pad_token_id": 0,
17
+ "problem_type": "single_label_classification",
18
+ "qa_dropout": 0.1,
19
+ "seq_classif_dropout": 0.2,
20
+ "sinusoidal_pos_embds": false,
21
+ "tie_weights_": true,
22
+ "torch_dtype": "float32",
23
+ "transformers_version": "4.36.0",
24
+ "vocab_size": 30522
25
+ }
model_bert/model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6dd5ae00c4acfaada74cca42d9181dc55e9cc907c84f6eb806bd24103e191944
3
+ size 267832560
model_bert/training_args.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6b01cfc38acaaaa60208e5937e4dd3ac6ab311aef216d5c12b1474f7a7fe513e
3
+ size 4155
model_roberta/config.json ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "distilroberta-base",
3
+ "architectures": [
4
+ "RobertaForSequenceClassification"
5
+ ],
6
+ "attention_probs_dropout_prob": 0.1,
7
+ "bos_token_id": 0,
8
+ "classifier_dropout": null,
9
+ "eos_token_id": 2,
10
+ "hidden_act": "gelu",
11
+ "hidden_dropout_prob": 0.1,
12
+ "hidden_size": 768,
13
+ "initializer_range": 0.02,
14
+ "intermediate_size": 3072,
15
+ "layer_norm_eps": 1e-05,
16
+ "max_position_embeddings": 514,
17
+ "model_type": "roberta",
18
+ "num_attention_heads": 12,
19
+ "num_hidden_layers": 6,
20
+ "pad_token_id": 1,
21
+ "position_embedding_type": "absolute",
22
+ "problem_type": "single_label_classification",
23
+ "torch_dtype": "float32",
24
+ "transformers_version": "4.36.0",
25
+ "type_vocab_size": 1,
26
+ "use_cache": true,
27
+ "vocab_size": 50265
28
+ }
model_roberta/model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:38b9bdb64964633cb7a1fd069c326a35e118a8c798175c99a8b417c65ed1ae8e
3
+ size 328492280
model_roberta/training_args.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ef2cea918d8f268fa604d0638b5947092b595b80cc8417f1e63a28ab520199c7
3
+ size 4155
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio==3.50
2
+ transformers
3
+ torch