Merge pull request #7 from east-and-west-magic/feature-print-timecost
Browse files
app.py
CHANGED
@@ -9,6 +9,7 @@ from gradio_client import Client
|
|
9 |
from extract import extract
|
10 |
import app_util
|
11 |
from pgsoft.pgconst.const import service_list, functionality_list, game_list
|
|
|
12 |
|
13 |
#######################
|
14 |
# proxy version
|
@@ -65,57 +66,72 @@ def run(hf_token, service, game, functionality, nlp_command):
|
|
65 |
if functionality == "AI":
|
66 |
if nlp_command in ["", None]:
|
67 |
return "please make sure the command is not empty"
|
|
|
|
|
|
|
68 |
if service == "download game":
|
69 |
res = app_util.call_clouddisk(service, nlp_command, token)
|
70 |
if res is None:
|
71 |
outp = {"status": "Failure"}
|
72 |
else:
|
73 |
outp = {"status": "OK", "result": json.loads(res)}
|
74 |
-
|
75 |
-
if service == "upload game":
|
76 |
res = app_util.call_clouddisk(service, nlp_command, token)
|
77 |
if res is None:
|
78 |
outp = {"status": "Failure"}
|
79 |
else:
|
80 |
outp = {"status": "OK", "result": res}
|
81 |
-
|
82 |
-
if service == "list games":
|
83 |
res = app_util.call_clouddisk(service, nlp_command, token)
|
84 |
if res is None:
|
85 |
outp = {"status": "Failure"}
|
86 |
else:
|
87 |
outp = {"status": "OK", "result": json.loads(res)["result"]}
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
|
120 |
|
121 |
demo = gr.Interface(
|
|
|
9 |
from extract import extract
|
10 |
import app_util
|
11 |
from pgsoft.pgconst.const import service_list, functionality_list, game_list
|
12 |
+
from pgsoft.pgdate.date_utils import beijing
|
13 |
|
14 |
#######################
|
15 |
# proxy version
|
|
|
66 |
if functionality == "AI":
|
67 |
if nlp_command in ["", None]:
|
68 |
return "please make sure the command is not empty"
|
69 |
+
|
70 |
+
service_start = beijing()
|
71 |
+
print(f">>>>>>>>>>>>>>> service starts at {service_start} <<<<<<<<<<<<<<")
|
72 |
if service == "download game":
|
73 |
res = app_util.call_clouddisk(service, nlp_command, token)
|
74 |
if res is None:
|
75 |
outp = {"status": "Failure"}
|
76 |
else:
|
77 |
outp = {"status": "OK", "result": json.loads(res)}
|
78 |
+
elif service == "upload game":
|
|
|
79 |
res = app_util.call_clouddisk(service, nlp_command, token)
|
80 |
if res is None:
|
81 |
outp = {"status": "Failure"}
|
82 |
else:
|
83 |
outp = {"status": "OK", "result": res}
|
84 |
+
elif service == "list games":
|
|
|
85 |
res = app_util.call_clouddisk(service, nlp_command, token)
|
86 |
if res is None:
|
87 |
outp = {"status": "Failure"}
|
88 |
else:
|
89 |
outp = {"status": "OK", "result": json.loads(res)["result"]}
|
90 |
+
else:
|
91 |
+
try:
|
92 |
+
assert "games" in service_list
|
93 |
+
if service == "games":
|
94 |
+
print(f"{beijing()} [{user_name}] [{game}] {nlp_command}")
|
95 |
+
client = Client(
|
96 |
+
url,
|
97 |
+
hf_token=token,
|
98 |
+
verbose=False,
|
99 |
+
)
|
100 |
+
calling_start = beijing()
|
101 |
+
print(f"calling ai starts at {calling_start}")
|
102 |
+
res = client.predict(
|
103 |
+
service,
|
104 |
+
game,
|
105 |
+
functionality,
|
106 |
+
nlp_command, # hidden,
|
107 |
+
api_name="/predict",
|
108 |
+
)
|
109 |
+
calling_end = beijing()
|
110 |
+
timecost = calling_end.timestamp() - calling_start.timestamp()
|
111 |
+
print(f"calling ai ends at {calling_end}, costs {timecost:.2f}s")
|
112 |
+
outp = json.loads(res)
|
113 |
+
# add proxy version info to the output
|
114 |
+
outp["proxy-version"] = proxy_version
|
115 |
+
outp["user"] = user_name
|
116 |
+
outp["game"] = game
|
117 |
+
calling_start = beijing()
|
118 |
+
print(f"calling logger starts at {beijing()}")
|
119 |
+
app_util.call_logger(outp, identity, token)
|
120 |
+
calling_end = beijing()
|
121 |
+
timecost = calling_end.timestamp() - calling_start.timestamp()
|
122 |
+
print(f"calling logger ends at {calling_end}, costs {timecost:.2f}s")
|
123 |
+
except Exception as e:
|
124 |
+
return (
|
125 |
+
f"{type(e)}, {str(e)}. \nyou may want to make "
|
126 |
+
+ "sure your hf_token is correct"
|
127 |
+
)
|
128 |
+
service_end = beijing()
|
129 |
+
timecost = service_end.timestamp() - service_start.timestamp()
|
130 |
+
print(
|
131 |
+
f">>>>>>>>>>>>>>> service ends at {service_end}, "
|
132 |
+
+ f"costs {timecost:.2f}s <<<<<<<<<<<<<<<"
|
133 |
+
)
|
134 |
+
return json.dumps(outp, indent=4)
|
135 |
|
136 |
|
137 |
demo = gr.Interface(
|