aspgvu commited on
Commit
d401e1c
·
1 Parent(s): c10ee62
Files changed (2) hide show
  1. app.py +95 -0
  2. requirements.txt +1 -0
app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+
4
+ url = 'https://example.com/api/endpoint' # Replace with the actual API endpoint URL
5
+
6
+ def send_request(data, url):
7
+ while True:
8
+ try:
9
+ response = requests.post(url, data=data)
10
+
11
+ if response.status_code == 200:
12
+ print(f"POST request successful!\n{response.json()}")
13
+ return response.json()
14
+ else:
15
+ print(f"POST request failed with status code: {response.status_code}")
16
+
17
+ except requests.exceptions.RequestException as e:
18
+ print(f"An error occurred: {e}")
19
+
20
+ def add_new_game(user_id):
21
+ return send_request(locals(), url="https://3n1e869vil.execute-api.eu-central-1.amazonaws.com/test/add_new_game")
22
+
23
+ def start_new_story(user_id, game_id, character_name, character_description):
24
+ return send_request(locals(), url="https://3n1e869vil.execute-api.eu-central-1.amazonaws.com/test/start_new_story")
25
+
26
+ def add_user_msg(user_id, game_id, role, content):
27
+ return send_request(locals(), url="https://3n1e869vil.execute-api.eu-central-1.amazonaws.com/test/add_user_msg")
28
+
29
+ with gr.Blocks(theme=gr.themes.Default(), css="footer{display:none !important}") as demo:
30
+ game_var = gr.State({"user_id": "abcdabcd-abcd-abcd-abcd-abcdabcdabcd", "game_id": "", "player_name": ""})
31
+
32
+ with gr.Column(visible=True) as character_form:
33
+ name = gr.Textbox(label="Character name",
34
+ info="",
35
+ lines=1,
36
+ value="Oppo")
37
+ description = gr.Textbox(label="Character description",
38
+ info="",
39
+ lines=10,
40
+ 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.")
41
+ update_character_btn = gr.Button("Begin Your Journey")
42
+
43
+ with gr.Row(visible=False) as story_form:
44
+ with gr.Column(scale=1, min_width=300):
45
+ story_display = gr.Textbox(label="Story", interactive=False)
46
+
47
+ with gr.Column(scale=1, min_width=300):
48
+ player_response = gr.Textbox(label="Player response",
49
+ info="",
50
+ lines=3,
51
+ value="")
52
+ response_btn = gr.Button("Respond")
53
+
54
+ # Button actions
55
+ def char_form_action(game, name, description):
56
+ # First round
57
+ game["player_name"] = name
58
+ game["game_id"] = add_new_game(game["user_id"])
59
+ dm_msg = start_new_story(game["user_id"], game["game_id"], game["player_name"], description)
60
+
61
+ # Return list, not dict -> it is problematic to show the progress on the disappearing widgets for some weird reason....
62
+ # 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.
63
+
64
+ return [game,
65
+ "",
66
+ "",
67
+ gr.update(visible=False),
68
+ gr.update(visible=True),
69
+ gr.update(value=dm_msg)]
70
+
71
+ update_character_btn.click(fn=char_form_action,
72
+ inputs=[game_var, name, description],
73
+ outputs=[game_var, name, description, character_form, story_form, story_display])
74
+
75
+
76
+ def story_form_action(game, player_response):
77
+ if len(player_response.strip()) != 0:
78
+ dm_msg = add_user_msg(game["user_id"], game["game_id"], game["player_name"], player_response)
79
+ story_str = f'{game["player_name"]}: {player_response}\n\nDM: {dm_msg}'
80
+ return {
81
+ game_var: game,
82
+ story_display: gr.update(value=story_str),
83
+ }
84
+ else:
85
+ return {
86
+ game_var: game,
87
+ story_display: gr.update(visible=True),
88
+ }
89
+
90
+
91
+ response_btn.click(fn=story_form_action,
92
+ inputs=[game_var, player_response],
93
+ outputs=[game_var, story_display])
94
+
95
+ demo.launch(debug=True)
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ requests