Spaces:
Sleeping
Sleeping
import streamlit as st | |
from transformers import pipeline | |
import torch | |
import time | |
# ---- Page Configuration ---- | |
st.set_page_config( | |
page_title="Emotion Prediction App", | |
page_icon="π€", | |
layout="centered", | |
initial_sidebar_state="expanded", | |
) | |
# ---- App Title ---- | |
st.title("πΊ Emotion Prediction App π") | |
st.subheader("Aloha! Enter your thoughts and let me predict your emotions. π§ π‘") | |
# ---- Background Information ---- | |
st.markdown( | |
""" | |
Welcome to the Emotion Prediction App! | |
This tool uses a state-of-the-art natural language processing (NLP) model to analyze your responses and predict your emotions. | |
Perfect for everyone in Hawaii or anywhere looking for a simple, fun way to understand feelings better! π΄β¨ | |
""" | |
) | |
# ---- Function to Load Emotion Analysis Model ---- | |
def load_emotion_model(): | |
try: | |
st.info("β³ Loading the emotion analysis model, please wait...") | |
# Using a public model for emotion classification | |
model = pipeline( | |
"text-classification", | |
model="bhadresh-savani/distilbert-base-uncased-emotion", | |
device=0 if torch.cuda.is_available() else -1, # Automatically use GPU if available | |
) | |
st.success("β Model loaded successfully!") | |
return model | |
except Exception as e: | |
st.error(f"β οΈ Error loading model: {e}") | |
return None | |
# ---- Load the Model ---- | |
emotion_analyzer = load_emotion_model() | |
# ---- Function for Predicting Emotion ---- | |
def predict_emotion(text): | |
if emotion_analyzer is None: | |
return {"Error": "Emotion analyzer model not initialized. Please reload the app."} | |
try: | |
# Analyze emotions | |
result = emotion_analyzer([text]) | |
return {res["label"]: round(res["score"], 4) for res in result} | |
except Exception as e: | |
return {"Error": f"Prediction failed: {e}"} | |
# ---- User Input Section ---- | |
st.write("### π Let's Get Started!") | |
questions = [ | |
"How are you feeling today?", | |
"Describe your mood in a few words.", | |
"What was the most significant emotion you felt this week?", | |
"How do you handle stress or challenges?", | |
"What motivates you the most right now?", | |
] | |
responses = {} | |
# ---- Ask Questions and Analyze Responses ---- | |
for i, question in enumerate(questions, start=1): | |
st.write(f"#### π§ Question {i}: {question}") | |
user_response = st.text_input(f"Your answer to Q{i}:", key=f"q{i}") | |
if user_response: | |
with st.spinner("Analyzing emotion... π"): | |
analysis = predict_emotion(user_response) | |
responses[question] = {"Response": user_response, "Analysis": analysis} | |
st.success(f"π― Emotion Analysis: {analysis}") | |
# ---- Display Results ---- | |
if st.button("Submit Responses"): | |
st.write("### π Emotion Analysis Results") | |
if responses: | |
for i, (question, details) in enumerate(responses.items(), start=1): | |
st.write(f"#### Question {i}: {question}") | |
st.write(f"**Your Response:** {details['Response']}") | |
st.write(f"**Emotion Analysis:** {details['Analysis']}") | |
else: | |
st.warning("Please answer at least one question before submitting!") | |
# ---- Footer ---- | |
st.markdown( | |
""" | |
--- | |
**Developed with π€ Transformers by OpenAI** | |
Designed for an intuitive and aesthetic experience. πΊ | |
""" | |
) | |