StringJammer's picture
Upload folder using huggingface_hub
22bad87 verified
# -*- 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)