File size: 1,956 Bytes
a2e1faa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr

from akinator.async_aki import Akinator
import akinator

aki = Akinator()
game_type = "en"
count = 0
to_verify = False

def clear_state():
    global count, to_verify
    count = 0
    to_verify = False

def change_game_type(value):
    global game_type
    game_type = value

async def fetch_response(message, history, value):    
    global count, to_verify, game_type
    count += 1
    if message == "start":
        q = await aki.start_game(
                language=game_type,
                child_mode=True
        )
        q = f"{count}. " + q
    elif (aki.progression <= 80) and (message == "back"):
        try:
            q = await aki.back()
            q = f"{count}. " + q
        except akinator.CantGoBackAnyFurther:
            pass
    elif aki.progression <= 80:
        q = await aki.answer(message)
        q = f"{count}. " + q
    elif to_verify and message == "yes":
        q = "Yay!!"
        clear_state()
    elif to_verify and message == "yes":
        q = "Oof.."
        clear_state()
    else:
        await aki.win()
        q = f"It's {aki.first_guess['name']} ({aki.first_guess['description']})! Was I correct?\n{aki.first_guess['absolute_picture_path']}\n\t"
        q = f"{count}. " + q

    return q

clear_button = gr.Button("Clear")
drop_down_ls = gr.Dropdown(choices=["en", "en_animals", "en_objects"], label="Game Mode")

chat = gr.ChatInterface(
            fn=fetch_response,
            textbox=gr.Textbox(placeholder="Ask me a yes or no question", container=False, scale=7),
            examples=[["start"], ["yes"], ["no"], ["i don't know"], ["probably"], ["probably not"], ["back"]],
            clear_btn=clear_button,
            additional_inputs=[drop_down_ls]
)

with gr.Blocks(fill_height=True) as demo:
    chat.render()
    clear_button.click(fn=clear_state)
    drop_down_ls.input(fn=change_game_type, inputs=[drop_down_ls])

if __name__ == "__main__":
    
    demo.launch()