|
import requests |
|
import json |
|
import os |
|
from datetime import datetime |
|
from zoneinfo import ZoneInfo |
|
|
|
import gradio as gr |
|
from gradio_client import Client |
|
from extract import extract |
|
import app_util |
|
|
|
|
|
|
|
|
|
proxy_version = "1.0.0-2023-11-28-a" |
|
|
|
|
|
functionality_shared = [ |
|
"games", |
|
"greet", |
|
"tokenization", |
|
"upload game", |
|
"download game", |
|
"list games", |
|
] |
|
|
|
game_list = [ |
|
"angry birds", |
|
"snake", |
|
] |
|
|
|
functionality_list = [ |
|
"AI", |
|
"functionality", |
|
"json dedup", |
|
"json", |
|
] |
|
|
|
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)}") |
|
|
|
|
|
|
|
|
|
spaces = { |
|
"b_demo_hf": "stevez-ai", |
|
"pgdemo2": "stevez-ai2", |
|
"pgdemo3": "stevez-ai3", |
|
} |
|
|
|
identity = os.environ.get("identity") |
|
if not identity: |
|
identity = "b_demo_hf" |
|
space = "stevez-ai" |
|
if identity in spaces: |
|
space = spaces[identity] |
|
|
|
|
|
def run(hf_token, service, game, functionality, nlp_command): |
|
""" |
|
event handler |
|
""" |
|
|
|
|
|
token, user_name, redirect, _ = extract(hf_token) |
|
if user_name is None: |
|
user_name = "__fake__" |
|
|
|
|
|
global space |
|
if redirect is not None: |
|
space = redirect |
|
url = f"https://{space}.hf.space" |
|
|
|
if token is None or token == "": |
|
return "please specify hf token" |
|
|
|
if service not in functionality_shared[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" |
|
if service == "download game": |
|
res = app_util.call_clouddisk(service, nlp_command, token) |
|
if res is None: |
|
outp = {"status": "Failure"} |
|
else: |
|
outp = {"status": "OK", "result": json.loads(res)} |
|
return json.dumps(outp) |
|
if service == "upload game": |
|
res = app_util.call_clouddisk(service, nlp_command, token) |
|
if res is None: |
|
outp = {"status": "Failure"} |
|
else: |
|
outp = {"status": "OK", "result": res} |
|
return json.dumps(outp) |
|
if service == "list games": |
|
res = app_util.call_clouddisk(service, nlp_command, token) |
|
if res is None: |
|
outp = {"status": "Failure"} |
|
else: |
|
outp = {"status": "OK", "result": json.loads(res)["result"]} |
|
return json.dumps(outp) |
|
|
|
try: |
|
if service not in functionality_shared[1:]: |
|
t = datetime.now() |
|
t = t.astimezone(ZoneInfo("Asia/Shanghai")) |
|
|
|
print(f"{t} [{user_name}] [{game}] {nlp_command}") |
|
client = Client( |
|
url, |
|
hf_token=token, |
|
verbose=False, |
|
) |
|
res = client.predict( |
|
service, |
|
game, |
|
functionality, |
|
nlp_command, |
|
api_name="/predict", |
|
) |
|
outp = json.loads(res[0]) |
|
|
|
outp["proxy-version"] = proxy_version |
|
outp["user"] = user_name |
|
outp["game"] = game |
|
app_util.call_logger(outp, token) |
|
return json.dumps(outp, indent=4) |
|
except Exception as e: |
|
return ( |
|
f"{type(e)}, {str(e)}. \nyou may want to make sure your hf_token is correct" |
|
) |
|
|
|
|
|
demo = gr.Interface( |
|
fn=run, |
|
inputs=[ |
|
"text", |
|
gr.Radio( |
|
functionality_shared, |
|
value=functionality_shared[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], |
|
|
|
info="What functionality?", |
|
), |
|
"text", |
|
], |
|
outputs="text", |
|
title="Demo", |
|
allow_flagging="never", |
|
) |
|
|
|
demo.launch() |
|
|