File size: 2,766 Bytes
df947d2 79e3204 df947d2 79e3204 df947d2 79e3204 df947d2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | import joblib
import pandas as pd
import streamlit as st
import altair as alt
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.sequence import pad_sequences
# Load the model
model = load_model('src/model/text_emotions_model.keras')
# Load the tokenizer
tokenizer = joblib.load('src/model/tokenizer.pkl')
# Load the encoder
encoder = joblib.load('src/model/encoder.pkl')
# Streamlit app
st.title("Text Emotions Classification")
st.write("Predict the emotions of a text.")
st.image("https://t4.ftcdn.net/jpg/16/58/09/95/360_F_1658099569_2DVa2bX9QN14KmF4c00wmPjIWH6RNDCH.jpg")
# Emoji mapping for classes
EMOJI_BY_CLASS = {
"anger": "π ",
"fear": "π¨",
"joy": "π",
"love": "β€οΈ",
"sadness": "π’",
"surprise": "π²",
}
# Color mapping for classes
EMOTION_COLORS = {
"anger": "#e74c3c",
"fear": "#8e44ad",
"joy": "#f1c40f",
"love": "#e84393",
"sadness": "#3498db",
"surprise": "#2ecc71",
}
# Input text
text = st.text_input("Enter a text")
# Predict emotion probabilities
if text:
sequences = tokenizer.texts_to_sequences([text])
padded_sequences = pad_sequences(sequences, maxlen=66)
prediction = model.predict(padded_sequences, verbose=0)
probabilities = prediction[0]
class_names = list(encoder.classes_)
# Sort emotions by probability descending
sorted_pairs = sorted(zip(class_names, probabilities), key=lambda x: x[1], reverse=True)
# Top prediction highlight
top_class, top_prob = sorted_pairs[0]
top_emoji = EMOJI_BY_CLASS.get(top_class, "πΉ")
st.markdown(f"### {top_emoji} Top emotion: **{top_class}** β {top_prob * 100:.2f}%")
st.subheader("Emotion probabilities")
display_names = [f"{EMOJI_BY_CLASS.get(name, 'πΉ')} {name}" for name, _ in sorted_pairs]
df = pd.DataFrame({
"Class": [name for name, _ in sorted_pairs],
"Emotion": display_names,
"Probability (%)": [round(p * 100, 2) for _, p in sorted_pairs],
})
st.dataframe(df, width='stretch')
# Optional visualization with fixed colors and sorted order
df_sorted = df.sort_values(by="Probability (%)", ascending=False)
color_domain = list(EMOTION_COLORS.keys())
color_range = list(EMOTION_COLORS.values())
chart = (
alt.Chart(df_sorted)
.mark_bar()
.encode(
x=alt.X("Probability (%)", type="quantitative"),
y=alt.Y("Emotion", type="nominal", sort=df_sorted["Emotion"].tolist()),
color=alt.Color("Class", scale=alt.Scale(domain=color_domain, range=color_range), legend=None),
tooltip=["Emotion", "Probability (%)"]
)
.properties(height=400)
)
st.altair_chart(chart, use_container_width=True) |