Spaces:
Runtime error
Runtime error
kwabs22
commited on
Commit
·
756ea61
1
Parent(s):
a4229ac
Load game supports media now?
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import gradio as gr
|
|
| 2 |
import random
|
| 3 |
import json
|
| 4 |
import re
|
|
|
|
| 5 |
|
| 6 |
# Default configuration template
|
| 7 |
default_config = {
|
|
@@ -169,7 +170,7 @@ def show_elements(text):
|
|
| 169 |
# if location == 'end':
|
| 170 |
# continue
|
| 171 |
|
| 172 |
-
# with gr.Accordion(f"Location: {location}"):
|
| 173 |
# description = gr.Textbox(label="Description", value=details['description'], interactive=True)
|
| 174 |
# outputs.append(description)
|
| 175 |
|
|
@@ -182,16 +183,15 @@ def show_elements(text):
|
|
| 182 |
# transitions = gr.Textbox(label="Transitions", value=json.dumps(details['transitions']), interactive=True)
|
| 183 |
# outputs.append(transitions)
|
| 184 |
|
| 185 |
-
# def update_json():
|
| 186 |
# updated_data = {"masterlocation1": {}}
|
| 187 |
-
# for
|
| 188 |
-
#
|
| 189 |
-
# continue
|
| 190 |
# updated_data["masterlocation1"][location] = {
|
| 191 |
-
# "description":
|
| 192 |
-
# "events": json.loads(
|
| 193 |
-
# "choices": json.loads(
|
| 194 |
-
# "transitions": json.loads(
|
| 195 |
# }
|
| 196 |
# updated_data["masterlocation1"]["end"] = masterlocation1["end"]
|
| 197 |
# return json.dumps(updated_data, indent=2)
|
|
@@ -199,7 +199,7 @@ def show_elements(text):
|
|
| 199 |
# update_button = gr.Button("Update JSON")
|
| 200 |
# json_output = gr.Textbox(label="Updated JSON", lines=10)
|
| 201 |
|
| 202 |
-
# update_button.click(update_json, outputs=json_output)
|
| 203 |
|
| 204 |
# return outputs + [update_button, json_output]
|
| 205 |
|
|
@@ -213,7 +213,7 @@ def show_elements_json_input(json_input):
|
|
| 213 |
if location == 'end':
|
| 214 |
continue
|
| 215 |
|
| 216 |
-
with gr.Accordion(f"Location: {location}"):
|
| 217 |
description = gr.Textbox(label="Description", value=details['description'], interactive=True)
|
| 218 |
outputs.append(description)
|
| 219 |
|
|
@@ -225,16 +225,21 @@ def show_elements_json_input(json_input):
|
|
| 225 |
|
| 226 |
transitions = gr.Textbox(label="Transitions", value=json.dumps(details['transitions']), interactive=True)
|
| 227 |
outputs.append(transitions)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
def update_json(*current_values):
|
| 230 |
updated_data = {"masterlocation1": {}}
|
| 231 |
locations = [loc for loc in masterlocation1.keys() if loc != 'end']
|
| 232 |
for i, location in enumerate(locations):
|
| 233 |
updated_data["masterlocation1"][location] = {
|
| 234 |
-
"description": current_values[i*
|
| 235 |
-
"events": json.loads(current_values[i*
|
| 236 |
-
"choices": json.loads(current_values[i*
|
| 237 |
-
"transitions": json.loads(current_values[i*
|
|
|
|
| 238 |
}
|
| 239 |
updated_data["masterlocation1"]["end"] = masterlocation1["end"]
|
| 240 |
return json.dumps(updated_data, indent=2)
|
|
@@ -246,6 +251,19 @@ def show_elements_json_input(json_input):
|
|
| 246 |
|
| 247 |
return outputs + [update_button, json_output]
|
| 248 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
def convert_timeline_to_game_structure(timeline):
|
| 250 |
lines = timeline.split('\n')
|
| 251 |
game_structure = {}
|
|
@@ -490,7 +508,7 @@ def start_game(starting_location='village', starting_state='start'):
|
|
| 490 |
description, choices, game_log = game_session.get_current_state_info()
|
| 491 |
return description, choices, game_log, game_session
|
| 492 |
|
| 493 |
-
def make_choice(choice, game_session):
|
| 494 |
if not choice:
|
| 495 |
description, choices, game_log = game_session.get_current_state_info()
|
| 496 |
return description, choices, "Please select a choice before proceeding.", game_session
|
|
@@ -500,7 +518,7 @@ def make_choice(choice, game_session):
|
|
| 500 |
|
| 501 |
return result[0], gr.update(choices=result[1]), result[2], game_session
|
| 502 |
|
| 503 |
-
def load_game(custom_config=None):
|
| 504 |
global all_states
|
| 505 |
if custom_config:
|
| 506 |
try:
|
|
@@ -515,7 +533,17 @@ def load_game(custom_config=None):
|
|
| 515 |
game_session = GameSession(starting_location, starting_state)
|
| 516 |
description, choices, game_log = game_session.get_current_state_info()
|
| 517 |
new_path_errors = validate_transitions(all_states)
|
| 518 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 519 |
return gr.update(value=f"Custom configuration loaded successfully! \n{new_path_errors}"), game_log, description, gr.update(choices=choices), game_session, gr.update(value=custom_config)
|
| 520 |
except json.JSONDecodeError as e:
|
| 521 |
# Get the line number and column of the error
|
|
@@ -581,7 +609,6 @@ with gr.Blocks() as demo:
|
|
| 581 |
generate_no_media_timeline_points = gr.Slider(minimum=1, value=5, step=1, maximum=30, label="Choose the amount of media timeline points")
|
| 582 |
generate_with_media_check = gr.Checkbox(label="Generate with media", value=True)
|
| 583 |
generate_button = gr.Button("Generate Story and Timeline")
|
| 584 |
-
gr.HTML("Still under construction - The update json below doesnt work for so dont edit here yet")
|
| 585 |
|
| 586 |
@gr.render(inputs=game_structure_output_text_with_media)
|
| 587 |
def update(game_structure_output_text_with_media):
|
|
@@ -635,7 +662,10 @@ with gr.Blocks() as demo:
|
|
| 635 |
gr.Markdown("# Text-based Adventure Game")
|
| 636 |
|
| 637 |
wadescription = gr.Textbox(label="Current Situation", lines=4, value=initgameinfo[0])
|
| 638 |
-
|
|
|
|
|
|
|
|
|
|
| 639 |
wachoices = gr.Radio(label="Your Choices", choices=initgameinfo[1])
|
| 640 |
wasubmit_btn = gr.Button("Make Choice")
|
| 641 |
wagame_log = gr.Textbox(label="Game Log", lines=20, value=initgameinfo[2])
|
|
@@ -654,8 +684,8 @@ with gr.Blocks() as demo:
|
|
| 654 |
|
| 655 |
wacustom_configbtn.click(
|
| 656 |
load_game,
|
| 657 |
-
inputs=[wacustom_config],
|
| 658 |
-
outputs=[waerror_box, wagame_log, wadescription, wachoices, wagame_session, wacustom_config]
|
| 659 |
)
|
| 660 |
with gr.Tab("Custom JS Config Creator"):
|
| 661 |
gr.HTML("Companion Space for zerogpu / client api workflow planning for a way to send a zip to the Basic Game Engine at the bottom of https://huggingface.co/spaces/KwabsHug/TestSvelteStatic (Also to test how much can be done majority on cpu)")
|
|
|
|
| 2 |
import random
|
| 3 |
import json
|
| 4 |
import re
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
# Default configuration template
|
| 8 |
default_config = {
|
|
|
|
| 170 |
# if location == 'end':
|
| 171 |
# continue
|
| 172 |
|
| 173 |
+
# with gr.Accordion(f"Location: {location} - Previous desciption {details['description']}", open=False):
|
| 174 |
# description = gr.Textbox(label="Description", value=details['description'], interactive=True)
|
| 175 |
# outputs.append(description)
|
| 176 |
|
|
|
|
| 183 |
# transitions = gr.Textbox(label="Transitions", value=json.dumps(details['transitions']), interactive=True)
|
| 184 |
# outputs.append(transitions)
|
| 185 |
|
| 186 |
+
# def update_json(*current_values):
|
| 187 |
# updated_data = {"masterlocation1": {}}
|
| 188 |
+
# locations = [loc for loc in masterlocation1.keys() if loc != 'end']
|
| 189 |
+
# for i, location in enumerate(locations):
|
|
|
|
| 190 |
# updated_data["masterlocation1"][location] = {
|
| 191 |
+
# "description": current_values[i*4],
|
| 192 |
+
# "events": json.loads(current_values[i*4 + 1]),
|
| 193 |
+
# "choices": json.loads(current_values[i*4 + 2]),
|
| 194 |
+
# "transitions": json.loads(current_values[i*4 + 3])
|
| 195 |
# }
|
| 196 |
# updated_data["masterlocation1"]["end"] = masterlocation1["end"]
|
| 197 |
# return json.dumps(updated_data, indent=2)
|
|
|
|
| 199 |
# update_button = gr.Button("Update JSON")
|
| 200 |
# json_output = gr.Textbox(label="Updated JSON", lines=10)
|
| 201 |
|
| 202 |
+
# update_button.click(update_json, inputs=outputs, outputs=json_output)
|
| 203 |
|
| 204 |
# return outputs + [update_button, json_output]
|
| 205 |
|
|
|
|
| 213 |
if location == 'end':
|
| 214 |
continue
|
| 215 |
|
| 216 |
+
with gr.Accordion(f"Location: {location} - Previous description {details['description']}", open=False):
|
| 217 |
description = gr.Textbox(label="Description", value=details['description'], interactive=True)
|
| 218 |
outputs.append(description)
|
| 219 |
|
|
|
|
| 225 |
|
| 226 |
transitions = gr.Textbox(label="Transitions", value=json.dumps(details['transitions']), interactive=True)
|
| 227 |
outputs.append(transitions)
|
| 228 |
+
|
| 229 |
+
# New media field
|
| 230 |
+
media = gr.Textbox(label="Media", value=json.dumps(details.get('media', [])), interactive=True)
|
| 231 |
+
outputs.append(media)
|
| 232 |
|
| 233 |
def update_json(*current_values):
|
| 234 |
updated_data = {"masterlocation1": {}}
|
| 235 |
locations = [loc for loc in masterlocation1.keys() if loc != 'end']
|
| 236 |
for i, location in enumerate(locations):
|
| 237 |
updated_data["masterlocation1"][location] = {
|
| 238 |
+
"description": current_values[i*5],
|
| 239 |
+
"events": json.loads(current_values[i*5 + 1]),
|
| 240 |
+
"choices": json.loads(current_values[i*5 + 2]),
|
| 241 |
+
"transitions": json.loads(current_values[i*5 + 3]),
|
| 242 |
+
"media": json.loads(current_values[i*5 + 4]) # New media field
|
| 243 |
}
|
| 244 |
updated_data["masterlocation1"]["end"] = masterlocation1["end"]
|
| 245 |
return json.dumps(updated_data, indent=2)
|
|
|
|
| 251 |
|
| 252 |
return outputs + [update_button, json_output]
|
| 253 |
|
| 254 |
+
def create_media_component(file_path):
|
| 255 |
+
_, extension = os.path.splitext(file_path)
|
| 256 |
+
extension = extension.lower()[1:] # Remove the dot and convert to lowercase
|
| 257 |
+
|
| 258 |
+
if extension in ['jpg', 'jpeg', 'png', 'gif']:
|
| 259 |
+
return gr.Image(value=file_path, label="Image Input")
|
| 260 |
+
elif extension in ['mp4', 'avi', 'mov']:
|
| 261 |
+
return gr.Video(value=file_path, label="Video Input")
|
| 262 |
+
elif extension in ['mp3', 'wav', 'ogg']:
|
| 263 |
+
return gr.Audio(value=file_path, label="Audio Input")
|
| 264 |
+
else:
|
| 265 |
+
return gr.Textbox(value=file_path, label=f"File: {os.path.basename(file_path)}")
|
| 266 |
+
|
| 267 |
def convert_timeline_to_game_structure(timeline):
|
| 268 |
lines = timeline.split('\n')
|
| 269 |
game_structure = {}
|
|
|
|
| 508 |
description, choices, game_log = game_session.get_current_state_info()
|
| 509 |
return description, choices, game_log, game_session
|
| 510 |
|
| 511 |
+
def make_choice(choice, game_session): #Calls the nested make choice function in the game session class
|
| 512 |
if not choice:
|
| 513 |
description, choices, game_log = game_session.get_current_state_info()
|
| 514 |
return description, choices, "Please select a choice before proceeding.", game_session
|
|
|
|
| 518 |
|
| 519 |
return result[0], gr.update(choices=result[1]), result[2], game_session
|
| 520 |
|
| 521 |
+
def load_game(custom_config=None, with_media=False):
|
| 522 |
global all_states
|
| 523 |
if custom_config:
|
| 524 |
try:
|
|
|
|
| 533 |
game_session = GameSession(starting_location, starting_state)
|
| 534 |
description, choices, game_log = game_session.get_current_state_info()
|
| 535 |
new_path_errors = validate_transitions(all_states)
|
| 536 |
+
media = None
|
| 537 |
+
output_media = None
|
| 538 |
+
|
| 539 |
+
if with_media:
|
| 540 |
+
if all_states[starting_location][starting_state][media]:
|
| 541 |
+
media = all_states[starting_location][starting_state][media]
|
| 542 |
+
for media_path in media:
|
| 543 |
+
media_component = create_media_component(media_path)
|
| 544 |
+
output_media.append(media_component)
|
| 545 |
+
return gr.update(value=f"Custom configuration loaded successfully! \n{new_path_errors}"), game_log, description, gr.update(choices=choices), game_session, gr.update(value=custom_config), output_media
|
| 546 |
+
|
| 547 |
return gr.update(value=f"Custom configuration loaded successfully! \n{new_path_errors}"), game_log, description, gr.update(choices=choices), game_session, gr.update(value=custom_config)
|
| 548 |
except json.JSONDecodeError as e:
|
| 549 |
# Get the line number and column of the error
|
|
|
|
| 609 |
generate_no_media_timeline_points = gr.Slider(minimum=1, value=5, step=1, maximum=30, label="Choose the amount of media timeline points")
|
| 610 |
generate_with_media_check = gr.Checkbox(label="Generate with media", value=True)
|
| 611 |
generate_button = gr.Button("Generate Story and Timeline")
|
|
|
|
| 612 |
|
| 613 |
@gr.render(inputs=game_structure_output_text_with_media)
|
| 614 |
def update(game_structure_output_text_with_media):
|
|
|
|
| 662 |
gr.Markdown("# Text-based Adventure Game")
|
| 663 |
|
| 664 |
wadescription = gr.Textbox(label="Current Situation", lines=4, value=initgameinfo[0])
|
| 665 |
+
wamediabool = gr.State(value=True)
|
| 666 |
+
with gr.Group() as wamedia:
|
| 667 |
+
gr.HTML("Placeholder to load all media tests")
|
| 668 |
+
|
| 669 |
wachoices = gr.Radio(label="Your Choices", choices=initgameinfo[1])
|
| 670 |
wasubmit_btn = gr.Button("Make Choice")
|
| 671 |
wagame_log = gr.Textbox(label="Game Log", lines=20, value=initgameinfo[2])
|
|
|
|
| 684 |
|
| 685 |
wacustom_configbtn.click(
|
| 686 |
load_game,
|
| 687 |
+
inputs=[wacustom_config, wamediabool],
|
| 688 |
+
outputs=[waerror_box, wagame_log, wadescription, wachoices, wagame_session, wacustom_config, wamedia]
|
| 689 |
)
|
| 690 |
with gr.Tab("Custom JS Config Creator"):
|
| 691 |
gr.HTML("Companion Space for zerogpu / client api workflow planning for a way to send a zip to the Basic Game Engine at the bottom of https://huggingface.co/spaces/KwabsHug/TestSvelteStatic (Also to test how much can be done majority on cpu)")
|