Spaces:
Runtime error
Runtime error
Upload 7 files
Browse files- app.py +58 -0
- config.json +25 -0
- pytorch_model.bin +3 -0
- special_tokens_map.json +7 -0
- tokenizer_config.json +15 -0
- training_args.bin +3 -0
- vocab.txt +0 -0
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
from transformers import BertModel, BertTokenizer
|
| 5 |
+
|
| 6 |
+
# define the labels for the mutli-classification model
|
| 7 |
+
class_names = ['Negative', 'Neutral', 'Positive']
|
| 8 |
+
|
| 9 |
+
# Build the Sentiment Classifier class
|
| 10 |
+
class SentimentClassifier(nn.Module):
|
| 11 |
+
# Constructor class
|
| 12 |
+
def __init__(self, n_classes):
|
| 13 |
+
super(SentimentClassifier, self).__init__()
|
| 14 |
+
self.bert = BertModel.from_pretrained('lothritz/LuxemBERT')
|
| 15 |
+
self.drop = nn.Dropout(p=0.3)
|
| 16 |
+
self.out = nn.Linear(self.bert.config.hidden_size, n_classes)
|
| 17 |
+
|
| 18 |
+
# Forward propagaion class
|
| 19 |
+
def forward(self, input_ids, attention_mask):
|
| 20 |
+
_, pooled_output = self.bert(
|
| 21 |
+
input_ids=input_ids,
|
| 22 |
+
attention_mask=attention_mask,
|
| 23 |
+
return_dict=False
|
| 24 |
+
)
|
| 25 |
+
# Add a dropout layer
|
| 26 |
+
output = self.drop(pooled_output)
|
| 27 |
+
return self.out(output)
|
| 28 |
+
# load the CNN binary classification model
|
| 29 |
+
model = SentimentClassifier(len(class_names))
|
| 30 |
+
model.load_state_dict(torch.load('model/pytorch_model.bin'))
|
| 31 |
+
tokenizer = BertTokenizer.from_pretrained('./model/')
|
| 32 |
+
|
| 33 |
+
def encode(text):
|
| 34 |
+
encoded_text = tokenizer.encode_plus(
|
| 35 |
+
text,
|
| 36 |
+
max_length=50,
|
| 37 |
+
add_special_tokens=True,
|
| 38 |
+
return_token_type_ids=False,
|
| 39 |
+
pad_to_max_length=True,
|
| 40 |
+
return_attention_mask=True,
|
| 41 |
+
return_tensors='pt',
|
| 42 |
+
)
|
| 43 |
+
return encoded_text
|
| 44 |
+
|
| 45 |
+
def classify(text):
|
| 46 |
+
encoded_comment = encode(text)
|
| 47 |
+
input_ids = encoded_comment['input_ids']
|
| 48 |
+
attention_mask = encoded_comment['attention_mask']
|
| 49 |
+
|
| 50 |
+
output = model(input_ids, attention_mask)
|
| 51 |
+
_, prediction = torch.max(output, dim=1)
|
| 52 |
+
|
| 53 |
+
return class_names[prediction]
|
| 54 |
+
|
| 55 |
+
demo = gr.Interface(fn=classify, inputs="text", outputs="text", title="Sentiment Analyser", description="Text classifer for Luxembourgish")
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
demo.launch()
|
config.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_name_or_path": "neojex/testing",
|
| 3 |
+
"architectures": [
|
| 4 |
+
"BertForSequenceClassification"
|
| 5 |
+
],
|
| 6 |
+
"attention_probs_dropout_prob": 0.1,
|
| 7 |
+
"classifier_dropout": null,
|
| 8 |
+
"gradient_checkpointing": false,
|
| 9 |
+
"hidden_act": "gelu",
|
| 10 |
+
"hidden_dropout_prob": 0.1,
|
| 11 |
+
"hidden_size": 768,
|
| 12 |
+
"initializer_range": 0.02,
|
| 13 |
+
"intermediate_size": 3072,
|
| 14 |
+
"layer_norm_eps": 1e-12,
|
| 15 |
+
"max_position_embeddings": 514,
|
| 16 |
+
"model_type": "bert",
|
| 17 |
+
"num_attention_heads": 12,
|
| 18 |
+
"num_hidden_layers": 12,
|
| 19 |
+
"pad_token_id": 0,
|
| 20 |
+
"position_embedding_type": "absolute",
|
| 21 |
+
"transformers_version": "4.29.2",
|
| 22 |
+
"type_vocab_size": 2,
|
| 23 |
+
"use_cache": true,
|
| 24 |
+
"vocab_size": 30000
|
| 25 |
+
}
|
pytorch_model.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5bb586ffd8105810b89040e6cd3147af150aebbd2232b63a64f10f1eb574c196
|
| 3 |
+
size 436427573
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cls_token": "[CLS]",
|
| 3 |
+
"mask_token": "[MASK]",
|
| 4 |
+
"pad_token": "[PAD]",
|
| 5 |
+
"sep_token": "[SEP]",
|
| 6 |
+
"unk_token": "[UNK]"
|
| 7 |
+
}
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"clean_up_tokenization_spaces": true,
|
| 3 |
+
"cls_token": "[CLS]",
|
| 4 |
+
"do_basic_tokenize": true,
|
| 5 |
+
"do_lower_case": true,
|
| 6 |
+
"mask_token": "[MASK]",
|
| 7 |
+
"model_max_length": 1000000000000000019884624838656,
|
| 8 |
+
"never_split": null,
|
| 9 |
+
"pad_token": "[PAD]",
|
| 10 |
+
"sep_token": "[SEP]",
|
| 11 |
+
"strip_accents": null,
|
| 12 |
+
"tokenize_chinese_chars": true,
|
| 13 |
+
"tokenizer_class": "BertTokenizer",
|
| 14 |
+
"unk_token": "[UNK]"
|
| 15 |
+
}
|
training_args.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1585bd57bdff40999fba176b024c23515bef076536ee57f6033a20efe9335422
|
| 3 |
+
size 3899
|
vocab.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|