-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·137 lines (92 loc) · 4.38 KB
/
main.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from flask import Flask, render_template, request, jsonify, send_file, url_for, redirect
import numpy as np
import json
import scipy.misc
import base64
from io import BytesIO
import time
import pandas as pd
import os
#from keras.models import load_model
#from keras import backend as K
from random import randint
app = Flask(__name__)
# def load_models():
# embedded_model = load_model('static/data/model.h5')
# return embedded_model
@app.route('/')
def index():
return render_template("index.html")
# app.config["IMAGE_UPLOADS"] = "/Users/sairah/Documents/GitHub/Image-To-Affect-Website/static/imgs/user_uploads"
# @app.route('/GetVector', methods=['GET', 'POST'])
# def GetVector():
# return '5'
#helpful tutorial - https://pythonise.com/series/learning-flask/flask-uploading-files
# @app.route('/GetUserImage', methods=['GET', 'POST'])
# def GetUserImage():
# if request.method == "POST":
# if request.files:
# image = request.files["image"]
# image.save(os.path.join(app.config["IMAGE_UPLOADS"], "userImage.jpg"))
# print(image.filename)
# print("Image saved")
# #resize image
# # send through the ML
# return redirect(request.url)
# return render_template("user_input_output.html")#, jsonify(image.filename)
# #https://stackoverflow.com/questions/11262518/how-to-pass-uploaded-image-to-template-html-in-flask
# pred_embedding = embedded_model.predict(touch_data) # run first stage
# print("pred.shape", pred.shape) # SF
# features = np.load('static/data/affect.npy') # feature vectors for each image
# liwc_csv = pd.read_csv('static/data/liwc.csv') # text for each image
# csv_len = len(liwc_csv)
# chosen_index = min(range(csv_len), key=lambda i: np.linalg.norm(features[i] - pred)) # search for closest lyric, SF added [9:]
@app.route('/TouchToArt', methods=['GET', 'POST'])
def TouchToArt():
data_from_js = request.get_json()
pred = [data_from_js['positive'],
data_from_js['anxiety'],
data_from_js['anger'],
data_from_js['sad'],
data_from_js['affiliation']
]
print(pred)
print('main.py clicked getArt')
features = np.load('static/data/affect.npy') # feature vectors for each image
liwc_csv = pd.read_csv('static/data/liwc.csv') # text for each image
csv_len = len(liwc_csv)
chosen_index = min(range(csv_len), key=lambda i: np.linalg.norm(features[i] - pred)) # search for closest art
#print("features of selected art are", features[chosen_index])
chosen_js_list = json.dumps((features[chosen_index]).tolist())
print("type of index is", type(chosen_js_list))
#chosen_js_list = json.dumps(np.arange(features[chosen_index]))
print("chosen js list is", chosen_js_list) # now it's a string
#K.clear_session()
print('Result and the chosen_index C:',liwc_csv.iloc[chosen_index]['C'])
print('Result and the chosen_index:B ',liwc_csv.iloc[chosen_index]['B'])
return jsonify(liwc_csv.iloc[chosen_index]['B'] + ";" + chosen_js_list)
@app.route('/GradientArt', methods=['GET', 'POST'])
def GradientArt():
data_from_js = request.get_json()
pred = [data_from_js['positive'],
data_from_js['anxiety'],
data_from_js['anger'],
data_from_js['sad'],
data_from_js['affiliation']
]
print('main.py clicked gradient')
features = np.load('static/data/affect.npy') # feature vectors for each image
liwc_csv = pd.read_csv('static/data/liwc.csv') # text for each image
csv_len = len(liwc_csv)
chosen_index = min(range(csv_len), key=lambda i: np.linalg.norm(features[i] - pred)) # search for closest art
#print("features of selected art are", features[chosen_index])
chosen_js_list = json.dumps((features[chosen_index]).tolist())
print("type of index is", type(chosen_js_list))
#chosen_js_list = json.dumps(np.arange(features[chosen_index]))
print("chosen js list is", chosen_js_list) # now it's a string
#K.clear_session()
print('Result and the chosen_index C:',liwc_csv.iloc[chosen_index]['C'])
print('Result and the chosen_index:B ',liwc_csv.iloc[chosen_index]['B'])
return jsonify(liwc_csv.iloc[chosen_index]['B'] + ";" + chosen_js_list)
if __name__=="__main__":
app.run(host='0.0.0.0', port=80)