Spaces:
Runtime error
Runtime error
Updated Space
Browse files- app.py +118 -0
- label_encoder.pkl +3 -0
- model.pth +3 -0
app.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import AutoTokenizer
|
| 4 |
+
import torch.nn as nn
|
| 5 |
+
import joblib
|
| 6 |
+
|
| 7 |
+
# Load the label encoder
|
| 8 |
+
label_encoder = joblib.load("label_encoder.pkl") # Save it during training using joblib.dump(label_encoder, "label_encoder.pkl")
|
| 9 |
+
|
| 10 |
+
class TransformerEncoderLayer(nn.Module):
|
| 11 |
+
def __init__(self, embed_size, num_heads, ff_size, dropout=0.1):
|
| 12 |
+
super(TransformerEncoderLayer, self).__init__()
|
| 13 |
+
self.attention = nn.MultiheadAttention(embed_size, num_heads, dropout=dropout)
|
| 14 |
+
self.ffn = nn.Sequential(
|
| 15 |
+
nn.Linear(embed_size, ff_size),
|
| 16 |
+
nn.ReLU(),
|
| 17 |
+
nn.Linear(ff_size, embed_size)
|
| 18 |
+
)
|
| 19 |
+
self.norm1 = nn.LayerNorm(embed_size)
|
| 20 |
+
self.norm2 = nn.LayerNorm(embed_size)
|
| 21 |
+
self.dropout = nn.Dropout(dropout)
|
| 22 |
+
|
| 23 |
+
def forward(self, x):
|
| 24 |
+
# Multi-head attention
|
| 25 |
+
attn_output, _ = self.attention(x, x, x)
|
| 26 |
+
x = self.norm1(x + self.dropout(attn_output))
|
| 27 |
+
|
| 28 |
+
# Feed-forward layer
|
| 29 |
+
ffn_output = self.ffn(x)
|
| 30 |
+
x = self.norm2(x + self.dropout(ffn_output))
|
| 31 |
+
|
| 32 |
+
return x
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
class CustomTransformerEncoder(nn.Module):
|
| 36 |
+
def __init__(self, vocab_size, embed_size=768, num_heads=8, num_layers=6, ff_size=2048, max_len=512):
|
| 37 |
+
super(CustomTransformerEncoder, self).__init__()
|
| 38 |
+
|
| 39 |
+
# Token embedding layer
|
| 40 |
+
self.embedding = nn.Embedding(vocab_size, embed_size)
|
| 41 |
+
|
| 42 |
+
# Positional encoding
|
| 43 |
+
self.positional_encoding = nn.Parameter(torch.zeros(1, max_len, embed_size))
|
| 44 |
+
|
| 45 |
+
# Transformer encoder layers
|
| 46 |
+
self.layers = nn.ModuleList([
|
| 47 |
+
TransformerEncoderLayer(embed_size, num_heads, ff_size) for _ in range(num_layers)
|
| 48 |
+
])
|
| 49 |
+
|
| 50 |
+
def forward(self, input_ids):
|
| 51 |
+
# Token embeddings
|
| 52 |
+
token_embeddings = self.embedding(input_ids)
|
| 53 |
+
|
| 54 |
+
# Add positional encoding
|
| 55 |
+
seq_len = input_ids.size(1)
|
| 56 |
+
embeddings_with_pos = token_embeddings + self.positional_encoding[:, :seq_len, :]
|
| 57 |
+
|
| 58 |
+
# Pass through the transformer layers
|
| 59 |
+
x = embeddings_with_pos.transpose(0, 1) # Transpose for multihead attention (seq_len, batch_size, embed_size)
|
| 60 |
+
for layer in self.layers:
|
| 61 |
+
x = layer(x)
|
| 62 |
+
|
| 63 |
+
return x.transpose(0, 1) # Transpose back to (batch_size, seq_len, embed_size)
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
class CustomTransformerClassifier(nn.Module):
|
| 67 |
+
def __init__(self, vocab_size, num_classes=2, embed_size=768, num_heads=8, num_layers=6, ff_size=2048, max_len=512):
|
| 68 |
+
super(CustomTransformerClassifier, self).__init__()
|
| 69 |
+
|
| 70 |
+
# Custom Transformer Encoder
|
| 71 |
+
self.encoder = CustomTransformerEncoder(vocab_size, embed_size, num_heads, num_layers, ff_size, max_len)
|
| 72 |
+
|
| 73 |
+
# Classification head
|
| 74 |
+
self.fc = nn.Linear(embed_size, num_classes)
|
| 75 |
+
|
| 76 |
+
# Dropout for regularization
|
| 77 |
+
self.dropout = nn.Dropout(0.3)
|
| 78 |
+
|
| 79 |
+
def forward(self, input_ids):
|
| 80 |
+
# Pass the input through the encoder
|
| 81 |
+
encoder_output = self.encoder(input_ids)
|
| 82 |
+
|
| 83 |
+
# Use the output of the [CLS] token (first token) for classification
|
| 84 |
+
cls_output = encoder_output[:, 0, :]
|
| 85 |
+
|
| 86 |
+
# Dropout for regularization
|
| 87 |
+
cls_output = self.dropout(cls_output)
|
| 88 |
+
|
| 89 |
+
# Final classification layer
|
| 90 |
+
logits = self.fc(cls_output)
|
| 91 |
+
|
| 92 |
+
return logits
|
| 93 |
+
# model = CustomTransformerClassifier(vocab_size=len(tokenizer), num_classes= len(label_encoder.classes_))
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
# Load tokenizer
|
| 97 |
+
tokenizer = AutoTokenizer.from_pretrained("bert-base-multilingual-cased")
|
| 98 |
+
|
| 99 |
+
# Load saved model
|
| 100 |
+
model = CustomTransformerClassifier(vocab_size=len(tokenizer), num_classes=3)
|
| 101 |
+
model.load_state_dict(torch.load("model.pth"))
|
| 102 |
+
model.eval()
|
| 103 |
+
|
| 104 |
+
def predict(text):
|
| 105 |
+
encoding = tokenizer(text, max_length=256, padding="max_length", truncation=True, return_tensors="pt")
|
| 106 |
+
input_ids = encoding["input_ids"]
|
| 107 |
+
|
| 108 |
+
with torch.no_grad():
|
| 109 |
+
logits = model(input_ids)
|
| 110 |
+
prediction_index = torch.argmax(logits, dim=1).item()
|
| 111 |
+
|
| 112 |
+
predicted_label = label_encoder.inverse_transform([prediction_index])[0]
|
| 113 |
+
return f"Predicted Label: {predicted_label}"
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
# Gradio UI
|
| 117 |
+
demo = gr.Interface(fn=predict, inputs=gr.Textbox(placeholder="Enter Nepali text"), outputs="text")
|
| 118 |
+
demo.launch()
|
label_encoder.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3826f7213efe9876f26275927bbf3cda44572a4bbbdf106ab0c4436c268d1596
|
| 3 |
+
size 483
|
model.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d0e05b3a344f29c574d6e623d2ce077563ff696aea968680ce791083fac4f573
|
| 3 |
+
size 501188690
|