tarrasyed19472007 commited on
Commit
eb7d7a3
1 Parent(s): 1b12be2

Create emotion-app.py

Browse files
Files changed (1) hide show
  1. emotion-app.py +86 -0
emotion-app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import necessary libraries
2
+ import streamlit as st
3
+ from transformers import pipeline
4
+ import torch
5
+ from datasets import load_dataset
6
+
7
+ # Load the T5-based Emotion Classifier model
8
+ @st.cache_resource
9
+ def load_model():
10
+ try:
11
+ st.write("Loading the emotion analysis model...")
12
+ emotion_analyzer = pipeline("text-classification", model="suryakiran786/T5-emotion")
13
+ st.write("Model loaded successfully!")
14
+ return emotion_analyzer
15
+ except Exception as e:
16
+ st.write(f"Error loading the model: {e}")
17
+ return None
18
+
19
+ # Initialize the model (with caching to prevent reloads)
20
+ emotion_analyzer = load_model()
21
+
22
+ # Load the dataset if needed for any additional logic
23
+ @st.cache_data
24
+ def load_data():
25
+ try:
26
+ # For demonstration purposes, let's load a sentiment analysis dataset from Hugging Face
27
+ dataset = load_dataset("glue", "sst2")
28
+ st.write("Dataset loaded successfully!")
29
+ return dataset
30
+ except Exception as e:
31
+ st.write(f"Error loading dataset: {e}")
32
+ return None
33
+
34
+ # Load data (just to show usage, not used in emotion analysis directly)
35
+ dataset = load_data()
36
+
37
+ # Function to predict emotion for a single response
38
+ def predict_emotion_single(response):
39
+ if not emotion_analyzer:
40
+ return {"Error": "Emotion analyzer model not initialized. Please check model loading."}
41
+ try:
42
+ response = response.strip()
43
+ result = emotion_analyzer([response])
44
+ return {res["label"]: round(res["score"], 4) for res in result}
45
+ except Exception as e:
46
+ return {"Error": str(e)}
47
+
48
+ # Streamlit App Layout
49
+ st.title("Behavior Prediction App")
50
+ st.write("Enter your thoughts or feelings, and let the app predict your emotional states.")
51
+
52
+ # Define questions for the user
53
+ questions = [
54
+ "How are you feeling today?",
55
+ "Describe your mood in a few words.",
56
+ "What was the most significant emotion you felt this week?",
57
+ "How do you handle stress or challenges?",
58
+ "What motivates you the most right now?"
59
+ ]
60
+
61
+ # Initialize a dictionary to store responses
62
+ responses = {}
63
+
64
+ # Ask each question and get response
65
+ for i, question in enumerate(questions, start=1):
66
+ user_response = st.text_input(f"Question {i}: {question}")
67
+ if user_response:
68
+ analysis = predict_emotion_single(user_response)
69
+ responses[question] = (user_response, analysis)
70
+ st.write(f"**Your Response**: {user_response}")
71
+ st.write(f"**Emotion Analysis**: {analysis}")
72
+
73
+ # Provide button to clear input fields
74
+ if st.button("Clear Responses"):
75
+ st.experimental_rerun()
76
+
77
+ # Display results once all responses are filled
78
+ if st.button("Submit Responses"):
79
+ if responses:
80
+ st.write("-- Emotion Analysis Results ---")
81
+ for i, (question, (response, analysis)) in enumerate(responses.items(), start=1):
82
+ st.write(f"\n**Question {i}:** {question}")
83
+ st.write(f"Your Response: {response}")
84
+ st.write(f"Emotion Analysis: {analysis}")
85
+ else:
86
+ st.write("Please answer all the questions before submitting.")