jbpb123 commited on
Commit
fd16b28
·
verified ·
1 Parent(s): 6cd46d2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +284 -0
app.py ADDED
@@ -0,0 +1,284 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ import json
3
+ from datetime import datetime
4
+ import gradio as gr
5
+ from dateutil import parser
6
+
7
+ def _get_scene_titles(storydata):
8
+ scene_titles = {}
9
+ choice_names = {}
10
+ for chapter in storydata.get("chapters", []):
11
+ for scene_id, scene in chapter.get("contents", {}).get("scenes", {}).items():
12
+ scene_titles[scene_id] = scene["title"]
13
+ for comp in scene.get("components", []):
14
+ if comp["type"] == "choiceComponent":
15
+ choice_names[comp["id"]] = comp["name"]
16
+
17
+ return scene_titles, choice_names
18
+
19
+
20
+ def _print_text_component(comp, output_lines):
21
+ print(comp["text"])
22
+ output_lines.append(comp['text'])
23
+ print()
24
+
25
+
26
+ def _print_dialog_component(comp, config, output_lines):
27
+ character_key = comp.get("characterAssetId")
28
+ character_name = config.get("characterMap", {}).get(character_key, {}).get("characterName")
29
+ dialog = comp["dialog"]
30
+
31
+ # Format the dialog nice
32
+ dialog = "> " + dialog.replace("\n", "\n> ")
33
+
34
+ if character_name is not None:
35
+ print(f"{character_name}:")
36
+ print()
37
+ output_lines.append(f"{character_name}:")
38
+ output_lines.append("")
39
+ print(dialog)
40
+ output_lines.append(dialog)
41
+ output_lines.append("")
42
+ print()
43
+
44
+
45
+ def _print_jump_component(comp, scene_titles, output_lines):
46
+ dest = comp["jumpTransition"]["destination"]
47
+ if dest == "end-of-chapter":
48
+ print("JUMP TO END OF CHAPTER")
49
+ print()
50
+ output_lines.append("JUMP TO END OF CHAPTER")
51
+ output_lines.append("")
52
+ elif dest == "specific-scene":
53
+ scene_id = comp["jumpTransition"]["targetScene"]
54
+ scene_title = scene_titles.get(scene_id, "")
55
+ print(f"JUMP TO SCENE: {scene_title}")
56
+ print()
57
+ output_lines.append(f"JUMP TO SCENE: {scene_title}")
58
+ output_lines.append("")
59
+
60
+
61
+
62
+ def _print_switch_component(comp, variables, scene_titles, choice_names, config, output_lines, mapping):
63
+ var_id = comp["variableId"]
64
+ var_name = [v["name"] for v in variables if v["id"] == var_id][0]
65
+ print(f"VARIABLE CHECK. DEPENDING ON `{var_name}`, TAKE ONE BRANCH:")
66
+ print()
67
+ output_lines.append(f"VARIABLE CHECK. DEPENDING ON `{var_name}`, TAKE ONE BRANCH:")
68
+ output_lines.append("")
69
+ for ibranch, branch in enumerate(comp["branches"], start=1):
70
+ print(f"BRANCH {ibranch}")
71
+ print()
72
+ for branch_comp in branch["components"]:
73
+ _print_component(branch_comp, variables, scene_titles, choice_names, config, output_lines, mapping)
74
+ print("END VARIABLE CHECK")
75
+ print()
76
+
77
+
78
+ def _print_if_component(comp, variables, scene_titles, choice_names, config, output_lines, mapping):
79
+ choice_id = comp.get("choiceId")
80
+ choice_name = choice_names.get(choice_id, "")
81
+ if choice_name:
82
+ choice_name = f' "{choice_name}"'
83
+ print(f'CHOICE CHECK. DEPENDING ON CHOICE{choice_name}, TAKE ONE BRANCH:')
84
+ print()
85
+ output_lines.append(f'CHOICE CHECK. DEPENDING ON CHOICE{choice_name}, TAKE ONE BRANCH:')
86
+ output_lines.append("")
87
+ for ibranch, branch in enumerate(comp["conditions"], start=1):
88
+ print(f"BRANCH {ibranch}")
89
+ print()
90
+ output_lines.append(f"BRANCH {ibranch}")
91
+ output_lines.append("")
92
+ for branch_comp in branch["components"]:
93
+ _print_component(branch_comp, variables, scene_titles, choice_names, config, output_lines, mapping)
94
+ print(f'END CHOICE CHECK{choice_name}')
95
+ print()
96
+ output_lines.append(f'END CHOICE CHECK{choice_name}')
97
+ output_lines.append("")
98
+
99
+
100
+ def _print_choice_component(comp, variables, scene_titles, choice_names, config, output_lines, mapping):
101
+ choice_name = comp.get("name", "")
102
+ if choice_name:
103
+ choice_name = f' "{choice_name}"'
104
+ print(f"BEGIN CHOICE BLOCK{choice_name}")
105
+ print()
106
+ output_lines.append(f"BEGIN CHOICE BLOCK{choice_name}")
107
+ output_lines.append("")
108
+ prompt = comp.get("promptComponent")
109
+ if prompt is not None:
110
+ _print_component(prompt, variables, scene_titles, choice_names, config, output_lines, mapping)
111
+ for iopt, option in enumerate(comp.get("options", []), start=1):
112
+ choice_text = option.get("displayText")
113
+ if choice_text is not None:
114
+ print(f"OPTION {iopt}: {choice_text}")
115
+ print()
116
+ output_lines.append(f"OPTION {iopt}: {choice_text}")
117
+ output_lines.append("")
118
+ for opt_comp in option.get("components", []):
119
+ _print_component(opt_comp, variables, scene_titles, choice_names, config, output_lines, mapping)
120
+ print(f"END CHOICE BLOCK{choice_name}")
121
+ print()
122
+ output_lines.append(f"END CHOICE BLOCK{choice_name}")
123
+ output_lines.append("")
124
+
125
+
126
+ def _print_note_component(comp, output_lines):
127
+ print("---")
128
+ print()
129
+ print(f'NOTE: {comp["note"]}')
130
+ print()
131
+ print("---")
132
+ print()
133
+ output_lines.append("---")
134
+ output_lines.append("")
135
+ output_lines.append(f'NOTE: {comp["note"]}')
136
+ output_lines.append("")
137
+ output_lines.append("---")
138
+ output_lines.append("")
139
+
140
+ def _print_background(comp, mapping, output_lines):
141
+ asset_id = comp.get('backgroundAssetId')
142
+ output_lines.append(f"Transition: {comp.get('transition')}")
143
+ output_lines.append("")
144
+ output_lines.append(f'Background: {mapping.get(asset_id)}')
145
+ output_lines.append("")
146
+ print(f'Background: {mapping.get(asset_id)}')
147
+ print()
148
+
149
+ def _print_component(comp, variables, scene_titles, choice_names, config, output_lines, mapping):
150
+ component_type = comp.get("type")
151
+ if component_type == 'backgroundComponent':
152
+ _print_background(comp, mapping, output_lines)
153
+ if component_type == "textComponent":
154
+ _print_text_component(comp, output_lines)
155
+ elif component_type == "dialogComponent":
156
+ _print_dialog_component(comp, config, output_lines)
157
+ elif component_type == "jumpComponent":
158
+ _print_jump_component(comp, scene_titles, output_lines)
159
+ elif component_type in ("choiceV2Component", "choiceComponent"):
160
+ _print_choice_component(comp, variables, scene_titles, choice_names, config, output_lines, mapping)
161
+ elif component_type == "switchComponent":
162
+ _print_switch_component(comp, variables, scene_titles, choice_names, config, output_lines, mapping)
163
+ elif component_type == "ifComponent":
164
+ _print_if_component(comp, variables, scene_titles, choice_names, config, output_lines, mapping)
165
+ elif component_type == "noteComponent":
166
+ _print_note_component(comp, output_lines)
167
+
168
+ def format_story(storydata):
169
+ output_lines=[]
170
+ background_map = storydata["configuration"]["backgroundMap"]
171
+
172
+ # Creating a new dictionary with the id's as keys and the names as values
173
+ background_dic={bg_id: details['name'] for bg_id, details in background_map.items()}
174
+
175
+ # Get the story configuration data, etc.
176
+ variables = storydata.get("variables", {})
177
+ scene_titles, choice_names = _get_scene_titles(storydata)
178
+ config = storydata.get("configuration", {})
179
+
180
+ # Story title
181
+
182
+ title = storydata.get("name", "Untitled")
183
+ #_write_to_file(file, f"# {title} #")
184
+ #_write_to_file(file, "")
185
+ print(f"# {title} #")
186
+ print()
187
+ output_lines.append(title)
188
+
189
+ # Dates of creation and last modification
190
+ created = storydata.get("createdOn")
191
+ if created is not None:
192
+ created = parser.isoparse(created).strftime("%Y/%m/%d")
193
+ output_lines.append(created)
194
+ #_write_to_file(file, f"{created}")
195
+ #_write_to_file(file, "")
196
+ lastmod = storydata.get("lastModified")
197
+ if lastmod is not None:
198
+ lastmod = parser.isoparse(lastmod).strftime("%Y/%m/%d")
199
+ output_lines.append(lastmod)
200
+ #_write_to_file(file, f"{lastmod}")
201
+ #_write_to_file(file, "")
202
+ print(f"Created {created}")
203
+ print()
204
+ print(f"Last modified {lastmod}")
205
+ print()
206
+
207
+ # Story description
208
+ description = storydata.get("description")
209
+ print("### Description ###")
210
+ print()
211
+ print(description)
212
+ print()
213
+ output_lines.append(description)
214
+ #_write_to_file(file, f"{description}")
215
+ #_write_to_file(file, "")
216
+
217
+ # Chapter and scene information
218
+ chapters = sorted(storydata.get("chapters", []),
219
+ key=lambda c: c.get("order", 0))
220
+ for ichapter, chapter in enumerate(chapters, start=1):
221
+ # Chapter title
222
+ chapter_title = chapter.get("title")
223
+ print(f"## Chapter {ichapter}", end="")
224
+ print(" ##" if chapter_title is None else f": {chapter_title} ##")
225
+ print()
226
+ if chapter_title is not None:
227
+ output_lines.append(chapter_title)
228
+ #_write_to_file(file, f"{description}")
229
+ #_write_to_file(file, "")
230
+ else:
231
+ output_lines.append('##')
232
+ #_write_to_file(file, "##")
233
+ #_write_to_file(file, "")
234
+
235
+ # Chapter description
236
+ chapter_description = chapter.get("description")
237
+ if chapter_description is not None:
238
+ print("---")
239
+ print()
240
+ print(chapter_description)
241
+ print()
242
+ print("---")
243
+ print()
244
+ output_lines.append(chapter_description)
245
+ #_write_to_file(file, f"{chapter_description}")
246
+ #_write_to_file(file, "")
247
+
248
+ scenes = sorted(chapter.get("contents", {}).get("scenes", {}).values(),
249
+ key=lambda s: s.get("order", 0))
250
+ for scene in scenes:
251
+ # Scene title
252
+ scene_title = scene.get("title", "Untitled")
253
+ print(f"### Scene: {scene_title} ###")
254
+ print()
255
+ output_lines.append(scene_title)
256
+
257
+ #_write_to_file(file, f"{scene_title}")
258
+ #_write_to_file(file, "")
259
+
260
+
261
+ # Print the scene components
262
+ for comp in scene.get("components", []):
263
+ _print_component(comp, variables, scene_titles, choice_names, config, output_lines, background_dic)
264
+
265
+ return "\n".join(output_lines)
266
+
267
+ def process_story(file):
268
+
269
+ with open(file.name, "r", encoding="utf-8") as f:
270
+ storydata = json.load(f)
271
+
272
+ output=format_story(storydata)
273
+ return output
274
+
275
+ iface = gr.Interface(
276
+ fn=process_story,
277
+ inputs=gr.File(type="filepath"),
278
+ outputs="text",
279
+ title="Story Formatter",
280
+ description="Upload your JSON file and get the formatted story"
281
+ )
282
+
283
+ if __name__ == "__main__":
284
+ iface.launch()