-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.py.html
161 lines (107 loc) · 4.75 KB
/
ui.py.html
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
# coding: utf-8
# In[1]:
import sys
get_ipython().system('{sys.executable} -m pip install google_images_download pytesseract opencv-python')
# In[56]:
from contextlib import contextmanager
import sys, os
@contextmanager
def suppress_stdout():
with open(os.devnull, "w") as devnull:
old_stdout = sys.stdout
sys.stdout = devnull
try:
yield
finally:
sys.stdout = old_stdout
from google_images_download import google_images_download
response = google_images_download.googleimagesdownload()
def get_image(kw):
arguments = {"keywords": kw, "limit": 1, "format": "jpg", "size": ">2MP"}
absolute_image_paths = response.download(arguments)
return absolute_image_paths
import cv2 as cv
from matplotlib import pyplot as plt
import pytesseract
def grid_display(list_of_images, list_of_titles=[], no_of_columns=4, figsize=(20,3)):
fig = plt.figure(figsize=figsize)
column = 0
for i in range(len(list_of_images)):
column += 1
# check for end of column and create a new figure
if column == no_of_columns+1:
fig = plt.figure(figsize=figsize)
column = 1
fig.add_subplot(1, no_of_columns, column)
img = cv.imread(list_of_images[i])
rgb_img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
plt.imshow(rgb_img)
plt.axis('off')
if len(list_of_titles) >= len(list_of_images):
plt.title(list_of_titles[i])
from IPython.display import Markdown, display
def printmd(string):
display(Markdown(string))
import requests, json
def user_reccom(user_id):
url = 'http://docker.for.mac.localhost:8080/user/{}'.format(user_id)
r = requests.get(url)
data = json.loads(r.content.decode())
printmd('**Response:**')
print(json.dumps(data, indent=4, sort_keys=True))
return data
def random_user_reccom():
url = 'http://docker.for.mac.localhost:8080/user/random'
r = requests.get(url)
data = json.loads(r.content.decode())
# printmd('**Response:**')
# print(json.dumps(data, indent=4, sort_keys=True))
return data
def display_user_history(data):
history_titles = [x.get("title") for x in data.get("history")][:16]
with suppress_stdout():
history_image_links = list(map(lambda x: next(iter(next(iter(get_image("movie - {}".format(x)).values())))), history_titles))
printmd('**User {} history (top 16):**'.format(data.get("id")))
grid_display(history_image_links, history_titles)
def display_user_reccom(data):
titles = list(map(lambda x: "{} - {}".format(x.get("title")[:25], str(x.get("score"))[:4]), data.get("scores")[:16]))
with suppress_stdout():
image_links = list(map(lambda x: next(iter(next(iter(get_image("movie - {}".format(x.get("title"))).values())))), data.get("scores")[:16]))
printmd('**Recommendations for user {}:**'.format(data.get("id")))
grid_display(image_links, titles)
from IPython.core.display import Image, display
def item_reccom(item_id):
url = 'http://docker.for.mac.localhost:8080/item/{}'.format(item_id)
r = requests.get(url)
data = json.loads(r.content.decode())
printmd('**Response:**')
print(json.dumps(data, indent=4, sort_keys=True))
titles = list(map(lambda x: "{} - {}".format(x.get("title")[:25], str(x.get("score"))[:4]), data.get("scores")[:8]))
with suppress_stdout():
image_links = list(map(lambda x: next(iter(next(iter(get_image(x.get("title")).values())))), data.get("scores")[:8]))
item_url = next(iter(next(iter(get_image(data.get("movie")).values()))))
display(Image(item_url, width=100, unconfined=True))
printmd('**Recommendations for {}:**'.format(data.get("movie")))
grid_display(image_links, titles)
def random_item_reccom():
url = 'http://docker.for.mac.localhost:8080/item/random'
r = requests.get(url)
data = json.loads(r.content.decode())
printmd('**Response:**')
print(json.dumps(data, indent=4, sort_keys=True))
titles = list(map(lambda x: "{} - {}".format(x.get("title")[:25], str(x.get("score"))[:4]), data.get("scores")[:8]))
with suppress_stdout():
image_links = list(map(lambda x: next(iter(next(iter(get_image(x.get("title")).values())))), data.get("scores")[:8]))
item_url = next(iter(next(iter(get_image(data.get("movie")).values()))))
display(Image(item_url, width=100, unconfined=True))
printmd('**Recommendations for {}:**'.format(data.get("movie")))
grid_display(image_links, titles)
# In[61]:
user_data = random_user_reccom()
display_user_history(user_data)
# In[62]:
display_user_reccom(user_data)
# In[6]:
item_reccom(99112)
# In[7]:
random_item_reccom()