Skip to content

Commit

Permalink
Target app refactor (#52)
Browse files Browse the repository at this point in the history
* Remove conditional panel

* Add data tab with value boxes
  • Loading branch information
Gordon Shotwell authored Mar 27, 2024
1 parent cbbaf79 commit c8afe93
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 79 deletions.
142 changes: 64 additions & 78 deletions apps/target-app/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from numpy import char, place
from shiny.express import ui, input, render
from shiny import render_plot, req, reactive
from shiny import reactive
import pandas as pd
from pathlib import Path
from plots import (
Expand All @@ -11,45 +12,33 @@
import faicons as fa
import io
from shinywidgets import render_plotly
import time

file_path = Path(__file__).parent / "simulated-data.csv"

ui.page_opts(title="Monitoring")
df = pd.read_csv(file_path, dtype={"sub_account": str})
df["date"] = pd.to_datetime(df["date"], errors="coerce")


@reactive.calc
def monitor_sampled_data() -> pd.DataFrame:
start_date, end_date = input.dates()
start_date = pd.to_datetime(start_date)
end_date = pd.to_datetime(end_date)
df_value = df
out = df_value[
(df_value["date"] > start_date) & (df_value["date"] <= end_date)
].sample(n=input.sample(), replace=True)
return out


@reactive.calc()
def monitor_filtered_data() -> pd.DataFrame:
sample_df = monitor_sampled_data()
sample_df = sample_df.loc[sample_df["account"] == input.account()]
return sample_df.reset_index(drop=True)
def account_data():
return df[
(df["account"] == input.account()) & (df["sub_account"] == input.sub_account())
]


@reactive.calc()
def training_data():
return df[
(df["account"] == input.account()) & (df["sub_account"] == input.sub_account())
def character_filter():
return account_data()[
(account_data()["text"].str.len() >= input.chars()[0])
& (account_data()["text"].str.len() <= input.chars()[1])
]


@reactive.effect
@reactive.event(input.reset)
def reset_vals():
ui.update_date_range("dates", start="2023-01-01", end="2023-04-01")
ui.update_numeric("sample", value=10000)
ui.update_slider("chars", value=[500, 6000])


with ui.sidebar():
Expand All @@ -71,56 +60,70 @@ def sub_selector():
choices = choice_data["sub_account"].unique().tolist()
ui.input_select("sub_account", "Sub Account", choices=choices)

with ui.panel_conditional("input.tabs !== 'Training Dashboard'"):
ui.input_date_range(
"dates",
"Dates",
start="2023-01-01",
end="2023-04-01",
with ui.tooltip(id="btn_tooltip", placement="right"):
ui.input_slider(
"chars",
"Text length",
min=0,
max=df["text"].str.len().max(),
value=[500, 6000],
)
ui.input_numeric("sample", "Sample Size", value=10000, step=5000)
ui.input_action_button("reset", "Reset Values", class_="btn-primary")


with ui.navset_bar(id="tabs", title="Monitoring"):
with ui.nav_panel("Training Dashboard"):
with ui.layout_columns():
with ui.card():
ui.card_header("Model Metrics")

@render_plotly
def metric():
if input.metric() == "ROC Curve":
return plot_auc_curve(
training_data(), "is_electronics", "training_score"
)
else:
return plot_precision_recall_curve(
training_data(), "is_electronics", "training_score"
)
"The number of characters in the text"

ui.input_action_button("reset", "Reset Values", class_="btn-primary")

with ui.nav_panel("Training Dashboard"):
with ui.layout_columns():
with ui.card():
ui.card_header("Model Metrics")

@render_plotly
def metric():
df_value = df()
df_filtered = df_value[df_value["account"] == input.account()]
if input.metric() == "ROC Curve":
return plot_auc_curve(
df_filtered, "is_electronics", "training_score"
character_filter(), "is_electronics", "training_score"
)
else:
return plot_precision_recall_curve(
df_filtered, "is_electronics", "training_score"
character_filter(), "is_electronics", "training_score"
)

@render_plotly
def score_dist():
return plot_score_distribution(training_data())
ui.input_select(
"metric", "Metric", choices=["ROC Curve", "Precision Recall"]
)

with ui.card():
ui.card_header("Model Scores")

@render_plotly
def score_dist():
df_value = df()
df_filtered = df_value[df_value["account"] == input.account()]
return plot_score_distribution(df_filtered)
return plot_score_distribution(character_filter())


vb_theme = "bg-blue"

with ui.nav_panel("Data"):
with ui.layout_columns():
with ui.value_box(theme=vb_theme):
"Number of records"

@render.text
def data_count():
return str(character_filter().shape[0])

with ui.value_box(theme=vb_theme):
"Mean Score"

@render.text
def mean_score(theme=vb_theme):
return f"{character_filter()['training_score'].mean() * 100:.2f}%"

with ui.value_box(theme=vb_theme):
"Mean Text Length"

@render.text
def mean_text_length():
return f"{character_filter()['text'].str.len().mean():.2f} characters"

with ui.card(full_screen=True):
with ui.card_header():
Expand All @@ -134,27 +137,10 @@ def score_dist():
@render.download(filename=lambda: f"{input.account()}_scores.csv")
def download_data():
with io.BytesIO() as buf:
filtered_data().to_csv(buf, index=False)
account_data().to_csv(buf, index=False)
buf.seek(0)
yield buf.getvalue()

@render.data_frame
def data_output():
return filtered_data().drop(columns=["text"])


with ui.nav_panel("Model Monitoring"):
with ui.layout_columns():
with ui.card():
ui.card_header("API Response Time")

@render_plotly
def api_response():
return plot_api_response(filtered_data())

with ui.card():
ui.card_header("Production Scores")

@render_plotly
def prod_score_dist():
return plot_score_distribution(filtered_data())
return account_data().drop(columns=["text"])
Loading

0 comments on commit c8afe93

Please sign in to comment.