File size: 1,005 Bytes
5ee7e9c f950953 5ee7e9c f950953 5ee7e9c |
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 |
from functools import cache
from gradio_client import Client
from pgsoft.pgdate.date_utils import beijing
import json
from_cache = True
@cache
def call_ai(service, game, functionality, nlp_command, url, hf_token):
calling_start = beijing()
print(f"calling ai starts at {calling_start}")
try:
client = Client(
url,
hf_token=hf_token,
verbose=False,
)
res = client.predict(
service,
game,
functionality,
nlp_command, # hidden,
api_name="/predict",
)
except Exception as e:
return (
f"{type(e)}, {str(e)}. \nyou may want to make "
+ "sure your hf_token is correct"
)
calling_end = beijing()
timecost = calling_end.timestamp() - calling_start.timestamp()
print(f"calling ai ends at {calling_end}, costs {timecost:.2f}s")
outp = json.loads(res)
global from_cache
from_cache = False
return outp
|