Spaces:
Sleeping
Sleeping
File size: 5,904 Bytes
b0dd51d |
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 141 142 143 144 145 146 147 148 149 |
import os
import uuid
import logging
from flask import Flask, flash, render_template, request, jsonify, url_for
from flask.templating import TemplateNotFound
from flask_cors import CORS
#from modules import Module
#model = Module()
app = Flask(__name__)
CORS(app)
app.secret_key = "Testing"
app.config['STATIC_FOLDER'] = 'static'
emotion_categories = {
"admiration": "soothieanduplift",
"amusement": "soothieanduplift",
"anger": "distractandchange",
"annoyance": "distractandchange",
"approval": "soothieanduplift",
"caring": "soothieanduplift",
"confusion": "engageandexplore",
"curiosity": "engageandexplore",
"desire": "engageandexplore",
"disappointment": "reflectiveandunderstand",
"disapproval": "distractandchange",
"disgust": "distractandchange",
"embarrassment": "distractandchange",
"excitement": "energeticandmotivate",
"fear": "distractandchange",
"gratitude": "soothieanduplift",
"grief": "reflectiveandunderstand",
"joy": "energeticandmotivate",
"love": "soothieanduplift",
"nervousness": "distractandchange",
"optimism": "energeticandmotivate",
"pride": "energeticandmotivate",
"realization": "engageandexplore",
"relief": "soothieanduplift",
"remorse": "reflectiveandunderstand",
"sadness": "reflectiveandunderstand",
"surprise": "engageandexplore",
"neutral": "soothieanduplift"
}
@app.route('/')
def index():
return render_template('index.html')
@app.route('/audio_to_text/')
def audio_to_text():
return render_template('audio_to_text.html')
@app.route('/audio', methods=['POST', 'GET'])
def audio():
if request.method == 'POST':
try:
output_file = f"./tmp/{uuid.uuid4()}.wav"
open(output_file, 'wb').write(request.data)
text, emotion = model.predict(audio_path=output_file)
os.remove(output_file)
predicted_category = emotion_categories.get(emotion, "defaultcategory")
return jsonify({
"url": url_for(predicted_category, text=text, emotion=emotion)
})
print("Predicted Category:", predicted_category) # Print predicted category
print("Text:", text) # Print predicted text
print("Emotion:", emotion) # Print predicted emotion
logging.info("Predicted Category: %s", predicted_category)
logging.info("Text: %s", text)
logging.info("Emotion: %s", emotion)
return jsonify({"url": url_for(predicted_category)})
except (FileNotFoundError, TemplateNotFound) as e:
logging.error(e)
return jsonify({"error": "Template file not found."}), 404
except Exception as e:
logging.error(e)
return jsonify({"error": "An error occurred"}), 500
elif request.method == 'GET':
return jsonify({"message": "This route only accepts POST requests."}), 405
@app.route('/reflectiveandunderstand', methods=['GET'])
def reflectiveandunderstand():
text = request.args.get('text', '') # Get predicted text
emotion = request.args.get('emotion', '') # Get predicted emotion
if emotion in emotion_data:
emotionInfo = emotion_data[emotion]
return render_template('reflectiveandunderstand.html', text=text, emotion=emotion, emotionInfo=emotionInfo)
else:
return jsonify({"error": "Emotion not found"}), 404
@app.route('/distractandchange', methods=['GET'])
def distractandchange():
text = request.args.get('text', '') # Get predicted text
emotion = request.args.get('emotion', '') # Get predicted emotion
if emotion in emotion_data:
emotionInfo = emotion_data[emotion]
return render_template('distractandchange.html', text=text, emotion=emotion, emotionInfo=emotionInfo)
else:
return jsonify({"error": "Emotion not found"}), 404@app.route('/energeticandmotivate', methods=['GET'])
def energeticandmotivate():
text = request.args.get('text', '') # Get predicted text
emotion = request.args.get('emotion', '') # Get predicted emotion
if emotion in emotion_data:
emotionInfo = emotion_data[emotion]
return render_template('energeticandmotivate.html', text=text, emotion=emotion, emotionInfo=emotionInfo)
else:
return jsonify({"error": "Emotion not found"}), 404@app.route('/energeticandmotivate', methods=['GET'])
@app.route('/engageandexplore', methods=['GET'])
def engageandexplore():
text = request.args.get('text', '') # Get predicted text
emotion = request.args.get('emotion', '') # Get predicted emotion
if emotion in emotion_data:
emotionInfo = emotion_data[emotion]
return render_template('engageandexplore.html', text=text, emotion=emotion, emotionInfo=emotionInfo)
else:
return jsonify({"error": "Emotion not found"}), 404@app.route('/energeticandmotivate', methods=['GET'])
# Add more routes as needed, mirroring your category names
@app.route('/soothieanduplift', methods=['GET'])
def soothieanduplift():
text = request.args.get('text', '') # Get predicted text
emotion = request.args.get('emotion', '') # Get predicted emotion
if emotion in emotion_data:
emotionInfo = emotion_data[emotion]
return render_template('soothieanduplift.html', text=text, emotion=emotion, emotionInfo=emotionInfo)
else:
return jsonify({"error": "Emotion not found"}), 404@app.route('/energeticandmotivate', methods=['GET'])
# Add more routes as needed, mirroring your category names
if __name__ == "__main__":
# Configure logging for Hugging Face environment
logging.basicConfig(level=logging.DEBUG)
app.run(debug=True, port=7860, host='0.0.0.0')
|