mecoffey commited on
Commit
ed10af2
·
1 Parent(s): b2ace65

changed csv to save per session so csv data is not shared

Browse files
Files changed (1) hide show
  1. app.py +111 -106
app.py CHANGED
@@ -30,42 +30,45 @@ BACK_SYSTEM_PROMPT = (
30
  )
31
 
32
 
33
- strength = ''
34
- dexterity = ''
35
- constitution = ''
36
- intelligence = ''
37
- wisdom = ''
38
- charisma = ''
39
- ac = ''
40
- hp = ''
41
- charm = ''
42
- pref_traits = ''
43
- ick_traits = ''
44
- speed = ''
45
-
46
- def stats_gen():
47
- global strength
48
- strength = random.randint(2,12)
49
- global dexterity
50
- dexterity = random.randint(2,12)
51
- global constitution
52
- constitution = random.randint(2,12)
53
- global intelligence
54
- intelligence = random.randint(2,12)
55
- global wisdom
56
- wisdom = random.randint(2,12)
57
- global charisma
58
- charisma = random.randint(2,12)
59
- return strength, dexterity, constitution, intelligence, wisdom, charisma
60
-
61
- def stats_gen_2():
62
- global ac
63
- ac = random.randint(2,14)
64
- global hp
65
- hp = constitution + random.randint(1,6)
66
- global speed
67
- speed = '25 ft'
68
- return ac, hp, speed
 
 
 
69
 
70
  def traits_picker(filepath):
71
  with open(filepath, 'r') as f:
@@ -79,14 +82,14 @@ def rand_race_gen(filepath):
79
  items = [line.strip() for line in f if line.strip()]
80
  return random.choice(items)
81
 
82
- def bard_stats_gen():
83
- global charm
84
- charm = random.randint(1,30)
85
- global pref_traits
86
- pref_traits = traits_picker('traits.txt')
87
- global ick_traits
88
- ick_traits = traits_picker('traits.txt')
89
- return charm, pref_traits, ick_traits
90
 
91
 
92
  def make_session_csv():
@@ -137,64 +140,64 @@ def rand_char_description(prompt):
137
  return output1[0]["generated_text"].strip(),output2[0]["generated_text"].strip()
138
 
139
 
140
- def char_description_csv(prompt):
141
- global csv_path
142
- if csv_path is None:
143
- csv_path = make_session_csv()
144
  description, backstory = char_description(prompt)
145
  append_to_csv(
146
- csv_path,
147
  prompt,
148
  description,
149
  backstory,
150
- strength,
151
- dexterity,
152
- constitution,
153
- intelligence,
154
- wisdom,
155
- charisma,
156
- ac,
157
- hp,
158
- speed,
159
- charm,
160
- pref_traits,
161
- ick_traits,
162
  )
163
- return description, backstory
164
 
165
 
166
- def rand_char_description_csv():
167
- global csv_path
168
- if csv_path is None:
169
- csv_path = make_session_csv()
170
  rand_race = rand_race_gen('core_races.txt')
171
  description, backstory = rand_char_description(rand_race)
172
  append_to_csv(
173
- csv_path,
174
  rand_race,
175
  description,
176
  backstory,
177
- strength,
178
- dexterity,
179
- constitution,
180
- intelligence,
181
- wisdom,
182
- charisma,
183
- ac,
184
- hp,
185
- speed,
186
- charm,
187
- pref_traits,
188
- ick_traits,
189
  )
190
- return description, backstory
191
 
192
 
193
- def get_csv_file():
194
- global csv_path
195
- if csv_path is None:
196
- csv_path = make_session_csv()
197
- return csv_path
198
 
199
 
200
  ##UI is fine as-is
@@ -212,46 +215,48 @@ with gr.Blocks() as demo:
212
 
213
  with gr.Row():
214
 
 
 
215
  with gr.Column(scale = 0):
216
- stre_box = gr.Textbox(value = strength, label = "Strength", interactive = False)
217
- dex_box = gr.Textbox(value = dexterity, label = "Dexterity", interactive = False)
218
- con_box = gr.Textbox(value = constitution, label = "Constitution", interactive = False)
219
- intl_box = gr.Textbox(value = intelligence, label = "Intelligence", interactive = False)
220
- wis_box = gr.Textbox(value = wisdom, label = "Wisdom", interactive = False)
221
- char_box = gr.Textbox(value = charisma, label = "Charisma", interactive = False)
222
- btn.click(fn = stats_gen, outputs = [stre_box, dex_box, con_box, intl_box, wis_box, char_box])
223
- rand.click(fn = stats_gen, outputs = [stre_box, dex_box, con_box, intl_box, wis_box, char_box])
224
 
225
  with gr.Column(scale = 2):
226
  text_out1 = gr.Textbox(label = "Description")
227
  text_out2 = gr.Textbox(label = "Backstory")
228
- btn.click(fn = char_description_csv,inputs = text_in, outputs = [text_out1, text_out2])
229
- rand.click(fn = rand_char_description_csv, outputs = [text_out1, text_out2])
230
 
231
 
232
  with gr.Column(scale = 0):
233
- ac_box = gr.Textbox(value = ac, label = "AC", interactive = False)
234
- hp_box = gr.Textbox(value = hp, label = "HP", interactive = False)
235
- speed_box = gr.Textbox(value = speed, label = "Speed", interactive = False)
236
- btn.click(fn = stats_gen_2, outputs = [ac_box, hp_box, speed_box])
237
- rand.click(fn = stats_gen_2, outputs = [ac_box, hp_box, speed_box])
238
 
239
 
240
  with gr.Row():
241
 
242
  with gr.Accordion("Bard Stats", open = False):
243
  with gr.Row():
244
- dcs = gr.Textbox(value = charm, label = "DC to Charm")
245
- prefs = gr.Textbox(value = pref_traits, label = "Advantage if:")
246
- icks = gr.Textbox(value = ick_traits, label = "Disdvantage if:")
247
- btn.click(fn = bard_stats_gen, outputs = [dcs, prefs, icks])
248
- rand.click(fn = bard_stats_gen, outputs = [dcs, prefs, icks])
249
 
250
  with gr.Row():
251
  download_btn = gr.Button("Download CSV")
252
  with gr.Row():
253
  download_file = gr.File(label="Generated CSV", interactive=False)
254
- download_btn.click(fn=get_csv_file, outputs=download_file)
255
 
256
 
257
  demo.launch(theme = gr.themes.Monochrome())
 
30
  )
31
 
32
 
33
+ DEFAULT_SESSION_STATE = {
34
+ "csv_path": None,
35
+ "strength": "",
36
+ "dexterity": "",
37
+ "constitution": "",
38
+ "intelligence": "",
39
+ "wisdom": "",
40
+ "charisma": "",
41
+ "ac": "",
42
+ "hp": "",
43
+ "speed": "",
44
+ "charm": "",
45
+ "pref_traits": "",
46
+ "ick_traits": "",
47
+ }
48
+
49
+ def new_session_state():
50
+ return dict(DEFAULT_SESSION_STATE)
51
+
52
+ def stats_gen(state):
53
+ state = dict(state or DEFAULT_SESSION_STATE)
54
+ state.update({
55
+ "strength": random.randint(2, 12),
56
+ "dexterity": random.randint(2, 12),
57
+ "constitution": random.randint(2, 12),
58
+ "intelligence": random.randint(2, 12),
59
+ "wisdom": random.randint(2, 12),
60
+ "charisma": random.randint(2, 12),
61
+ })
62
+ return state["strength"], state["dexterity"], state["constitution"], state["intelligence"], state["wisdom"], state["charisma"], state
63
+
64
+ def stats_gen_2(state):
65
+ state = dict(state or DEFAULT_SESSION_STATE)
66
+ state.update({
67
+ "ac": random.randint(2, 14),
68
+ "hp": state.get("constitution", 0) + random.randint(1, 6),
69
+ "speed": "25 ft",
70
+ })
71
+ return state["ac"], state["hp"], state["speed"], state
72
 
73
  def traits_picker(filepath):
74
  with open(filepath, 'r') as f:
 
82
  items = [line.strip() for line in f if line.strip()]
83
  return random.choice(items)
84
 
85
+ def bard_stats_gen(state):
86
+ state = dict(state or DEFAULT_SESSION_STATE)
87
+ state.update({
88
+ "charm": random.randint(1, 30),
89
+ "pref_traits": traits_picker('traits.txt'),
90
+ "ick_traits": traits_picker('traits.txt'),
91
+ })
92
+ return state["charm"], state["pref_traits"], state["ick_traits"], state
93
 
94
 
95
  def make_session_csv():
 
140
  return output1[0]["generated_text"].strip(),output2[0]["generated_text"].strip()
141
 
142
 
143
+ def char_description_csv(prompt, state):
144
+ state = dict(state or DEFAULT_SESSION_STATE)
145
+ if not state.get("csv_path"):
146
+ state["csv_path"] = make_session_csv()
147
  description, backstory = char_description(prompt)
148
  append_to_csv(
149
+ state["csv_path"],
150
  prompt,
151
  description,
152
  backstory,
153
+ state.get("strength", ""),
154
+ state.get("dexterity", ""),
155
+ state.get("constitution", ""),
156
+ state.get("intelligence", ""),
157
+ state.get("wisdom", ""),
158
+ state.get("charisma", ""),
159
+ state.get("ac", ""),
160
+ state.get("hp", ""),
161
+ state.get("speed", ""),
162
+ state.get("charm", ""),
163
+ state.get("pref_traits", ""),
164
+ state.get("ick_traits", ""),
165
  )
