stevez commited on
Commit
d877f5a
·
1 Parent(s): ed43fbb
Files changed (1) hide show
  1. app.py +97 -0
app.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import json
3
+ from datetime import datetime
4
+
5
+ import gradio
6
+ from gradio_client import Client
7
+
8
+ function_list1 = [
9
+ "angry birds",
10
+ 'snake',
11
+ "greet",
12
+ "list AI backend functionality",
13
+ "show json",
14
+ "game"
15
+ # "flappy birds",
16
+ ]
17
+ function_list1 = [
18
+ "angry birds",
19
+ 'snake',
20
+ ]
21
+ function_list2 = [
22
+ "greet",
23
+ "list AI backend functionality",
24
+ "show json",
25
+ "game"
26
+ # "flappy birds",
27
+ ]
28
+ # function_list.pop()
29
+
30
+ space = 'stevez-abc123'
31
+ # space = 'stevez-test-public'
32
+
33
+ # url = f'https://{space}.hf.space/run/predict'
34
+ # url = f'https://{space}.hf.space/api/predict'
35
+ url = f'https://{space}.hf.space'
36
+
37
+ def run(hf_token, game, r, nlp_command):
38
+ if hf_token is None or hf_token == '':
39
+ return 'please specify hf token'
40
+ if game is None:
41
+ return "please specify which game"
42
+ if r is None:
43
+ return "please choose the AI functionality"
44
+ try:
45
+ print(f"{datetime.today()}\t{nlp_command}")
46
+ myobj = {
47
+ 'data': [game, r, nlp_command],
48
+ }
49
+ # myauth = ('Bearer', hf_token)
50
+ # x = requests.post(
51
+ # url,
52
+ # auth = myauth,
53
+ # json = myobj,
54
+ # )
55
+ # print(f"x: {x}")
56
+ # x2 = json.loads(x.text)
57
+ # res = x2['data'][0]
58
+ # res2 = json.loads(res)
59
+ client = Client(
60
+ url,
61
+ hf_token=hf_token,
62
+ )
63
+ res = client.predict(
64
+ r, nlp_command,
65
+ api_name='/predict',
66
+ )
67
+ res2 = json.loads(res[0])
68
+ # print(json.dumps(res2, indent=4))
69
+ # if (r == 'greet') or (r == 'angry birds'):
70
+ # res2['demo-version'] = "2.0.0.2023-08-04-a" # use gradio_client
71
+ return json.dumps(res2, indent=4)
72
+ except Exception as e:
73
+ return f"{type(e)}, {str(e)}. \nyou many want to make sure your hf_token is correct"
74
+
75
+ demo = gradio.Interface(
76
+ fn=run,
77
+ inputs=[
78
+ "text",
79
+ gradio.Radio(
80
+ function_list1,
81
+ value='angry birds',
82
+ label = "Which game?",
83
+ # info = "Greet for testing server",
84
+ info = "Only 'angry birds' is implemented as of now",
85
+ ),
86
+ gradio.Radio(
87
+ function_list2,
88
+ value='game',
89
+ label = "What do you want to do?",
90
+ info = "Greet for testing server",
91
+ ),
92
+ "text"],
93
+ outputs="text",
94
+ title = "Demo",
95
+ )
96
+
97
+ demo.launch()