-
Notifications
You must be signed in to change notification settings - Fork 9
/
app.py
109 lines (89 loc) · 3.39 KB
/
app.py
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
"""Streamlit app for the Generative AI prototypes."""
import os
import openai
import streamlit as st
from dotenv import load_dotenv
from streamlit_option_menu import option_menu
from genai.streamlit_pages import early_year_activity_plan
from genai.streamlit_pages import eli3
from genai.streamlit_pages import eyfs_compare
from genai.streamlit_pages import eyfs_dm_kb
from genai.streamlit_pages import eyfs_kb_bbc
from genai.streamlit_pages import parenting_chatbot
from genai.streamlit_pages.utils import reset_state
load_dotenv()
def auth_openai() -> None:
"""Authenticate with OpenAI."""
try:
openai.api_key = os.environ["OPENAI_API_KEY"]
except Exception:
openai.api_key = st.secrets["OPENAI_API_KEY"]
def s3_creds() -> None:
"""Get s3 creds."""
try:
aws_key = os.environ["AWS_ACCESS_KEY_ID"]
aws_secret = os.environ["AWS_SECRET_ACCESS_KEY"]
s3_path = os.environ["S3_BUCKET"]
except Exception:
aws_key = st.secrets["AWS_ACCESS_KEY_ID"]
aws_secret = st.secrets["AWS_SECRET_ACCESS_KEY"]
s3_path = st.secrets["S3_BUCKET"]
return aws_key, aws_secret, s3_path
def check_password() -> bool:
"""Return `True` if the user had the correct password."""
def password_entered() -> None:
"""Check whether a password entered by the user is correct."""
if st.session_state["password"] == st.secrets["password"]:
st.session_state["password_correct"] = True
del st.session_state["password"] # don't store password
else:
st.session_state["password_correct"] = False
if "password_correct" not in st.session_state:
# First run, show input for password.
st.text_input("Password", type="password", on_change=password_entered, key="password")
return False
elif not st.session_state["password_correct"]:
# Password not correct, show input + error.
st.text_input("Password", type="password", on_change=password_entered, key="password")
st.error("😕 Password incorrect")
return False
else:
# Password correct.
return True
def main() -> None:
"""Run the app."""
auth_openai()
aws_key, aws_secret, s3_path = s3_creds()
with st.sidebar:
selected = option_menu(
"Prototypes",
[
"Home page",
"ELI3",
"EYFS-based activities",
"EYFS-based activities + BBC activities",
"EYFS prototypes comparison",
"Development Matters prototype",
"Parenting Chatbot",
],
default_index=0,
on_change=reset_state,
key="menu_selection",
)
if selected == "Home page":
st.title("Nesta Discovery: Generative AI Prototypes")
st.write("Welcome to the Nesta Discovery Generative AI prototypes. Please select a prototype from the menu.")
elif selected == "ELI3":
eli3()
elif selected == "EYFS-based activities":
early_year_activity_plan()
elif selected == "EYFS-based activities + BBC activities":
eyfs_kb_bbc()
elif selected == "EYFS prototypes comparison":
eyfs_compare()
elif selected == "Development Matters prototype":
eyfs_dm_kb()
elif selected == "Parenting Chatbot":
parenting_chatbot(aws_key, aws_secret, s3_path)
if check_password():
main()