Instructions to use pollitoconpapass/intent_classification_model with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Keras
How to use pollitoconpapass/intent_classification_model with Keras:
# Available backend options are: "jax", "torch", "tensorflow". import os os.environ["KERAS_BACKEND"] = "jax" import keras model = keras.saving.load_model("hf://pollitoconpapass/intent_classification_model") - Notebooks
- Google Colab
- Kaggle
Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Model to detect Chat Intention
|
| 2 |
+
|
| 3 |
+
This model was trained for academic purposes to detect the intention of the user while chatting with a bot
|
| 4 |
+
|
| 5 |
+
## Classes
|
| 6 |
+
As part of a college project about a HealthCare Org Chatbot the classes are:
|
| 7 |
+
- 0: Normal Conversation
|
| 8 |
+
- 1: Patient Information
|
| 9 |
+
- 2: Administrative Questions
|
| 10 |
+
|
| 11 |
+
IMPORTANT: The model was trained with Spanish Sentences
|
| 12 |
+
|
| 13 |
+
## Accuracy
|
| 14 |
+
We ended up with a 0.85 percent of accuracy.
|
| 15 |
+
|
| 16 |
+
```sh
|
| 17 |
+
Classification Report:
|
| 18 |
+
precision recall f1-score support
|
| 19 |
+
|
| 20 |
+
Normal conversation 0.87 0.82 0.85 40
|
| 21 |
+
Patient information 0.83 0.85 0.84 40
|
| 22 |
+
Administrative questions 0.85 0.88 0.86 40
|
| 23 |
+
|
| 24 |
+
accuracy 0.85 120
|
| 25 |
+
macro avg 0.85 0.85 0.85 120
|
| 26 |
+
weighted avg 0.85 0.85 0.85 120
|
| 27 |
+
|
| 28 |
+
```
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
## How to use it?
|
| 32 |
+
Use the following script:
|
| 33 |
+
```py
|
| 34 |
+
|
| 35 |
+
import json
|
| 36 |
+
import numpy as np
|
| 37 |
+
import tensorflow as tf
|
| 38 |
+
from huggingface_hub import hf_hub_download
|
| 39 |
+
from tensorflow.keras.preprocessing.text import tokenizer_from_json
|
| 40 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
| 41 |
+
|
| 42 |
+
repo_id = "pollitoconpapass/intent_classification_model"
|
| 43 |
+
tokenizer_path = hf_hub_download(repo_id=repo_id, filename="tokenizer.json")
|
| 44 |
+
|
| 45 |
+
# with open(tokenizer_path, 'r', encoding='utf-8') as f:
|
| 46 |
+
# loaded_tokenizer_config = json.load(f)
|
| 47 |
+
# loaded_tokenizer = tokenizer_from_json(loaded_tokenizer_config)
|
| 48 |
+
|
| 49 |
+
with open(tokenizer_path, 'r', encoding='utf-8') as f:
|
| 50 |
+
loaded_tokenizer_config = json.load(f)
|
| 51 |
+
loaded_max_len = loaded_tokenizer_config['config']['max_len']
|
| 52 |
+
|
| 53 |
+
del loaded_tokenizer_config['config']['max_len']
|
| 54 |
+
loaded_tokenizer = tokenizer_from_json(json.dumps(loaded_tokenizer_config))
|
| 55 |
+
|
| 56 |
+
model_file_path = hf_hub_download(repo_id=repo_id, filename="intent_classification_model.keras")
|
| 57 |
+
loaded_model = tf.keras.models.load_model(model_file_path)
|
| 58 |
+
|
| 59 |
+
INTENT_MAP = {
|
| 60 |
+
0: "Normal conversation",
|
| 61 |
+
1: "Patient information",
|
| 62 |
+
2: "Administrative questions"
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
def predict_single_sentence(sentence, max_len) -> tuple[str, float]:
|
| 66 |
+
# Preprocess the whole sentence
|
| 67 |
+
sequence = loaded_tokenizer.texts_to_sequences([sentence])
|
| 68 |
+
# Use the loaded_max_len for padding
|
| 69 |
+
padded_sequence = pad_sequences(sequence, maxlen=loaded_max_len, padding='post')
|
| 70 |
+
|
| 71 |
+
prediction = loaded_model.predict(padded_sequence, verbose=0)[0] # -> get 1st prediction
|
| 72 |
+
|
| 73 |
+
# Prediction + confidence
|
| 74 |
+
predicted_class = np.argmax(prediction)
|
| 75 |
+
confidence = prediction[predicted_class] * 100
|
| 76 |
+
|
| 77 |
+
intent = INTENT_MAP[predicted_class]
|
| 78 |
+
return intent, confidence
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
sentence = "Holaaaa"
|
| 82 |
+
intent, confidence = predict_single_sentence(sentence, 10)
|
| 83 |
+
print(f"Intent: {intent} (Confidence: {confidence:.2f}%)")
|
| 84 |
+
```
|