CoolFace
Apppublic

ssdaimari44/bodoBOT

sourceHugging Faceapache-2.0updated 1y agoView on Hugging Face
0likes
model_utils.py42 linesDownload Raw Back to root
1# model_utils.py2import numpy as np3import random4import pickle5from tensorflow.keras.models import load_model6from tensorflow.keras.preprocessing.sequence import pad_sequences7import json8 9# Load model, tokenizer, and label encoder10model = load_model("bodo_bot_model.h5")11with open("tokenizer.pkl", "rb") as f:12    tokenizer = pickle.load(f)13with open("label_encoder.pkl", "rb") as f:14    le = pickle.load(f)15with open("data.json", "r", encoding="utf-8") as f:16    data = json.load(f)17 18# Prepare responses dictionary19responses = {intent["tag"]: intent["responses"] for intent in data["intents"]}20 21 22def preprocess_input(text):23    """Preprocess the input text for prediction."""24    sequence = tokenizer.texts_to_sequences([text])  # No lower(), no strip()25    padded = pad_sequences(sequence, maxlen=model.input_shape[1])26    print(f"Tokenized: {sequence}")27    print(f"Padded: {padded}")28    return padded29 30 31def predict_intent(text):32    """Make a prediction and return the response."""33    padded_input = preprocess_input(text)34    prediction = model.predict(padded_input, batch_size=1)35    predicted_class = np.argmax(prediction, axis=1)36    intent = le.inverse_transform(predicted_class)37    response = random.choice(responses.get(intent[0], ["निमाहा हो, आं बेखौ बुजियाखै।"]))38 39    print(f"User message: {text}")40    print(f"Predicted tag: {intent[0]}")41    return response42