This repository has been archived by the owner on May 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NFT.mine.py
161 lines (140 loc) · 5.48 KB
/
NFT.mine.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
'''
Install dependencies: pip install -r requirements.txt
Launch server: python NFT.mine.py
'''
from http.server import HTTPServer, BaseHTTPRequestHandler
from urllib.parse import urlparse, parse_qs
import pandas as pd
import matplotlib.pyplot as plt
from PIL import Image
from urllib import request
import os
plt.style.use('ggplot')
HOST = ""
PORT = 8080
suggestion_cols = 5
suggestion_rows = 5
num_suggestions = suggestion_rows * suggestion_cols
to_addr_index = 0
asset_name_index = 1
probability_index = 2
image_url_index = 3
collection_slug_index = 4
cache_path = 'cache/'
data_path = 'data/result_matrix.csv'
result_matrix = pd.read_csv(data_path)[
['to_addr', 'asset_name', 'probability', 'image_url', 'collection_slug']]
# print(f'Loaded {result_matrix.shape[0]} results')
n_all_wallets = result_matrix['to_addr'].nunique()
all_wallets = result_matrix['to_addr'].unique()
# print(f'Found {n_all_wallets} unique wallet addresses')
n_all_collections = result_matrix['collection_slug'].nunique()
all_collections = result_matrix['collection_slug'].unique()
# print(f'Found {n_all_collections} unique collection slugs')
def generate_header(header):
return bytes(f'<html><body><h1>{header}</h1></body></html>', 'utf-8')
def parse_nft_query(url_path):
wallet = ''
collection = ''
parsed_path = urlparse(url_path)
if parsed_path.path != '/recommend' or parsed_path.query == '':
return '', ''
parsed_query = parse_qs(parsed_path.query)
if 'wallet_address' not in parsed_query.keys():
return '', ''
wallets = parsed_query['wallet_address']
if len(wallets) != 1:
return '', ''
wallet = wallets[0]
collections = []
if 'collection_slug' in parsed_query.keys():
collections = parsed_query['collection_slug']
if len(collections) > 1:
return '', ''
if len(collections) == 1:
collection = collections[0]
return wallet, collection
def generate_recommendation(to_addr, collection_slug):
path_to_image = cache_path + to_addr + '_' + collection_slug + '.png'
try:
return open(path_to_image, 'rb').read()
except:
pass
if collection_slug == '':
user_0_assets = result_matrix[result_matrix['to_addr'] == to_addr].sort_values(
'probability', ascending=False)[:num_suggestions*2]
else:
user_0_assets = result_matrix[result_matrix['to_addr'] == to_addr][result_matrix['collection_slug'] == collection_slug].sort_values(
'probability', ascending=False)[:num_suggestions*2]
fig, axis = plt.subplots(suggestion_rows, suggestion_cols,
figsize=(4 * suggestion_cols, 4 * suggestion_rows), dpi=65)
fig.suptitle(' NFT.mine - ' + to_addr, fontsize=22, horizontalalignment='left', x=0)
image_i = 0
image_found = 0
while (image_found < num_suggestions):
current = axis[image_found //
suggestion_cols][image_found % suggestion_cols]
try:
image = Image.open(request.urlopen(user_0_assets.iloc[image_i, image_url_index]))
current.imshow(image)
except:
if image_i > 2 * num_suggestions:
image_found = image_found + 1
current.axis('off')
current.set_xlabel('off')
continue
image_i = image_i + 1
continue
current.set_title('\n'
+ f'{image_found + 1}. {user_0_assets.iloc[image_i, asset_name_index]}'
+ '\n' + user_0_assets.iloc[image_i, collection_slug_index]
+ '\n' + f'confidence: {(user_0_assets.iloc[image_i, probability_index] * 100):.2f}%'
, loc='left')
current.axis('off')
current.set_xlabel('off')
image_i = image_i + 1
image_found = image_found + 1
fig.tight_layout()
plt.savefig(path_to_image)
return generate_recommendation(to_addr, collection_slug)
class MyHandler(BaseHTTPRequestHandler):
def do_GET(self):
target_wallet, target_collection = parse_nft_query(self.path)
if target_wallet == '' or not target_wallet in all_wallets:
self.send_response(404)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(generate_header('Invalid Wallet Address'))
return
if target_collection != '' and not target_collection in all_collections:
self.send_response(404)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(generate_header('Invalid Collection Slug'))
return
try:
img = generate_recommendation(target_wallet, target_collection)
except:
self.send_response(500)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(generate_header('Internal Error'))
return
self.send_response(200)
self.end_headers()
self.wfile.write(img)
def main():
server = HTTPServer((HOST, PORT), MyHandler)
os.makedirs(cache_path, exist_ok=True)
print(f'NFT.mine is up -> ', end='')
print(f'http://localhost:{PORT}/recommend?wallet_address=&collection_slug=')
try:
server.serve_forever()
except KeyboardInterrupt:
print('Interrupted')
print('Shutting down')
for file in os.scandir(cache_path):
os.remove(file.path)
server.shutdown()
if __name__ == '__main__':
main()