File size: 3,010 Bytes
72fce14
 
 
dfe4a9e
6325706
dfe4a9e
6325706
 
dfe4a9e
6325706
dfe4a9e
72fce14
6325706
1985126
 
6325706
 
72fce14
6325706
72fce14
6325706
1985126
 
6325706
 
1985126
6325706
dfe4a9e
1985126
72fce14
6325706
72fce14
 
6325706
 
 
 
 
8988fb4
6325706
dfe4a9e
72fce14
6325706
 
72fce14
 
986d689
72fce14
6325706
1985126
dfe4a9e
6325706
 
 
 
 
dfe4a9e
 
 
72fce14
6325706
dfe4a9e
1985126
6325706
 
dfe4a9e
6325706
 
 
 
72fce14
6325706
dfe4a9e
6325706
dfe4a9e
6325706
 
 
 
dfe4a9e
6325706
 
 
 
 
 
1985126
 
6325706
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import streamlit as st
from transformers import pipeline
import torch

# ---- 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("Understand your emotions better with AI-powered predictions!")

# ---- Function to Load Emotion Analysis Model ----
@st.cache_resource
def load_emotion_model():
    try:
        st.info("⏳ Loading the emotion analysis model, please wait...")
        # Using a publicly available model
        emotion_analyzer = pipeline(
            "text-classification",
            model="bhadresh-savani/distilbert-base-uncased-emotion",
            device=0 if torch.cuda.is_available() else -1,  # Use GPU if available
        )
        st.success("βœ… Model loaded successfully!")
        return emotion_analyzer
    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 using πŸ€— Transformers**  
    Designed for a fun and intuitive experience! 🌟  
    """
)