pollitoconpapass/intent_classification_model
04
1---2language:3- es4metrics:5- accuracy6library_name: keras7tags:8- code9---10# Model to detect Chat Intention11 12This model was trained for academic purposes to detect the intention of the user while chatting with a bot13 14## Classes15As part of a college project about a HealthCare Org Chatbot the classes are: 16- 0: Normal Conversation17- 1: Patient Information18- 2: Administrative Questions19 20IMPORTANT: The model was trained with Spanish Sentences21 22## Accuracy23We ended up with a 0.85 percent of accuracy. 24 25```sh26Classification Report:27 precision recall f1-score support28 29 Normal conversation 0.87 0.82 0.85 4030 Patient information 0.83 0.85 0.84 4031Administrative questions 0.85 0.88 0.86 4032 33 accuracy 0.85 12034 macro avg 0.85 0.85 0.85 12035 weighted avg 0.85 0.85 0.85 12036 37```38 39 40## How to use it? 41Use the following script: 42```py43 44import json45import numpy as np46import tensorflow as tf47from huggingface_hub import hf_hub_download48from tensorflow.keras.preprocessing.text import tokenizer_from_json49from tensorflow.keras.preprocessing.sequence import pad_sequences50 51repo_id = "pollitoconpapass/intent_classification_model"52tokenizer_path = hf_hub_download(repo_id=repo_id, filename="tokenizer.json")53 54# with open(tokenizer_path, 'r', encoding='utf-8') as f:55# loaded_tokenizer_config = json.load(f)56# loaded_tokenizer = tokenizer_from_json(loaded_tokenizer_config)57 58with open(tokenizer_path, 'r', encoding='utf-8') as f:59 loaded_tokenizer_config = json.load(f)60 loaded_max_len = loaded_tokenizer_config['config']['max_len']61 62 del loaded_tokenizer_config['config']['max_len']63 loaded_tokenizer = tokenizer_from_json(json.dumps(loaded_tokenizer_config))64 65model_file_path = hf_hub_download(repo_id=repo_id, filename="intent_classification_model.keras")66loaded_model = tf.keras.models.load_model(model_file_path)67 68INTENT_MAP = {69 0: "Normal conversation",70 1: "Patient information",71 2: "Administrative questions"72}73 74def predict_single_sentence(sentence, max_len) -> tuple[str, float]:75 # Preprocess the whole sentence76 sequence = loaded_tokenizer.texts_to_sequences([sentence])77 # Use the loaded_max_len for padding78 padded_sequence = pad_sequences(sequence, maxlen=loaded_max_len, padding='post')79 80 prediction = loaded_model.predict(padded_sequence, verbose=0)[0] # -> get 1st prediction81 82 # Prediction + confidence83 predicted_class = np.argmax(prediction)84 confidence = prediction[predicted_class] * 10085 86 intent = INTENT_MAP[predicted_class]87 return intent, confidence88 89 90sentence = "Holaaaa"91intent, confidence = predict_single_sentence(sentence, 10)92print(f"Intent: {intent} (Confidence: {confidence:.2f}%)")93```