updated app.py code
Browse files- App/app.py +12 -109
App/app.py
CHANGED
|
@@ -70,120 +70,23 @@ def load_models():
|
|
| 70 |
return None
|
| 71 |
return models
|
| 72 |
|
| 73 |
-
# Load
|
| 74 |
-
|
| 75 |
|
| 76 |
-
# --- UI and Prediction Functions ---
|
| 77 |
-
|
| 78 |
-
# Dictionary for mapping emotions to emojis
|
| 79 |
-
emotions_emoji_dict = {
|
| 80 |
-
"anger": "😠", "disgust": "🤮", "fear": "😨", "joy": "😂",
|
| 81 |
-
"neutral": "😐", "sadness": "😔", "shame": "😳", "surprise": "😮"
|
| 82 |
-
}
|
| 83 |
-
|
| 84 |
-
def predict_emotions(docx):
|
| 85 |
-
"""
|
| 86 |
-
Predicts the emotion of a single text string using the loaded BERT model.
|
| 87 |
-
"""
|
| 88 |
-
if model is None or tokenizer is None:
|
| 89 |
-
return "Model not loaded"
|
| 90 |
-
|
| 91 |
-
inputs = tokenizer(docx, return_tensors="pt", padding=True, truncation=True, max_length=128)
|
| 92 |
-
|
| 93 |
-
with torch.no_grad():
|
| 94 |
-
logits = model(**inputs).logits
|
| 95 |
-
|
| 96 |
-
predicted_class_id = torch.argmax(logits, dim=1).item()
|
| 97 |
-
return model.config.id2label[predicted_class_id]
|
| 98 |
-
|
| 99 |
-
def get_prediction_proba(docx):
|
| 100 |
-
"""
|
| 101 |
-
Gets the prediction probabilities for each emotion for a single text string.
|
| 102 |
-
"""
|
| 103 |
-
if model is None or tokenizer is None:
|
| 104 |
-
return np.array([])
|
| 105 |
-
|
| 106 |
-
inputs = tokenizer(docx, return_tensors="pt", padding=True, truncation=True, max_length=128)
|
| 107 |
-
|
| 108 |
-
with torch.no_grad():
|
| 109 |
-
logits = model(**inputs).logits
|
| 110 |
-
|
| 111 |
-
probabilities = torch.nn.functional.softmax(logits, dim=1)
|
| 112 |
-
return probabilities.numpy()
|
| 113 |
-
|
| 114 |
-
# --- Main Application ---
|
| 115 |
|
| 116 |
def main():
|
| 117 |
-
|
| 118 |
-
The main function that runs the Streamlit application.
|
| 119 |
-
"""
|
| 120 |
-
# --- KEY CHANGE: Reverted to the older 'use_column_width' argument ---
|
| 121 |
-
st.image('https://i.ibb.co/rsbYCsN/senttext-low-resolution-logo-white-on-black-background.png', use_column_width=True)
|
| 122 |
-
# --- End of Change ---
|
| 123 |
|
| 124 |
-
st.title("SentText Emotion Analyzer")
|
| 125 |
-
st.subheader("Analyze the emotional tone of your text with a fine-tuned BERT model.")
|
| 126 |
-
|
| 127 |
with st.sidebar:
|
| 128 |
choice = option_menu(
|
| 129 |
-
"Menu", ["
|
| 130 |
-
icons=['
|
| 131 |
-
menu_icon="cast",
|
| 132 |
-
default_index=0
|
| 133 |
)
|
| 134 |
|
| 135 |
-
if choice == "
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
if submit_text:
|
| 142 |
-
if raw_text.strip() == "":
|
| 143 |
-
st.warning("Please enter some text to analyze.")
|
| 144 |
-
else:
|
| 145 |
-
col1, col2 = st.columns(2)
|
| 146 |
-
|
| 147 |
-
prediction = predict_emotions(raw_text)
|
| 148 |
-
probability = get_prediction_proba(raw_text)
|
| 149 |
-
|
| 150 |
-
with col1:
|
| 151 |
-
st.success("Original Text")
|
| 152 |
-
st.write(raw_text)
|
| 153 |
-
|
| 154 |
-
st.success("Prediction Probability")
|
| 155 |
-
labels = list(model.config.id2label.values())
|
| 156 |
-
proba_df = pd.DataFrame(probability, columns=labels)
|
| 157 |
-
st.write(proba_df.T.rename(columns={0: 'Probability'}))
|
| 158 |
-
|
| 159 |
-
with col2:
|
| 160 |
-
st.success("Prediction")
|
| 161 |
-
emoji_icon = emotions_emoji_dict.get(prediction, "🙂")
|
| 162 |
-
st.metric(label="Predicted Emotion", value=f"{prediction.capitalize()} {emoji_icon}")
|
| 163 |
-
st.metric(label="Confidence", value=f"{np.max(probability):.4f}")
|
| 164 |
-
|
| 165 |
-
proba_df_clean = proba_df.T.reset_index()
|
| 166 |
-
proba_df_clean.columns = ["emotions", "probability"]
|
| 167 |
-
|
| 168 |
-
fig = alt.Chart(proba_df_clean).mark_bar().encode(
|
| 169 |
-
x=alt.X('emotions', sort=None),
|
| 170 |
-
y='probability',
|
| 171 |
-
color='emotions',
|
| 172 |
-
tooltip=['emotions', 'probability']
|
| 173 |
-
).properties(
|
| 174 |
-
title="Emotion Probabilities"
|
| 175 |
-
)
|
| 176 |
-
st.altair_chart(fig, use_container_width=True)
|
| 177 |
-
else:
|
| 178 |
-
st.info("The model is not available. Please check the error messages above.")
|
| 179 |
-
|
| 180 |
-
else: # About page
|
| 181 |
-
st.header("About SentText")
|
| 182 |
-
st.markdown("""
|
| 183 |
-
This application provides nuanced emotional analysis that goes beyond simple positive or negative sentiment. By classifying text into granular categories such as joy, anger, sadness, and fear - SentText offers a deeper understanding of the underlying tone and intent of a message.
|
| 184 |
-
|
| 185 |
-
The analysis is powered by a DistilBERT model, a streamlined and efficient version of the powerful BERT language model. This model has been specifically fine-tuned on a large dataset of emotional texts to accurately identify and differentiate between various human emotions. The result is a tool that provides fast, reliable, and insightful analysis of textual communication.
|
| 186 |
-
""")
|
| 187 |
-
|
| 188 |
-
if __name__ == '__main__':
|
| 189 |
-
main()
|
|
|
|
| 70 |
return None
|
| 71 |
return models
|
| 72 |
|
| 73 |
+
# Load all models at startup
|
| 74 |
+
models = load_models()
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
def main():
|
| 78 |
+
st.sidebar.image('https://i.ibb.co/rsbYCsN/senttext-low-resolution-logo-white-on-black-background.png', use_column_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
|
|
|
|
|
|
|
|
|
| 80 |
with st.sidebar:
|
| 81 |
choice = option_menu(
|
| 82 |
+
"Menu", ["Emotion Analyzer", "Financial Analysis", "About"],
|
| 83 |
+
icons=['emoji-smile', 'bank', 'info-circle'],
|
| 84 |
+
menu_icon="cast", default_index=0
|
|
|
|
| 85 |
)
|
| 86 |
|
| 87 |
+
if choice == "Emotion Analyzer":
|
| 88 |
+
render_emotion_analyzer()
|
| 89 |
+
elif choice == "Financial Analysis":
|
| 90 |
+
render_financial_analyzer()
|
| 91 |
+
elif choice == "About":
|
| 92 |
+
render_about_page()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|