/
app.py
76 lines (62 loc) · 2.21 KB
/
app.py
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
from flask import Flask, flash, redirect, render_template, request, session, abort
from pipeline import Pipeline
from musixmatch.api import Musix, Track;
from ibm.tone import ToneAnalyzer;
import pandas as pd
from argparse import ArgumentParser
FILE_NAME = "";
app = Flask(__name__)
# Home Page
@app.route("/")
def index():
return render_template("index.html")
# Classifier Function
@app.route('/classify', methods=['POST'])
def handle_data():
# Get Lyrics
lyrics = request.form['lyrics']
if not lyrics :
return "No Lyrics Found!"
# Convert to list if it is not.
if type(lyrics) == type("") :
lyrics = [lyrics]
# Instantiate a pipeline object
pipeline = Pipeline(lyrics);
# Get the results.
return pipeline.vectorize()
@app.route("/musixmatch",methods=['POST'])
def musixmatch() :
k = int(request.form['k'])
country = request.form['country']
musix = Musix(country)
tracks = musix.get_top_lyrics(k)
f = open(FILE_NAME,"w");
f.write("song\ttone-analyzer\tibm\n");
f.close();
result = ""
for i, track in enumerate(tracks) :
try :
pipeline = Pipeline([track.lyrics])
track.label(pipeline.vectorize())
ibm = ToneAnalyzer();
ibm_results = ibm.analyze(track);
result = result + (track.name + " by <i>" + track.artist + "</i> : <b><u>" + track.mood + "</b></u> || IBM's Results <b><u>" + ', '.join(ibm_results)) + "</b></u><br><br>"
f = open(FILE_NAME,"a")
f.write(str(track.name + " by " + track.artist + "\t" + track.mood + "\t" + ', '.join(ibm_results) + "\n"))
f.close()
print("Completed classifying " + str(i+1) + " tracks");
except AttributeError :
pass
return render_template("musixmatch.html", k=k, result=result)
# Main Function
if __name__ == "__main__":
# Run the Flask Server
parser = ArgumentParser()
parser.add_argument("-f",
"--file",
type = str,
help = "File name",
dest = "filename")
args = parser.parse_args()
FILE_NAME = args.filename if args.filename else "result.csv"
app.run()