File size: 804 Bytes
d4829ef
 
 
 
 
 
 
 
 
9e290f8
d4829ef
 
9e290f8
d4829ef
 
9e290f8
 
 
 
 
d4829ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
from pydantic import BaseModel

# SCHEMA
class Schema(BaseModel):
	title: str
	n: int = 5

# Request Handler
def recommender(req, data_path):
	title = req.title
	n = req.n
	output = predict(title, n, data_path)
	return output

def predict(title, n, data_path):
	
	with open(data_path, 'rb') as f:
		data = json.load(f)

	index = data['titles'].index(title)
	recs = data['recs'][index][:n]
	output = []

	for rec in [[index, 0]] + recs:
		i, score = rec
		new_rec = {
			"title": data['titles'][i],
			"score": score,
			"img": None,
			"info": None
		}
		if (data['imgs']):
			new_rec['img'] = data['imgs'][i]
		if (data['infos']):
			new_rec['info'] = data['infos'][i],
		
		if type(new_rec['info']) == tuple:
			new_rec['info'] = new_rec['info'][0]

		output.append(new_rec)

	return output