Advertisement
UF6

Server 500 Localhost For Emotion Detector

UF6
Jan 29th, 2024
762
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.54 KB | Source Code | 0 0
  1. """
  2. This module defines a Flask web application for emotion detection.
  3. """
  4. from flask import Flask, render_template, request
  5. from EmotionDetection.emotion_detection import emotion_detector, emotion_predictor
  6.  
  7. app = Flask("Emotion Detection")
  8.  
  9. def run_emotion_detection():
  10.     """Run the Flask application."""
  11.     app.run(host="0.0.0.0", port=5000)
  12.  
  13. def get_response_text(text_to_analyze):
  14.     """Get the response text for emotion detection."""
  15.     response = emotion_detector(text_to_analyze)
  16.     formatted_response = emotion_predictor(response)
  17.  
  18.     if formatted_response['dominant_emotion'] is None:
  19.         return "Invalid text! Please try again."
  20.  
  21.     return (
  22.         f"For the given statement, the system response is 'anger': {formatted_response['anger']} "
  23.         f"'disgust': {formatted_response['disgust']}, 'fear': {formatted_response['fear']}, "
  24.         f"'joy': {formatted_response['joy']}, 'sadness': {formatted_response['sadness']}. "
  25.         f"The dominant emotion is {formatted_response['dominant_emotion']}."
  26.     )
  27.  
  28. @app.route('/emotionDetector')
  29. def sent_detector():
  30.     """
  31.    Analyze the user-provided text for emotions and return the result.
  32.    """
  33.     text_to_detect = request.args.get('textToAnalyze', '')
  34.     return get_response_text(text_to_detect)
  35.  
  36. @app.route('/')
  37. def render_index_page():
  38.     ''' This function initiates the rendering of the main application
  39.        page over the Flask channel
  40.    '''
  41.     return render_template('index.html')
  42.  
  43. if __name__ == "__main__":
  44.     run_emotion_detection()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement