Beracles
commited on
Commit
·
5ee7e9c
1
Parent(s):
427886e
construct call_ai
Browse files- call_ai.py +33 -0
call_ai.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from functools import cache
|
2 |
+
from gradio_client import Client
|
3 |
+
from pgsoft.pgdate.date_utils import beijing
|
4 |
+
import json
|
5 |
+
|
6 |
+
|
7 |
+
@cache
|
8 |
+
def call_ai(service, game, functionality, nlp_command, url, hf_token):
|
9 |
+
calling_start = beijing()
|
10 |
+
print(f"calling ai starts at {calling_start}")
|
11 |
+
try:
|
12 |
+
client = Client(
|
13 |
+
url,
|
14 |
+
hf_token=hf_token,
|
15 |
+
verbose=False,
|
16 |
+
)
|
17 |
+
res = client.predict(
|
18 |
+
service,
|
19 |
+
game,
|
20 |
+
functionality,
|
21 |
+
nlp_command, # hidden,
|
22 |
+
api_name="/predict",
|
23 |
+
)
|
24 |
+
except Exception as e:
|
25 |
+
return (
|
26 |
+
f"{type(e)}, {str(e)}. \nyou may want to make "
|
27 |
+
+ "sure your hf_token is correct"
|
28 |
+
)
|
29 |
+
calling_end = beijing()
|
30 |
+
timecost = calling_end.timestamp() - calling_start.timestamp()
|
31 |
+
print(f"calling ai ends at {calling_end}, costs {timecost:.2f}s")
|
32 |
+
outp = json.loads(res)
|
33 |
+
return outp
|