Files changed (1) hide show
  1. README.md +93 -0
README.md ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Model Description
2
+
3
+ This model is based on Mistral 7B and has been fine-tuned for emotion recognition and empathetic conversational support within mental health contexts. It is derived from the research presented in the paper "The Emotional Spectrum of LLMs: Leveraging Empathy and Emotion-Based Markers for Mental Health Support". The model’s architecture and fine-tuning details follow the methodology outlined in that publication—specifically, leveraging next-token prediction for emotion labeling, progressive construction of a user emotional profile throughout conversation, and interpretable emotional embeddings for preliminary mental health screening.aclanthology+4​
4
+ Reference
5
+ For full details see:
6
+ De Grandi, Ravenda et al. (2025). "The Emotional Spectrum of LLMs: Leveraging Empathy and Emotion-Based Markers for Mental Health Support."
7
+
8
+ # Usage Example: Emotional Profile Extraction
9
+
10
+ Suppose you have a list of sentences and want to compute the aggregated emotional profile (distribution of emotions predicted over the set):
11
+
12
+ ## Example Python Code
13
+
14
+ ```{python}
15
+ import torch
16
+ import transformers
17
+ from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
18
+
19
+ model_name = 'DeGra/RACLETTE-v0.2'
20
+
21
+ bnb_config = BitsAndBytesConfig(
22
+ load_in_4bit=True,
23
+ bnb_4bit_quant_type="nf4",
24
+ bnb_4bit_compute_dtype=torch.float16,
25
+ )
26
+
27
+ model = AutoModelForCausalLM.from_pretrained(
28
+ model_name,
29
+ quantization_config=bnb_config,
30
+ trust_remote_code=True
31
+ )
32
+ model.config.use_cache = False
33
+
34
+ tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
35
+ tokenizer.pad_token = tokenizer.eos_token
36
+
37
+ generation_pipeline = transformers.pipeline(
38
+ "text-generation",
39
+ model=model,
40
+ tokenizer=tokenizer,
41
+ torch_dtype=torch.bfloat16,
42
+ trust_remote_code=True,
43
+ device_map="auto",
44
+ )
45
+
46
+ def filter_limit_chars(text, limit_chars, max_limited_chars=2, stop_at_max=False):
47
+ count, index = 0, [0]
48
+ for separator in limit_chars:
49
+ separator_count = text.count(separator)
50
+ count += separator_count
51
+ i = 0
52
+ for _ in range(separator_count):
53
+ i = text.find(separator, i)
54
+ index.append(i if not stop_at_max else i+len(separator))
55
+ i += len(separator)
56
+ index.sort()
57
+ index.append(len(text))
58
+ if count >= max_limited_chars:
59
+ text = text[0:index[max_limited_chars+1] if not stop_at_max else index[max_limited_chars]]
60
+ return text
61
+
62
+ def predict_emotion(prompt, num_return_emotions=10):
63
+ sequences = generation_pipeline(
64
+ prompt,
65
+ min_new_tokens=2,
66
+ max_new_tokens=5,
67
+ do_sample=True,
68
+ top_k=5,
69
+ num_return_sequences=num_return_emotions,
70
+ eos_token_id=tokenizer.eos_token_id,
71
+ )
72
+ emotions_count = {}
73
+ for seq in sequences:
74
+ emotion = seq['generated_text'][len(prompt):].strip()
75
+ emotion = emotion.split('<|assistant|>',1)[0].split('<|endoftext|>',1)[0]
76
+ emotion = filter_limit_chars(emotion, ['|','<','>',',','.'], 0, False).strip()
77
+ emotions_count[emotion] = emotions_count.get(emotion, 0) + 1
78
+ return emotions_count
79
+
80
+ # Example: Extract emotional profile from sentences
81
+ emotion_dict = {e: 0 for e in ["surprised","excited","angry","proud","sad","annoyed","grateful","lonely","afraid","terrified","guilty","impressed","disgusted","hopeful","confident","furious","anxious","anticipating","joyful","nostalgic","disappointed","prepared","jealous","content","devastated","embarrassed","caring","sentimental","trusting","ashamed","apprehensive","faithful"]}
82
+
83
+ sentences = ["I'm feeling really down lately.", "I don't know if I can handle this anymore.", "Today I got some good news!"]
84
+
85
+ for sent in sentences:
86
+ prompt = f'<|prompter|>{sent}<|endoftext|><|emotion|>'
87
+ emotions_count = predict_emotion(prompt)
88
+ for emotion, count in emotions_count.items():
89
+ if emotion in emotion_dict:
90
+ emotion_dict[emotion] += count
91
+
92
+ print(emotion_dict)
93
+ ```