[Python] Sentiment analysis model return identical output for any input

Discussão em 'Python' iniciado por Stack, Março 12, 2025.

  1. Stack

    Stack Membro Participativo

    So I made this sentiment analysis model, and it works just fine in the training-testing script. I built a simple interface using streamlit for my saved model, but it always returned identical scores for any input text. Plus, it somehow returns so many scores when it should only return a single score for a single input.

    here is my code:

    import streamlit as st
    import tensorflow as tf
    from tensorflow.keras.preprocessing.text import Tokenizer
    from tensorflow.keras.preprocessing.sequence import pad_sequences
    import numpy as np
    import pickle

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

    @st.cache(allow_output_mutation=True)
    def load_model():
    model = tf.keras.models.load_model('C:/Users/k/Downloads/test/model_final.h5')
    return model

    if __name__ == '__main__':

    model = load_model()
    st.title('Analisis Sentimen')

    txt = st.text_input('masukkan teks')

    if not txt:
    st.warning("masukkan teks sebelum lanjut")
    st.stop()
    else:
    text = txt
    text = tokenizer.texts_to_sequences(text)
    text = pad_sequences(text)
    prediction = model.predict(text)
    st.title('sentimen: ')
    if (prediction > 0.5).any():
    st.write(prediction)
    st.write('positif')
    else:
    st.write(prediction)
    st.write('negatif')


    here are some snaps when i try it with streamlit [​IMG]

    [​IMG]

    Continue reading...

Compartilhe esta Página