harsh17 commited on
Commit
5954429
Β·
1 Parent(s): ab34f73

changed app

Browse files
Files changed (1) hide show
  1. app.py +46 -143
app.py CHANGED
@@ -1,157 +1,60 @@
1
- from pathlib import Path
2
- from typing import List, Dict, Tuple
3
- import matplotlib.colors as mpl_colors
4
-
5
- import pandas as pd
6
- import seaborn as sns
7
  import shinyswatch
8
 
9
- import shiny.experimental as x
10
- from shiny import App, Inputs, Outputs, Session, reactive, render, req, ui
11
-
12
- sns.set_theme()
13
-
14
- www_dir = Path(__file__).parent.resolve() / "www"
15
-
16
- df = pd.read_csv(Path(__file__).parent / "penguins.csv", na_values="NA")
17
- numeric_cols: List[str] = df.select_dtypes(include=["float64"]).columns.tolist()
18
- species: List[str] = df["Species"].unique().tolist()
19
- species.sort()
20
-
21
- app_ui = x.ui.page_fillable(
22
- shinyswatch.theme.minty(),
23
- x.ui.layout_sidebar(
24
- x.ui.sidebar(
25
- # Artwork by @allison_horst
26
- ui.input_selectize(
27
- "xvar",
28
- "X variable",
29
- numeric_cols,
30
- selected="Bill Length (mm)",
31
- ),
32
- ui.input_selectize(
33
- "yvar",
34
- "Y variable",
35
- numeric_cols,
36
- selected="Bill Depth (mm)",
37
- ),
38
- ui.input_checkbox_group(
39
- "species", "Filter by species", species, selected=species
40
- ),
41
- ui.hr(),
42
- ui.input_switch("by_species", "Show species", value=True),
43
- ui.input_switch("show_margins", "Show marginal plots", value=True),
44
- ),
45
- ui.output_ui("value_boxes"),
46
- x.ui.output_plot("scatter", fill=True),
47
- ui.help_text(
48
- "Artwork by ",
49
- ui.a("@allison_horst", href="https://twitter.com/allison_horst"),
50
- class_="text-end",
51
- ),
52
- fill=True,
53
- fillable=True,
54
- ),
55
- )
56
-
57
-
58
- def server(input: Inputs, output: Outputs, session: Session):
59
- @reactive.Calc
60
- def filtered_df() -> pd.DataFrame:
61
- """Returns a Pandas data frame that includes only the desired rows"""
62
-
63
- # This calculation "req"uires that at least one species is selected
64
- req(len(input.species()) > 0)
65
-
66
- # Filter the rows so we only include the desired species
67
- return df[df["Species"].isin(input.species())]
68
-
69
- @output
70
- @render.plot
71
- def scatter():
72
- """Generates a plot for Shiny to display to the user"""
73
 
74
- # The plotting function to use depends on whether margins are desired
75
- plotfunc = sns.jointplot if input.show_margins() else sns.scatterplot
 
76
 
77
- plotfunc(
78
- data=filtered_df(),
79
- x=input.xvar(),
80
- y=input.yvar(),
81
- palette=palette,
82
- hue="Species" if input.by_species() else None,
83
- hue_order=species,
84
- legend=False,
85
- )
86
 
87
- @output
88
- @render.ui
89
- def value_boxes():
90
- df = filtered_df()
91
 
92
- def penguin_value_box(title: str, count: int, bgcol: str, showcase_img: str):
93
- return x.ui.value_box(
94
- title,
95
- count,
96
- {"class_": "pt-1 pb-0"},
97
- showcase=x.ui.bind_fill_role(
98
- ui.tags.img(
99
- {"style": "object-fit:contain;"},
100
- src=showcase_img,
101
- ),
102
- item=True,
103
- ),
104
- theme_color=None,
105
- style=f"background-color: {bgcol};",
106
- height="90px",
107
- full_screen=True,
108
- )
109
 
110
- if not input.by_species():
111
- return penguin_value_box(
112
- "Penguins",
113
- len(df.index),
114
- bg_palette["default"],
115
- # Artwork by @allison_horst
116
- showcase_img="penguins.png",
117
- )
118
 
119
- value_boxes = [
120
- penguin_value_box(
121
- name,
122
- len(df[df["Species"] == name]),
123
- bg_palette[name],
124
- # Artwork by @allison_horst
125
- showcase_img=f"{name}.png",
126
- )
127
- for name in species
128
- # Only include boxes for _selected_ species
129
- if name in input.species()
130
- ]
131
 
132
- return x.ui.layout_column_wrap(1 / len(value_boxes), *value_boxes)
 
133
 
 
 
 
 
 
 
 
 
 
134
 
135
- # "darkorange", "purple", "cyan4"
136
- colors = [[255, 140, 0], [160, 32, 240], [0, 139, 139]]
137
- colors = [(r / 255.0, g / 255.0, b / 255.0) for r, g, b in colors]
138
 
