Instructions to use kauzan25/pathora-bert-classifier with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Keras
How to use kauzan25/pathora-bert-classifier with Keras:
# Available backend options are: "jax", "torch", "tensorflow". import os os.environ["KERAS_BACKEND"] = "jax" import keras model = keras.saving.load_model("hf://kauzan25/pathora-bert-classifier") - Notebooks
- Google Colab
- Kaggle
Upload artifacts/inference_pathora.py with huggingface_hub
Browse files
artifacts/inference_pathora.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np, re, joblib, torch
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
from tensorflow import keras
|
| 4 |
+
from tensorflow.keras import backend as K
|
| 5 |
+
from transformers import BertTokenizer, BertModel
|
| 6 |
+
from sentence_transformers import SentenceTransformer
|
| 7 |
+
|
| 8 |
+
class FeatureAttention(keras.layers.Layer):
|
| 9 |
+
def __init__(self, **kw): super().__init__(**kw)
|
| 10 |
+
def build(self, s): self.W = self.add_weight(shape=(s[-1],), initializer="ones", trainable=True)
|
| 11 |
+
def call(self, x): return x * tf.nn.softmax(self.W)
|
| 12 |
+
|
| 13 |
+
@keras.saving.register_keras_serializable()
|
| 14 |
+
def focal_loss(g=2.0, a=0.5):
|
| 15 |
+
def fn(y, p):
|
| 16 |
+
y = tf.squeeze(tf.cast(y, tf.int32))
|
| 17 |
+
p = K.clip(p, K.epsilon(), 1-K.epsilon())
|
| 18 |
+
ce = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y, logits=tf.math.log(p+K.epsilon()))
|
| 19 |
+
idx = tf.stack([tf.range(tf.shape(p)[0], dtype=tf.int32), y], axis=-1)
|
| 20 |
+
return K.mean(a * K.pow(1-tf.gather_nd(p,idx), g) * ce)
|
| 21 |
+
return fn
|
| 22 |
+
|
| 23 |
+
class PathOraPredictor:
|
| 24 |
+
def __init__(self, model_path="pathora_model.keras", le_path="label_encoder.joblib"):
|
| 25 |
+
self.model = keras.models.load_model(model_path, custom_objects={"FeatureAttention":FeatureAttention,"loss_fn":focal_loss()})
|
| 26 |
+
self.le = joblib.load(le_path)
|
| 27 |
+
self.device = torch.device("cpu")
|
| 28 |
+
self.tokenizer = BertTokenizer.from_pretrained("bert-resume-classifier-final")
|
| 29 |
+
self.bert = BertModel.from_pretrained("bert-resume-classifier-final").to(self.device)
|
| 30 |
+
self.bert.eval()
|
| 31 |
+
self.st = SentenceTransformer("all-MiniLM-L6-v2")
|
| 32 |
+
|
| 33 |
+
def predict(self, text, top_n=5):
|
| 34 |
+
text = re.sub(r"<[^>]+>", " ", re.sub(r"\s+", " ", text)).strip()
|
| 35 |
+
enc = self.tokenizer(text, padding=True, truncation=True, max_length=128, return_tensors="pt")
|
| 36 |
+
with torch.no_grad():
|
| 37 |
+
out = self.bert(**enc)
|
| 38 |
+
bert_emb = out.last_hidden_state[:, 0, :].numpy()
|
| 39 |
+
st_emb = self.st.encode([text], convert_to_numpy=True)
|
| 40 |
+
emb = np.concatenate([bert_emb, st_emb], axis=1)
|
| 41 |
+
probs = self.model.predict(emb, verbose=0)[0]
|
| 42 |
+
top = np.argsort(probs)[::-1][:top_n]
|
| 43 |
+
return [(self.le.classes_[i], float(probs[i])) for i in top]
|
| 44 |
+
|
| 45 |
+
if __name__ == "__main__":
|
| 46 |
+
p = PathOraPredictor()
|
| 47 |
+
for s in ["Python developer, TensorFlow, AWS.", "Financial analyst, CFA, Excel.", "Nurse, ICU, BLS."]:
|
| 48 |
+
print("Text:", s)
|
| 49 |
+
for cat, conf in p.predict(s):
|
| 50 |
+
print(" {}: {:.1%}".format(cat, conf))
|
| 51 |
+
print()
|