carebuds / app.py
quantresearch1's picture
add password
56b4129
raw
history blame contribute delete
No virus
7 kB
import gradio as gr
from kids_main import optimize_schedule_for_pool, Duty
import pandas as pd
import PIL
import cv2
import gradio as gr
title = "CareBuds"
def load_image(path: str):
init_image = cv2.imread(path)
init_image = cv2.cvtColor(init_image, cv2.COLOR_BGR2RGB)
init_image = PIL.Image.fromarray(init_image).resize((512, 512))
return init_image
def find_pod(path: str):
init_image = load_image(path)
return [init_image, gr.Image(visible=bool(1)), gr.Textbox(visible=bool(1))]
def find_matches_based_on_schedule(
mon: bool, tue: bool, wed: bool, thu: bool, fri: bool
):
"""Basic compatibility means removing family without any availibility on the days you need them"""
from database import offer
cols = []
for col, v in zip(
["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"],
[mon, tue, wed, thu, fri],
):
if v is True:
cols.append(col)
is_compatible = offer[cols].any(axis=1)
return offer.loc[is_compatible, "family"]
def create_df(
name: str, mon: bool, tue: bool, wed: bool, thu: bool, fri: bool
) -> pd.DataFrame:
df = pd.DataFrame(
[
(
name,
1 if mon is True else 0,
1 if tue is True else 0,
1 if wed is True else 0,
1 if thu is True else 0,
1 if fri is True else 0,
)
],
columns=[
"Family",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
],
# dtype=[str, int, int, int, int, int],
)
return df
def gr_optimize_schedule_for_pool(
name: str, mon: bool, tue: bool, wed: bool, thu: bool, fri: bool
):
df = create_df(name, mon, tue, wed, thu, fri)
calendar = optimize_schedule_for_pool(df) # .set_index("Family")
styler = calendar.style.map(
lambda v: "color:lightgreen" if v == Duty.OnDuty else "color:red",
subset=pd.IndexSlice[:, calendar.columns[1:]],
)
return [styler, gr.Image(visible=bool(1))]
# Styline themes:
with gr.Blocks(
theme=gr.themes.Soft(primary_hue="blue", secondary_hue="cyan"), title=title
) as demo:
with gr.Tab("Find care buds"):
with gr.Row():
gr.Markdown(
"""
# Welcome to CareBuds!
Let's find care buds for you! Please fill the form below to get started.
"""
)
with gr.Row():
gr_textbox_family = gr.Textbox(label="Family name")
gr_textbox_homeaddress = gr.Textbox(label="Home address")
gr_textbox_schoolname = gr.Textbox(label="School name")
gr_textbox_kid = gr.Textbox(label="Child name")
with gr.Row():
gr.Markdown(
"""
# Submit your schedule
Tick the days you CAN be on-duty:
"""
)
with gr.Row():
gr_textbox_user = gr.Textbox(value="Skywalker", visible=False)
gr_checkbox_mon = gr.Checkbox(label="Monday")
gr_checkbox_tue = gr.Checkbox(label="Tuesday")
gr_checkbox_wed = gr.Checkbox(label="Wednesday")
gr_checkbox_thu = gr.Checkbox(label="Thursday")
gr_checkbox_fri = gr.Checkbox(label="Friday")
gr_button_find_pod = gr.Button("Find care buds")
with gr.Row():
gr_image_stark = gr.Image(
value=None, type="pil", show_label=False, visible=False, scale=0.3
)
gr_image_potter = gr.Image(
value=None, type="pil", show_label=False, visible=False, scale=0.3
)
with gr.Row():
gr_button_contactfamily1 = gr.Button(
"Contact this family", visible=False, scale=0.3
)
gr_button_contactfamily2 = gr.Button(
"Contact this family", visible=False, scale=0.3
)
gr_button_find_pod.click(
find_pod,
inputs=[gr.Textbox(value="Stark.png", visible=False)],
outputs=[gr_image_stark, gr_image_stark, gr_button_contactfamily1],
)
gr_button_find_pod.click(
find_pod,
inputs=[gr.Textbox(value="Potter.png", visible=False)],
outputs=[gr_image_potter, gr_image_potter, gr_button_contactfamily2],
)
with gr.Tab("Get your schedule"):
with gr.Row():
gr.Markdown(
"""
Care buds
"""
)
with gr.Row():
image_skywalker = load_image("Skywalker.png")
image_stark = load_image("Stark.png")
image_potter = load_image("Potter.png")
gr_image_pod_sky = gr.Image(
value=image_skywalker, type="pil", label="Skywalker", container=True
)
gr_image_pod_stark = gr.Image(value=image_stark, type="pil", label="Stark")
gr_image_pod_potter = gr.Image(
value=image_potter, type="pil", label="Potter"
)
with gr.Row():
gr.Markdown(
"""
# Submit your schedule
Tick the days you CAN be on-duty:
"""
)
with gr.Row():
gr_textbox_user = gr.Textbox(value="Skywalker", visible=False)
gr_checkbox_mon = gr.Checkbox(label="Monday")
gr_checkbox_tue = gr.Checkbox(label="Tuesday")
gr_checkbox_wed = gr.Checkbox(label="Wednesday")
gr_checkbox_thu = gr.Checkbox(label="Thursday")
gr_checkbox_fri = gr.Checkbox(label="Friday")
gr_solve_event = gr.Button("Get schedule", scale=0.2)
gr_df_offer = gr.DataFrame(visible=False)
with gr.Row():
gr.Markdown(
"""
# Get the care buds schedule
"""
)
# Could skip the button and use .change or .input on gr_df
with gr.Row():
gr_sol = gr.DataFrame(
headers=[
"Family",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
],
datatype=["str", "number", "number", "number", "number", "number"],
col_count=(6, "fixed"),
row_count=(3, "fixed"),
visible=False,
)
gr_solve_event.click(
gr_optimize_schedule_for_pool,
inputs=[
gr_textbox_user,
gr_checkbox_mon,
gr_checkbox_tue,
gr_checkbox_wed,
gr_checkbox_thu,
gr_checkbox_fri,
],
outputs=[gr_sol, gr_sol],
)
demo.launch(auth=("foryc", "letsgo"))