|
import requests |
|
import json |
|
from datetime import datetime |
|
|
|
import gradio |
|
from gradio_client import Client |
|
|
|
function_list1 = [ |
|
"angry birds", |
|
'snake', |
|
"greet", |
|
"list AI backend functionality", |
|
"show json", |
|
"game" |
|
|
|
] |
|
function_list1 = [ |
|
"angry birds", |
|
'snake', |
|
] |
|
function_list2 = [ |
|
"greet", |
|
"list AI backend functionality", |
|
"show json", |
|
"game" |
|
|
|
] |
|
|
|
|
|
space = 'stevez-abc123' |
|
|
|
|
|
|
|
|
|
url = f'https://{space}.hf.space' |
|
|
|
def run(hf_token, game, r, nlp_command): |
|
if hf_token is None or hf_token == '': |
|
return 'please specify hf token' |
|
if game is None: |
|
return "please specify which game" |
|
if r is None: |
|
return "please choose the AI functionality" |
|
try: |
|
print(f"{datetime.today()}\t{nlp_command}") |
|
myobj = { |
|
'data': [game, r, nlp_command], |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
client = Client( |
|
url, |
|
hf_token=hf_token, |
|
) |
|
res = client.predict( |
|
r, 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 many want to make sure your hf_token is correct" |
|
|
|
demo = gradio.Interface( |
|
fn=run, |
|
inputs=[ |
|
"text", |
|
gradio.Radio( |
|
function_list1, |
|
value='angry birds', |
|
label = "Which game?", |
|
|
|
info = "Only 'angry birds' is implemented as of now", |
|
), |
|
gradio.Radio( |
|
function_list2, |
|
value='game', |
|
label = "What do you want to do?", |
|
info = "Greet for testing server", |
|
), |
|
"text"], |
|
outputs="text", |
|
title = "Demo", |
|
) |
|
|
|
demo.launch() |
|
|