-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.py
229 lines (213 loc) · 4.88 KB
/
server.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
from flask import Flask
from flask import json
from flask import request
from flask import Response
import os
app = Flask(__name__)
import logging
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
from gensim import utils
from simserver import SessionServer
#BEFORE TRAINING NEW MODEL - CHANGE PATH BELOW
service = SessionServer('/tmp/mirFlickr9500')
# FORMAT FOR DATA POSTED TO /index: {"id":NUMBER,"tokens":["STRING","STRING","STRING"]}
@app.route('/test', methods=['GET'])
def test():
return "server is running"
@app.route('/index', methods=['POST'])
def indexPhoto():
print(request.json)
service.index(request.json)
return "Recieved: " + json.dumps(request.json)
#FORMAT FOR GET PARAMS: /query?id=PHOTO_ID
@app.route('/query', methods=['GET'])
def queryModel():
id = str(request.args['id'])
tuples = service.find_similar(id, max_results=100)
justIds = [x[0] for x in tuples]
return Response(json.dumps(justIds), mimetype='application/json')
# TRAIN NEW MODEL
@app.route('/train', methods=['POST'])
def train():
SITE_ROOT = os.path.realpath(os.path.dirname(__file__))
json_url = os.path.join(SITE_ROOT, "data/", "trainingCorpus.json")
corpus = json.load(open(json_url))
service.train(corpus, method='lsi')
# service.index(corpus)
return 'model trained'
# @app.route('/indexAll', methods=['GET'])
# def indexAll():
# service.index(corpus)
# return 'model indexed with whole corpus'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
# corpus = [ { 'id': 0,
# 'tokens':
# [ 'decoration',
# 'pattern',
# 'ornate',
# 'art',
# 'traditional',
# 'design',
# 'religion',
# 'desktop',
# 'handmade',
# 'no person',
# 'craft',
# 'Easter',
# 'texture' ] },
# { 'id': 1,
# 'tokens':
# [ 'recreation',
# 'water',
# 'travel',
# 'kayak',
# 'leisure',
# 'canoe',
# 'watercraft',
# 'adventure',
# 'woman',
# 'outdoors',
# 'boatman',
# 'people',
# 'paddle',
# 'water sports',
# 'mountain',
# 'fun' ] },
# { 'id': 2,
# 'tokens':
# [ 'camel',
# 'Arabian camel',
# 'sitting',
# 'desert',
# 'train',
# 'Bedouin',
# 'people',
# 'two',
# 'travel',
# 'herder',
# 'silhouette',
# 'group',
# 'livestock',
# 'one',
# 'bulge',
# 'adult',
# 'no person',
# 'cavalry',
# 'three' ] },
# { 'id': 3,
# 'tokens':
# [ 'water',
# 'travel',
# 'vacation',
# 'sea',
# 'people',
# 'outdoors',
# 'recreation',
# 'tourism',
# 'leisure',
# 'lake',
# 'seashore',
# 'sky',
# 'summer',
# 'landscape',
# 'boat',
# 'tourist' ] },
# { 'id': 4,
# 'tokens':
# [ 'people',
# 'group',
# 'child',
# 'adult',
# 'woman',
# 'portrait',
# 'man',
# 'festival',
# 'facial expression',
# 'wear',
# 'race' ] },
# { 'id': 5,
# 'tokens':
# [ 'water',
# 'fun',
# 'leisure',
# 'vacation',
# 'sea',
# 'swimming',
# 'summer',
# 'recreation',
# 'wet',
# 'travel',
# 'ocean',
# 'beach' ] },
# { 'id': 6,
# 'tokens':
# [ 'seashore',
# 'water',
# 'no person',
# 'beach',
# 'sea',
# 'ocean',
# 'daylight',
# 'surf',
# 'travel',
# 'outdoors',
# 'wave',
# 'motion' ] },
# { 'id': 7,
# 'tokens':
# [ 'architecture',
# 'outdoors',
# 'travel',
# 'building',
# 'city',
# 'people',
# 'tower',
# 'sky' ] },
# { 'id': 8,
# 'tokens':
# [ 'mountain',
# 'travel',
# 'nature',
# 'landscape',
# 'rock',
# 'sky',
# 'dinosaur',
# 'outdoors',
# 'no person',
# 'tree',
# 'stone' ] },
# { 'id': 9,
# 'tokens':
# [ 'water',
# 'sunset',
# 'sea',
# 'beach',
# 'reflection',
# 'dawn',
# 'river',
# 'ocean',
# 'boat',
# 'fisherman' ] },
# { 'id': 10,
# 'tokens':
# [ 'water',
# 'beach',
# 'tropical',
# 'travel',
# 'ocean',
# 'sand',
# 'sea',
# 'summer',
# 'seashore',
# 'sky',
# 'relaxation',
# 'island',
# 'sun',
# 'exotic',
# 'vacation',
# 'idyllic',
# 'leisure',
# 'turquoise',
# 'seascape' ] },
# {"id":"11","tokens":["people","indoors","adult","man","one","cooking","healthcare","woman","family","competition","room","wear","food","medicine","child","preparation","portrait","two","chef","industry"]} ]