-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
65 lines (51 loc) · 2.71 KB
/
server.py
File metadata and controls
65 lines (51 loc) · 2.71 KB
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
# Import necessary libraries for the Flask app and functionalities
from flask import Flask, request, render_template
from predict import predict_sentiments # Function to predict sentiments for comments
from youtube import get_video_comments # Function to get video comments from YouTube
from flask_cors import CORS # For handling Cross-Origin Resource Sharing (CORS)
import requests # For making HTTP requests (not currently used in this code, but imported)
# Initialize the Flask app and enable CORS support
app = Flask(__name__)
CORS(app)
# Function to get video details, comments, and sentiment predictions
def get_video(video_id):
# Check if the video_id is provided
if not video_id:
return {"error": "video_id is required"} # Return error if video_id is missing
# Get the video comments using the provided video_id
comments = get_video_comments(video_id)
# Get sentiment predictions for the comments
predictions = predict_sentiments(comments)
# Calculate the number of positive and negative sentiments
positive = predictions.count("Positive")
negative = predictions.count("Negative")
# Prepare a summary dictionary with the sentiment counts, total comments, and rating
summary = {
"positive": positive,
"negative": negative,
"num_comments": len(comments), # Total number of comments
"rating": (positive / len(comments)) * 100 # Percentage of positive sentiments
}
# Return the predictions, comments, and summary
return {"predictions": predictions, "comments": comments, "summary": summary}
# Define the route for the home page ('/') to display the analysis form and results
@app.route('/', methods=['GET', 'POST'])
def index():
summary = None # Initialize summary as None initially
comments = [] # Initialize an empty list for comments
# Check if the form was submitted via POST request
if request.method == 'POST':
# Get the video URL from the form input
video_url = request.form.get('video_url')
# Extract the video_id from the YouTube URL
video_id = video_url.split("v=")[1]
# Call the get_video function to fetch predictions and summary
data = get_video(video_id)
# Set the summary and comments for the template
summary = data['summary']
comments = list(zip(data['comments'], data['predictions'])) # Combine comments with their sentiments
# Render the index.html template and pass the summary and comments to it
return render_template('index.html', summary=summary, comments=comments)
# Run the Flask app in debug mode
if __name__ == '__main__':
app.run(debug=True)