Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added emotion detection feature based on text input #349

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions Emotion Detection using Text/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import streamlit as st

import pandas as pd
import numpy as np
import altair as alt

import joblib

pipe_lr = joblib.load(open("model/text_emotion.pkl", "rb"))

emotions_emoji_dict = {"anger": "😠", "disgust": "🤮", "fear": "😨😱", "happy": "🤗", "joy": "😂", "neutral": "😐", "sad": "😔",
"sadness": "😔", "shame": "😳", "surprise": "😮"}


def predict_emotions(docx):
results = pipe_lr.predict([docx])
return results[0]


def get_prediction_proba(docx):
results = pipe_lr.predict_proba([docx])
return results


def main():
st.title("Text Emotion Detection")
st.subheader("Detect Emotions In Text")

with st.form(key='my_form'):
raw_text = st.text_area("Type Here")
submit_text = st.form_submit_button(label='Submit')

if submit_text:
col1, col2 = st.columns(2)

prediction = predict_emotions(raw_text)
probability = get_prediction_proba(raw_text)

with col1:
st.success("Original Text")
st.write(raw_text)

st.success("Prediction")
emoji_icon = emotions_emoji_dict[prediction]
st.write("{}:{}".format(prediction, emoji_icon))
st.write("Confidence:{}".format(np.max(probability)))

with col2:
st.success("Prediction Probability")
#st.write(probability)
proba_df = pd.DataFrame(probability, columns=pipe_lr.classes_)
#st.write(proba_df.T)
proba_df_clean = proba_df.T.reset_index()
proba_df_clean.columns = ["emotions", "probability"]

fig = alt.Chart(proba_df_clean).mark_bar().encode(x='emotions', y='probability', color='emotions')
st.altair_chart(fig, use_container_width=True)






if __name__ == '__main__':
main()
Loading