Text Classification
Keras
English
chemistry
scientific-literature
multilabel
classification
embeddings
Eval Results (legacy)
Instructions to use Bocklitz-Lab/lit2vec-subfield-classifier-model with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Keras
How to use Bocklitz-Lab/lit2vec-subfield-classifier-model with Keras:
# !pip install -U keras tensorflow huggingface_hub # Keras needs TensorFlow installed to read "hf://" paths, so the tensorflow backend is selected here; # "jax" and "torch" also work for computation once TensorFlow is installed. import os os.environ["KERAS_BACKEND"] = "tensorflow" import keras model = keras.saving.load_model("hf://Bocklitz-Lab/lit2vec-subfield-classifier-model") - Notebooks
- Google Colab
- Kaggle
Mahmoud Amiri commited on
Commit ·
f964a65
1
Parent(s): baff529
update readme file
Browse files
README.md
CHANGED
|
@@ -170,34 +170,62 @@ If included in the repo, the plot `f1_vs_freq.png` shows F1 vs. training label f
|
|
| 170 |
### Quick start (load from Hub, inference)
|
| 171 |
|
| 172 |
```python
|
|
|
|
|
|
|
| 173 |
import numpy as np
|
|
|
|
| 174 |
from huggingface_hub import hf_hub_download
|
| 175 |
from tensorflow import keras
|
| 176 |
-
import
|
| 177 |
|
| 178 |
REPO_ID = "Bocklitz-Lab/lit2vec-subfield-classifier-model"
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
-
#
|
| 181 |
model_path = hf_hub_download(REPO_ID, filename="mlp_model.h5")
|
| 182 |
label_map_path = hf_hub_download(REPO_ID, filename="label_mapping.json")
|
| 183 |
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
-
probs = model.predict(x, verbose=0)[0]
|
| 197 |
-
threshold = 0.5
|
| 198 |
-
pred_ids = [i for i, p in enumerate(probs) if p > threshold]
|
| 199 |
-
pred_labels = [index_to_label[i] for i in pred_ids]
|
| 200 |
-
print(list(zip(pred_labels, probs[pred_ids])))
|
| 201 |
```
|
| 202 |
|
| 203 |
### Batch inference
|
|
|
|
| 170 |
### Quick start (load from Hub, inference)
|
| 171 |
|
| 172 |
```python
|
| 173 |
+
# pip install sentence-transformers huggingface_hub tensorflow==2.* # if needed
|
| 174 |
+
import json
|
| 175 |
import numpy as np
|
| 176 |
+
from typing import List, Tuple
|
| 177 |
from huggingface_hub import hf_hub_download
|
| 178 |
from tensorflow import keras
|
| 179 |
+
from sentence_transformers import SentenceTransformer
|
| 180 |
|
| 181 |
REPO_ID = "Bocklitz-Lab/lit2vec-subfield-classifier-model"
|
| 182 |
+
EMBED_MODEL = "intfloat/e5-large-v2" # must match what you used to train!
|
| 183 |
+
TEXT_PREFIX = {"abstract": "abstract: ", "summary": "summary: "} # keep consistent with your pipeline
|
| 184 |
+
THRESHOLD = 0.5 # decision threshold for multilabel
|
| 185 |
|
| 186 |
+
# ----- Load model + label mapping -----
|
| 187 |
model_path = hf_hub_download(REPO_ID, filename="mlp_model.h5")
|
| 188 |
label_map_path = hf_hub_download(REPO_ID, filename="label_mapping.json")
|
| 189 |
|
| 190 |
+
with open(label_map_path, "r", encoding="utf-8") as f:
|
| 191 |
+
mapping = json.load(f)
|
| 192 |
+
index_to_label = {int(k): v for k, v in mapping["index_to_label"].items()}
|
| 193 |
+
|
| 194 |
+
model = keras.models.load_model(model_path, compile=False) # inference only
|
| 195 |
+
encoder = SentenceTransformer(EMBED_MODEL)
|
| 196 |
+
|
| 197 |
+
def encode_text(text: str, text_type: str = "summary") -> np.ndarray:
|
| 198 |
+
"""
|
| 199 |
+
Encode text into a normalized embedding compatible with the classifier.
|
| 200 |
+
text_type: "summary" or "abstract" (affects prefix)
|
| 201 |
+
"""
|
| 202 |
+
prefix = TEXT_PREFIX.get(text_type, "")
|
| 203 |
+
emb = encoder.encode([prefix + text], normalize_embeddings=True) # shape: (1, D)
|
| 204 |
+
return emb.astype("float32")
|
| 205 |
+
|
| 206 |
+
def predict_labels_from_text(text: str, text_type: str = "summary", threshold: float = THRESHOLD
|
| 207 |
+
) -> Tuple[List[int], List[str], np.ndarray]:
|
| 208 |
+
"""
|
| 209 |
+
Returns (predicted_ids, predicted_labels, probabilities)
|
| 210 |
+
"""
|
| 211 |
+
x = encode_text(text, text_type=text_type) # (1, D)
|
| 212 |
+
probs = model.predict(x, verbose=0)[0] # (18,)
|
| 213 |
+
pred_ids = [i for i, p in enumerate(probs) if p > threshold]
|
| 214 |
+
pred_labels = [index_to_label[i] for i in pred_ids]
|
| 215 |
+
return pred_ids, pred_labels, probs
|
| 216 |
+
|
| 217 |
+
# ----- Example -----
|
| 218 |
+
if __name__ == "__main__":
|
| 219 |
+
sample_text = (
|
| 220 |
+
"We investigate Pt-based nanoparticle catalysts for hydrogen evolution, "
|
| 221 |
+
"demonstrating improved activity via ligand tuning and support engineering."
|
| 222 |
+
)
|
| 223 |
+
ids, labels, probs = predict_labels_from_text(sample_text, text_type="abstract", threshold=0.5)
|
| 224 |
+
print("Predicted IDs:", ids)
|
| 225 |
+
print("Predicted Labels:", labels)
|
| 226 |
+
print("Top scores:", sorted(((index_to_label[i], float(p)) for i, p in enumerate(probs)),
|
| 227 |
+
key=lambda x: x[1], reverse=True)[:5])
|
| 228 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
```
|
| 230 |
|
| 231 |
### Batch inference
|