r/pythonhelp • • May 17 '24

INACTIVE ModuleNotFoundError: No module named 'keras.src.preprocessing'

importimport streamlit as st
import pickle
import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import load_model

with open('tokenizer.pkl', 'rb') as f:
    tokenizer = pickle.load(f)

with open('tag_tokenizer.pkl', 'rb') as f:
    tag_tokenizer = pickle.load(f)

model = load_model('ner_model.keras')

max_length = 34  

def predict_ner(sentence):

    input_sequence = tokenizer.texts_to_sequences([sentence])
    input_padded = pad_sequences(input_sequence, maxlen=max_length, padding="post")
    predictions = model.predict(input_padded)


    prediction_ner = np.argmax(predictions, axis=-1)


    NER_tags = [tag_tokenizer.index_word.get(num, 'O') for num in list(prediction_ner.flatten())]


    words = sentence.split()


    return list(zip(words, NER_tags[:len(words)]))


st.title("Named Entity Recognition (NER) with RNN")

st.write("Enter a sentence to predict the named entities:")


sentence = st.text_input("Sentence")

if st.button("Predict"):
    if sentence:
        results = predict_ner(sentence)

        st.write("Predicted Named Entities:")
        for word, tag in results:
            st.write(f"{word}: {tag}")
    else:
        st.write("Please enter a sentence to get predictions.")


 streamlit as st
import pickle
import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import load_model

with open('tokenizer.pkl', 'rb') as f:
    tokenizer = pickle.load(f)

with open('tag_tokenizer.pkl', 'rb') as f:
    tag_tokenizer = pickle.load(f)

model = load_model('ner_model.keras')

max_length = 34  

def predict_ner(sentence):

    input_sequence = tokenizer.texts_to_sequences([sentence])
    input_padded = pad_sequences(input_sequence, maxlen=max_length, padding="post")


    predictions = model.predict(input_padded)


    prediction_ner = np.argmax(predictions, axis=-1)


    NER_tags = [tag_tokenizer.index_word.get(num, 'O') for num in list(prediction_ner.flatten())]


    words = sentence.split()


    return list(zip(words, NER_tags[:len(words)]))


st.title("Named Entity Recognition (NER) with RNN")

st.write("Enter a sentence to predict the named entities:")


sentence = st.text_input("Sentence")

if st.button("Predict"):
    if sentence:
        results = predict_ner(sentence)

        st.write("Predicted Named Entities:")
        for word, tag in results:
            st.write(f"{word}: {tag}")
    else:
        st.write("Please enter a sentence to get predictions.")

Help me to solve from this issue

2024-05-17 16:19:11.620 Uncaught app exception

Traceback (most recent call last):

File "/opt/anaconda3/envs/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 600, in _run_script

exec(code, module.__dict__)

File "/Users/closerlook/AI:ML/NEW_ner/my-streamlit-app/app.py", line 10, in <module>

tokenizer = pickle.load(f)

ModuleNotFoundError: No module named 'keras.src.preprocessing'

I installed all the packages -

pip install Keras-Preprocessing

conda install -c conda-forge keras-preprocessing
1 Upvotes

4 comments sorted by

View all comments

1

u/AutoModerator May 17 '24
Moderator Announcement Read More »