Spaces:
Runtime error
Runtime error
import streamlit as st | |
import pandas as pd | |
import numpy as np | |
import altair as alt | |
import joblib | |
import webbrowser | |
pipe_lr = joblib.load(open("scri.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("How was your Day Dude ? ๐ค ") | |
st.subheader("Detect Emotions In Text and Recommend Movies") | |
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) | |
emotion = prediction | |
youtube_link = f"https://www.youtube.com/results?search_query={emotion}+movie" | |
st.info(f"Recommended movies for {emotion}:") | |
st.write(youtube_link) | |
if st.button("Open YouTube"): | |
webbrowser.open(youtube_link) | |
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") | |
proba_df = pd.DataFrame(probability, columns=pipe_lr.classes_) | |
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() | |