File size: 2,067 Bytes
c061ce5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# ==============================================================================
# PROJECT: DEPRESSION-DETECTION-USING-TWEETS
# AUTHORS: AMEY THAKUR & MEGA SATISH
# GITHUB (AMEY): https://github.com/Amey-Thakur
# GITHUB (MEGA): https://github.com/msatmod
# REPOSITORY: https://github.com/Amey-Thakur/DEPRESSION-DETECTION-USING-TWEETS
# RELEASE DATE: June 5, 2022
# LICENSE: MIT License
# DESCRIPTION: Flask application entry point for the tweet analysis project.
# ==============================================================================

#!/usr/bin/env python3

import pickle
from flask import Flask, request, render_template
from flask_bootstrap import Bootstrap
import app_utilities

# Initialize the Flask application
# Flask-Bootstrap is utilized for enhanced UI styling consistency
app = Flask(__name__)
Bootstrap(app)

@app.route('/')
def index():
    """Renders the landing page for tweet input."""
    return render_template('index.html')

@app.route('/predict', methods=['POST'])
def predict():
    """
    Handles the form submission and displays the prediction result.
        
    Returns:
        Rendered result HTML with the model's prediction outcome.
    """
    if request.method == 'POST':
        # Retrieve the tweet content submitted via the web interface
        tweet = request.form["tweet"]
        input_data = [tweet]
        
        # Invoke the backend prediction utility to classify the tweet's sentiment
        # The engine utilizes an SVM classifier with spaCy word embeddings
        my_prediction = app_utilities.tweet_prediction(str(input_data))
        
        return render_template("result.html", prediction=my_prediction, name=tweet)

@app.errorhandler(404)
def page_not_found(e):
    """
    Custom 404 error handler.
    Renders the personalized 404 page when a resource is not found.
    """
    return render_template('404.html'), 404

# Entry point for the Flask development server
if __name__ == '__main__':
    # Execution on port 7860 as required for Hugging Face Spaces
    app.run(host='0.0.0.0', port=7860)