Create app.py
Browse files
app.py
CHANGED
|
@@ -1,157 +1,49 @@
|
|
| 1 |
-
from
|
| 2 |
-
|
| 3 |
-
import
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
import pandas as pd
|
| 6 |
-
import seaborn as sns
|
| 7 |
-
import shinyswatch
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 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 |
-
|
| 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 |
-
)
|
|
|
|
|
|
|
|
|
| 1 |
+
from IPython.display import Audio
|
| 2 |
+
import openai
|
| 3 |
+
import requests
|
| 4 |
+
import json
|
| 5 |
+
import os
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# Set up OpenAI API credentials
|
| 9 |
+
openai.api_key = "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"
|
| 10 |
|
| 11 |
+
# Set up ElevenLabs API credentials
|
| 12 |
+
elevenlabs_api_key = "3ea525bd0795cc4367ca2016f0327d96"
|
| 13 |
|
| 14 |
+
def generate_response(prompt):
|
| 15 |
+
response = openai.ChatCompletion.create(
|
| 16 |
+
model="gpt-3.5-turbo",
|
| 17 |
+
messages=[
|
| 18 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 19 |
+
{"role": "user", "content": prompt}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
]
|
| 21 |
+
)
|
| 22 |
+
return response.choices[0].message.content.strip()
|
| 23 |
+
|
| 24 |
+
def text_to_speech(text):
|
| 25 |
+
url = 'https://api.elevenlabs.io/v1/text-to-speech/21m00Tcm4TlvDq8ikWAM/stream?optimize_streaming_latency=0'
|
| 26 |
+
headers = {
|
| 27 |
+
'accept': '*/*',
|
| 28 |
+
'xi-api-key': elevenlabs_api_key,
|
| 29 |
+
'Content-Type': 'application/json'
|
| 30 |
+
}
|
| 31 |
+
data = {
|
| 32 |
+
'text': text,
|
| 33 |
+
'model_id': 'eleven_monolingual_v1',
|
| 34 |
+
'voice_settings': {
|
| 35 |
+
'stability': 0,
|
| 36 |
+
'similarity_boost': 0
|
| 37 |
+
}
|
| 38 |
+
}
|
| 39 |
+
response = requests.post(url, headers=headers, data=json.dumps(data))
|
| 40 |
+
return response.content
|
| 41 |
+
|
| 42 |
+
# Main conversation loop
|
| 43 |
+
while True:
|
| 44 |
+
user_input = input('User: ')
|
| 45 |
+
response = generate_response(user_input)
|
| 46 |
+
print('ChatGPT: ' + response)
|
| 47 |
+
speech = text_to_speech(response)
|
| 48 |
+
audio = Audio(data=speech, autoplay=True)
|
| 49 |
+
display(audio)
|