AnkitGeotek/Emotion_recognition
0
1#--- Important Libraries2import streamlit as st3from pathlib import Path4import numpy as np5import tensorflow as tf6from transformers import AutoTokenizer7from transformers import TFAutoModelForSequenceClassification8 9current_dir =Path(__file__).parent if "__file__" in locals() else Path.cwd() #cwd =current working directory10css_file =current_dir / "styles" / "main.css"11 12st.set_page_config(13 page_title="Text2emotion",14 page_icon="๐"15)16 17#--- Load CSS18# with open(css_file) as f:19# st.markdown("<style>{}</style>".format(f.read()), unsafe_allow_html=True)20 21#--- Loading Tokenizer and Model22model_path =r'saved/results/model'23loaded_tokenizer = AutoTokenizer.from_pretrained(model_path)24loaded_model = TFAutoModelForSequenceClassification.from_pretrained(model_path)25 26st.title("Emotions Predictor")27 28st.markdown(29 """30 <script>31 document.addEventListener('DOMContentLoaded', function() {32 document.querySelector(".css-1lajcqt").focus(); // You might need to adjust this selector33 });34 </script>35 """,36 unsafe_allow_html=True37)38 39 40st.subheader("Please enter the text:") 41input_text = [st.text_input("")]42submit= st.button("Predict emotion")43 44if submit:45 test_encodings = loaded_tokenizer(input_text, truncation = True, padding = True )46 47 test_dataset = tf.data.Dataset.from_tensor_slices((48 dict(test_encodings) 49 ))50 51 y_pred = loaded_model.predict(test_dataset)[0]52 pridicted_emotion = tf.argmax(y_pred, axis = 1).numpy()[0]53 answer= "The emotion of this comment is:- "+['sadness ๐', 'joy ๐', 'love ๐ฅฐ', 'anger ๐ก', 'fear ๐จ', 'surprise ๐ฎ'][pridicted_emotion]54 st.subheader(answer)55 56else:57 st.subheader("The emotion of this comment is:- _______") 58 59 60st.write("#")61st.subheader("Example text:")62st.write('''63 - India is a beautiful country!64 - I hate people who do not fulfil their promise.65 66 ''')67 68 69st.subheader(70 '''71 Note:- 72 - Prediction Emotions of User through his comment on your Social Media Post73 - Emotion it can predict are sadness, joy, love, anger, fear and surprise74 '''75 )76st.write("author: ankitgeotek@gmail.com")