Jiang commited on
Commit
5bf3b30
·
unverified ·
1 Parent(s): a41de9b

deprecate clouddisk (#20)

Browse files

* deprecate clouddisk and use pgsoft.pgfile instead

Files changed (4) hide show
  1. app.py +10 -22
  2. app_util.py +108 -14
  3. requirements-local.txt +1 -1
  4. requirements.txt +1 -1
app.py CHANGED
@@ -7,6 +7,7 @@ import gradio as gr
7
  from extract import extract
8
  import app_util
9
  from pgsoft.pgconst.const import service_list, functionality_list, game_list
 
10
  from pgsoft.pgdate.date_utils import beijing
11
  import call_ai
12
  from str_util import normalize_text
@@ -14,7 +15,7 @@ from str_util import normalize_text
14
  #######################
15
  # proxy version
16
  #######################
17
- proxy_version = "1.0.0-2023-12-13-a" # reconstruct ai calling
18
 
19
  t = datetime.now()
20
  t = t.astimezone(ZoneInfo("Asia/Shanghai"))
@@ -23,28 +24,16 @@ t = t.astimezone(ZoneInfo("America/Los_Angeles"))
23
  print(f"[Seattle]: {t.replace(microsecond=0)}")
24
 
25
 
26
- ais = {
27
- "b_demo_hf": "stevez/ai",
28
- "pgdemo2": "stevez/ai2",
29
- "pgdemo_dev": "stevez/ai_dev",
30
- }
31
-
32
- clouddisks = {
33
- "b_demo_hf": "pgsoft/clouddisk",
34
- "pgdemo2": "pgsoft/clouddisk",
35
- "pgdemo_dev": "pgsoft/clouddisk_dev",
36
- }
37
-
38
  identity = os.environ.get("identity")
39
  print(f"identity: {identity}")
40
  if not identity:
41
  identity = "pgdemo_dev"
42
- ai = "stevez/ai_dev"
43
  if identity in ais:
44
  ai = ais[identity]
45
- clouddisk = "pgsoft/clouddisk_dev"
46
- if identity in clouddisks:
47
- clouddisk = clouddisks[identity]
48
 
49
 
50
  def run(hf_token, service, game, functionality, nlp_command):
@@ -58,7 +47,7 @@ def run(hf_token, service, game, functionality, nlp_command):
58
  user_name = "__fake__"
59
 
60
  # redirect all traffic to the proxy sever
61
- global ai, clouddisk
62
  if redirect is not None:
63
  ai = redirect
64
 
@@ -77,12 +66,11 @@ def run(hf_token, service, game, functionality, nlp_command):
77
  service_start = beijing()
78
  print(f"<<<<<<<<<<<<<< service starts at {service_start} <<<<<<<<<<<<<<")
79
  if service in ["download game", "upload game", "list games"]:
80
- res = app_util.call_clouddisk(clouddisk, service, nlp_command, token)
81
- res = json.loads(res)
82
- if res["result"] is None:
83
  outp = {"status": "Failure"}
84
  else:
85
- outp = {"status": "OK", "result": res["result"]}
86
  else:
87
  assert "games" in service_list
88
  if service == "games":
 
7
  from extract import extract
8
  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 call_ai
13
  from str_util import normalize_text
 
15
  #######################
16
  # proxy version
17
  #######################
18
+ proxy_version = "1.0.0-2024-01-02-a" # reconstruct ai calling
19
 
20
  t = datetime.now()
21
  t = t.astimezone(ZoneInfo("Asia/Shanghai"))
 
24
  print(f"[Seattle]: {t.replace(microsecond=0)}")
25
 
26
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  identity = os.environ.get("identity")
28
  print(f"identity: {identity}")
29
  if not identity:
30
  identity = "pgdemo_dev"
31
+ ai = "stevez-ai-dev"
32
  if identity in ais:
33
  ai = ais[identity]
34
+ db_token = os.environ.get("db_token")
35
+ if db_token:
36
+ print(db_token[:5])
37
 
38
 
39
  def run(hf_token, service, game, functionality, nlp_command):
 
47
  user_name = "__fake__"
48
 
49
  # redirect all traffic to the proxy sever
50
+ global ai
51
  if redirect is not None:
52
  ai = redirect
53
 
 
66
  service_start = beijing()
67
  print(f"<<<<<<<<<<<<<< service starts at {service_start} <<<<<<<<<<<<<<")
68
  if service in ["download game", "upload game", "list games"]:
69
+ res = app_util.file_service(service, nlp_command, db_token)
70
+ if not res:
 
71
  outp = {"status": "Failure"}
72
  else:
73
+ outp = {"status": "OK", "result": res}
74
  else:
75
  assert "games" in service_list
76
  if service == "games":
app_util.py CHANGED
@@ -2,6 +2,11 @@ import json
2
 
3
  from gradio_client import Client
4
  from pgsoft.pgdate.date_utils import beijing
 
 
 
 
 
5
 
6
 
7
  def call_logger(log_info, caller, hf_token) -> None:
@@ -32,19 +37,108 @@ def call_logger(log_info, caller, hf_token) -> None:
32
  print(f"calling logger ends at {calling_end}, costs {timecost:.2f}s")
33
 
34
 
35
- def call_clouddisk(url: str, service, arg, hf_token) -> str | None:
36
- ##########################
37
- # upload or download game
38
- ##########################
39
- try:
40
- client = Client(
41
- url,
42
- hf_token=hf_token,
43
- verbose=False,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  )
45
- res = client.predict(service, arg, api_name="/predict")
46
- print(f"[call_clouddisk] {service} from {url} [OK]")
47
- return res
48
- except Exception as e:
49
- print(f"[call_clouddisk] {type(e)}: {e}")
 
 
50
  return None
 
2
 
3
  from gradio_client import Client
4
  from pgsoft.pgdate.date_utils import beijing
5
+ from pgsoft.pgfile import download, upload, list_files
6
+ from pgsoft.pghash.md5 import md5
7
+ from time import sleep
8
+ from huggingface_hub import HfApi
9
+ import os
10
 
11
 
12
  def call_logger(log_info, caller, hf_token) -> None:
 
37
  print(f"calling logger ends at {calling_end}, costs {timecost:.2f}s")
38
 
39
 
40
+ dataset_id = "hubei-hunan/games"
41
+ local_dir = "game"
42
+ if not os.path.exists(local_dir):
43
+ os.mkdir(local_dir)
44
+ hf_api = HfApi()
45
+
46
+
47
+ def file_service(service, arg: str, token: str):
48
+ """download game, upload game, or list games"""
49
+ if service == "download game":
50
+ filepath = arg.strip() + ".json"
51
+ res = download(
52
+ dataset_id,
53
+ filepath,
54
+ repo_type="dataset",
55
+ localdir=local_dir,
56
+ token=token,
57
+ )
58
+ if not res:
59
+ return None
60
+ with open(res, "r") as f:
61
+ outp = json.load(f)
62
+ print(f"[{service}] OK")
63
+ return outp
64
+ elif service == "upload game":
65
+ try:
66
+ game = json.loads(arg)
67
+ except json.JSONDecodeError as e:
68
+ print(f"[{service}] {type(e)}: {e}")
69
+ return None
70
+
71
+ if not isinstance(game, dict):
72
+ print(f"[{service}] not a dict")
73
+ return None
74
+
75
+ needed_keys = ["game-file", "device-id"]
76
+ for key in needed_keys:
77
+ if key not in game:
78
+ print(f'[{service}] error: missed "{key}"')
79
+ return None
80
+ if not isinstance(game["device-id"], str):
81
+ print(f'[{service}] error: "device-id" is not a str')
82
+ return None
83
+ if not isinstance(game["game-file"], dict):
84
+ print(f'[{service}] error: "game-file" is not a dict')
85
+ return None
86
+
87
+ obj = {
88
+ "upload-time": beijing().__str__(),
89
+ "game-file": game["game-file"],
90
+ }
91
+
92
+ maxtry = 5
93
+ for retry in range(maxtry):
94
+ md5code = md5(obj)
95
+ if not hf_api.file_exists(
96
+ repo_id=dataset_id,
97
+ filename=md5code + ".json",
98
+ repo_type="dataset",
99
+ token=token,
100
+ ):
101
+ break
102
+ sleep(0.1)
103
+ obj["upload-time"] = beijing().__str__()
104
+ maxtry -= 1
105
+ filename = md5code + ".json"
106
+ if not maxtry and hf_api.file_exists(
107
+ repo_id=dataset_id,
108
+ filename=md5code + ".json",
109
+ repo_type="dataset",
110
+ token=token,
111
+ ):
112
+ print(f"[{service}] error: file exists")
113
+ return None
114
+ localpath = os.sep.join([local_dir, filename])
115
+ content = json.dumps(game, indent=4)
116
+ with open(localpath, "w") as f:
117
+ f.write(content)
118
+ res = upload(
119
+ localpath,
120
+ filename,
121
+ dataset_id,
122
+ "dataset",
123
+ token,
124
+ f"Updated at {beijing()}",
125
+ )
126
+ if not res:
127
+ print(f"[{service}] error: upload failed")
128
+ return None
129
+ print(f"[{service}] OK")
130
+ return md5code
131
+ elif service == "list games":
132
+ games = list_files(
133
+ repo_id=dataset_id,
134
+ repo_type="dataset",
135
+ token=token,
136
  )
137
+ if not games:
138
+ return None
139
+ games = {item.split(".")[0]: item for item in games if len(item) >= 32}
140
+ print(f"[{service}] OK")
141
+ return games
142
+ else:
143
+ print(f"[{service}] error: unknown service")
144
  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-2023-12-01-a
 
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-02-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-2023-12-01-a
 
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-02-a