Krishna Velama
commited on
Commit
•
286abe0
1
Parent(s):
bfbf553
first move
Browse files- .gitignore +2 -0
- app.py +170 -0
- mentalhealth-roberta-base_nemotron_model/config.json +36 -0
- mentalhealth-roberta-base_nemotron_model/merges.txt +0 -0
- mentalhealth-roberta-base_nemotron_model/model.safetensors +3 -0
- mentalhealth-roberta-base_nemotron_model/special_tokens_map.json +51 -0
- mentalhealth-roberta-base_nemotron_model/tokenizer_config.json +56 -0
- mentalhealth-roberta-base_nemotron_model/vocab.json +0 -0
- requirements.txt +7 -0
.gitignore
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
.env
|
2 |
+
prompt.txt
|
app.py
ADDED
@@ -0,0 +1,170 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import re
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
import torch
|
5 |
+
from transformers import RobertaForSequenceClassification, RobertaTokenizerFast, pipeline as text_pipeline
|
6 |
+
import gradio as gr
|
7 |
+
from openai import OpenAI
|
8 |
+
|
9 |
+
# Load environment variables from .env file
|
10 |
+
load_dotenv()
|
11 |
+
|
12 |
+
# Get API key from environment
|
13 |
+
API_KEY = os.getenv("API_KEY")
|
14 |
+
|
15 |
+
# Initialize OpenAI client
|
16 |
+
client = OpenAI(
|
17 |
+
base_url="https://integrate.api.nvidia.com/v1",
|
18 |
+
api_key=API_KEY
|
19 |
+
)
|
20 |
+
|
21 |
+
# Load classification model
|
22 |
+
def load_emotion_model(model_path):
|
23 |
+
model = RobertaForSequenceClassification.from_pretrained(model_path)
|
24 |
+
tokenizer = RobertaTokenizerFast.from_pretrained(model_path)
|
25 |
+
return model, tokenizer
|
26 |
+
|
27 |
+
# Map prediction to readable labels
|
28 |
+
def map_to_labels(label):
|
29 |
+
return "Happy/Positive Mindset" if label.lower() == "positive" else "Depressed/Negative Mindset"
|
30 |
+
|
31 |
+
# Classify mental state based on user input
|
32 |
+
def classify_emotion(user_input, model, tokenizer, device):
|
33 |
+
nlp = text_pipeline("text-classification", model=model, tokenizer=tokenizer, device=device)
|
34 |
+
result = nlp(user_input)
|
35 |
+
return map_to_labels(result[0]['label'])
|
36 |
+
|
37 |
+
# Analyze emotion using the LLM
|
38 |
+
def emotion_analysis(user_input):
|
39 |
+
|
40 |
+
# # Validate input
|
41 |
+
# if not user_input.strip(): # Check for empty or blank input
|
42 |
+
# progress_callback("Please provide valid input before submitting.", False)
|
43 |
+
# return "No input provided.", ""
|
44 |
+
|
45 |
+
# Load model
|
46 |
+
model_path = "mentalhealth-roberta-base_nemotron_model" # Replace with your model path
|
47 |
+
model, tokenizer = load_emotion_model(model_path)
|
48 |
+
device = 0 if torch.cuda.is_available() else -1
|
49 |
+
|
50 |
+
# Step 1: Classify emotion
|
51 |
+
predicted_emotion = classify_emotion(user_input, model, tokenizer, device)
|
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. Strictly respond only to questions or input related to mental health or emotional well-being. For unrelated input, reply with: "Not a valid question."
|
63 |
+
- Example: If the user asks about weather, sports, or other unrelated topics, respond with: "Not a valid question."
|
64 |
+
2. Use the **User Input** as the primary source for determining the emotional state, while considering the **Model Output** ("happy" or "depressed") only as a secondary reference.
|
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 for the assigned stage:
|
73 |
+
- If the user is **depressed**, suggest stage-specific remedies to improve their emotional state.
|
74 |
+
- If the user is **happy**, suggest strategies to maintain or enhance their happiness.
|
75 |
+
5. Prioritize clarity, empathy, and practicality in your analysis and suggestions.
|
76 |
+
|
77 |
+
[Response Rules]:
|
78 |
+
- Do NOT attempt to provide an output if the input is not related to mental health.
|
79 |
+
- Always analyze the user’s input independently, even if it conflicts with the model’s predicted output.
|
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.2,
|
93 |
+
top_p=0.7,
|
94 |
+
max_tokens=512,
|
95 |
+
stream=True
|
96 |
+
)
|
97 |
+
|
98 |
+
# Iterate over the streaming response
|
99 |
+
response = ""
|
100 |
+
for chunk in completion:
|
101 |
+
if chunk.choices[0].delta.content is not None:
|
102 |
+
print(chunk.choices[0].delta.content, end="")
|
103 |
+
# response = chunk.choices[0].delta.content
|
104 |
+
response_chunk = chunk.choices[0].delta.content
|
105 |
+
response += response_chunk
|
106 |
+
else:
|
107 |
+
print(f"Unexpected chunk format: {chunk}")
|
108 |
+
|
109 |
+
except Exception as e:
|
110 |
+
response = f"An error occurred while processing the response: {e}"
|
111 |
+
response= str(response).replace("*", '')
|
112 |
+
return response
|
113 |
+
|
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":
|
120 |
+
feelings = feelings + " with Negative Mindset"
|
121 |
+
else:
|
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
|
129 |
+
pattern = r"(Suggested Remedies|Suggested Remedies/Strategies|Suggested Strategies):.*"
|
130 |
+
match = re.search(pattern, analysis_text, re.DOTALL)
|
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
|
141 |
+
return "Please provide valid input before submitting.", "Not Applicable", "Not Applicable"
|
142 |
+
else:
|
143 |
+
response = emotion_analysis(user_input)
|
144 |
+
return extract_analysis_details(response)
|
145 |
+
|
146 |
+
|
147 |
+
# Gradio interface
|
148 |
+
iface = gr.Interface(
|
149 |
+
fn=validate_and_run,
|
150 |
+
inputs=gr.Textbox(#lines=2,
|
151 |
+
label="How are you feeling today?",
|
152 |
+
placeholder="Share your thoughts here...!"),
|
153 |
+
outputs=[
|
154 |
+
# gr.Textbox(label="Analysing Your State of Mind...."),
|
155 |
+
# gr.Textbox(label="Providing Best Strategies")
|
156 |
+
# gr.Textbox(label="Original"),
|
157 |
+
gr.Textbox(label="Feelings"),
|
158 |
+
gr.Textbox(label="Emotional Stage"),
|
159 |
+
gr.Textbox(label="Providing Best Strategies")
|
160 |
+
],
|
161 |
+
# live=True,
|
162 |
+
title="Analyze your emotions and generate stage-specific psychological insights\n",
|
163 |
+
# title = "Emotion Analysis and Dynamic Response Generator"
|
164 |
+
# description="Analyze your emotions and receive dynamic psychological insights."
|
165 |
+
)
|
166 |
+
|
167 |
+
# Launch the app
|
168 |
+
if __name__ == "__main__":
|
169 |
+
iface.launch()
|
170 |
+
|
mentalhealth-roberta-base_nemotron_model/config.json
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_name_or_path": "roberta-base",
|
3 |
+
"architectures": [
|
4 |
+
"RobertaForSequenceClassification"
|
5 |
+
],
|
6 |
+
"attention_probs_dropout_prob": 0.1,
|
7 |
+
"bos_token_id": 0,
|
8 |
+
"classifier_dropout": null,
|
9 |
+
"eos_token_id": 2,
|
10 |
+
"hidden_act": "gelu",
|
11 |
+
"hidden_dropout_prob": 0.1,
|
12 |
+
"hidden_size": 768,
|
13 |
+
"id2label": {
|
14 |
+
"0": "negative",
|
15 |
+
"1": "positive"
|
16 |
+
},
|
17 |
+
"initializer_range": 0.02,
|
18 |
+
"intermediate_size": 3072,
|
19 |
+
"label2id": {
|
20 |
+
"negative": 0,
|
21 |
+
"positive": 1
|
22 |
+
},
|
23 |
+
"layer_norm_eps": 1e-05,
|
24 |
+
"max_position_embeddings": 514,
|
25 |
+
"model_type": "roberta",
|
26 |
+
"num_attention_heads": 12,
|
27 |
+
"num_hidden_layers": 12,
|
28 |
+
"pad_token_id": 1,
|
29 |
+
"position_embedding_type": "absolute",
|
30 |
+
"problem_type": "single_label_classification",
|
31 |
+
"torch_dtype": "float32",
|
32 |
+
"transformers_version": "4.46.2",
|
33 |
+
"type_vocab_size": 1,
|
34 |
+
"use_cache": true,
|
35 |
+
"vocab_size": 50265
|
36 |
+
}
|
mentalhealth-roberta-base_nemotron_model/merges.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|
mentalhealth-roberta-base_nemotron_model/model.safetensors
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:20f6612a4917ba6e0b7d9b062ec628cd77b29b61a9053e6ba875041adccfcb82
|
3 |
+
size 498612824
|
mentalhealth-roberta-base_nemotron_model/special_tokens_map.json
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"bos_token": {
|
3 |
+
"content": "<s>",
|
4 |
+
"lstrip": false,
|
5 |
+
"normalized": true,
|
6 |
+
"rstrip": false,
|
7 |
+
"single_word": false
|
8 |
+
},
|
9 |
+
"cls_token": {
|
10 |
+
"content": "<s>",
|
11 |
+
"lstrip": false,
|
12 |
+
"normalized": true,
|
13 |
+
"rstrip": false,
|
14 |
+
"single_word": false
|
15 |
+
},
|
16 |
+
"eos_token": {
|
17 |
+
"content": "</s>",
|
18 |
+
"lstrip": false,
|
19 |
+
"normalized": true,
|
20 |
+
"rstrip": false,
|
21 |
+
"single_word": false
|
22 |
+
},
|
23 |
+
"mask_token": {
|
24 |
+
"content": "<mask>",
|
25 |
+
"lstrip": true,
|
26 |
+
"normalized": false,
|
27 |
+
"rstrip": false,
|
28 |
+
"single_word": false
|
29 |
+
},
|
30 |
+
"pad_token": {
|
31 |
+
"content": "<pad>",
|
32 |
+
"lstrip": false,
|
33 |
+
"normalized": true,
|
34 |
+
"rstrip": false,
|
35 |
+
"single_word": false
|
36 |
+
},
|
37 |
+
"sep_token": {
|
38 |
+
"content": "</s>",
|
39 |
+
"lstrip": false,
|
40 |
+
"normalized": true,
|
41 |
+
"rstrip": false,
|
42 |
+
"single_word": false
|
43 |
+
},
|
44 |
+
"unk_token": {
|
45 |
+
"content": "<unk>",
|
46 |
+
"lstrip": false,
|
47 |
+
"normalized": true,
|
48 |
+
"rstrip": false,
|
49 |
+
"single_word": false
|
50 |
+
}
|
51 |
+
}
|
mentalhealth-roberta-base_nemotron_model/tokenizer_config.json
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"add_prefix_space": false,
|
3 |
+
"added_tokens_decoder": {
|
4 |
+
"0": {
|
5 |
+
"content": "<s>",
|
6 |
+
"lstrip": false,
|
7 |
+
"normalized": true,
|
8 |
+
"rstrip": false,
|
9 |
+
"single_word": false,
|
10 |
+
"special": true
|
11 |
+
},
|
12 |
+
"1": {
|
13 |
+
"content": "<pad>",
|
14 |
+
"lstrip": false,
|
15 |
+
"normalized": true,
|
16 |
+
"rstrip": false,
|
17 |
+
"single_word": false,
|
18 |
+
"special": true
|
19 |
+
},
|
20 |
+
"2": {
|
21 |
+
"content": "</s>",
|
22 |
+
"lstrip": false,
|
23 |
+
"normalized": true,
|
24 |
+
"rstrip": false,
|
25 |
+
"single_word": false,
|
26 |
+
"special": true
|
27 |
+
},
|
28 |
+
"3": {
|
29 |
+
"content": "<unk>",
|
30 |
+
"lstrip": false,
|
31 |
+
"normalized": true,
|
32 |
+
"rstrip": false,
|
33 |
+
"single_word": false,
|
34 |
+
"special": true
|
35 |
+
},
|
36 |
+
"50264": {
|
37 |
+
"content": "<mask>",
|
38 |
+
"lstrip": true,
|
39 |
+
"normalized": false,
|
40 |
+
"rstrip": false,
|
41 |
+
"single_word": false,
|
42 |
+
"special": true
|
43 |
+
}
|
44 |
+
},
|
45 |
+
"bos_token": "<s>",
|
46 |
+
"clean_up_tokenization_spaces": false,
|
47 |
+
"cls_token": "<s>",
|
48 |
+
"eos_token": "</s>",
|
49 |
+
"errors": "replace",
|
50 |
+
"mask_token": "<mask>",
|
51 |
+
"model_max_length": 512,
|
52 |
+
"pad_token": "<pad>",
|
53 |
+
"sep_token": "</s>",
|
54 |
+
"tokenizer_class": "RobertaTokenizer",
|
55 |
+
"unk_token": "<unk>"
|
56 |
+
}
|
mentalhealth-roberta-base_nemotron_model/vocab.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
python-dotenv
|
2 |
+
gradio
|
3 |
+
transformers
|
4 |
+
torch
|
5 |
+
openai
|
6 |
+
|
7 |
+
|