Beracles commited on
Commit
111b72c
·
1 Parent(s): cbc040b

merge proxy

Browse files
Files changed (3) hide show
  1. .gitignore +2 -0
  2. app.py +46 -20
  3. app_util.py +49 -0
.gitignore CHANGED
@@ -1 +1,3 @@
1
  .venv
 
 
 
1
  .venv
2
+ __pycache__
3
+ *.json
app.py CHANGED
@@ -7,6 +7,13 @@ from zoneinfo import ZoneInfo
7
  import gradio as gr
8
  from gradio_client import Client
9
  from extract import extract
 
 
 
 
 
 
 
10
 
11
  functionality_shared = [
12
  "games",
@@ -14,6 +21,7 @@ functionality_shared = [
14
  "tokenization",
15
  "upload game",
16
  "download game",
 
17
  ]
18
 
19
  game_list = [
@@ -38,15 +46,15 @@ print(f"[Seattle]: {t.replace(microsecond=0)}")
38
 
39
 
40
  spaces = {
41
- "b_demo_hf": "stevez-proxy",
42
- "pgdemo2": "stevez-proxy2",
43
- "pgdemo3": "pgsoft-proxy3",
44
  }
45
 
46
  identity = os.environ.get("identity")
47
  if not identity:
48
  identity = "b_demo_hf"
49
- space = "stevez-proxy"
50
  if identity in spaces:
51
  space = spaces[identity]
52
 
@@ -78,36 +86,53 @@ def run(hf_token, service, game, functionality, nlp_command):
78
  if functionality == "AI":
79
  if nlp_command in ["", None]:
80
  return "please make sure the command is not empty"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
82
  try:
83
  if service not in functionality_shared[1:]:
84
- pass
85
-
 
 
86
  client = Client(
87
  url,
88
  hf_token=token,
89
  verbose=False,
90
  )
91
  res = client.predict(
92
- user_name,
93
  service,
94
  game,
95
  functionality,
96
- nlp_command,
97
- nlp_command,
98
  api_name="/predict",
99
  )
100
- # if service in [
101
- # "upload game",
102
- # "download game",
103
- # ]:
104
- # res2 = json.loads(res[0])
105
- # return json.dumps(res2,indent=4)
106
- # else:
107
- # res2 = json.loads(res[0])
108
- # return json.dumps(res2[0], indent=4)
109
- res2 = json.loads(res[0])
110
- return json.dumps(res2, indent=4)
111
  except Exception as e:
112
  return (
113
  f"{type(e)}, {str(e)}. \nyou may want to make sure your hf_token is correct"
@@ -138,6 +163,7 @@ demo = gr.Interface(
138
  ],
139
  outputs="text",
140
  title="Demo",
 
141
  )
142
 
143
  demo.launch()
 
7
  import gradio as gr
8
  from gradio_client import Client
9
  from extract import extract
10
+ import app_util
11
+
12
+ #######################
13
+ # proxy version
14
+ #######################
15
+ proxy_version = "1.0.0-2023-11-28-a" # merge to demo
16
+
17
 
18
  functionality_shared = [
19
  "games",
 
21
  "tokenization",
22
  "upload game",
23
  "download game",
24
+ "list games",
25
  ]
26
 
27
  game_list = [
 
46
 
47
 
48
  spaces = {
49
+ "b_demo_hf": "stevez-ai",
50
+ "pgdemo2": "stevez-ai2",
51
+ "pgdemo3": "stevez-ai3",
52
  }
53
 
54
  identity = os.environ.get("identity")
55
  if not identity:
56
  identity = "b_demo_hf"
57
+ space = "stevez-ai"
58
  if identity in spaces:
59
  space = spaces[identity]
60
 
 
86
  if functionality == "AI":
87
  if nlp_command in ["", None]:
88
  return "please make sure the command is not empty"
89
+ if service == "download game":
90
+ res = app_util.call_clouddisk(service, nlp_command, token)
91
+ if res is None:
92
+ outp = {"status": "Failure"}
93
+ else:
94
+ outp = {"status": "OK", "result": json.loads(res)}
95
+ return json.dumps(outp)
96
+ if service == "upload game":
97
+ res = app_util.call_clouddisk(service, nlp_command, token)
98
+ if res is None:
99
+ outp = {"status": "Failure"}
100
+ else:
101
+ outp = {"status": "OK", "result": res}
102
+ return json.dumps(outp)
103
+ if service == "list games":
104
+ res = app_util.call_clouddisk(service, nlp_command, token)
105
+ if res is None:
106
+ outp = {"status": "Failure"}
107
+ else:
108
+ outp = {"status": "OK", "result": json.loads(res)["result"]}
109
+ return json.dumps(outp)
110
 
111
  try:
112
  if service not in functionality_shared[1:]:
113
+ t = datetime.now()
114
+ t = t.astimezone(ZoneInfo("Asia/Shanghai"))
115
+ # t = t.replace(microsecond=0)
116
+ print(f"{t} [{user_name}] [{game}] {nlp_command}")
117
  client = Client(
118
  url,
119
  hf_token=token,
120
  verbose=False,
121
  )
122
  res = client.predict(
 
123
  service,
124
  game,
125
  functionality,
126
+ nlp_command, # hidden,
 
127
  api_name="/predict",
128
  )
129
+ outp = json.loads(res[0])
130
+ # add proxy version info to the output
131
+ outp["proxy-version"] = proxy_version
132
+ outp["user"] = user_name
133
+ outp["game"] = game
134
+ app_util.call_logger(outp, token)
135
+ return json.dumps(outp, indent=4)
 
 
 
 
136
  except Exception as e:
137
  return (
138
  f"{type(e)}, {str(e)}. \nyou may want to make sure your hf_token is correct"
 
163
  ],
164
  outputs="text",
165
  title="Demo",
166
+ allow_flagging="never",
167
  )
168
 
169
  demo.launch()
app_util.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+
3
+ from gradio_client import Client
4
+
5
+
6
+ def call_logger(log_info, hf_token) -> None:
7
+ #######################
8
+ # logging
9
+ #######################
10
+ urls = [
11
+ 'https://hubei-hunan-logger.hf.space',
12
+ 'https://hubei-hunan-logger2.hf.space',
13
+ ]
14
+
15
+ for url in urls:
16
+ try:
17
+ client = Client(
18
+ url,
19
+ hf_token = hf_token,
20
+ verbose = False,
21
+ )
22
+ client.submit(json.dumps(log_info))
23
+ print(f'[logging to {url}] OK')
24
+ except Exception as e:
25
+ print(f'[logging to {url}] error: {e}')
26
+
27
+
28
+ def call_clouddisk(service, arg, hf_token) -> str | None:
29
+ ##########################
30
+ # upload or download game
31
+ ##########################
32
+ urls = [
33
+ "https://pgsoft-clouddisk.hf.space",
34
+ # "https://pgsoft-clouddisk2.hf.space",
35
+ ]
36
+
37
+ for url in urls:
38
+ try:
39
+ client = Client(
40
+ url,
41
+ hf_token=hf_token,
42
+ verbose=False,
43
+ )
44
+ res = client.predict(service, arg, api_name="/predict")
45
+ print("[call_clouddisk] OK")
46
+ return res
47
+ except Exception as e:
48
+ print(f"[call_clouddisk] {type(e)}: {e}")
49
+ return None