-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunModel.py
54 lines (43 loc) · 1.25 KB
/
runModel.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
import os
import cohere
from tqdm import tqdm
import time
import numpy as np
import pickle
from dotenv import load_dotenv
load_dotenv(dotenv_path='.env')
#device = 'cuda' # nvidia-gpu
# device = 'mps' # apple-gpu
device = 'cpu' # no gpu
co = cohere.Client(os.getenv("COHERE_API_KEY")) # or None if you dont want to use Cohere
def encode(text):
if co is not None:
if len(text) > 95:
embed = []
# prod key is 10000 per minute, free is 100. Cohere offers $300 in credits using htn2023
sleep_time = 60 / 100
k = 0
start = time.time()
for i in tqdm(range(0, len(text), 95)):
embed += co.embed(texts=text[i:i + 95]).embeddings
k += 1
if k == 100:
end = time.time()
dur = end - start
time.sleep(60 - dur if 60 - dur > 0 else 0)
start = time.time()
k = 0
else:
embed = co.embed(
texts=text,
model='embed-english-v2.0'
).embeddings
embed = np.array(embed)
else:
raise Exception("No API Key was found")
return embed
def runModel(input: str) -> int:
with open('model.pkl', 'rb') as f:
pipeline = pickle.load(f)
encoded = encode([input])
return pipeline.predict(encoded.reshape(1, -1))[0]