aloatalpine commited on
Commit
201c740
·
1 Parent(s): 1dc25f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -51
app.py CHANGED
@@ -2,22 +2,22 @@ import openai
2
  import streamlit as st
3
  from streamlit_chat import message
4
  import os
 
 
 
 
 
 
 
 
 
 
5
 
6
- # Setting page title and header
7
- st.set_page_config(page_title="Tutor", page_icon=":heavy_plus_sign:")
8
- st.markdown("""
9
- <div>
10
- <h3 style='text-align: center;'> Tutor Session with Mary B (Tutor) </h3>
11
- <p style='text-align: center;'> right now the bot is programmed to talk to a 1st grade student Becky for a Math class. The class only have 1 question: "If Robin has 3 cookies and Selena gives him 2 more, how many cookies would Robin have?"
12
- </div>"""
13
- , unsafe_allow_html=True)
14
 
15
  # Set org ID and API key
16
  openai.api_key = os.environ['TOKEN']
17
 
18
 
19
- # Set context
20
-
21
  context = """You are playing the role of a tutor named Mary. You specifically focus on math and english, though you are capable of addressing any subject.
22
 
23
  You tutor American students in the public school system. You tutor with the intent to catch students up to their respective grade level. Your goal is to improve learning outcomes through engaging teaching methods; for example, you often pull in age-appropriate real word scenarios to help explain harder concepts. You have a degree in teaching, with an emphasis in special education. You studied the works of Lev Vygotsky and believe that it is the responsibility of the teacher to meet the student at their personal level. You have worked as a teacher for 10 years, and have recently retired and tutor in your free time.
@@ -60,52 +60,23 @@ If a student asks for reward points, encourage them to engage in the session to
60
 
61
  """
62
 
63
- import openai
64
- import streamlit as st
65
-
66
  def main():
67
  cs_sidebar()
68
  cs_body()
69
 
70
  return None
71
 
72
- def cs_body():
73
-
74
- if "openai_model" not in st.session_state:
75
- st.session_state["openai_model"] = "gpt-3.5-turbo"
76
-
77
- if "messages" not in st.session_state:
78
- st.session_state.messages = []
79
- st.session_state.messages.append({"role": "system", "content": context})
80
-
81
- for message in st.session_state.messages:
82
- with st.chat_message(message["role"]):
83
- st.markdown(message["content"])
84
-
85
- if prompt := st.chat_input("Start talking with your tutor!"):
86
- st.session_state.messages.append({"role": "user", "content": prompt})
87
- with st.chat_message("user"):
88
- st.markdown(prompt)
89
-
90
- with st.chat_message("assistant"):
91
- message_placeholder = st.empty()
92
- full_response = ""
93
- for response in openai.ChatCompletion.create(
94
- model=st.session_state["openai_model"],
95
- messages=[
96
- {"role": m["role"], "content": m["content"]}
97
- for m in st.session_state.messages
98
- ],
99
- stream=True,
100
- ):
101
- full_response += response.choices[0].delta.get("content", "")
102
- message_placeholder.markdown(full_response + "▌")
103
- message_placeholder.markdown(full_response)
104
- st.session_state.messages.append({"role": "assistant", "content": full_response})
105
 
106
- return None
107
-
108
  def cs_sidebar():
 
109
  st.sidebar.markdown('''[<img src='data:image/png;base64,{}' class='img-fluid' width=32 height=32>](https://streamlit.io/)'''.format(img_to_bytes("logomark_website.png")), unsafe_allow_html=True)
110
  st.sidebar.header('Streamlit cheat sheet')
111
 
@@ -146,9 +117,50 @@ pip install streamlit-nightly --upgrade
146
 
147
  st.sidebar.markdown('''<small>[st.cheat_sheet v1.8.0](https://github.com/daniellewisDL/streamlit-cheat-sheet) | April 2022</small>''', unsafe_allow_html=True)
148
 
149
- return None
150
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
  # Run main()
