Upload 3 files
Browse files- README.md +61 -13
- app.py +72 -54
- requirements.txt +4 -1
README.md
CHANGED
@@ -1,13 +1,61 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Mental Health Sentiment Analysis API
|
2 |
+
|
3 |
+
This Hugging Face Space provides an API endpoint for mental health-related sentiment analysis using the RoBERTa model fine-tuned on emotion detection.
|
4 |
+
|
5 |
+
## Model Details
|
6 |
+
- Base Model: RoBERTa
|
7 |
+
- Fine-tuned Version: SamLowe/roberta-base-go_emotions
|
8 |
+
- Task: Multi-label emotion classification
|
9 |
+
- Input: Text
|
10 |
+
- Output: Emotion probabilities and processed mental health context
|
11 |
+
|
12 |
+
## API Usage
|
13 |
+
|
14 |
+
### Using Python
|
15 |
+
```python
|
16 |
+
import requests
|
17 |
+
|
18 |
+
API_URL = "https://your-space-name.hf.space/api/predict"
|
19 |
+
response = requests.post(API_URL, json={"inputs": "your text here"})
|
20 |
+
print(response.json())
|
21 |
+
```
|
22 |
+
|
23 |
+
### Using JavaScript/Node.js
|
24 |
+
```javascript
|
25 |
+
const response = await fetch("https://your-space-name.hf.space/api/predict", {
|
26 |
+
method: "POST",
|
27 |
+
headers: { "Content-Type": "application/json" },
|
28 |
+
body: JSON.stringify({ inputs: "your text here" })
|
29 |
+
});
|
30 |
+
const result = await response.json();
|
31 |
+
```
|
32 |
+
|
33 |
+
## Response Format
|
34 |
+
```json
|
35 |
+
{
|
36 |
+
"emotions": {
|
37 |
+
"joy": 0.8,
|
38 |
+
"sadness": 0.1,
|
39 |
+
"anger": 0.05,
|
40 |
+
// ... other emotions
|
41 |
+
},
|
42 |
+
"primaryEmotion": "joy",
|
43 |
+
"emotionalState": {
|
44 |
+
"state": "joy",
|
45 |
+
"intensity": "High",
|
46 |
+
"needsAttention": false,
|
47 |
+
"description": "Detected joy with 80% confidence"
|
48 |
+
},
|
49 |
+
"success": true,
|
50 |
+
"needsAttention": false
|
51 |
+
}
|
52 |
+
```
|
53 |
+
|
54 |
+
## Error Handling
|
55 |
+
If there's an error, the API will return:
|
56 |
+
```json
|
57 |
+
{
|
58 |
+
"error": "Error message here",
|
59 |
+
"success": false
|
60 |
+
}
|
61 |
+
```
|
app.py
CHANGED
@@ -1,64 +1,82 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
""
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
def respond(
|
11 |
-
message,
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
-
|
20 |
-
for val in history:
|
21 |
-
if val[0]:
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
-
|
26 |
-
messages.append({"role": "user", "content": message})
|
27 |
-
|
28 |
-
response = ""
|
29 |
-
|
30 |
-
for message in client.chat_completion(
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
],
|
|
|
60 |
)
|
61 |
|
62 |
-
|
63 |
if __name__ == "__main__":
|
64 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import numpy as np
|
4 |
|
5 |
+
# Initialize the sentiment classifier
|
6 |
+
classifier = pipeline(
|
7 |
+
"text-classification",
|
8 |
+
model="SamLowe/roberta-base-go_emotions",
|
9 |
+
top_k=None # Return all emotions
|
10 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
def process_emotions(emotions):
|
13 |
+
"""Convert raw emotions to our application's format"""
|
14 |
+
# Convert to dictionary format
|
15 |
+
emotion_dict = {item['label']: float(item['score']) for item in emotions}
|
16 |
+
|
17 |
+
# Find primary emotion
|
18 |
+
primary_emotion = max(emotion_dict.items(), key=lambda x: x[1])
|
19 |
+
|
20 |
+
# Calculate intensity
|
21 |
+
max_score = primary_emotion[1]
|
22 |
+
intensity = 'High' if max_score > 0.66 else 'Medium' if max_score > 0.33 else 'Low'
|
23 |
+
|
24 |
+
# Check if needs attention
|
25 |
+
needs_attention = (
|
26 |
+
primary_emotion[0] in ['anger', 'anxiety', 'depression']
|
27 |
+
and max_score > 0.5
|
28 |
+
)
|
29 |
+
|
30 |
+
return {
|
31 |
+
'emotions': emotion_dict,
|
32 |
+
'primaryEmotion': primary_emotion[0],
|
33 |
+
'emotionalState': {
|
34 |
+
'state': primary_emotion[0],
|
35 |
+
'intensity': intensity,
|
36 |
+
'needsAttention': needs_attention,
|
37 |
+
'description': f"Detected {primary_emotion[0]} with {round(max_score * 100)}% confidence"
|
38 |
+
},
|
39 |
+
'success': True,
|
40 |
+
'needsAttention': needs_attention
|
41 |
+
}
|
42 |
|
43 |
+
def analyze_text(text):
|
44 |
+
"""Analyze text and return processed emotions"""
|
45 |
+
if not text or not text.strip():
|
46 |
+
return {
|
47 |
+
'error': 'Please provide some text to analyze',
|
48 |
+
'success': False
|
49 |
+
}
|
50 |
+
|
51 |
+
try:
|
52 |
+
# Get raw emotions from model
|
53 |
+
emotions = classifier(text)
|
54 |
+
|
55 |
+
# Process emotions into our format
|
56 |
+
result = process_emotions(emotions[0])
|
57 |
+
|
58 |
+
return result
|
59 |
+
except Exception as e:
|
60 |
+
return {
|
61 |
+
'error': str(e),
|
62 |
+
'success': False
|
63 |
+
}
|
64 |
|
65 |
+
# Create Gradio interface
|
66 |
+
demo = gr.Interface(
|
67 |
+
fn=analyze_text,
|
68 |
+
inputs=gr.Textbox(label="Enter text to analyze", lines=3),
|
69 |
+
outputs=gr.JSON(label="Sentiment Analysis Results"),
|
70 |
+
title="Mental Health Sentiment Analysis",
|
71 |
+
description="Analyzes text for emotions related to mental health using the RoBERTa model.",
|
72 |
+
examples=[
|
73 |
+
["I'm feeling really anxious about my upcoming presentation"],
|
74 |
+
["Today was a great day, I accomplished all my goals!"],
|
75 |
+
["I've been feeling down and unmotivated lately"],
|
|
|
|
|
|
|
|
|
|
|
76 |
],
|
77 |
+
allow_flagging="never"
|
78 |
)
|
79 |
|
80 |
+
# Launch the app
|
81 |
if __name__ == "__main__":
|
82 |
demo.launch()
|
requirements.txt
CHANGED
@@ -1 +1,4 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
1 |
+
transformers==4.36.2
|
2 |
+
torch==2.1.2
|
3 |
+
gradio==4.12.0
|
4 |
+
numpy==1.26.3
|