Beracles commited on
Commit
9a51839
·
1 Parent(s): 5924ba7

use call_ai

Browse files
Files changed (1) hide show
  1. app.py +23 -53
app.py CHANGED
@@ -1,21 +1,19 @@
1
- import requests
2
  import json
3
  import os
4
  from datetime import datetime
5
  from zoneinfo import ZoneInfo
6
 
7
  import gradio as gr
8
- from gradio_client import Client
9
  from extract import extract
10
  import app_util
11
- import cache
12
  from pgsoft.pgconst.const import service_list, functionality_list, game_list
13
  from pgsoft.pgdate.date_utils import beijing
 
14
 
15
  #######################
16
  # proxy version
17
  #######################
18
- proxy_version = "1.0.0-2023-12-12-a" # use cache
19
 
20
  t = datetime.now()
21
  t = t.astimezone(ZoneInfo("Asia/Shanghai"))
@@ -23,8 +21,6 @@ print(f"[Beijing]: {t.replace(microsecond=0)}")
23
  t = t.astimezone(ZoneInfo("America/Los_Angeles"))
24
  print(f"[Seattle]: {t.replace(microsecond=0)}")
25
 
26
- # assert False
27
-
28
 
29
  spaces = {
30
  "b_demo_hf": "stevez-ai",
@@ -38,8 +34,6 @@ if not identity:
38
  space = "stevez-ai"
39
  if identity in spaces:
40
  space = spaces[identity]
41
- filepath = os.sep.join(["cache", "cached_ai.json"])
42
- cache.load_cache(filepath)
43
 
44
 
45
  def run(hf_token, service, game, functionality, nlp_command):
@@ -91,51 +85,27 @@ def run(hf_token, service, game, functionality, nlp_command):
91
  else:
92
  outp = {"status": "OK", "result": json.loads(res)["result"]}
93
  else:
94
- try:
95
- assert "games" in service_list
96
- if service == "games":
97
- print(f"{beijing()} [{user_name}] [{game}] {nlp_command}")
98
- outp = cache.get_cache(nlp_command)
99
- if outp is None:
100
- client = Client(
101
- url,
102
- hf_token=token,
103
- verbose=False,
104
- )
105
- calling_start = beijing()
106
- print(f"calling ai starts at {calling_start}")
107
- res = client.predict(
108
- service,
109
- game,
110
- functionality,
111
- nlp_command, # hidden,
112
- api_name="/predict",
113
- )
114
- calling_end = beijing()
115
- timecost = calling_end.timestamp() - calling_start.timestamp()
116
- print(f"calling ai ends at {calling_end}, costs {timecost:.2f}s")
117
- outp = json.loads(res)
118
- outp["cache"] = False
119
- cache.add_cache(nlp_command, outp)
120
- else:
121
- print(f"[cache] return from cache")
122
- # add proxy version info to the output
123
- outp["proxy-version"] = proxy_version
124
- outp["user"] = user_name
125
- outp["game"] = game
126
- if source:
127
- outp["source"] = source
128
- calling_start = beijing()
129
- print(f"calling logger starts at {beijing()}")
130
- app_util.call_logger(outp, identity, token)
131
- calling_end = beijing()
132
- timecost = calling_end.timestamp() - calling_start.timestamp()
133
- print(f"calling logger ends at {calling_end}, costs {timecost:.2f}s")
134
- except Exception as e:
135
- return (
136
- f"{type(e)}, {str(e)}. \nyou may want to make "
137
- + "sure your hf_token is correct"
138
- )
139
  service_end = beijing()
140
  timecost = service_end.timestamp() - service_start.timestamp()
141
  print(
 
 
1
  import json
2
  import os
3
  from datetime import datetime
4
  from zoneinfo import ZoneInfo
5
 
6
  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
+ from call_ai import call_ai
12
 
13
  #######################
14
  # proxy version
15
  #######################
16
+ proxy_version = "1.0.0-2023-12-13-a" # reconstruct ai calling
17
 
18
  t = datetime.now()
19
  t = t.astimezone(ZoneInfo("Asia/Shanghai"))
 
21
  t = t.astimezone(ZoneInfo("America/Los_Angeles"))
22
  print(f"[Seattle]: {t.replace(microsecond=0)}")
23
 
 
 
24
 
25
  spaces = {
26
  "b_demo_hf": "stevez-ai",
 
34
  space = "stevez-ai"
35
  if identity in spaces:
36
  space = spaces[identity]
 
 
37
 
38
 
39
  def run(hf_token, service, game, functionality, nlp_command):
 
85
  else:
86
  outp = {"status": "OK", "result": json.loads(res)["result"]}
87
  else:
88
+ assert "games" in service_list
89
+ if service == "games":
90
+ print(f"{beijing()} [{user_name}] [{game}] {nlp_command}")
91
+ outp = call_ai(
92
+ service,
93
+ game,
94
+ functionality,
95
+ nlp_command,
96
+ url,
97
+ token,
98
+ )
99
+ if isinstance(outp, str):
100
+ return outp
101
+ # add proxy version info to the output
102
+ outp["timestamp"] = beijing().__str__()
103
+ outp["proxy-version"] = proxy_version
104
+ outp["user"] = user_name
105
+ outp["game"] = game
106
+ if source:
107
+ outp["source"] = source
108
+ app_util.call_logger(outp, identity, token)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
  service_end = beijing()
110
  timecost = service_end.timestamp() - service_start.timestamp()
111
  print(