carblacac commited on
Commit
bfa9948
·
1 Parent(s): bfa54c4

add model and inference function

Browse files
app.py CHANGED
@@ -1,7 +1,31 @@
1
  import gradio as gr
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  iface.launch()
 
1
  import gradio as gr
2
+ import torch
3
+ from transformers import AutoTokenizer
4
+ from transformers import AutoModelForSequenceClassification
5
 
6
+
7
+ # Specify the path of the model
8
+ model_ckpt = Path("./distilbert-base-uncased-finetuned-emotion")
9
 
10
+ # Load the fine-tuned tokenizer and model
11
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
12
+ tokenizer = AutoTokenizer.from_pretrained(model_ckpt)
13
+ model = AutoModelForSequenceClassification.from_pretrained(model_ckpt).to(device)
14
+
15
+ class_names = ['sadness', 'joy', 'love', 'anger', 'fear', 'surprise']
16
+
17
+
18
+ def inference(text: str) -> str:
19
+ inputs = tokenizer(text, return_tensors="pt")
20
+
21
+ inputs = {k:v.to(device) for k,v in inputs.items()}
22
+ with torch.no_grad():
23
+ outputs = model(**inputs)
24
+ predictions = torch.nn.functional.softmax(outputs.logits, dim=-1).tolist()[0]
25
+
26
+ max_vale = max(predictions)
27
+ idx = predictions.index(max_vale)
28
+ return model.config.id2label[idx]
29
+
30
+ iface = gr.Interface(fn=inference, inputs="text", outputs="text")
31
  iface.launch()
distilbert-base-uncased-finetuned-emotion/config.json ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ "id2label": {
12
+ "0": "sadness",
13
+ "1": "joy",
14
+ "2": "love",
15
+ "3": "anger",
16
+ "4": "fear",
17
+ "5": "surprise"
18
+ },
19
+ "initializer_range": 0.02,
20
+ "label2id": {
21
+ "anger": 3,
22
+ "fear": 4,
23
+ "joy": 1,
24
+ "love": 2,
25
+ "sadness": 0,
26
+ "surprise": 5
27
+ },
28
+ "max_position_embeddings": 512,
29
+ "model_type": "distilbert",
30
+ "n_heads": 12,
31
+ "n_layers": 6,
32
+ "pad_token_id": 0,
33
+ "problem_type": "single_label_classification",
34
+ "qa_dropout": 0.1,
35
+ "seq_classif_dropout": 0.2,
36
+ "sinusoidal_pos_embds": false,
37
+ "tie_weights_": true,
38
+ "torch_dtype": "float32",
39
+ "transformers_version": "4.28.1",
40
+ "vocab_size": 30522
41
+ }
distilbert-base-uncased-finetuned-emotion/pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:781800fb16c200d330072a7d2b95bd7b3d34054b3f3f33023bf3020b96406a2a
3
+ size 267866413
distilbert-base-uncased-finetuned-emotion/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
+ }
distilbert-base-uncased-finetuned-emotion/test_metrics.csv ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ test_loss,test_accuracy,test_f1,test_runtime,test_samples_per_second,test_steps_per_second
2
+ 0.21811038255691528,0.92,0.919083343174122,25.5537,78.266,1.252
distilbert-base-uncased-finetuned-emotion/tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
distilbert-base-uncased-finetuned-emotion/tokenizer_config.json ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "clean_up_tokenization_spaces": true,
3
+ "cls_token": "[CLS]",
4
+ "do_lower_case": true,
5
+ "mask_token": "[MASK]",
6
+ "model_max_length": 512,
7
+ "pad_token": "[PAD]",
8
+ "sep_token": "[SEP]",
9
+ "strip_accents": null,
10
+ "tokenize_chinese_chars": true,
11
+ "tokenizer_class": "DistilBertTokenizer",
12
+ "unk_token": "[UNK]"
13
+ }
distilbert-base-uncased-finetuned-emotion/training_args.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ebcad4e947ae20ae35cf3bdf68a31a91b16facc628995d5448c2ed9bb76a3351
3
+ size 3643
distilbert-base-uncased-finetuned-emotion/vocab.txt ADDED
The diff for this file is too large to render. See raw diff