-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #349 from Saniya-BZ/main
Added emotion detection feature based on text input
- Loading branch information
Showing
4 changed files
with
35,573 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Oops, something went wrong.