call_pgai
Browse files- app.py +8 -6
- call_pgai.py +51 -0
- requirements-local.txt +1 -1
- requirements.txt +1 -1
app.py
CHANGED
|
@@ -9,7 +9,7 @@ import app_util
|
|
| 9 |
from pgsoft.pgconst.const import service_list, functionality_list, game_list
|
| 10 |
from pgsoft.pghost import ais
|
| 11 |
from pgsoft.pgdate.date_utils import beijing
|
| 12 |
-
import
|
| 13 |
from str_util import normalize_text
|
| 14 |
|
| 15 |
#######################
|
|
@@ -76,8 +76,8 @@ def run(hf_token, service, game, functionality, nlp_command):
|
|
| 76 |
if service == "games":
|
| 77 |
print(f"{beijing()} [{user_name}] [{game}] {nlp_command}")
|
| 78 |
nlp_command = normalize_text(nlp_command)
|
| 79 |
-
|
| 80 |
-
outp =
|
| 81 |
service,
|
| 82 |
game,
|
| 83 |
functionality,
|
|
@@ -85,6 +85,8 @@ def run(hf_token, service, game, functionality, nlp_command):
|
|
| 85 |
ai_url,
|
| 86 |
token,
|
| 87 |
)
|
|
|
|
|
|
|
| 88 |
if isinstance(outp, str):
|
| 89 |
return outp
|
| 90 |
# add proxy version info to the output
|
|
@@ -94,7 +96,7 @@ def run(hf_token, service, game, functionality, nlp_command):
|
|
| 94 |
outp["game"] = game
|
| 95 |
if source:
|
| 96 |
outp["source"] = source
|
| 97 |
-
outp["cache"] =
|
| 98 |
app_util.call_logger(outp, identity, token)
|
| 99 |
service_end = beijing()
|
| 100 |
timecost = service_end.timestamp() - service_start.timestamp()
|
|
@@ -115,8 +117,8 @@ demo = gr.Interface(
|
|
| 115 |
info="Shared services",
|
| 116 |
),
|
| 117 |
gr.Radio(
|
| 118 |
-
|
| 119 |
-
value=
|
| 120 |
info="Which game you want the AI to support?",
|
| 121 |
),
|
| 122 |
gr.Radio(
|
|
|
|
| 9 |
from pgsoft.pgconst.const import service_list, functionality_list, game_list
|
| 10 |
from pgsoft.pghost import ais
|
| 11 |
from pgsoft.pgdate.date_utils import beijing
|
| 12 |
+
import call_pgai
|
| 13 |
from str_util import normalize_text
|
| 14 |
|
| 15 |
#######################
|
|
|
|
| 76 |
if service == "games":
|
| 77 |
print(f"{beijing()} [{user_name}] [{game}] {nlp_command}")
|
| 78 |
nlp_command = normalize_text(nlp_command)
|
| 79 |
+
call_pgai.from_cache = True
|
| 80 |
+
outp = call_pgai.call_pgai(
|
| 81 |
service,
|
| 82 |
game,
|
| 83 |
functionality,
|
|
|
|
| 85 |
ai_url,
|
| 86 |
token,
|
| 87 |
)
|
| 88 |
+
if outp is None:
|
| 89 |
+
return "no output"
|
| 90 |
if isinstance(outp, str):
|
| 91 |
return outp
|
| 92 |
# add proxy version info to the output
|
|
|
|
| 96 |
outp["game"] = game
|
| 97 |
if source:
|
| 98 |
outp["source"] = source
|
| 99 |
+
outp["cache"] = call_pgai.from_cache
|
| 100 |
app_util.call_logger(outp, identity, token)
|
| 101 |
service_end = beijing()
|
| 102 |
timecost = service_end.timestamp() - service_start.timestamp()
|
|
|
|
| 117 |
info="Shared services",
|
| 118 |
),
|
| 119 |
gr.Radio(
|
| 120 |
+
["watermelon"],
|
| 121 |
+
value="watermelon",
|
| 122 |
info="Which game you want the AI to support?",
|
| 123 |
),
|
| 124 |
gr.Radio(
|
call_pgai.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from functools import cache
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
from typing import Any
|
| 5 |
+
from pgsoft.pgdate.date_utils import beijing
|
| 6 |
+
import json
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def post_to_pgai_helper(command: str) -> Any:
|
| 10 |
+
myobj = {
|
| 11 |
+
"code": os.getenv("pgai_code"),
|
| 12 |
+
"command": command,
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
header = {'accept': 'application/json', 'Content-Type': 'application/json'}
|
| 16 |
+
url_base = "https://steveagi-pgai.hf.space/games/watermelon/nlp"
|
| 17 |
+
url_read = f"{url_base}/r"
|
| 18 |
+
|
| 19 |
+
try:
|
| 20 |
+
res = requests.post(url_read, headers=header, json=myobj)
|
| 21 |
+
if res.status_code == 200:
|
| 22 |
+
print(res.text)
|
| 23 |
+
return res.text
|
| 24 |
+
else:
|
| 25 |
+
print(res.text)
|
| 26 |
+
return None
|
| 27 |
+
except Exception as e:
|
| 28 |
+
print(e)
|
| 29 |
+
return None
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def call_pgai(service, game, functionality, nlp_command, url, hf_token):
|
| 33 |
+
calling_start = beijing()
|
| 34 |
+
print(f"calling ai starts at {calling_start}")
|
| 35 |
+
try:
|
| 36 |
+
res = post_to_pgai_helper(nlp_command)
|
| 37 |
+
except Exception as e:
|
| 38 |
+
return (
|
| 39 |
+
f"{type(e)}, {str(e)}. \nyou may want to make "
|
| 40 |
+
+ "sure your hf_token is correct"
|
| 41 |
+
)
|
| 42 |
+
calling_end = beijing()
|
| 43 |
+
timecost = calling_end.timestamp() - calling_start.timestamp()
|
| 44 |
+
print(f"calling ai ends at {calling_end}, costs {timecost:.2f}s")
|
| 45 |
+
global from_cache
|
| 46 |
+
from_cache = False
|
| 47 |
+
if res is not None:
|
| 48 |
+
outp = json.loads(res)
|
| 49 |
+
return outp
|
| 50 |
+
else:
|
| 51 |
+
return None
|
requirements-local.txt
CHANGED
|
@@ -57,4 +57,4 @@ tzdata==2023.3
|
|
| 57 |
urllib3==2.0.6
|
| 58 |
uvicorn==0.23.2
|
| 59 |
websockets==11.0.3
|
| 60 |
-
git+ssh://git@github.com/east-and-west-magic/pgsoft.git@tag-2024-01-
|
|
|
|
| 57 |
urllib3==2.0.6
|
| 58 |
uvicorn==0.23.2
|
| 59 |
websockets==11.0.3
|
| 60 |
+
git+ssh://git@github.com/east-and-west-magic/pgsoft.git@tag-2024-01-11-a
|
requirements.txt
CHANGED
|
@@ -57,4 +57,4 @@ tzdata==2023.3
|
|
| 57 |
urllib3==2.0.6
|
| 58 |
uvicorn==0.23.2
|
| 59 |
websockets==11.0.3
|
| 60 |
-
git+https://github.com/east-and-west-magic/pgsoft.git@tag-2024-01-
|
|
|
|
| 57 |
urllib3==2.0.6
|
| 58 |
uvicorn==0.23.2
|
| 59 |
websockets==11.0.3
|
| 60 |
+
git+https://github.com/east-and-west-magic/pgsoft.git@tag-2024-01-11-a
|