File size: 4,900 Bytes
c960e90
 
 
 
 
 
 
 
 
 
 
 
 
e3f3029
c960e90
 
 
 
 
 
 
 
e3f3029
 
c960e90
e3f3029
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c960e90
e3f3029
 
c960e90
e3f3029
c960e90
e3f3029
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c960e90
e3f3029
 
 
c960e90
 
 
 
 
 
 
e3f3029
 
c960e90
 
 
 
 
 
071fea3
 
 
 
c960e90
 
 
 
 
 
 
 
 
 
 
e3f3029
c960e90
 
 
 
 
 
e3f3029
 
c960e90
 
 
 
 
 
 
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import gradio as gr

from akinator.async_aki import Akinator
import akinator
import time

aki = Akinator()

### gradio state variables
count = gr.State(value=1)
to_verify = gr.State(value=False)
game_type = gr.State(value="en")
game_ended = gr.State(value=False)
last_call = gr.State(value=False)

def clear_state():
    print("clear_state")
    return 1, False

def change_game_type(drop_down_value):
    return drop_down_value

def orchestrate_game_state(message, count, to_verify, last_call, game_ended):
    
    time.sleep(1.5)
    if aki.progression is not None or message == "start":
        if message == "start":
            count_add_one = count + 1
            count = 2 if count > 2 else count_add_one
            game_ended = False
        elif (aki.progression <= 80) and (message == "back") and (not game_ended):
            count -= 1
            to_verify = False
        elif (aki.progression <= 80) and (not game_ended):
            count += 1
            to_verify = False
        elif to_verify and (message in ["yes", "no"]) and (not game_ended):
            game_ended = True
        elif (not game_ended) and not last_call:
            count += 1
            last_call = True
        elif (not game_ended) and last_call:
            count += 1
            to_verify = True
            last_call = False

    print("orchestrate_game_state", aki.progression, message, count, to_verify, last_call, game_ended)
    return count, to_verify, last_call, game_ended

async def fetch_response(message, history, drop_down_value, count, to_verify, last_call, game_type, game_ended):

    print("fetch_response", message, drop_down_value, count, to_verify, last_call, game_type, game_ended)
    if aki.progression is not None or message == "start":
        if message == "start":
            count = 1 if count > 1 else count
            q = await aki.start_game(
                    language=game_type,
                    child_mode=True
            )
            q = f"{count}. " + q
        elif (aki.progression <= 80) and (message == "back") and (not game_ended):
            try:
                q = await aki.back()
                q = f"{count}. " + q
            except akinator.CantGoBackAnyFurther:
                pass
        elif (aki.progression <= 80) and (not game_ended):
            q = await aki.answer(message)
            q = f"{count}. " + q
        elif to_verify and message == "yes" and (not game_ended):
            q = "Yay!!"
        elif to_verify and message == "no" and (not game_ended):
            q = "Oof.."
        elif (not game_ended) and not last_call:
            q = await aki.answer(message)
            q = f"{count}. " + q
        elif (not game_ended) and last_call:
            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
        elif game_ended:
            q = "The game has ended. To replay, enter `start` again."
        return q

retry_button = gr.Button("Retry", interactive=False)
undo_button = gr.Button("Undo", interactive=False)
clear_button = gr.Button("Clear")
submit_button = gr.Button("Submit")
input_textbox = gr.Textbox(
                    placeholder=(
                            "To play, click [start] and submit. "
                            "Thereafter, answer me genuinely 😉, "
                            "and I will guess what is in your mind :)"
                    ),
                    interactive=False,
                    container=False, 
                    scale=2
)
drop_down_ls = gr.Dropdown(choices=[
                                "en", "en_animals", "en_objects", 
                                "cn", "jp", "jp_animals", "id"
                            ], 
                           value="en",
                           label="Game Mode")

chat = gr.ChatInterface(
            fn=fetch_response,
            textbox=input_textbox,
            examples=[["start"], ["yes"], ["no"], ["i don't know"], ["probably"], ["probably not"], ["back"]],
            retry_btn=retry_button,
            undo_btn=undo_button,
            clear_btn=clear_button,
            submit_btn=submit_button,
            additional_inputs=[drop_down_ls, count, to_verify, last_call, game_type, game_ended]
)

with gr.Blocks(fill_height=True) as demo:
    clear_button.click(fn=clear_state, inputs=None, outputs=[count, to_verify])
    drop_down_ls.input(fn=change_game_type, inputs=[drop_down_ls], outputs=[game_type])
    submit_button.click(fn=orchestrate_game_state, 
                        inputs=[input_textbox, count, to_verify, last_call, game_ended], 
                        outputs=[count, to_verify, last_call, game_ended],
                        trigger_mode="always_last")

    chat.render()

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