Merge pull request #3 from east-and-west-magic/feature-three-pipeline
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import requests
|
| 2 |
import json
|
|
|
|
| 3 |
from datetime import datetime
|
| 4 |
from zoneinfo import ZoneInfo
|
| 5 |
|
|
@@ -8,16 +9,16 @@ from gradio_client import Client
|
|
| 8 |
from extract import extract
|
| 9 |
|
| 10 |
functionality_shared = [
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
"upload game",
|
| 15 |
"download game",
|
| 16 |
]
|
| 17 |
|
| 18 |
game_list = [
|
| 19 |
-
|
| 20 |
-
|
| 21 |
]
|
| 22 |
|
| 23 |
functionality_list = [
|
|
@@ -28,13 +29,28 @@ functionality_list = [
|
|
| 28 |
]
|
| 29 |
|
| 30 |
t = datetime.now()
|
| 31 |
-
t = t.astimezone(ZoneInfo(
|
| 32 |
print(f"[Beijing]: {t.replace(microsecond=0)}")
|
| 33 |
-
t = t.astimezone(ZoneInfo(
|
| 34 |
print(f"[Seattle]: {t.replace(microsecond=0)}")
|
| 35 |
|
| 36 |
# assert False
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
def run(hf_token, service, game, functionality, nlp_command):
|
| 39 |
"""
|
| 40 |
event handler
|
|
@@ -43,22 +59,22 @@ def run(hf_token, service, game, functionality, nlp_command):
|
|
| 43 |
# reuse hf_token field as json string
|
| 44 |
token, user_name, redirect, _ = extract(hf_token)
|
| 45 |
if user_name is None:
|
| 46 |
-
user_name =
|
| 47 |
|
| 48 |
# redirect all traffic to the proxy sever
|
| 49 |
-
space = 'stevez-proxy'
|
| 50 |
if redirect is not None:
|
| 51 |
space = redirect
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
| 55 |
|
| 56 |
if service not in functionality_shared[1:]:
|
| 57 |
if game is None:
|
| 58 |
return "please specify which game"
|
| 59 |
if functionality is None:
|
| 60 |
return "please choose the AI functionality"
|
| 61 |
-
if functionality ==
|
| 62 |
if nlp_command in ["", None]:
|
| 63 |
return "please make sure the command is not empty"
|
| 64 |
|
|
@@ -67,13 +83,18 @@ def run(hf_token, service, game, functionality, nlp_command):
|
|
| 67 |
pass
|
| 68 |
|
| 69 |
client = Client(
|
| 70 |
-
|
| 71 |
hf_token=token,
|
| 72 |
verbose=False,
|
| 73 |
)
|
| 74 |
res = client.predict(
|
| 75 |
-
user_name,
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
)
|
| 78 |
# if service in [
|
| 79 |
# "upload game",
|
|
@@ -87,31 +108,35 @@ def run(hf_token, service, game, functionality, nlp_command):
|
|
| 87 |
res2 = json.loads(res[0])
|
| 88 |
return json.dumps(res2, indent=4)
|
| 89 |
except Exception as e:
|
| 90 |
-
return
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
| 92 |
demo = gr.Interface(
|
| 93 |
-
fn=run,
|
| 94 |
inputs=[
|
| 95 |
"text",
|
| 96 |
gr.Radio(
|
| 97 |
functionality_shared,
|
| 98 |
value=functionality_shared[0],
|
| 99 |
-
info
|
| 100 |
-
|
| 101 |
gr.Radio(
|
| 102 |
game_list,
|
| 103 |
-
value=game_list[1],
|
| 104 |
-
info
|
| 105 |
-
|
| 106 |
gr.Radio(
|
| 107 |
functionality_list,
|
| 108 |
value=functionality_list[0],
|
| 109 |
# label = "What do you want to do?",
|
| 110 |
-
info
|
| 111 |
-
|
| 112 |
-
"text"
|
|
|
|
| 113 |
outputs="text",
|
| 114 |
-
title
|
| 115 |
)
|
| 116 |
|
| 117 |
demo.launch()
|
|
|
|
| 1 |
import requests
|
| 2 |
import json
|
| 3 |
+
import os
|
| 4 |
from datetime import datetime
|
| 5 |
from zoneinfo import ZoneInfo
|
| 6 |
|
|
|
|
| 9 |
from extract import extract
|
| 10 |
|
| 11 |
functionality_shared = [
|
| 12 |
+
"games",
|
| 13 |
+
"greet",
|
| 14 |
+
"tokenization",
|
| 15 |
"upload game",
|
| 16 |
"download game",
|
| 17 |
]
|
| 18 |
|
| 19 |
game_list = [
|
| 20 |
+
"angry birds",
|
| 21 |
+
"snake",
|
| 22 |
]
|
| 23 |
|
| 24 |
functionality_list = [
|
|
|
|
| 29 |
]
|
| 30 |
|
| 31 |
t = datetime.now()
|
| 32 |
+
t = t.astimezone(ZoneInfo("Asia/Shanghai"))
|
| 33 |
print(f"[Beijing]: {t.replace(microsecond=0)}")
|
| 34 |
+
t = t.astimezone(ZoneInfo("America/Los_Angeles"))
|
| 35 |
print(f"[Seattle]: {t.replace(microsecond=0)}")
|
| 36 |
|
| 37 |
# assert False
|
| 38 |
|
| 39 |
+
|
| 40 |
+
spaces = {
|
| 41 |
+
"b_demo_hf": "stevez-proxy",
|
| 42 |
+
"pgdemo2": "stevez-proxy2",
|
| 43 |
+
"pgdemo3": "stevez-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 |
+
|
| 53 |
+
|
| 54 |
def run(hf_token, service, game, functionality, nlp_command):
|
| 55 |
"""
|
| 56 |
event handler
|
|
|
|
| 59 |
# reuse hf_token field as json string
|
| 60 |
token, user_name, redirect, _ = extract(hf_token)
|
| 61 |
if user_name is None:
|
| 62 |
+
user_name = "__fake__"
|
| 63 |
|
| 64 |
# redirect all traffic to the proxy sever
|
|
|
|
| 65 |
if redirect is not None:
|
| 66 |
space = redirect
|
| 67 |
+
url = f"https://{space}.hf.space"
|
| 68 |
+
|
| 69 |
+
if token is None or token == "":
|
| 70 |
+
return "please specify hf token"
|
| 71 |
|
| 72 |
if service not in functionality_shared[1:]:
|
| 73 |
if game is None:
|
| 74 |
return "please specify which game"
|
| 75 |
if functionality is None:
|
| 76 |
return "please choose the AI functionality"
|
| 77 |
+
if functionality == "AI":
|
| 78 |
if nlp_command in ["", None]:
|
| 79 |
return "please make sure the command is not empty"
|
| 80 |
|
|
|
|
| 83 |
pass
|
| 84 |
|
| 85 |
client = Client(
|
| 86 |
+
url,
|
| 87 |
hf_token=token,
|
| 88 |
verbose=False,
|
| 89 |
)
|
| 90 |
res = client.predict(
|
| 91 |
+
user_name,
|
| 92 |
+
service,
|
| 93 |
+
game,
|
| 94 |
+
functionality,
|
| 95 |
+
nlp_command,
|
| 96 |
+
nlp_command,
|
| 97 |
+
api_name="/predict",
|
| 98 |
)
|
| 99 |
# if service in [
|
| 100 |
# "upload game",
|
|
|
|
| 108 |
res2 = json.loads(res[0])
|
| 109 |
return json.dumps(res2, indent=4)
|
| 110 |
except Exception as e:
|
| 111 |
+
return (
|
| 112 |
+
f"{type(e)}, {str(e)}. \nyou may want to make sure your hf_token is correct"
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
|
| 116 |
demo = gr.Interface(
|
| 117 |
+
fn=run,
|
| 118 |
inputs=[
|
| 119 |
"text",
|
| 120 |
gr.Radio(
|
| 121 |
functionality_shared,
|
| 122 |
value=functionality_shared[0],
|
| 123 |
+
info="Shared services",
|
| 124 |
+
),
|
| 125 |
gr.Radio(
|
| 126 |
game_list,
|
| 127 |
+
value=game_list[1],
|
| 128 |
+
info="Which game you want the AI to support?",
|
| 129 |
+
),
|
| 130 |
gr.Radio(
|
| 131 |
functionality_list,
|
| 132 |
value=functionality_list[0],
|
| 133 |
# label = "What do you want to do?",
|
| 134 |
+
info="What functionality?",
|
| 135 |
+
),
|
| 136 |
+
"text",
|
| 137 |
+
],
|
| 138 |
outputs="text",
|
| 139 |
+
title="Demo",
|
| 140 |
)
|
| 141 |
|
| 142 |
demo.launch()
|