Skip to content

Commit

Permalink
Merge pull request #349 from Saniya-BZ/main
Browse files Browse the repository at this point in the history
Added emotion detection feature based on text input
  • Loading branch information
TAHIR0110 authored Aug 8, 2024
2 parents df5d502 + dc6accd commit 3212cc1
Show file tree
Hide file tree
Showing 4 changed files with 35,573 additions and 0 deletions.
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

0 comments on commit 3212cc1

Please sign in to comment.