File size: 4,129 Bytes
d401e1c
 
 
05e4ef7
d401e1c
 
05e4ef7
d401e1c
 
 
 
 
 
 
 
 
53bbcb2
d401e1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a884c1a
 
d401e1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a884c1a
d401e1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import requests

def send_request(params, url):
    while True:
        try:
            response = requests.post(url, params=params)

            if response.status_code == 200:
                print(f"POST request successful!\n{response.json()}")
                return response.json()
            else:
                print(f"POST request failed with status code: {response.status_code}")

        except requests.exceptions.RequestException as e:
            print(f"An error occurred: {e}")
        break

def add_new_game(user_id):
    return send_request(locals(), url="https://3n1e869vil.execute-api.eu-central-1.amazonaws.com/test/add_new_game")

def start_new_story(user_id, game_id, character_name, character_description):
    return send_request(locals(), url="https://3n1e869vil.execute-api.eu-central-1.amazonaws.com/test/start_new_story")

def add_user_msg(user_id, game_id, role, content):
    return send_request(locals(), url="https://3n1e869vil.execute-api.eu-central-1.amazonaws.com/test/add_user_msg")

with gr.Blocks(theme=gr.themes.Default(), css="footer{display:none !important}") as demo:
    game_var = gr.State({"user_id": "abcdabcd-abcd-abcd-abcd-abcdabcdabcd", "game_id": "", "player_name": ""})

    with gr.Column(visible=True) as character_form:
        name = gr.Textbox(label="Character name",
                          info="",
                          lines=1,
                          value="Oppo")
        description = gr.Textbox(label="Character description",
                                 info="",
                                 lines=10,
                                 value="Gracious loser of card games, patron of the arts and friend to everyone from dukes to barmaids. It's a shame he likes to steal.")
        update_character_btn = gr.Button("Begin Your Journey")

    with gr.Row(visible=False) as story_form:
        with gr.Column(scale=1, min_width=300):
            story_display = gr.Textbox(label="Story", interactive=False)

        with gr.Column(scale=1, min_width=300):
            player_response = gr.Textbox(label="Player response",
                                         info="",
                                         lines=3,
                                         value="")
            response_btn = gr.Button("Respond")

    # Button actions
    def char_form_action(game, name, description):
        # First round
        game["player_name"] = name
        game["game_id"] = add_new_game(game["user_id"])['id']
        dm_msg = start_new_story(game["user_id"], game["game_id"], game["player_name"], description)['content']

        # Return list, not dict -> it is problematic to show the progress on the disappearing widgets for some weird reason....
        # ValueError: Returned dictionary included some keys as Components. Either all keys must be Components to assign Component values, or return a List of values to assign output values in order.

        return [game,
                "",
                "",
                gr.update(visible=False),
                gr.update(visible=True),
                gr.update(value=dm_msg)]

    update_character_btn.click(fn=char_form_action,
                               inputs=[game_var, name, description],
                               outputs=[game_var, name, description, character_form, story_form, story_display])


    def story_form_action(game, player_response):
        if len(player_response.strip()) != 0:
            dm_msg = add_user_msg(game["user_id"], game["game_id"], game["player_name"], player_response)['content']
            story_str = f'{game["player_name"]}: {player_response}\n\nDM: {dm_msg}'
            return {
                game_var: game,
                story_display: gr.update(value=story_str),
            }
        else:
            return {
                game_var: game,
                story_display: gr.update(visible=True),
            }


    response_btn.click(fn=story_form_action,
                       inputs=[game_var, player_response],
                       outputs=[game_var, story_display])

demo.launch(debug=True)