Testys commited on
Commit
73b342f
β€’
1 Parent(s): 20dad1e

Readding documents necessary for Streamlit Document

Browse files
.gitignore ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Python
2
+ *.pyc
3
+ __pycache__/
4
+ venv/
5
+ *.env
6
+
7
+ # VS Code
8
+ .vscode/
9
+ .settings/
10
+ .cache/
11
+
12
+ # Other
13
+ *.log
14
+ *.swp
15
+ .DS_Store
README.md CHANGED
@@ -1,7 +1,7 @@
1
  ---
2
  title: YorubaCNN
3
- emoji: πŸ“š
4
- colorFrom: yellow
5
  colorTo: gray
6
  sdk: streamlit
7
  sdk_version: 1.37.1
 
1
  ---
2
  title: YorubaCNN
3
+ emoji: πŸ“‰
4
+ colorFrom: green
5
  colorTo: gray
6
  sdk: streamlit
7
  sdk_version: 1.37.1
main.py ADDED
@@ -0,0 +1 @@
 
 
1
+ print('Hello, Lightning World!')
model_loader.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import HfApi, HfFolder
2
+ import os
3
+ import os
4
+ from dotenv import load_dotenv
5
+
6
+ # Load environment variables from .env file if it exists
7
+ load_dotenv()
8
+
9
+ hf_token = os.getenv('HF_TOKEN')
10
+ api = HfApi()
11
+
12
+ # Specify the model details
13
+ model_id = "Testys/cnn_yor_ner"
14
+ sent_id = "Testys/cnn_sent_yor"
15
+ local_dir = "./my_model"
16
+ sent_dir = "./sent_model"
17
+
18
+ # Download the model folder
19
+ if not os.path.exists(local_dir):
20
+ os.makedirs(local_dir)
21
+
22
+ if not os.path.exists(os.path.join(local_dir, "pytorch_model.bin")):
23
+ api.hf_hub_download(repo_id=model_id, filename="pytorch_model.bin", local_dir=local_dir, use_auth_token=hf_token)
24
+
25
+ if not os.path.exists(os.path.join(local_dir, "config.json")):
26
+ api.hf_hub_download(repo_id=model_id, filename="config.json", local_dir=local_dir, use_auth_token=hf_token)
27
+
28
+
29
+ # Check if the model is already downloaded
30
+ if not os.path.exists(sent_dir):
31
+ os.makedirs(sent_dir)
32
+
33
+ # Download the model files only if they don't exist
34
+ if not os.path.exists(os.path.join(sent_dir, "sent_pytorch_model.bin")):
35
+ api.hf_hub_download(repo_id=sent_id, filename="sent_pytorch_model.bin", local_dir=sent_dir, use_auth_token=hf_token)
36
+ if not os.path.exists(os.path.join(sent_dir, "config.json")):
37
+ api.hf_hub_download(repo_id=sent_id, filename="config.json", local_dir=sent_dir, use_auth_token=hf_token)
modelling_cnn.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import torch
4
+ import torch.nn as nn
5
+ import torch.nn.functional as F
6
+ from transformers import pipeline
7
+ from huggingface_hub import HfApi, HfFolder
8
+ from transformers import AutoTokenizer, AutoConfig, AutoModelForTokenClassification, AutoModel
9
+
10
+
11
+ class CNNForNER(nn.Module):
12
+ def __init__(self, pretrained_model_name, num_classes, max_length=128):
13
+ super(CNNForNER, self).__init__()
14
+ self.transformer = AutoModelForTokenClassification.from_pretrained(pretrained_model_name)
15
+ self.max_length = max_length
16
+
17
+ # Get the number of labels from the pretrained model
18
+ pretrained_num_labels = self.transformer.num_labels
19
+
20
+ self.conv1 = nn.Conv1d(in_channels=pretrained_num_labels, out_channels=256, kernel_size=3, padding=1)
21
+ self.conv2 = nn.Conv1d(in_channels=256, out_channels=128, kernel_size=3, padding=1)
22
+ self.dropout = nn.Dropout(0.3)
23
+ self.fc = nn.Linear(in_features=128, out_features=num_classes)
24
+
25
+ def forward(self, input_ids, attention_mask):
26
+ outputs = self.transformer(input_ids=input_ids, attention_mask=attention_mask)
27
+ logits = outputs.logits # Shape: (batch_size, sequence_length, pretrained_num_labels)
28
+
29
+ # Apply CNN layers
30
+ logits = logits.permute(0, 2, 1) # Shape: (batch_size, pretrained_num_labels, sequence_length)
31
+ conv1_out = F.relu(self.conv1(logits))
32
+ conv2_out = F.relu(self.conv2(conv1_out))
33
+ conv2_out = self.dropout(conv2_out)
34
+ conv2_out = conv2_out.permute(0, 2, 1) # Shape: (batch_size, sequence_length, 128)
35
+ final_logits = self.fc(conv2_out) # Shape: (batch_size, sequence_length, num_classes)
36
+ return final_logits
37
+
38
+
39
+
40
+ import torch
41
+ import torch.nn as nn
42
+ import torch.nn.functional as F
43
+ from transformers import AutoModel
44
+
45
+ class SentimentCNNModel(nn.Module):
46
+ def __init__(self, transformer_model_name, num_classes, cnn_out_channels=100, cnn_kernel_sizes=[3, 5, 7]):
47
+ super(SentimentCNNModel, self).__init__()
48
+ # Load pre-trained transformer model
49
+ self.transformer = AutoModel.from_pretrained(transformer_model_name)
50
+
51
+ # CNN layers with multiple kernel sizes
52
+ self.convs = nn.ModuleList([
53
+ nn.Conv1d(in_channels=self.transformer.config.hidden_size,
54
+ out_channels=cnn_out_channels,
55
+ kernel_size=k)
56
+ for k in cnn_kernel_sizes
57
+ ])
58
+
59
+ # Dropout layer
60
+ self.dropout = nn.Dropout(0.5)
61
+
62
+ # Fully connected layer
63
+ self.fc = nn.Linear(len(cnn_kernel_sizes) * cnn_out_channels, num_classes)
64
+
65
+ def forward(self, input_ids, attention_mask):
66
+ # Get hidden states from the transformer model
67
+ transformer_outputs = self.transformer(input_ids=input_ids, attention_mask=attention_mask)
68
+ hidden_states = transformer_outputs.last_hidden_state # Shape: (batch_size, seq_len, hidden_size)
69
+
70
+ # Transpose for CNN input: (batch_size, hidden_size, seq_len)
71
+ hidden_states = hidden_states.transpose(1, 2)
72
+
73
+ # Apply convolution and pooling
74
+ conv_outputs = [torch.relu(conv(hidden_states)) for conv in self.convs]
75
+ pooled_outputs = [torch.max(output, dim=2)[0] for output in conv_outputs]
76
+
77
+ # Concatenate pooled outputs and apply dropout
78
+ cat_output = torch.cat(pooled_outputs, dim=1)
79
+ cat_output = self.dropout(cat_output)
80
+
81
+ # Final classification
82
+ logits = self.fc(cat_output)
83
+
84
+ return logits
85
+
86
+
87
+ class SentimentCNNModel(nn.Module):
88
+ def __init__(self, transformer_model_name, num_classes, cnn_out_channels=100, cnn_kernel_sizes=[3, 5, 7]):
89
+ super(SentimentCNNModel, self).__init__()
90
+ # Load pre-trained transformer model
91
+ self.transformer = AutoModel.from_pretrained(transformer_model_name)
92
+
93
+ # CNN layers with multiple kernel sizes
94
+ self.convs = nn.ModuleList([
95
+ nn.Conv1d(in_channels=self.transformer.config.hidden_size,
96
+ out_channels=cnn_out_channels,
97
+ kernel_size=k)
98
+ for k in cnn_kernel_sizes
99
+ ])
100
+
101
+ # Dropout layer
102
+ self.dropout = nn.Dropout(0.5)
103
+
104
+ # Fully connected layer
105
+ self.fc = nn.Linear(len(cnn_kernel_sizes) * cnn_out_channels, num_classes)
106
+
107
+ def forward(self, input_ids, attention_mask):
108
+ # Get hidden states from the transformer model
109
+ transformer_outputs = self.transformer(input_ids=input_ids, attention_mask=attention_mask)
110
+ hidden_states = transformer_outputs.last_hidden_state # Shape: (batch_size, seq_len, hidden_size)
111
+
112
+ # Transpose for CNN input: (batch_size, hidden_size, seq_len)
113
+ hidden_states = hidden_states.transpose(1, 2)
114
+
115
+ # Apply convolution and pooling
116
+ conv_outputs = [torch.relu(conv(hidden_states)) for conv in self.convs]
117
+ pooled_outputs = [torch.max(output, dim=2)[0] for output in conv_outputs]
118
+
119
+ # Concatenate pooled outputs and apply dropout
120
+ cat_output = torch.cat(pooled_outputs, dim=1)
121
+ cat_output = self.dropout(cat_output)
122
+
123
+ # Final classification
124
+ logits = self.fc(cat_output)
125
+
126
+ return logits
my_model/config.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"model_type": "CNNForYorubaNER", "num_classes": 9, "max_length": 128, "pretrained_model_name": "masakhane/afroxlmr-large-ner-masakhaner-1.0_2.0"}
my_model/pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f6df5d161a93b1a7470e4e0cb4f1d8a9dbeb74a9fb56b72d318dad0731c9a379
3
+ size 2236003390
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ torch
2
+ transformers
3
+ huggingface_hub
4
+ streamlit
sent_model/config.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"model_type": "CNNForSentimentAnalysis", "num_classes": 2, "max_length": 128, "pretrained_model_name": "Davlan/naija-twitter-sentiment-afriberta-large"}
sent_model/sent_pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1f9eb93cf4592a046678a953616e4281e7bba91703e19a563cb7074ed7c69d33
3
+ size 507210373