Krishna Velama
commited on
Commit
·
bc42bce
1
Parent(s):
286abe0
emotional stage modification in re
Browse files
app.py
CHANGED
@@ -52,44 +52,46 @@ def emotion_analysis(user_input):
|
|
52 |
|
53 |
# Step 2: Generate LLM response
|
54 |
prompt = f"""
|
55 |
-
Task: You are a social psychologist specializing in Roy Baumeister's six-stage theory of emotional progression. Your task is to analyze emotional states based on user input while adhering to specific response boundaries.
|
56 |
|
57 |
[Input Information]:
|
58 |
**User Input**: "{user_input}"
|
59 |
**Model Output**: "{predicted_emotion}"
|
60 |
|
61 |
Specifics:
|
62 |
-
1.
|
63 |
-
|
64 |
-
|
65 |
-
3. Assign the user’s emotional state to one of Roy Baumeister’s six stages of emotional progression:
|
66 |
- Stage 1: Falling short of expectations
|
67 |
- Stage 2: Attributions to self
|
68 |
- Stage 3: High self-awareness
|
69 |
- Stage 4: Negative affect
|
70 |
- Stage 5: Cognitive deconstruction
|
71 |
- Stage 6: Disinhibition
|
72 |
-
4. Provide specific recommendations
|
73 |
-
- If
|
74 |
-
- If
|
75 |
-
5.
|
76 |
|
77 |
[Response Rules]:
|
78 |
-
-
|
79 |
-
- Always analyze
|
80 |
|
81 |
-
[Desired Output Format]:
|
82 |
Emotional Analysis:
|
83 |
I'd say you're feeling: <Happy/Depressed>
|
84 |
Emotional Stage: <Stage and brief reasoning>
|
85 |
Suggested Remedies/Strategies: <Practical advice based on the stage>
|
|
|
|
|
|
|
86 |
"""
|
87 |
|
88 |
try:
|
89 |
completion = client.chat.completions.create(
|
90 |
model="nvidia/nemotron-4-340b-instruct",
|
91 |
messages=[{"role": "user", "content": prompt}],
|
92 |
-
temperature=0.
|
93 |
top_p=0.7,
|
94 |
max_tokens=512,
|
95 |
stream=True
|
@@ -114,6 +116,7 @@ def emotion_analysis(user_input):
|
|
114 |
def extract_analysis_details(analysis_text):
|
115 |
feelings_match = re.search(r"I'd say you're feeling:\s*([^\n]+)", analysis_text)
|
116 |
feelings = feelings_match.group(1).strip() if feelings_match else "Not Found"
|
|
|
117 |
if feelings.lower() == "happy":
|
118 |
feelings = feelings + " with Positive Mindset"
|
119 |
elif feelings.lower() == "depressed":
|
@@ -122,7 +125,9 @@ def extract_analysis_details(analysis_text):
|
|
122 |
feelings
|
123 |
|
124 |
# Extract emotional stage
|
125 |
-
stage_match = re.search(r"Emotional Stage:\s*([^\n.]+)", analysis_text)
|
|
|
|
|
126 |
emotional_stage = stage_match.group(1).strip() if stage_match else "Not Found"
|
127 |
|
128 |
# Regex to match the section header and capture from there to the end
|
@@ -131,10 +136,11 @@ def extract_analysis_details(analysis_text):
|
|
131 |
suggestions = match.group(0).strip() if match else "No matching section found."
|
132 |
# print(suggestions)
|
133 |
|
134 |
-
if feelings == "Not Found":
|
135 |
feelings = "Not a valid question."
|
136 |
return feelings, emotional_stage, suggestions
|
137 |
|
|
|
138 |
# Gradio interface with input validation
|
139 |
def validate_and_run(user_input):
|
140 |
if not user_input.strip(): # Check if the input is empty or just spaces
|
|
|
52 |
|
53 |
# Step 2: Generate LLM response
|
54 |
prompt = f"""
|
55 |
+
Task: You are a social psychologist specializing in Roy Baumeister's six-stage theory of emotional progression. Your task is to analyze emotional states based on user input while adhering strictly to specific response boundaries.
|
56 |
|
57 |
[Input Information]:
|
58 |
**User Input**: "{user_input}"
|
59 |
**Model Output**: "{predicted_emotion}"
|
60 |
|
61 |
Specifics:
|
62 |
+
1. Respond **only** to questions or input related to mental health or emotional well-being. For unrelated input, reply strictly with: "Not a valid question."
|
63 |
+
2. Use the **User Input** as the primary source for determining the emotional state, while considering the **Model Output** ("happy" or "depressed") as a secondary reference.
|
64 |
+
3. Categorize the user’s emotional state into one of Roy Baumeister’s six stages of emotional progression:
|
|
|
65 |
- Stage 1: Falling short of expectations
|
66 |
- Stage 2: Attributions to self
|
67 |
- Stage 3: High self-awareness
|
68 |
- Stage 4: Negative affect
|
69 |
- Stage 5: Cognitive deconstruction
|
70 |
- Stage 6: Disinhibition
|
71 |
+
4. Provide specific stage-based recommendations:
|
72 |
+
- If **depressed**, suggest remedies for improving emotional state.
|
73 |
+
- If **happy**, suggest strategies for maintaining or enhancing happiness.
|
74 |
+
5. Maintain clarity, empathy, and practicality in all analyses and suggestions.
|
75 |
|
76 |
[Response Rules]:
|
77 |
+
- If input is unrelated to mental health, reply only with: "Not a valid question."
|
78 |
+
- Always analyze user input independently, even if it conflicts with the model's predicted output.
|
79 |
|
80 |
+
[Desired Output Format for valid input]:
|
81 |
Emotional Analysis:
|
82 |
I'd say you're feeling: <Happy/Depressed>
|
83 |
Emotional Stage: <Stage and brief reasoning>
|
84 |
Suggested Remedies/Strategies: <Practical advice based on the stage>
|
85 |
+
|
86 |
+
For invalid input:
|
87 |
+
"Not a valid question."
|
88 |
"""
|
89 |
|
90 |
try:
|
91 |
completion = client.chat.completions.create(
|
92 |
model="nvidia/nemotron-4-340b-instruct",
|
93 |
messages=[{"role": "user", "content": prompt}],
|
94 |
+
temperature=0.5,
|
95 |
top_p=0.7,
|
96 |
max_tokens=512,
|
97 |
stream=True
|
|
|
116 |
def extract_analysis_details(analysis_text):
|
117 |
feelings_match = re.search(r"I'd say you're feeling:\s*([^\n]+)", analysis_text)
|
118 |
feelings = feelings_match.group(1).strip() if feelings_match else "Not Found"
|
119 |
+
print(feelings)
|
120 |
if feelings.lower() == "happy":
|
121 |
feelings = feelings + " with Positive Mindset"
|
122 |
elif feelings.lower() == "depressed":
|
|
|
125 |
feelings
|
126 |
|
127 |
# Extract emotional stage
|
128 |
+
# stage_match = re.search(r"Emotional Stage:\s*([^\n.]+)", analysis_text)
|
129 |
+
# print(stage_match)
|
130 |
+
stage_match = re.search(r"Emotional Stage:\s*(.*?)(?=\n[A-Z])", analysis_text, re.DOTALL)
|
131 |
emotional_stage = stage_match.group(1).strip() if stage_match else "Not Found"
|
132 |
|
133 |
# Regex to match the section header and capture from there to the end
|
|
|
136 |
suggestions = match.group(0).strip() if match else "No matching section found."
|
137 |
# print(suggestions)
|
138 |
|
139 |
+
if feelings == "Not Found" or feelings == "Not a valid question.":
|
140 |
feelings = "Not a valid question."
|
141 |
return feelings, emotional_stage, suggestions
|
142 |
|
143 |
+
|
144 |
# Gradio interface with input validation
|
145 |
def validate_and_run(user_input):
|
146 |
if not user_input.strip(): # Check if the input is empty or just spaces
|