File size: 3,855 Bytes
d877f5a 74b5ebb d877f5a 5046452 d877f5a 761ae49 703e6fc 111b72c 5711245 5bf3b30 f9d919d f950953 3818a89 111b72c 5bf3b30 d877f5a 651f405 74b5ebb ff0123b 74b5ebb ff0123b 651f405 74b5ebb 89a35b5 74b5ebb 0019c58 5bf3b30 572ca12 5bf3b30 74b5ebb 655703b 094500b cb39903 703e6fc 458f0bb 896c15b 74b5ebb 094500b 6354b1b 5bf3b30 d1c021a 572ca12 7a375af 74b5ebb 703e6fc 5711245 761ae49 655703b 761ae49 74b5ebb 761ae49 f9d919d 90abc51 408959d 5bf3b30 111b72c 5bf3b30 f9d919d 9a51839 924963e f950953 9a51839 7a375af 9a51839 f950953 9a51839 f9d919d 23586d1 90abc51 f9d919d 74b5ebb 761ae49 74b5ebb d877f5a 761ae49 5711245 74b5ebb 761ae49 74b5ebb 761ae49 6a7682d 761ae49 74b5ebb d877f5a 74b5ebb 111b72c d877f5a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
import json
import os
from datetime import datetime
from zoneinfo import ZoneInfo
import gradio as gr
from extract import extract
import app_util
from pgsoft.pgconst.const import service_list, functionality_list, game_list
from pgsoft.pghost import ais
from pgsoft.pgdate.date_utils import beijing
import call_ai
from str_util import normalize_text
#######################
# proxy version
#######################
proxy_version = "1.0.0-2024-01-02-a" # reconstruct ai calling
t = datetime.now()
t = t.astimezone(ZoneInfo("Asia/Shanghai"))
print(f"[Beijing]: {t.replace(microsecond=0)}")
t = t.astimezone(ZoneInfo("America/Los_Angeles"))
print(f"[Seattle]: {t.replace(microsecond=0)}")
identity = os.environ.get("identity")
print(f"identity: {identity}")
if not identity:
identity = "pgdemo_dev"
ai = "stevez-ai-dev"
if identity in ais:
ai = ais[identity]
db_token = os.environ.get("db_token")
if db_token:
print(db_token[:5])
def run(hf_token, service, game, functionality, nlp_command):
"""
event handler
"""
# reuse hf_token field as json string
token, user_name, redirect, source, _ = extract(hf_token)
if user_name is None:
user_name = "__fake__"
# redirect all traffic to the proxy sever
global ai
if redirect is not None:
ai = redirect
ai_url = f"https://{ai}.hf.space"
if token is None or token == "":
return "please specify hf token"
if service not in service_list[1:]:
if game is None:
return "please specify which game"
if functionality is None:
return "please choose the AI functionality"
if functionality == "AI":
if nlp_command in ["", None]:
return "please make sure the command is not empty"
service_start = beijing()
print(f"<<<<<<<<<<<<<< service starts at {service_start} <<<<<<<<<<<<<<")
if service in ["download game", "upload game", "list games"]:
res = app_util.file_service(service, nlp_command, db_token)
if not res:
outp = {"status": "Failure"}
else:
outp = {"status": "OK", "result": res}
else:
assert "games" in service_list
if service == "games":
print(f"{beijing()} [{user_name}] [{game}] {nlp_command}")
nlp_command = normalize_text(nlp_command)
call_ai.from_cache = True
outp = call_ai.call_ai(
service,
game,
functionality,
nlp_command,
ai_url,
token,
)
if isinstance(outp, str):
return outp
# add proxy version info to the output
outp["timestamp"] = beijing().__str__()
outp["proxy-version"] = proxy_version
outp["user"] = user_name
outp["game"] = game
if source:
outp["source"] = source
outp["cache"] = call_ai.from_cache
app_util.call_logger(outp, identity, token)
service_end = beijing()
timecost = service_end.timestamp() - service_start.timestamp()
print(
f">>>>>>>>>>>>>>> service ends at {service_end}, "
+ f"costs {timecost:.2f}s >>>>>>>>>>>>>>>\n"
)
return json.dumps(outp, indent=4)
demo = gr.Interface(
fn=run,
inputs=[
"text",
gr.Radio(
service_list,
value=service_list[0],
info="Shared services",
),
gr.Radio(
game_list,
value=game_list[1],
info="Which game you want the AI to support?",
),
gr.Radio(
functionality_list,
value=functionality_list[0],
# label = "What do you want to do?",
info="What functionality?",
),
"text",
],
outputs="text",
title="Demo",
allow_flagging="never",
)
demo.launch()
|