166
+ return description, backstory, state
167
 
168
 
169
+ def rand_char_description_csv(state):
170
+ state = dict(state or DEFAULT_SESSION_STATE)
171
+ if not state.get("csv_path"):
172
+ state["csv_path"] = make_session_csv()
173
  rand_race = rand_race_gen('core_races.txt')
174
  description, backstory = rand_char_description(rand_race)
175
  append_to_csv(
176
+ state["csv_path"],
177
  rand_race,
178
  description,
179
  backstory,
180
+ state.get("strength", ""),
181
+ state.get("dexterity", ""),
182
+ state.get("constitution", ""),
183
+ state.get("intelligence", ""),
184
+ state.get("wisdom", ""),
185
+ state.get("charisma", ""),
186
+ state.get("ac", ""),
187
+ state.get("hp", ""),
188
+ state.get("speed", ""),
189
+ state.get("charm", ""),
190
+ state.get("pref_traits", ""),
191
+ state.get("ick_traits", ""),
192
  )
193
+ return description, backstory, state
194
 
195
 
196
+ def get_csv_file(state):
197
+ state = dict(state or DEFAULT_SESSION_STATE)
198
+ if not state.get("csv_path"):
199
+ state["csv_path"] = make_session_csv()
200
+ return state["csv_path"], state
201
 
202
 
203
  ##UI is fine as-is
 
215
 
216
  with gr.Row():
217
 
218
+ session_state = gr.State(value=new_session_state())
219
+
220
  with gr.Column(scale = 0):
221
+ stre_box = gr.Textbox(value = "", label = "Strength", interactive = False)
222
+ dex_box = gr.Textbox(value = "", label = "Dexterity", interactive = False)
223
+ con_box = gr.Textbox(value = "", label = "Constitution", interactive = False)
224
+ intl_box = gr.Textbox(value = "", label = "Intelligence", interactive = False)
225
+ wis_box = gr.Textbox(value = "", label = "Wisdom", interactive = False)
226
+ char_box = gr.Textbox(value = "", label = "Charisma", interactive = False)
227
+ btn.click(fn = stats_gen, inputs = session_state, outputs = [stre_box, dex_box, con_box, intl_box, wis_box, char_box, session_state])
228
+ rand.click(fn = stats_gen, inputs = session_state, outputs = [stre_box, dex_box, con_box, intl_box, wis_box, char_box, session_state])
229
 
230
  with gr.Column(scale = 2):
231
  text_out1 = gr.Textbox(label = "Description")
232
  text_out2 = gr.Textbox(label = "Backstory")
233
+ btn.click(fn = char_description_csv, inputs = [text_in, session_state], outputs = [text_out1, text_out2, session_state])
234
+ rand.click(fn = rand_char_description_csv, inputs = session_state, outputs = [text_out1, text_out2, session_state])
235
 
236
 
237
  with gr.Column(scale = 0):
238
+ ac_box = gr.Textbox(value = "", label = "AC", interactive = False)
239
+ hp_box = gr.Textbox(value = "", label = "HP", interactive = False)
240
+ speed_box = gr.Textbox(value = "", label = "Speed", interactive = False)
241
+ btn.click(fn = stats_gen_2, inputs = session_state, outputs = [ac_box, hp_box, speed_box, session_state])
242
+ rand.click(fn = stats_gen_2, inputs = session_state, outputs = [ac_box, hp_box, speed_box, session_state])
243
 
244
 
245
  with gr.Row():
246
 
247
  with gr.Accordion("Bard Stats", open = False):
248
  with gr.Row():
249
+ dcs = gr.Textbox(value = "", label = "DC to Charm")
250
+ prefs = gr.Textbox(value = "", label = "Advantage if:")
251
+ icks = gr.Textbox(value = "", label = "Disdvantage if:")
252
+ btn.click(fn = bard_stats_gen, inputs = session_state, outputs = [dcs, prefs, icks, session_state])
253
+ rand.click(fn = bard_stats_gen, inputs = session_state, outputs = [dcs, prefs, icks, session_state])
254
 
255
  with gr.Row():
256
  download_btn = gr.Button("Download CSV")
257
  with gr.Row():
258
  download_file = gr.File(label="Generated CSV", interactive=False)
259
+ download_btn.click(fn=get_csv_file, inputs=session_state, outputs=[download_file, session_state])
260
 
261
 
262
  demo.launch(theme = gr.themes.Monochrome())