Spaces:
Running
on
Zero
Running
on
Zero
kwabs22
commited on
Commit
·
5609a56
1
Parent(s):
94b8cb6
Timeline to config for testing added
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
import random
|
3 |
import json
|
|
|
4 |
|
5 |
# Default configuration template
|
6 |
default_config = {
|
@@ -36,6 +37,7 @@ def add_target(targets_items, name, x, y, collisionType, collisiontext):
|
|
36 |
|
37 |
#-----------------------------------------------------------------------------------------------------------------------------------
|
38 |
|
|
|
39 |
# List of player engagement UI elements
|
40 |
player_engagement_items = [
|
41 |
"Health Bar", "Mana/Energy Bar", "Experience Bar", "Mini-Map", "Quest Tracker",
|
@@ -104,6 +106,183 @@ def generate_story_and_timeline():
|
|
104 |
|
105 |
return formatted_timeline, story
|
106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
#-----------------------------------------------------------------------------------------------------------------------------------
|
108 |
|
109 |
class Player:
|
@@ -325,7 +504,6 @@ def load_game(custom_config=None):
|
|
325 |
|
326 |
initgameinfo = start_game()
|
327 |
|
328 |
-
|
329 |
#-----------------------------------------------------------------------------------------------------------------------------------
|
330 |
|
331 |
with gr.Blocks() as demo:
|
@@ -419,6 +597,26 @@ with gr.Blocks() as demo:
|
|
419 |
generate_button.click(generate_story_and_timeline, inputs=[], outputs=[timeline_output, story_output])
|
420 |
with gr.Tab("Asset generation considered"):
|
421 |
gr.HTML("placeholder")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
422 |
with gr.Tab("Test Example State Machine"):
|
423 |
with gr.Row():
|
424 |
with gr.Column(scale=2):
|
|
|
1 |
import gradio as gr
|
2 |
import random
|
3 |
import json
|
4 |
+
import re
|
5 |
|
6 |
# Default configuration template
|
7 |
default_config = {
|
|
|
37 |
|
38 |
#-----------------------------------------------------------------------------------------------------------------------------------
|
39 |
|
40 |
+
|
41 |
# List of player engagement UI elements
|
42 |
player_engagement_items = [
|
43 |
"Health Bar", "Mana/Energy Bar", "Experience Bar", "Mini-Map", "Quest Tracker",
|
|
|
106 |
|
107 |
return formatted_timeline, story
|
108 |
|
109 |
+
media_file_types = ["image", "video", "audio"]
|
110 |
+
|
111 |
+
def generate_media_file_list(n):
|
112 |
+
return [random.choice(media_file_types) for _ in range(n)]
|
113 |
+
|
114 |
+
# Example usage
|
115 |
+
media_files = generate_media_file_list(10)
|
116 |
+
#print(media_files)
|
117 |
+
|
118 |
+
def render_media_with_dropdowns(media_files):
|
119 |
+
rendered_output = []
|
120 |
+
for index, media in enumerate(media_files):
|
121 |
+
iframe = f'<iframe src="{media}_source_{index}.html"></iframe>'
|
122 |
+
dropdown = f'''
|
123 |
+
<select>
|
124 |
+
<option value="{media}_source_1">Source 1</option>
|
125 |
+
<option value="{media}_source_2">Source 2</option>
|
126 |
+
<option value="{media}_source_3">Source 3</option>
|
127 |
+
</select>
|
128 |
+
'''
|
129 |
+
rendered_output.append(f"{iframe}{dropdown}")
|
130 |
+
return rendered_output
|
131 |
+
|
132 |
+
# Example usage
|
133 |
+
rendered_media = render_media_with_dropdowns(media_files)
|
134 |
+
#for item in rendered_media:
|
135 |
+
# print(item)
|
136 |
+
|
137 |
+
def save_to_placeholder(rendered_media):
|
138 |
+
placeholder = []
|
139 |
+
for item in rendered_media:
|
140 |
+
placeholder.append(item)
|
141 |
+
return placeholder
|
142 |
+
|
143 |
+
# Example usage
|
144 |
+
placeholder = save_to_placeholder(rendered_media)
|
145 |
+
#for item in placeholder:
|
146 |
+
# print(item)
|
147 |
+
|
148 |
+
# Example usage with and without media-related items
|
149 |
+
timeline_with_media, story_with_media = generate_story_and_timeline(include_media=True)
|
150 |
+
timeline_without_media, story_without_media = generate_story_and_timeline(include_media=False)
|
151 |
+
|
152 |
+
def split_content_into_blocks(timeline):
|
153 |
+
blocks = []
|
154 |
+
current_text_block = []
|
155 |
+
|
156 |
+
for entry in timeline.split('\n'):
|
157 |
+
if 'Media -' in entry:
|
158 |
+
# If we have accumulated text, add it as a block
|
159 |
+
if current_text_block:
|
160 |
+
blocks.append((' '.join(current_text_block), 'text'))
|
161 |
+
current_text_block = []
|
162 |
+
|
163 |
+
# Extract the iframe and dropdown
|
164 |
+
match = re.search(r'<iframe.*?</select>', entry, re.DOTALL)
|
165 |
+
if match:
|
166 |
+
blocks.append((match.group(), 'iframe'))
|
167 |
+
else:
|
168 |
+
current_text_block.append(entry)
|
169 |
+
|
170 |
+
# Add any remaining text as a final block
|
171 |
+
if current_text_block:
|
172 |
+
blocks.append((' '.join(current_text_block), 'text'))
|
173 |
+
|
174 |
+
return blocks
|
175 |
+
|
176 |
+
def show_elements(text):
|
177 |
+
blocks = split_content_into_blocks(text)
|
178 |
+
outputs = []
|
179 |
+
|
180 |
+
for content, block_type in blocks:
|
181 |
+
if block_type == 'text':
|
182 |
+
outputs.append(gr.Markdown(content))
|
183 |
+
elif block_type == 'iframe':
|
184 |
+
outputs.append(gr.HTML(content))
|
185 |
+
|
186 |
+
return outputs
|
187 |
+
|
188 |
+
def convert_timeline_to_game_structure(timeline):
|
189 |
+
lines = timeline.split('\n')
|
190 |
+
game_structure = {}
|
191 |
+
current_location = 0
|
192 |
+
sub_location = 0
|
193 |
+
|
194 |
+
for i, line in enumerate(lines):
|
195 |
+
if line.strip() == "":
|
196 |
+
continue
|
197 |
+
|
198 |
+
if line[0].isdigit(): # New location starts
|
199 |
+
current_location += 1
|
200 |
+
sub_location = 0
|
201 |
+
location_key = f"location{current_location}"
|
202 |
+
game_structure[location_key] = {
|
203 |
+
"description": "",
|
204 |
+
"events": [],
|
205 |
+
"choices": ["continue"],
|
206 |
+
"transitions": {}
|
207 |
+
}
|
208 |
+
else: # Continue with sub-locations or media entries
|
209 |
+
sub_location += 1
|
210 |
+
location_key = f"location{current_location}_{sub_location}"
|
211 |
+
|
212 |
+
# Extract the event description
|
213 |
+
parts = line.split(': ', 1)
|
214 |
+
if len(parts) == 2:
|
215 |
+
prefix, rest = parts
|
216 |
+
event_parts = rest.split(' - ', 1)
|
217 |
+
if len(event_parts) == 2:
|
218 |
+
event_type, event_description = event_parts
|
219 |
+
else:
|
220 |
+
event_type, event_description = "Unknown", rest
|
221 |
+
else:
|
222 |
+
event_type, event_description = "Unknown", line
|
223 |
+
|
224 |
+
description = rest.strip() if event_type in ["Media", "UI"] else f"{event_type}: {event_description}"
|
225 |
+
|
226 |
+
if sub_location == 0:
|
227 |
+
game_structure[f"location{current_location}"]["description"] = description
|
228 |
+
else:
|
229 |
+
game_structure[f"location{current_location}"]["events"].append({
|
230 |
+
"description": description,
|
231 |
+
"type": event_type
|
232 |
+
})
|
233 |
+
|
234 |
+
# Set the transition to the next location or to the end
|
235 |
+
if i < len(lines) - 1:
|
236 |
+
next_line = lines[i + 1].strip()
|
237 |
+
if next_line and next_line[0].isdigit(): # New location starts
|
238 |
+
game_structure[f"location{current_location}"]["transitions"]["continue"] = f"masterlocation1_location{current_location + 1}"
|
239 |
+
else:
|
240 |
+
#game_structure[f"location{current_location}"]["transitions"]["continue"] = f"location_{current_location}_{sub_location + 1}"
|
241 |
+
game_structure[f"location{current_location}"]["transitions"]["continue"] = "end"
|
242 |
+
else:
|
243 |
+
game_structure[f"location{current_location}"]["transitions"]["continue"] = "end"
|
244 |
+
|
245 |
+
# Add an end location
|
246 |
+
game_structure["end"] = {
|
247 |
+
"description": "The adventure ends here.",
|
248 |
+
# "choices": [],
|
249 |
+
# "transitions": {}
|
250 |
+
"choices": ["restart"],
|
251 |
+
"transitions": {"restart": "location1"} # Assuming location_1 is the start
|
252 |
+
|
253 |
+
}
|
254 |
+
|
255 |
+
# Wrap the game structure in master_location1
|
256 |
+
wrapped_structure = {"masterlocation1": game_structure}
|
257 |
+
|
258 |
+
return wrapped_structure
|
259 |
+
|
260 |
+
def generate_game_structures():
|
261 |
+
timeline_with_media, _ = generate_story_and_timeline(include_media=True)
|
262 |
+
timeline_without_media, _ = generate_story_and_timeline(include_media=False)
|
263 |
+
|
264 |
+
game_structure_with_media = convert_timeline_to_game_structure(timeline_with_media)
|
265 |
+
game_structure_without_media = convert_timeline_to_game_structure(timeline_without_media)
|
266 |
+
|
267 |
+
return game_structure_with_media, game_structure_without_media
|
268 |
+
|
269 |
+
# Generate and print the game structures
|
270 |
+
game_structure_with_media, game_structure_without_media = generate_game_structures()
|
271 |
+
|
272 |
+
|
273 |
+
#print("Game Structure with Media:")
|
274 |
+
#print(json.dumps(game_structure_with_media, indent=2))
|
275 |
+
|
276 |
+
#print("\nGame Structure without Media:")
|
277 |
+
#print(json.dumps(game_structure_without_media, indent=2))
|
278 |
+
|
279 |
+
def show_game_structure(include_media):
|
280 |
+
if include_media:
|
281 |
+
return json.dumps(game_structure_with_media, indent=2)
|
282 |
+
else:
|
283 |
+
return json.dumps(game_structure_without_media, indent=2)
|
284 |
+
|
285 |
+
|
286 |
#-----------------------------------------------------------------------------------------------------------------------------------
|
287 |
|
288 |
class Player:
|
|
|
504 |
|
505 |
initgameinfo = start_game()
|
506 |
|
|
|
507 |
#-----------------------------------------------------------------------------------------------------------------------------------
|
508 |
|
509 |
with gr.Blocks() as demo:
|
|
|
597 |
generate_button.click(generate_story_and_timeline, inputs=[], outputs=[timeline_output, story_output])
|
598 |
with gr.Tab("Asset generation considered"):
|
599 |
gr.HTML("placeholder")
|
600 |
+
|
601 |
+
with gr.Tab("Prototype for finegrained editing of time line before conversion to game format"):
|
602 |
+
gr.Textbox(value=timeline_with_media)
|
603 |
+
gr.Textbox(value=timeline_without_media)
|
604 |
+
gr.HTML("Splits by new line")
|
605 |
+
input_text = gr.Textbox(label="Input Text", lines=10)
|
606 |
+
output_group = gr.Group()
|
607 |
+
|
608 |
+
@gr.render(inputs=input_text)
|
609 |
+
def update(text):
|
610 |
+
return show_elements(text)
|
611 |
+
with gr.Tab("Conversion of game version"):
|
612 |
+
media_checkbox = gr.Checkbox(label="Include Media")
|
613 |
+
output_text = gr.Code(language="json")
|
614 |
+
|
615 |
+
media_checkbox.change(
|
616 |
+
fn=show_game_structure,
|
617 |
+
inputs=[media_checkbox],
|
618 |
+
outputs=[output_text]
|
619 |
+
)
|
620 |
with gr.Tab("Test Example State Machine"):
|
621 |
with gr.Row():
|
622 |
with gr.Column(scale=2):
|