Spaces:
Runtime error
Runtime error
File size: 16,142 Bytes
09672b1 1e333df 09672b1 1e333df 09672b1 1e333df 09672b1 1e333df fd51647 1e333df 09672b1 1e333df 09672b1 1e333df 09672b1 1e333df 09672b1 1e333df 09672b1 1e333df 09672b1 1e333df 09672b1 fd51647 09672b1 82795a6 09672b1 fd51647 09672b1 fd51647 09672b1 fd51647 09672b1 e9c905e 09672b1 e9c905e 09672b1 fd51647 09672b1 e9c905e 09672b1 e9c905e 09672b1 e9c905e 09672b1 1e333df 09672b1 1e333df 09672b1 1e333df 09672b1 fd51647 09672b1 fd51647 09672b1 fd51647 09672b1 fd51647 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 |
from pathlib import Path
import duckdb
import holoviews as hv
import pandas as pd
import panel as pn
from bokeh.models import HoverTool
from langchain.callbacks.base import BaseCallbackHandler
from langchain.chat_models import ChatOpenAI
pn.extension(sizing_mode="stretch_width", notifications=True)
hv.extension("bokeh")
INSTRUCTIONS = """
#### Name Chronicles lets you explore the history of names in the United States.
- Enter a name to add to plot.
- See stats by hovering a line.
- Click on a line to see the gender distribution.
- Get a random name based on selected criteria.
- Ask AI for some background info on a name.
- Have ideas? [Open an issue](https://github.com/ahuang11/name-chronicles/issues).
"""
RANDOM_NAME_QUERY = """
SELECT name, count,
CASE
WHEN female_percent >= 0.2 AND female_percent <= 0.8 AND male_percent >= 0.2 AND male_percent <= 0.8 THEN 'unisex'
WHEN female_percent > 0.6 THEN 'female'
WHEN male_percent > 0.6 THEN 'male'
END AS gender
FROM (
SELECT
name,
MAX(male + female) AS count,
(SUM(female) / CAST(SUM(male + female) AS REAL)) AS female_percent,
(SUM(male) / CAST(SUM(male + female) AS REAL)) AS male_percent
FROM names
WHERE name LIKE ?
GROUP BY name
)
WHERE count >= ? AND count <= ?
AND gender = ?
ORDER BY RANDOM()
LIMIT 100
"""
TOP_NAMES_WILDCARD_QUERY = """
SELECT name, SUM(male + female) as count
FROM names
WHERE lower(name) LIKE ?
GROUP BY name
ORDER BY count DESC
LIMIT 10
"""
TOP_NAMES_SELECT_QUERY = """
SELECT name, SUM(male + female) as count
FROM names
WHERE lower(name) = ?
GROUP BY name
ORDER BY count DESC
"""
DATA_QUERY = """
SELECT name, year, male, female, SUM(male + female) AS count
FROM names
WHERE name in ({placeholders})
GROUP BY name, year, male, female
ORDER BY name, year
"""
class StreamHandler(BaseCallbackHandler):
def __init__(self, container, initial_text="", target_attr="value"):
self.container = container
self.text = initial_text
self.target_attr = target_attr
def on_llm_new_token(self, token: str, **kwargs) -> None:
self.text += token
setattr(self.container, self.target_attr, self.text)
class NameChronicles:
def __init__(self):
super().__init__()
self.db_path = Path("data/names.db")
# Main
self.holoviews_pane = pn.pane.HoloViews(
min_height=675, sizing_mode="stretch_both"
)
self.selection = hv.streams.Selection1D()
# Sidebar
# Name Widgets
self.names_input = pn.widgets.TextInput(name="Name Input", placeholder="Andrew")
self.names_input.param.watch(self._add_name, "value")
self.names_choice = pn.widgets.MultiChoice(
name="Selected Names",
options=["Andrew"],
solid=False,
)
self.names_choice.param.watch(self._update_plot, "value")
# Reset Widgets
self.clear_button = pn.widgets.Button(
name="Clear Names", button_style="outline", button_type="primary"
)
self.clear_button.on_click(
lambda event: setattr(self.names_choice, "value", [])
)
self.refresh_button = pn.widgets.Button(
name="Refresh Plot", button_style="outline", button_type="primary"
)
self.refresh_button.on_click(self._refresh_plot)
# Randomize Widgets
self.name_pattern = pn.widgets.TextInput(
name="Name Pattern", placeholder="*na*"
)
self.count_range = pn.widgets.IntRangeSlider(
name="Peak Count Range",
value=(10000, 50000),
start=0,
end=100000,
step=1000,
margin=(5, 20),
)
self.gender_select = pn.widgets.RadioButtonGroup(
name="Gender",
options=["Female", "Unisex", "Male"],
button_style="outline",
button_type="primary",
)
randomize_name = pn.widgets.Button(
name="Get Name", button_style="outline", button_type="primary"
)
randomize_name.param.watch(self._randomize_name, "clicks")
self.randomize_pane = pn.Card(
self.name_pattern,
self.count_range,
self.gender_select,
randomize_name,
title="Get Random Name",
collapsed=True,
)
# AI Widgets
self.ai_key = pn.widgets.PasswordInput(
name="OpenAI Key",
placeholder="",
)
self.ai_prompt = pn.widgets.TextInput(
name="AI Prompt",
value="Share a little history about the name:",
)
ai_button = pn.widgets.Button(
name="Get Response",
button_style="outline",
button_type="primary",
)
ai_button.on_click(self._prompt_ai)
self.ai_response = pn.widgets.TextAreaInput(
placeholder="",
disabled=True,
height=350,
)
self.ai_pane = pn.Card(
self.ai_key,
self.ai_prompt,
ai_button,
self.ai_response,
collapsed=True,
title="Ask AI",
)
pn.state.onload(self._initialize_database)
# Database Methods
def _initialize_database(self):
"""
Initialize database with data from the Social Security Administration.
"""
self.conn = duckdb.connect(":memory:")
df = pd.concat(
[
pd.read_csv(
path,
header=None,
names=["state", "gender", "year", "name", "count"],
)
for path in Path("data").glob("*.TXT")
]
)
df_processed = (
df.groupby(["gender", "year", "name"], as_index=False)[["count"]]
.sum()
.pivot(index=["name", "year"], columns="gender", values="count")
.reset_index()
.rename(columns={"F": "female", "M": "male"})
.fillna(0)
)
self.conn.execute("DROP TABLE IF EXISTS names")
self.conn.execute("CREATE TABLE names AS SELECT * FROM df_processed")
if self.names_choice.value == []:
self.names_choice.value = ["Andrew"]
else:
self.names_choice.param.trigger("value")
self.main.objects = [self.holoviews_pane]
def _query_names(self, names):
"""
Query the database for the given name.
"""
dfs = []
for name in names:
if "*" in name or "%" in name:
name = name.replace("*", "%")
top_names_query = TOP_NAMES_WILDCARD_QUERY
else:
top_names_query = TOP_NAMES_SELECT_QUERY
top_names = (
self.conn.execute(top_names_query, [name.lower()])
.fetch_df()["name"]
.tolist()
)
if len(top_names) == 0:
pn.state.notifications.info(f"No names found matching {name!r}")
continue
data_query = DATA_QUERY.format(
placeholders=", ".join(["?"] * len(top_names))
)
df = self.conn.execute(data_query, top_names).fetch_df()
dfs.append(df)
if len(dfs) > 0:
self.df = pd.concat(dfs).drop_duplicates(
subset=["name", "year", "male", "female"]
)
else:
self.df = pd.DataFrame(columns=["name", "year", "male", "female"])
# Widget Methods
def _randomize_name(self, event):
name_pattern = self.name_pattern.value.lower()
if not name_pattern:
name_pattern = "%"
else:
name_pattern = name_pattern.replace("*", "%")
count_range = self.count_range.value
gender_select = self.gender_select.value.lower()
random_names = (
self.conn.execute(
RANDOM_NAME_QUERY, [name_pattern, *count_range, gender_select]
)
.fetch_df()["name"]
.tolist()
)
if random_names:
for i in range(len(random_names)):
random_name = random_names[i]
if random_name in self.names_choice.value:
continue
self.names_input.value = random_name
break
else:
pn.state.notifications.info(
"All names matching the criteria are already added!"
)
else:
pn.state.notifications.info("No names found matching the criteria!")
def _add_name(self, event):
name = event.new.strip().title()
self.names_input.value = ""
if not name:
return
elif name in self.names_choice.options and name in self.names_choice.value:
pn.state.notifications.info(f"{name!r} already added!")
return
elif len(self.names_choice.value) > 10:
pn.state.notifications.info(
"Maximum of 10 names allowed; please remove some first!"
)
return
value = self.names_choice.value.copy()
options = self.names_choice.options.copy()
if name not in options:
options.append(name)
if name not in value:
value.append(name)
self.names_choice.param.update(
options=options,
value=value,
)
def _prompt_ai(self, event):
if not self.ai_key.value:
pn.state.notifications.info("Please enter an API key!")
return
if not self.ai_prompt.value:
pn.state.notifications.info("Please enter a prompt!")
return
stream_handler = StreamHandler(self.ai_response)
chat = ChatOpenAI(
max_tokens=500,
openai_api_key=self.ai_key.value,
streaming=True,
callbacks=[stream_handler],
)
self.ai_response.loading = True
try:
if self.selection.index:
names = [self._name_indices[self.selection.index[0]]]
else:
names = self.names_choice.value[:3]
chat.predict(f"{self.ai_prompt.value} {names}")
finally:
self.ai_response.loading = False
# Plot Methods
def _click_plot(self, index):
gender_nd_overlay = hv.NdOverlay(kdims=["Gender"])
if not index:
return hv.NdOverlay(
{
"curve": self._curve_nd_overlay,
"scatter": self._scatter_nd_overlay,
"label": self._label_nd_overlay,
}
)
name = self._name_indices[index[0]]
df_name = self.df.loc[self.df["name"] == name].copy()
df_name["female"] += df_name["male"]
gender_nd_overlay["Male"] = hv.Area(
df_name, ["year"], ["male"], label="Male"
).opts(alpha=0.3, color="#add8e6", line_alpha=0)
gender_nd_overlay["Female"] = hv.Area(
df_name, ["year"], ["male", "female"], label="Female"
).opts(alpha=0.3, color="#ffb6c1", line_alpha=0)
return hv.NdOverlay(
{
"curve": self._curve_nd_overlay[[index[0]]],
"scatter": self._scatter_nd_overlay,
"label": self._label_nd_overlay[[index[0]]].opts(text_color="black"),
"gender": gender_nd_overlay,
},
kdims=["Gender"],
).opts(legend_position="top_left")
@staticmethod
def _format_y(value):
return f"{value / 1000}k"
def _update_plot(self, event):
names = event.new
print(names)
self._query_names(names)
self._scatter_nd_overlay = hv.NdOverlay()
self._curve_nd_overlay = hv.NdOverlay(kdims=["Name"]).opts(
gridstyle={"xgrid_line_width": 0},
show_grid=True,
fontscale=1.28,
xlabel="Year",
ylabel="Count",
yformatter=self._format_y,
legend_limit=0,
padding=(0.2, 0.05),
title="Name Chronicles",
responsive=True,
)
self._label_nd_overlay = hv.NdOverlay(kdims=["Name"])
hover_tool = HoverTool(
tooltips=[("Name", "@name"), ("Year", "@year"), ("Count", "@count")],
)
self._name_indices = {}
scatter_cycle = hv.Cycle("Category10")
curve_cycle = hv.Cycle("Category10")
label_cycle = hv.Cycle("Category10")
for i, (name, df_name) in enumerate(self.df.groupby("name")):
df_name_total = df_name.groupby(
["name", "year", "male", "female"], as_index=False
)["count"].sum()
df_name_total["male"] = df_name_total["male"] / df_name_total["count"]
df_name_total["female"] = df_name_total["female"] / df_name_total["count"]
df_name_peak = df_name.loc[[df_name["count"].idxmax()]]
df_name_peak[
"label"
] = f'{df_name_peak["name"].item()} ({df_name_peak["year"].item()})'
hover_tool = HoverTool(
tooltips=[
("Name", "@name"),
("Year", "@year"),
("Count", "@count{(0a)}"),
("Male", "@male{(0%)}"),
("Female", "@female{(0%)}"),
],
)
self._scatter_nd_overlay[i] = hv.Scatter(
df_name_total, ["year"], ["count", "male", "female", "name"], label=name
).opts(
color=scatter_cycle,
size=4,
alpha=0.15,
marker="y",
tools=["tap", hover_tool],
line_width=3,
show_legend=False,
)
self._curve_nd_overlay[i] = hv.Curve(
df_name_total, ["year"], ["count"], label=name
).opts(
color=curve_cycle,
tools=["tap"],
line_width=3,
)
self._label_nd_overlay[i] = hv.Labels(
df_name_peak, ["year", "count"], ["label"], label=name
).opts(
text_align="right",
text_baseline="bottom",
text_color=label_cycle,
)
self._name_indices[i] = name
self.selection.source = self._curve_nd_overlay
if len(self._name_indices) == 1:
self.selection.update(index=[0])
else:
self.selection.update(index=[])
self.dynamic_map = hv.DynamicMap(
self._click_plot, kdims=[], streams=[self.selection]
).opts(responsive=True)
self._refresh_plot()
def _refresh_plot(self, event=None):
self.holoviews_pane.object = self.dynamic_map.clone()
def view(self):
reset_row = pn.Row(self.clear_button, self.refresh_button)
data_url = pn.pane.Markdown(
"<center>Data from the <a href='https://www.ssa.gov/oact/babynames/limits.html' "
"target='_blank'>U.S. Social Security Administration</a></center>",
align="end",
)
sidebar = pn.Column(
INSTRUCTIONS,
self.names_input,
self.names_choice,
reset_row,
pn.layout.Divider(),
self.randomize_pane,
self.ai_pane,
data_url,
)
self.main = pn.Column(
pn.widgets.StaticText(value="Loading, this may take a few seconds...", sizing_mode="stretch_both"),
)
template = pn.template.FastListTemplate(
sidebar=[sidebar],
main=[self.main],
title="Name Chronicles",
theme="dark",
)
return template
NameChronicles().view().servable()
|