Text Classification
Transformers
Safetensors
English
emotion-classification
healthcare
distilbert
patient-doctor-conversations
clinical-AI
mental-health
Instructions to use StringJammer/patient-emotion-classifier with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use StringJammer/patient-emotion-classifier with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="StringJammer/patient-emotion-classifier")# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("StringJammer/patient-emotion-classifier", dtype="auto") - Notebooks
- Google Colab
- Kaggle
File size: 5,255 Bytes
ac0d5f8 22bad87 ac0d5f8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | # -*- coding: utf-8 -*-
"""
Flask Main Program - Emotion Prediction Service
"""
from flask import Flask, render_template, jsonify, request
from flask_cors import CORS
import os
import random
from inference import get_classifier
from see_config import PORT, MAX_LENGTH, EMOTION_LABELS, EMOTION_COLORS
# Patient-Doctor conversation example texts
EXAMPLE_TEXTS = {
"Anxiety/Fear": [
"Doctor, I have been having panic attacks recently and I am very scared about my health",
"I am worried about the surgery, doctor. What if something goes wrong?",
"Doctor, my heart races every time I think about my diagnosis. I am terrified",
"I have been losing sleep because of anxiety. What should I do, doctor?",
"Doctor, I am afraid the medication might have serious side effects"
],
"Anger/Frustration": [
"This is the fourth time I am here and nothing is helping! I am so frustrated",
"Doctor, I have been following your instructions exactly but nothing works",
"I am tired of taking so many pills every day. This is driving me crazy",
"Why does no one listen to me? I have been explaining my symptoms for weeks",
"I paid so much for these treatments and I still feel terrible"
],
"Sadness/Helplessness": [
"Doctor, I feel like giving up. Nothing seems to make me happy anymore",
"I have been feeling so hopeless lately, like nothing will ever get better",
"Doctor, I broke down crying last night. I just do not know how to cope",
"My quality of life has been getting worse. I feel so helpless",
"I miss my old self before I got sick. I feel like I lost everything"
],
"Confusion/Doubt": [
"Doctor, can you explain my test results in simpler terms? I do not understand",
"I am confused about which treatment to choose. What do you recommend?",
"The instructions are very complicated. Can you clarify, doctor?",
"I do not know why this is happening to me. There is no history in my family",
"Doctor, I have doubts about the diagnosis. Could it be something else?"
],
"Gratitude/Relief": [
"Thank you so much, doctor! I finally feel like myself again",
"I am so relieved to hear that the treatment is working",
"Doctor, you saved my life. I cannot thank you enough",
"The pain is gone now. I feel so much better after your treatment",
"Thank you for explaining everything so patiently, doctor"
],
"Neutral": [
"Good morning, doctor. I am here for my regular check-up",
"Doctor, here are my test results as you requested",
"I have been taking the medicine as prescribed",
"My symptoms are about the same as last time, doctor",
"I need to reschedule my next appointment, doctor"
]
}
app = Flask(__name__)
app.config['SECRET_KEY'] = 'emotion-prediction-secret-key'
CORS(app)
classifier = get_classifier()
@app.route('/')
def index():
"""Home page"""
return render_template('index.html')
@app.route('/api/model/status', methods=['GET'])
def get_model_status():
"""Get model status"""
return jsonify({
'success': True,
'loaded': classifier.is_loaded(),
'model_path': '../best_model'
})
@app.route('/api/model/load', methods=['POST'])
def load_model():
"""Load model"""
result = classifier.load_model()
if 'error' in result:
return jsonify({'success': False, 'error': result['error']})
return jsonify({'success': True, **result})
@app.route('/api/model/predict', methods=['POST'])
def predict():
"""Predict emotion for single text"""
text = request.json.get('text', '')
if not text:
return jsonify({'success': False, 'error': 'No text provided'})
max_length = request.json.get('max_length', MAX_LENGTH)
result = classifier.predict(text, max_length)
if 'error' in result:
return jsonify({'success': False, 'error': result['error']})
return jsonify({'success': True, 'result': result})
@app.route('/api/labels', methods=['GET'])
def get_labels():
"""Get all emotion labels"""
return jsonify({
'success': True,
'labels': EMOTION_LABELS,
'colors': EMOTION_COLORS
})
@app.route('/api/examples/random', methods=['GET'])
def get_random_example():
"""Get random example text"""
emotion = request.args.get('emotion')
if emotion and emotion in EXAMPLE_TEXTS:
text = random.choice(EXAMPLE_TEXTS[emotion])
return jsonify({'success': True, 'text': text, 'emotion': emotion})
else:
random_emotion = random.choice(list(EXAMPLE_TEXTS.keys()))
text = random.choice(EXAMPLE_TEXTS[random_emotion])
return jsonify({'success': True, 'text': text, 'emotion': random_emotion})
@app.route('/api/examples/all', methods=['GET'])
def get_all_examples():
"""Get all example texts"""
return jsonify({'success': True, 'examples': EXAMPLE_TEXTS})
if __name__ == '__main__':
print("=" * 50)
print("Emotion Prediction Service")
print("=" * 50)
print(f"Open http://0.0.0.0:{PORT} in your browser")
print("=" * 50)
app.run(debug=False, host='0.0.0.0', port=PORT)
|