Spaces:
Sleeping
Sleeping
| import requests | |
| import json | |
| from datetime import datetime | |
| import gradio as gr | |
| from gradio_client import Client | |
| functionality_shared = [ | |
| 'games', | |
| 'greet', | |
| 'tokenization', | |
| ] | |
| game_list = [ | |
| 'angry birds', | |
| 'snake', | |
| ] | |
| functionality_list = [ | |
| "AI", | |
| "functionality", | |
| "json dedup", | |
| "json", | |
| ] | |
| space = 'stevez-bbc123' | |
| # space = 'stevez-test-public' | |
| # url = f'https://{space}.hf.space/run/predict' | |
| # url = f'https://{space}.hf.space/api/predict' | |
| url = f'https://{space}.hf.space' | |
| def run(hf_token, service, game, functionality, nlp_command): | |
| if hf_token is None or hf_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:]: | |
| print(f"{datetime.today()} [{game:<16}] {nlp_command}") | |
| # myobj = { | |
| # 'data': [service, game, r, nlp_command], | |
| # } | |
| # myauth = ('Bearer', hf_token) | |
| # x = requests.post( | |
| # url, | |
| # auth = myauth, | |
| # json = myobj, | |
| # ) | |
| # print(f"x: {x}") | |
| # x2 = json.loads(x.text) | |
| # res = x2['data'][0] | |
| # res2 = json.loads(res) | |
| client = Client( | |
| url, | |
| hf_token=hf_token, | |
| verbose=False, | |
| ) | |
| res = client.predict( | |
| 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() | |