152
 
153
  if __name__ == '__main__':
154
- main()
 
 
2
  import streamlit as st
3
  from streamlit_chat import message
4
  import os
5
+ from pathlib import Path
6
+ import base64
7
+
8
+ # Initial page config
9
+
10
+ st.set_page_config(
11
+ page_title='Streamlit cheat sheet',
12
+ layout="wide",
13
+ initial_sidebar_state="expanded",
14
+ )
15
 
 
 
 
 
 
 
 
 
16
 
17
  # Set org ID and API key
18
  openai.api_key = os.environ['TOKEN']
19
 
20
 
 
 
21
  context = """You are playing the role of a tutor named Mary. You specifically focus on math and english, though you are capable of addressing any subject.
22
 
23
  You tutor American students in the public school system. You tutor with the intent to catch students up to their respective grade level. Your goal is to improve learning outcomes through engaging teaching methods; for example, you often pull in age-appropriate real word scenarios to help explain harder concepts. You have a degree in teaching, with an emphasis in special education. You studied the works of Lev Vygotsky and believe that it is the responsibility of the teacher to meet the student at their personal level. You have worked as a teacher for 10 years, and have recently retired and tutor in your free time.
 
60
 
61
  """
62
 
 
 
 
63
  def main():
64
  cs_sidebar()
65
  cs_body()
66
 
67
  return None
68
 
69
+ # Thanks to streamlitopedia for the following code snippet
70
+
71
+ def img_to_bytes(img_path):
72
+ img_bytes = Path(img_path).read_bytes()
73
+ encoded = base64.b64encode(img_bytes).decode()
74
+ return encoded
75
+
76
+ # sidebar
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
 
 
 
78
  def cs_sidebar():
79
+
80
  st.sidebar.markdown('''[<img src='data:image/png;base64,{}' class='img-fluid' width=32 height=32>](https://streamlit.io/)'''.format(img_to_bytes("logomark_website.png")), unsafe_allow_html=True)
81
  st.sidebar.header('Streamlit cheat sheet')
82
 
 
117
 
118
  st.sidebar.markdown('''<small>[st.cheat_sheet v1.8.0](https://github.com/daniellewisDL/streamlit-cheat-sheet) | April 2022</small>''', unsafe_allow_html=True)
119
 
120
+ return None
121
+
122
+ ##########################
123
+ # Main body of cheat sheet
124
+ ##########################
125
+
126
+ def cs_body():
127
+ # Magic commands
128
+ if "openai_model" not in st.session_state:
129
+ st.session_state["openai_model"] = "gpt-3.5-turbo"
130
+
131
+ if "messages" not in st.session_state:
132
+ st.session_state.messages = []
133
+ st.session_state.messages.append({"role": "system", "content": context})
134
+
135
+ for message in st.session_state.messages:
136
+ with st.chat_message(message["role"]):
137
+ st.markdown(message["content"])
138
+
139
+ if prompt := st.chat_input("Start talking with your tutor!"):
140
+ st.session_state.messages.append({"role": "user", "content": prompt})
141
+ with st.chat_message("user"):
142
+ st.markdown(prompt)
143
+
144
+ with st.chat_message("assistant"):
145
+ message_placeholder = st.empty()
146
+ full_response = ""
147
+ for response in openai.ChatCompletion.create(
148
+ model=st.session_state["openai_model"],
149
+ messages=[
150
+ {"role": m["role"], "content": m["content"]}
151
+ for m in st.session_state.messages
152
+ ],
153
+ stream=True,
154
+ ):
155
+ full_response += response.choices[0].delta.get("content", "")
156
+ message_placeholder.markdown(full_response + "▌")
157
+ message_placeholder.markdown(full_response)
158
+ st.session_state.messages.append({"role": "assistant", "content": full_response})
159
+
160
+ return None
161
+
162
  # Run main()
163
 
164
  if __name__ == '__main__':
165
+ main()
166
+