-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
48 lines (38 loc) · 1.73 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
from flask import Flask, render_template, request
import classifier
import scrape_tweets
from vector_processing import get_average_vector, get_cosine_similarity
import pickle
import numpy as np
app = Flask(__name__)
classifier.train_model([[0],[0.1],[0.5],[0.3],[0.2],[0.9],[0.7],[0.6],[0.8],[0.55]],[[0],[0],[0],[0],[0],[1],[1],[1],[1],[1]]) # add training data here, right now contains test values
model = pickle.load(open('model.pkl','rb')) # load saved model
@app.route('/')
def index():
print('Request for index page received')
return render_template('index.html')
@app.route('/predictFromForm',methods=['POST'])
def predictFromForm():
# get usernames from user input and store in data
usernames = [str(x) for x in request.form.values()] ## gets all inputs from POST rest API call
print(usernames)
# scrape last 100 tweets of each user
tweet_data_1 = scrape_tweets.scrape_twitter(usernames[0])
tweet_data_2 = scrape_tweets.scrape_twitter(usernames[1])
# get average vector for each user
user1_avg = get_average_vector(tweet_data_1) # use values from scrape (previous step)
user2_avg = get_average_vector(tweet_data_2)
# get the cosine similarity based on above step
cos_sim_users = get_cosine_similarity(user1_avg,user2_avg)
print(cos_sim_users)
# cos_sim_users = get_cosine_similarity([1, 3, 5],[4, 2, 2]) # values for testing
# predict whether they'll be friends or not!
prediction = classifier.make_prediction(model,[cos_sim_users])
print(prediction)
if prediction == 0:
prediction = "unlikely"
else:
prediction = "likely"
return render_template('index.html', predictionText='These two users are {} to be friends!'.format(prediction))
if __name__ == '__main__':
app.run()