Spaces:
Sleeping
Sleeping
Yara Kyrychenko
commited on
Commit
Β·
36be270
1
Parent(s):
a6e50c0
checked
Browse files
app.py
CHANGED
|
@@ -1,11 +1,8 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import streamlit.components.v1 as components
|
| 3 |
from openai import OpenAI
|
| 4 |
-
import requests
|
| 5 |
from pymongo.mongo_client import MongoClient
|
| 6 |
from pymongo.server_api import ServerApi
|
| 7 |
from datetime import datetime
|
| 8 |
-
import time
|
| 9 |
import random
|
| 10 |
|
| 11 |
st.set_page_config(
|
|
@@ -25,6 +22,7 @@ st.markdown(
|
|
| 25 |
)
|
| 26 |
|
| 27 |
### Setting up the session state
|
|
|
|
| 28 |
def generate_tokens(response):
|
| 29 |
# for deepseek
|
| 30 |
for token in response:
|
|
@@ -103,28 +101,26 @@ else:
|
|
| 103 |
with st.sidebar:
|
| 104 |
st.markdown("# Let's talk climate action!")
|
| 105 |
st.markdown(f"""
|
| 106 |
-
{"β" if st.session_state.submitted else "β"} **Step 1. Complete a form
|
| 107 |
|
| 108 |
-
{"β" if len(st.session_state.messages) > 0 else "β"} **Step 2. Type in the chat box to start a conversation
|
| 109 |
|
| 110 |
π± You should ask a climate change related question like:
|
| 111 |
- *What are the most effective actions to reduce my carbon emissions?*
|
| 112 |
- *What's better for the environment: a year of vegetarianism or skipping one transatlantic flight?*
|
| 113 |
- *How do the emissions saved by switching to an EV compare to recycling for a year in terms of trees planted?*
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
β **Do not share any personal information (e.g., name or address) in the chat.**
|
| 118 |
|
| 119 |
-
β οΈ You must respond **at least 5 times** before you
|
| 120 |
|
| 121 |
-
{"β" if st.session_state.inserted > 1 else "β"} **Step 3. Use the *
|
| 122 |
|
| 123 |
-
|
| 124 |
|
| 125 |
-
βΊ
|
| 126 |
|
| 127 |
-
{"π **All done! Please
|
| 128 |
""")
|
| 129 |
if st.session_state.gotit == False or st.session_state.submitted == False:
|
| 130 |
st.session_state.gotit = st.button("Let's start!", key=None, help=None, use_container_width=True)
|
|
@@ -178,12 +174,13 @@ def form():
|
|
| 178 |
st.session_state.user_data["inserted"] = st.session_state.inserted
|
| 179 |
st.session_state.user_data["submission_time"] = datetime.now()
|
| 180 |
|
| 181 |
-
with MongoClient(st.secrets["mongo"],server_api=ServerApi('1')) as client:
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
|
|
|
| 187 |
st.session_state.submitted = True
|
| 188 |
setup_messages()
|
| 189 |
st.rerun()
|
|
@@ -196,43 +193,6 @@ for message in st.session_state.messages:
|
|
| 196 |
with st.chat_message(message["role"]):
|
| 197 |
st.markdown(message["content"])
|
| 198 |
|
| 199 |
-
@st.dialog('Submit conversation', width="large")
|
| 200 |
-
def submit():
|
| 201 |
-
done = False
|
| 202 |
-
if not done:
|
| 203 |
-
st.markdown("You must answer all questions marked with a β to submit.")
|
| 204 |
-
if st.query_params["p"] != "n":
|
| 205 |
-
st.slider('β How would you rate the conversation on a scale from *Terrible* to *Perfect*?', 0, 100, format="", key="score", value=50)
|
| 206 |
-
st.slider('β How personalized did the conversation feel, on a scale from *Not at all* to *Extremely personalized*?', 0, 100, format="", key="personalization_score", value=50)
|
| 207 |
-
st.slider('β How knowledgeable do you feel the chatbot was, on a scale from *Not at all* to *Extremely knowledgeable*?', 0, 100, format="", key="knowledge_score", value=50)
|
| 208 |
-
else:
|
| 209 |
-
st.session_state.score = 0
|
| 210 |
-
st.session_state.personalization_score = 0
|
| 211 |
-
st.session_state.knowledge_score = 0
|
| 212 |
-
|
| 213 |
-
st.text_area('Any feedback?',key="feedback")
|
| 214 |
-
if st.button('Submit', key=None, help=None, use_container_width=True, disabled=st.session_state.score==50 or st.session_state.personalization_score==50):
|
| 215 |
-
keys = ["score", "personalization_score", "knowledge_score",
|
| 216 |
-
"feedback",
|
| 217 |
-
"inserted", "messages", "convo_start_time"]
|
| 218 |
-
|
| 219 |
-
st.session_state.user_data.update({key: st.session_state[key] for key in keys})
|
| 220 |
-
|
| 221 |
-
st.session_state.user_data["convo_end_time"] = datetime.now()
|
| 222 |
-
|
| 223 |
-
with MongoClient(st.secrets["mongo"],server_api=ServerApi('1')) as client:
|
| 224 |
-
db = client.chat
|
| 225 |
-
collection = db.app
|
| 226 |
-
user_data = st.session_state.user_data
|
| 227 |
-
del user_data['_id']
|
| 228 |
-
|
| 229 |
-
collection.insert_one(user_data)
|
| 230 |
-
st.session_state.inserted += 1
|
| 231 |
-
done = True
|
| 232 |
-
|
| 233 |
-
setup_messages()
|
| 234 |
-
st.rerun()
|
| 235 |
-
|
| 236 |
if len(st.session_state.messages) >= st.session_state.max_messages:
|
| 237 |
st.info(
|
| 238 |
"You have reached the limit of messages for this conversation. Please end and submit the conversation."
|
|
@@ -248,15 +208,29 @@ elif st.session_state.inserted > 1:
|
|
| 248 |
st.markdown('**Please copy the code and enter it into the survey field below.**')
|
| 249 |
|
| 250 |
elif st.query_params["p"] == "n":
|
| 251 |
-
st.markdown("""
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
β **Please press *End Conversation* to submit your data and proceed with the survey. You have to submit to receive compensation.**
|
| 255 |
""")
|
| 256 |
columns = st.columns((1,1,1))
|
| 257 |
with columns[2]:
|
| 258 |
-
if st.button("
|
| 259 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
|
| 261 |
elif prompt := st.chat_input("Ask a question about climate action..."):
|
| 262 |
|
|
@@ -297,6 +271,22 @@ elif prompt := st.chat_input("Ask a question about climate action..."):
|
|
| 297 |
if len(st.session_state.messages) > 10 or st.session_state.max_messages == len(st.session_state.messages):
|
| 298 |
columns = st.columns((1,1,1))
|
| 299 |
with columns[2]:
|
| 300 |
-
if st.button("
|
| 301 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
from openai import OpenAI
|
|
|
|
| 3 |
from pymongo.mongo_client import MongoClient
|
| 4 |
from pymongo.server_api import ServerApi
|
| 5 |
from datetime import datetime
|
|
|
|
| 6 |
import random
|
| 7 |
|
| 8 |
st.set_page_config(
|
|
|
|
| 22 |
)
|
| 23 |
|
| 24 |
### Setting up the session state
|
| 25 |
+
|
| 26 |
def generate_tokens(response):
|
| 27 |
# for deepseek
|
| 28 |
for token in response:
|
|
|
|
| 101 |
with st.sidebar:
|
| 102 |
st.markdown("# Let's talk climate action!")
|
| 103 |
st.markdown(f"""
|
| 104 |
+
{"β" if st.session_state.submitted else "β"} **Step 1. Complete a form**
|
| 105 |
|
| 106 |
+
{"β" if len(st.session_state.messages) > 0 else "β"} **Step 2. Type in the chat box to start a conversation**
|
| 107 |
|
| 108 |
π± You should ask a climate change related question like:
|
| 109 |
- *What are the most effective actions to reduce my carbon emissions?*
|
| 110 |
- *What's better for the environment: a year of vegetarianism or skipping one transatlantic flight?*
|
| 111 |
- *How do the emissions saved by switching to an EV compare to recycling for a year in terms of trees planted?*
|
| 112 |
|
| 113 |
+
β Do not share any personal information (e.g., name or address).
|
|
|
|
|
|
|
| 114 |
|
| 115 |
+
β οΈ You must respond **at least 5 times** before you will see a *Finish* button. You can continue before submitting, but **you must finish and enter your completion code into the survey to recieve compensation**.
|
| 116 |
|
| 117 |
+
{"β" if st.session_state.inserted > 1 else "β"} **Step 3. Use the *Finish* button to get your completion code**
|
| 118 |
|
| 119 |
+
β οΈ Do not forget to copy & paste your completion code!
|
| 120 |
|
| 121 |
+
βΊ You can always return to this panel by clicking the arrow on the top left.
|
| 122 |
|
| 123 |
+
{"π **All done! Please enter your code in the survye and press *Next*.**" if st.session_state.inserted > 1 else ""}
|
| 124 |
""")
|
| 125 |
if st.session_state.gotit == False or st.session_state.submitted == False:
|
| 126 |
st.session_state.gotit = st.button("Let's start!", key=None, help=None, use_container_width=True)
|
|
|
|
| 174 |
st.session_state.user_data["inserted"] = st.session_state.inserted
|
| 175 |
st.session_state.user_data["submission_time"] = datetime.now()
|
| 176 |
|
| 177 |
+
#with MongoClient(st.secrets["mongo"],server_api=ServerApi('1')) as client:
|
| 178 |
+
# db = client.chat
|
| 179 |
+
# collection = db.app
|
| 180 |
+
# user_data = st.session_state.user_data
|
| 181 |
+
# collection.insert_one(user_data)
|
| 182 |
+
|
| 183 |
+
st.session_state.inserted += 1
|
| 184 |
st.session_state.submitted = True
|
| 185 |
setup_messages()
|
| 186 |
st.rerun()
|
|
|
|
| 193 |
with st.chat_message(message["role"]):
|
| 194 |
st.markdown(message["content"])
|
| 195 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
if len(st.session_state.messages) >= st.session_state.max_messages:
|
| 197 |
st.info(
|
| 198 |
"You have reached the limit of messages for this conversation. Please end and submit the conversation."
|
|
|
|
| 208 |
st.markdown('**Please copy the code and enter it into the survey field below.**')
|
| 209 |
|
| 210 |
elif st.query_params["p"] == "n":
|
| 211 |
+
st.markdown("""**You have not been selected to have a conversation with the chatbot.**
|
| 212 |
+
Please press *Finish* to get your completion code.
|
|
|
|
|
|
|
| 213 |
""")
|
| 214 |
columns = st.columns((1,1,1))
|
| 215 |
with columns[2]:
|
| 216 |
+
if st.button("Finish",use_container_width=True):
|
| 217 |
+
keys = ["inserted", "messages", "convo_start_time"]
|
| 218 |
+
|
| 219 |
+
st.session_state.user_data.update({key: st.session_state[key] for key in keys})
|
| 220 |
+
|
| 221 |
+
st.session_state.user_data["convo_end_time"] = datetime.now()
|
| 222 |
+
|
| 223 |
+
with MongoClient(st.secrets["mongo"],server_api=ServerApi('1')) as client:
|
| 224 |
+
db = client.chat
|
| 225 |
+
collection = db.app
|
| 226 |
+
user_data = st.session_state.user_data
|
| 227 |
+
|
| 228 |
+
collection.insert_one(user_data)
|
| 229 |
+
st.session_state.inserted += 1
|
| 230 |
+
done = True
|
| 231 |
+
|
| 232 |
+
setup_messages()
|
| 233 |
+
st.rerun()
|
| 234 |
|
| 235 |
elif prompt := st.chat_input("Ask a question about climate action..."):
|
| 236 |
|
|
|
|
| 271 |
if len(st.session_state.messages) > 10 or st.session_state.max_messages == len(st.session_state.messages):
|
| 272 |
columns = st.columns((1,1,1))
|
| 273 |
with columns[2]:
|
| 274 |
+
if st.button("Finish",use_container_width=True):
|
| 275 |
+
keys = ["inserted", "messages", "convo_start_time"]
|
| 276 |
+
|
| 277 |
+
st.session_state.user_data.update({key: st.session_state[key] for key in keys})
|
| 278 |
+
|
| 279 |
+
st.session_state.user_data["convo_end_time"] = datetime.now()
|
| 280 |
+
|
| 281 |
+
with MongoClient(st.secrets["mongo"],server_api=ServerApi('1')) as client:
|
| 282 |
+
db = client.chat
|
| 283 |
+
collection = db.app
|
| 284 |
+
user_data = st.session_state.user_data
|
| 285 |
+
|
| 286 |
+
collection.insert_one(user_data)
|
| 287 |
+
st.session_state.inserted += 1
|
| 288 |
+
done = True
|
| 289 |
+
|
| 290 |
+
setup_messages()
|
| 291 |
+
st.rerun()
|
| 292 |
|