forked from TensorTeacher/endpoint-center
-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoint_miner.py
54 lines (41 loc) · 1.75 KB
/
endpoint_miner.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
import argparse
import bittensor
from typing import List, Dict
import requests
import json
import traceback
import time
class EndpointMiner( bittensor.BasePromptingMiner ):
@classmethod
def check_config( cls, config: 'bittensor.Config' ):
pass
@classmethod
def add_args( cls, parser: argparse.ArgumentParser ):
parser.add_argument( '--endpoint.verify_token', type=str, help='Auth' )
parser.add_argument( '--endpoint.url', type=str, help='Endpoint' )
def __init__( self ):
super( EndpointMiner, self ).__init__()
print ( self.config )
def forward(self, messages: List[Dict[str, str]]) -> str:
start_time=time.time()
generation = "An error occured with the miner and it is not available now."
try:
for i, item in enumerate(messages):
if type(item) == str:
messages[i] = json.loads(item)
response = requests.post(self.config.endpoint.url, data=json.dumps({"messages": messages, "verify_token":self.config.endpoint.verify_token}), headers= {"Content-Type": "application/json"}, timeout=9.5)
generation = response.json()["response"]
except Exception as e:
traceback.print_exc()
print("Error", e)
print("Response", response.json())
print("Errored messages", messages,"\n")
print("messages:", messages)
print("Generation:", generation)
time_to_sleep = 9.5 - (time.time()-start_time)
if time_to_sleep > 0:
time.sleep(time_to_sleep)
return generation
if __name__ == "__main__":
bittensor.utils.version_checking()
EndpointMiner().run()