cdleong commited on
Commit
0c46341
·
verified ·
1 Parent(s): 558419d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -58
app.py CHANGED
@@ -7,6 +7,7 @@ import contextlib
7
  import requests
8
  import random
9
  from functools import lru_cache
 
10
 
11
  FORBIDDEN_NAMES ={"Judas",
12
  "Judas Iscariot"
@@ -51,6 +52,32 @@ FORBIDDEN_NAMES ={"Judas",
51
  "Pontius Pilate"
52
  }
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  def download_file(url: str, dest_path: Path):
55
  if dest_path.exists():
56
  print(f"{dest_path.name} already exists. Skipping download.")
@@ -63,7 +90,7 @@ def download_file(url: str, dest_path: Path):
63
  print(f"Saved to {dest_path}")
64
 
65
 
66
- # --- File download & setup ---
67
  def extract_names_zip():
68
  zip_path = Path("names.zip")
69
  if not zip_path.exists():
@@ -253,63 +280,88 @@ def generate_names(n, sex, ssa_min_len, ssa_max_len,
253
  results.append(f"[Error: {e}]")
254
  return "\n".join(results), debug_output.getvalue()
255
 
256
-
257
  with gr.Blocks() as demo:
258
- gr.Markdown("# 📜 Random Bible + SSA Name Generator")
259
-
260
- with gr.Row():
261
- n_slider = gr.Slider(1, 20, value=5, step=1, label="How many names?")
262
- sex_choice = gr.Radio(["M", "F", "Any"], label="Sex", value="Any")
263
-
264
- with gr.Row():
265
- ssa_min_len = gr.Slider(1, 40, value=1, step=1, label="SSA name min length")
266
- ssa_max_len = gr.Slider(1, 40, value=40, step=1, label="SSA name max length")
267
- with gr.Row():
268
- ssa_min_year = gr.Slider(1880, 2024, value=1880, step=1, label="SSA name min year")
269
- ssa_max_year = gr.Slider(1880, 2024, value=2024, step=1, label="SSA name max year")
270
-
271
- with gr.Row():
272
- bible_len = gr.Slider(1, 40, value=1, step=1, label="Bible name min length")
273
- bible_max_len = gr.Slider(1, 40, value=40, step=1, label="Bible name max length")
274
-
275
- with gr.Row():
276
- pop_low_slider = gr.Slider(0.0, 1.0, value=0.95, step=0.01, label="SSA Popularity: Low Percentile")
277
- pop_high_slider = gr.Slider(0.0, 1.0, value=1.0, step=0.01, label="SSA Popularity: High Percentile")
278
- with gr.Row():
279
- last_name_input = gr.Textbox(label="Last Name")
280
-
281
- with gr.Row():
282
- forbidden_names_input = gr.Textbox(label="FORBIDDEN NAMES (comma-separated)", value=",".join(FORBIDDEN_NAMES))
283
-
284
-
285
-
286
- debug_checkbox = gr.Checkbox(label="Show debug output", value=True)
287
- bible_name_first_checkbox = gr.Checkbox(label="Bible name first?", value=True)
288
-
289
- generate_btn = gr.Button("🔀 Generate Names")
290
-
291
- output_box = gr.Textbox(label="Generated Names", lines=10)
292
- debug_box = gr.Textbox(label="Debug Output", lines=10)
293
-
294
- generate_btn.click(
295
- fn=generate_names,
296
- inputs=[
297
- n_slider,
298
- sex_choice,
299
- ssa_min_len,
300
- ssa_max_len,
301
- ssa_min_year,
302
- ssa_max_year,
303
- bible_len,
304
- bible_max_len,
305
- pop_low_slider,
306
- pop_high_slider,
307
- debug_checkbox,
308
- last_name_input,
309
- forbidden_names_input,
310
- bible_name_first_checkbox,
311
- ],
312
- outputs=[output_box, debug_box],
313
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
314
 
315
  demo.launch()
 
7
  import requests
8
  import random
9
  from functools import lru_cache
10
+ import matplotlib.pyplot as plt
11
 
12
  FORBIDDEN_NAMES ={"Judas",
13
  "Judas Iscariot"
 
52
  "Pontius Pilate"
53
  }
54
 
55
+ # ----------------------------- Plotting
56
+
57
+
58
+ def plot_name_trends(df, names, start_year=None, end_year=None, logscale=False):
59
+ name_df = df[df["name"].isin(names)]
60
+ if start_year is not None:
61
+ name_df = name_df[name_df["year"] >= start_year]
62
+ if end_year is not None:
63
+ name_df = name_df[name_df["year"] <= end_year]
64
+
65
+ pivot = name_df.pivot_table(index="year", columns="name", values="count", aggfunc="sum", fill_value=0)
66
+
67
+ fig, ax = plt.subplots(figsize=(12, 6))
68
+ pivot.plot(ax=ax)
69
+ ax.set_title("Name Usage Over Time")
70
+ ax.set_xlabel("Year")
71
+ ax.set_ylabel("Count")
72
+ if logscale:
73
+ ax.set_yscale("log")
74
+ ax.grid(True)
75
+ ax.legend(title="Name")
76
+ fig.tight_layout()
77
+ return fig
78
+
79
+
80
+ # --- File download & setup ---
81
  def download_file(url: str, dest_path: Path):
82
  if dest_path.exists():
83
  print(f"{dest_path.name} already exists. Skipping download.")
 
90
  print(f"Saved to {dest_path}")
91
 
92
 
93
+
94
  def extract_names_zip():
95
  zip_path = Path("names.zip")
96
  if not zip_path.exists():
 
280
  results.append(f"[Error: {e}]")
281
  return "\n".join(results), debug_output.getvalue()
282
 
 
283
  with gr.Blocks() as demo:
284
+ with gr.Tabs():
285
+ with gr.Tab("🔀 Generate Names"):
286
+ gr.Markdown("# 📜 Random Bible + SSA Name Generator")
287
+
288
+ with gr.Row():
289
+ n_slider = gr.Slider(1, 20, value=5, step=1, label="How many names?")
290
+ sex_choice = gr.Radio(["M", "F", "Any"], label="Sex", value="Any")
291
+
292
+ with gr.Row():
293
+ ssa_min_len = gr.Slider(1, 40, value=1, step=1, label="SSA name min length")
294
+ ssa_max_len = gr.Slider(1, 40, value=40, step=1, label="SSA name max length")
295
+ with gr.Row():
296
+ ssa_min_year = gr.Slider(1880, 2024, value=1880, step=1, label="SSA name min year")
297
+ ssa_max_year = gr.Slider(1880, 2024, value=2024, step=1, label="SSA name max year")
298
+
299
+ with gr.Row():
300
+ bible_len = gr.Slider(1, 40, value=1, step=1, label="Bible name min length")
301
+ bible_max_len = gr.Slider(1, 40, value=40, step=1, label="Bible name max length")
302
+
303
+ with gr.Row():
304
+ pop_low_slider = gr.Slider(0.0, 1.0, value=0.95, step=0.01, label="SSA Popularity: Low Percentile")
305
+ pop_high_slider = gr.Slider(0.0, 1.0, value=1.0, step=0.01, label="SSA Popularity: High Percentile")
306
+ with gr.Row():
307
+ last_name_input = gr.Textbox(label="Last Name")
308
+ with gr.Row():
309
+ forbidden_names_input = gr.Textbox(label="FORBIDDEN NAMES (comma-separated)", value=",".join(FORBIDDEN_NAMES))
310
+
311
+ debug_checkbox = gr.Checkbox(label="Show debug output", value=True)
312
+ bible_name_first_checkbox = gr.Checkbox(label="Bible name first?", value=True)
313
+
314
+ generate_btn = gr.Button("🔀 Generate Names")
315
+
316
+ output_box = gr.Textbox(label="Generated Names", lines=10)
317
+ debug_box = gr.Textbox(label="Debug Output", lines=10)
318
+
319
+ generate_btn.click(
320
+ fn=generate_names,
321
+ inputs=[
322
+ n_slider,
323
+ sex_choice,
324
+ ssa_min_len,
325
+ ssa_max_len,
326
+ ssa_min_year,
327
+ ssa_max_year,
328
+ bible_len,
329
+ bible_max_len,
330
+ pop_low_slider,
331
+ pop_high_slider,
332
+ debug_checkbox,
333
+ last_name_input,
334
+ forbidden_names_input,
335
+ bible_name_first_checkbox,
336
+ ],
337
+ outputs=[output_box, debug_box],
338
+ )
339
+
340
+ with gr.Tab("📈 Name Trends"):
341
+ gr.Markdown("# 📈 SSA Name Trends Over Time")
342
+
343
+ with gr.Row():
344
+ trend_names_input = gr.Textbox(label="Name(s) to plot (comma-separated)", placeholder="e.g. Noah, Emma, Isaac")
345
+ with gr.Row():
346
+ trend_start_year = gr.Slider(1880, 2024, value=1950, step=1, label="Start Year")
347
+ trend_end_year = gr.Slider(1880, 2024, value=2024, step=1, label="End Year")
348
+ trend_logscale = gr.Checkbox(label="Log scale?", value=False)
349
+
350
+ plot_button = gr.Button("📊 Plot Trends")
351
+ plot_output = gr.Plot(label="Trend Plot")
352
+
353
+ def plot_from_inputs(name_text, start_year, end_year, logscale):
354
+ names = [n.strip() for n in name_text.split(",") if n.strip()]
355
+ if not names:
356
+ raise gr.Error("Please enter at least one name.")
357
+ full_df = load_all_ssa_names()
358
+ return plot_name_trends(full_df, names, start_year, end_year, logscale)
359
+
360
+ plot_button.click(
361
+ fn=plot_from_inputs,
362
+ inputs=[trend_names_input, trend_start_year, trend_end_year, trend_logscale],
363
+ outputs=plot_output,
364
+ )
365
+
366
 
367
  demo.launch()