File size: 2,595 Bytes
d877f5a
 
 
5046452
d877f5a
761ae49
d877f5a
703e6fc
d877f5a
761ae49
 
 
 
d877f5a
761ae49
 
 
d877f5a
 
761ae49
 
 
655703b
 
 
d877f5a
 
47e27a3
 
d877f5a
655703b
094500b
 
 
cb39903
703e6fc
d1c021a
896c15b
 
094500b
 
d1c021a
094500b
896c15b
 
 
703e6fc
761ae49
 
 
655703b
761ae49
655703b
761ae49
 
 
d877f5a
fbf93e6
4ef9db9
 
 
 
17f643b
d877f5a
 
896c15b
d4cb960
d877f5a
 
97eaafd
d877f5a
 
 
 
 
78c5616
d877f5a
761ae49
d877f5a
 
 
761ae49
 
6a7682d
761ae49
 
 
 
6a7682d
761ae49
d877f5a
761ae49
 
6a7682d
761ae49
 
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
import requests
import json
from datetime import datetime
from zoneinfo import ZoneInfo

import gradio as gr
from gradio_client import Client
from extract import extract

functionality_shared = [
    'games',
    'greet',
    'tokenization',
]

game_list = [
    'angry birds',
    'snake',
]

functionality_list = [
    "AI",
    "functionality",
    "json dedup",
    "json",
]

# redirect all traffic to the proxy sever
space = 'stevez-proxy'

def run(hf_token, service, game, functionality, nlp_command):
    """
    event handler
    """

    # reuse hf_token field as json string
    token, user_name, redirect, _ = extract(hf_token)
    if user_name is None:
        user_name = '__fake__'

    url = f'https://{space}.hf.space'
    if redirect is not None:
        url = f'https://{redirect}.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"

    try:
        if service not in functionality_shared[1:]:
            t = datetime.now()
            t = t.astimezone(ZoneInfo('Asia/Shanghai'))
            t = t.replace(microsecond=0)
            print(f"{t} [{game:<12}] [{user_name[:12]:<12}] {nlp_command}")

        client = Client(
            url,
            hf_token=token,
            verbose=False,
        )
        res = client.predict(
            user_name, service, game, functionality, nlp_command,
            api_name='/predict',
        )
        res2 = json.loads(res[0])
        return json.dumps(res2, 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],
            # label = "What do you want to do?",
            info = "What functionality?",
            ),
        "text"],
    outputs="text",
    title = "Demo",
)

demo.launch()