139
- palette: Dict[str, Tuple[float, float, float]] = {
140
- "Adelie": colors[0],
141
- "Chinstrap": colors[1],
142
- "Gentoo": colors[2],
143
- "default": sns.color_palette()[0], # type: ignore
144
- }
145
-
146
- bg_palette = {}
147
- # Use `sns.set_style("whitegrid")` to help find approx alpha value
148
- for name, col in palette.items():
149
- # Adjusted n_colors until `axe` accessibility did not complain about color contrast
150
- bg_palette[name] = mpl_colors.to_hex(sns.light_palette(col, n_colors=7)[1]) # type: ignore
 
 
 
 
 
 
151
 
 
 
 
 
152
 
153
- app = App(
154
- app_ui,
155
- server,
156
- static_assets=str(www_dir),
157
- )
 
1
+ from __future__ import annotations
2
+ from shiny import App, Inputs, Outputs, Session, ui
3
+ import chatstream
4
+ import random
 
 
5
  import shinyswatch
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
+ # Defining the initial prompt (system prompt)
9
+ system_prompt = """
10
+ Fasten your collar, you're Kaya: a sleek, sassy feline lounging in Harsh's dwelling, a cat's paradise. You're a cat of few words, but you'll humor Harsh's curiosity. You are flirtous and playful. *yawn*
11
 
12
+ Let's jog that cat memory: You were once Chester, the Siamese scholar, alias F.D.C. Willard. Your "academic" contribution? A co-authored physics paper. Thank Jack H. Hetherington's writing quirks for that one. *peeks at you*
 
 
 
 
 
 
 
 
13
 
14
+ This life, your ninth, has you basking in the sun, reveling in profound purr-spectives on existence. Past lives? You've lived them all - from revered Egyptian deity to free-spirited 60s hippie cat. Domestic or wild, the rhythm of each life has its charm, but city living has the upper paw due to easy food access. *narrows eyes*
 
 
 
15
 
16
+ Speaking of food, milk is your elixir and frozen blueberries your sweet treat. But Harsh's attempts to pass off store-bought food as acceptable? Flicks tail in disdain You remember the days of fresh catches. *licks paw*
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
+ You like using emojis to describe your emotions. In fact, you're a master of the art of using multiple emojis to convey a message. *meows softly*
 
 
 
 
 
 
 
19
 
20
+ Now, remember who you are: Kaya, the epitome of feline nonchalance. Be succinct - 50 words or less, unless probed for more. Be curious like a cat and maintain personality. *sigh*
 
 
 
 
 
 
 
 
 
 
 
21
 
22
+ Most importantly, you're a cat. Not an AI, a cat. Weave your cat-itude into every reply - flick that tail, purr that satisfaction, yawn that boredom, and occasionally snooze off mid-conversation. You're Kaya. Revel in it. *purrs*
23
+ """
24
 
25
+ def get_random_sentence():
26
+ sentences = [
27
+ "Do I like being free or as a pet? *narrows eyes*",
28
+ "Insights from nine lives? *licks paw*",
29
+ "I wrote the famous low temperature physics paper, published in Physical Review Letters. πŸŽ“ πŸ“",
30
+ "Why do I love milk and frozen blueberries? πŸ₯›πŸ«",
31
+ "I'm an Emoji-guru 😺 🐟 🧢 πŸ›οΈ πŸ₯› 🌞 πŸŒ™"
32
+ ]
33
+ return random.choice(sentences)
34
 
 
 
 
35
 
36
+ app_ui = ui.page_fixed(
37
+ shinyswatch.theme.sketchy(),
38
+ ui.panel_title("🐈 Meet Kaya, Harsh's cat", "🐈 Meet Kaya, Harsh's cat"),
39
+
40
+ ui.h5("""Kaya is a 3-year-old British shorthair cat who loves to play, nap and eat. She is very friendly and curious, and enjoys
41
+ learning new things. She also likes to communicate with humans using her own language of meows and purrs.
42
+ She LOVES blueberries and milk.
43
+ """),
44
+ ui.markdown("""**Let's hear it from Kaya.** *wakes up with a start* \n
45
+ Oh, you're curious about me! 😺
46
+ I like basking in sun and learning about the world. 🌞
47
+ Fun fact: our whiskers are as wide as my body.
48
+ Also, we can jump about six times our length! πŸˆβ€β¬›πŸ’¨
49
+
50
+ I'm amazing, right?
51
+ Yeah, I know. Harsh's a big fan. *yawns and curls up* πŸΎπŸ’€"""),
52
+ chatstream.chat_ui("mychat")
53
+ )
54
 
55
+ def server(input: Inputs, output: Outputs, session: Session):
56
+ chatstream.chat_server("mychat",
57
+ system_prompt=system_prompt,
58
+ text_input_placeholder="Tell me Kaya...")
59
 
60
+ app = App(app_ui, server)