-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathyoutube_model.py
executable file
·57 lines (46 loc) · 2.25 KB
/
youtube_model.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
import os
import pickle
os.environ['KERAS_BACKEND'] = 'theano'
from keras.models import load_model
from keras.preprocessing.sequence import pad_sequences
from utils import Utils
MAX_SEQUENCE_LENGTH = 100
@Utils
class YoutubeRelevanceModel:
def __init__(self):
self.tokenizer = pickle.load(
open(os.path.dirname(os.path.abspath(__file__)) + '/tokenizer.p',
'rb'))
self.model = load_model(
os.path.dirname(os.path.abspath(__file__)) + '/best_model.h5')
def predict(self, data):
query_sequences = self.tokenizer.texts_to_sequences(data['query'])
title_sequences = self.tokenizer.texts_to_sequences(data['title'])
desc_sequences = self.tokenizer.texts_to_sequences(data['description'])
query_data = pad_sequences(query_sequences, maxlen=MAX_SEQUENCE_LENGTH)
title_data = pad_sequences(title_sequences, maxlen=MAX_SEQUENCE_LENGTH)
desc_data = pad_sequences(desc_sequences, maxlen=MAX_SEQUENCE_LENGTH)
preds = self.model.predict([query_data, title_data, desc_data])
return preds
ym = YoutubeRelevanceModel.Instance()
if __name__ == '__main__':
data = {
"query": ["Algebra videos", "Algebra videos", "Algebra videos"],
"title": ["Quick Math Review to Prep for Algebra 1",
"Algebra - Completing the square",
"Algebra Basics: Solving Basic Equations Part 1 "
"- Math Antics"],
"description": ["This is 1 of 4 videos I custom made for an educator "
"in California for an experimental 1-week video "
"homework program. I have only edited the "
"beginning and ...",
"Hi Algebrinos! As we progress with our problem "
"solving prowess, we include solving by using the "
"nifty method of solving quadratic equations "
"titled, 'Completing ...",
"This video shows students how to solve simple 1-step "
"Algebra equations involving only addition or "
"subtraction. Part of the Algebra Basics Series: ..."]
}
preds = ym.predict(data)
print (preds)