Spaces:
Sleeping
Sleeping
Yara Kyrychenko
commited on
Commit
Β·
a6e50c0
1
Parent(s):
3472bd1
upd
Browse files- .gitignore +2 -1
- README.md +2 -2
- app.py +48 -53
.gitignore
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
-
*.DS_Store
|
|
|
|
|
|
| 1 |
+
*.DS_Store
|
| 2 |
+
.streamlit/secrets.toml
|
README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
emoji: π
|
| 4 |
colorFrom: red
|
| 5 |
colorTo: green
|
|
@@ -11,6 +11,6 @@ license: mit
|
|
| 11 |
---
|
| 12 |
|
| 13 |
|
| 14 |
-
#
|
| 15 |
|
| 16 |
Let's talk climate action!
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Climate Action Chat
|
| 3 |
emoji: π
|
| 4 |
colorFrom: red
|
| 5 |
colorTo: green
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
|
| 14 |
+
# Climate Action Chat
|
| 15 |
|
| 16 |
Let's talk climate action!
|
app.py
CHANGED
|
@@ -1,14 +1,15 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import streamlit.components.v1 as components
|
| 3 |
from openai import OpenAI
|
| 4 |
-
|
| 5 |
from pymongo.mongo_client import MongoClient
|
| 6 |
from pymongo.server_api import ServerApi
|
| 7 |
from datetime import datetime
|
| 8 |
import time
|
|
|
|
| 9 |
|
| 10 |
st.set_page_config(
|
| 11 |
-
page_title="
|
| 12 |
page_icon="π",
|
| 13 |
initial_sidebar_state="expanded",
|
| 14 |
layout="wide"
|
|
@@ -54,7 +55,7 @@ if 'inserted' not in st.session_state:
|
|
| 54 |
st.session_state.gotit = False
|
| 55 |
st.session_state.submitted = False
|
| 56 |
st.session_state.inserted = 0
|
| 57 |
-
st.session_state["model"] = "deepseek/deepseek-chat"
|
| 58 |
st.session_state.max_messages = 50
|
| 59 |
st.session_state.messages = []
|
| 60 |
st.session_state.user_data = {}
|
|
@@ -70,6 +71,8 @@ if 'inserted' not in st.session_state:
|
|
| 70 |
for field in st.session_state.fields:
|
| 71 |
st.session_state[field] = ''
|
| 72 |
|
|
|
|
|
|
|
| 73 |
st.session_state.recycling = 0
|
| 74 |
|
| 75 |
st.session_state.start_time = datetime.now()
|
|
@@ -81,7 +84,7 @@ if 'p' not in st.query_params:
|
|
| 81 |
def setup_messages():
|
| 82 |
# t = personalization, k = knowledge, f = formatting, n = no chat
|
| 83 |
|
| 84 |
-
if st.query_params["p"] == "f" or st.query_params["p"] == "n"
|
| 85 |
st.session_state.system_message = st.session_state.base_text
|
| 86 |
elif st.query_params["p"] == "k":
|
| 87 |
st.session_state.system_message = st.session_state.knowledge_text
|
|
@@ -93,7 +96,6 @@ def setup_messages():
|
|
| 93 |
|
| 94 |
if st.session_state.model.startswith("deepseek"):
|
| 95 |
client = OpenAI( base_url="https://openrouter.ai/api/v1", api_key=st.secrets["OPENROUTER_API_KEY"])
|
| 96 |
-
#client = Together(api_key=st.secrets["TOGETHER_API_KEY"])
|
| 97 |
else:
|
| 98 |
client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
|
| 99 |
|
|
@@ -118,6 +120,8 @@ with st.sidebar:
|
|
| 118 |
|
| 119 |
{"β" if st.session_state.inserted > 1 else "β"} **Step 3. Use the *End Conversation* button to submit your response.**
|
| 120 |
|
|
|
|
|
|
|
| 121 |
βΊ *You can always return to this panel by clicking the arrow on the top left.*
|
| 122 |
|
| 123 |
{"π **All done! Please press *Next* in the survey.**" if st.session_state.inserted > 1 else ""}
|
|
@@ -125,16 +129,9 @@ with st.sidebar:
|
|
| 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)
|
| 127 |
|
| 128 |
-
st.markdown("""
|
| 129 |
-
<script async src="https://cse.google.com/cse.js?cx=c6f2659061dc54b6e">
|
| 130 |
-
</script>
|
| 131 |
-
<div class="gcse-search"></div>""", unsafe_allow_html=True)
|
| 132 |
-
|
| 133 |
-
|
| 134 |
@st.dialog('Form', width="large")
|
| 135 |
def form():
|
| 136 |
-
st.markdown("
|
| 137 |
-
st.session_state.user_id = st.text_input(label="Enter your Prolific ID", value=st.session_state.user_id)
|
| 138 |
st.session_state.age = st.text_input("How old are you in years?")
|
| 139 |
st.session_state.gender = st.radio("Do you describe yourself as a man, a woman, or in some other way?",
|
| 140 |
['','Man', 'Woman', 'Other'])
|
|
@@ -193,7 +190,7 @@ def form():
|
|
| 193 |
|
| 194 |
if st.session_state.gotit and st.session_state.submitted == False:
|
| 195 |
form()
|
| 196 |
-
|
| 197 |
for message in st.session_state.messages:
|
| 198 |
if message['role']!='system':
|
| 199 |
with st.chat_message(message["role"]):
|
|
@@ -201,56 +198,54 @@ for message in st.session_state.messages:
|
|
| 201 |
|
| 202 |
@st.dialog('Submit conversation', width="large")
|
| 203 |
def submit():
|
| 204 |
-
|
| 205 |
-
if
|
| 206 |
-
st.
|
| 207 |
-
st.
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
|
|
|
|
|
|
| 223 |
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
st.success('**Your conversation has been submitted! Please proceed with the survey.**', icon="β
")
|
| 234 |
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
st.rerun()
|
| 238 |
|
| 239 |
if len(st.session_state.messages) >= st.session_state.max_messages:
|
| 240 |
st.info(
|
| 241 |
-
"You have reached the limit of messages for this conversation. Please end and submit the
|
| 242 |
)
|
| 243 |
|
| 244 |
elif st.session_state.submitted == False:
|
| 245 |
pass
|
| 246 |
|
| 247 |
-
elif st.
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
if search != "":
|
| 253 |
-
components.iframe(f"https://www.google.com/search?igu=1&q={search}",height=1000,scrolling=True)
|
| 254 |
|
| 255 |
elif st.query_params["p"] == "n":
|
| 256 |
st.markdown("""
|
|
|
|
| 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(
|
| 12 |
+
page_title="Climate Action",
|
| 13 |
page_icon="π",
|
| 14 |
initial_sidebar_state="expanded",
|
| 15 |
layout="wide"
|
|
|
|
| 55 |
st.session_state.gotit = False
|
| 56 |
st.session_state.submitted = False
|
| 57 |
st.session_state.inserted = 0
|
| 58 |
+
st.session_state["model"] = "deepseek/deepseek-chat"
|
| 59 |
st.session_state.max_messages = 50
|
| 60 |
st.session_state.messages = []
|
| 61 |
st.session_state.user_data = {}
|
|
|
|
| 71 |
for field in st.session_state.fields:
|
| 72 |
st.session_state[field] = ''
|
| 73 |
|
| 74 |
+
st.session_state.user_id = str(random.randint(100000, 999999))
|
| 75 |
+
|
| 76 |
st.session_state.recycling = 0
|
| 77 |
|
| 78 |
st.session_state.start_time = datetime.now()
|
|
|
|
| 84 |
def setup_messages():
|
| 85 |
# t = personalization, k = knowledge, f = formatting, n = no chat
|
| 86 |
|
| 87 |
+
if st.query_params["p"] == "f" or st.query_params["p"] == "n":
|
| 88 |
st.session_state.system_message = st.session_state.base_text
|
| 89 |
elif st.query_params["p"] == "k":
|
| 90 |
st.session_state.system_message = st.session_state.knowledge_text
|
|
|
|
| 96 |
|
| 97 |
if st.session_state.model.startswith("deepseek"):
|
| 98 |
client = OpenAI( base_url="https://openrouter.ai/api/v1", api_key=st.secrets["OPENROUTER_API_KEY"])
|
|
|
|
| 99 |
else:
|
| 100 |
client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
|
| 101 |
|
|
|
|
| 120 |
|
| 121 |
{"β" if st.session_state.inserted > 1 else "β"} **Step 3. Use the *End Conversation* button to submit your response.**
|
| 122 |
|
| 123 |
+
**Do not forget to copy & paste your completion code!**
|
| 124 |
+
|
| 125 |
βΊ *You can always return to this panel by clicking the arrow on the top left.*
|
| 126 |
|
| 127 |
{"π **All done! Please press *Next* in the survey.**" if st.session_state.inserted > 1 else ""}
|
|
|
|
| 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)
|
| 131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
@st.dialog('Form', width="large")
|
| 133 |
def form():
|
| 134 |
+
st.markdown("**Please answer every question to proceed.**")
|
|
|
|
| 135 |
st.session_state.age = st.text_input("How old are you in years?")
|
| 136 |
st.session_state.gender = st.radio("Do you describe yourself as a man, a woman, or in some other way?",
|
| 137 |
['','Man', 'Woman', 'Other'])
|
|
|
|
| 190 |
|
| 191 |
if st.session_state.gotit and st.session_state.submitted == False:
|
| 192 |
form()
|
| 193 |
+
|
| 194 |
for message in st.session_state.messages:
|
| 195 |
if message['role']!='system':
|
| 196 |
with st.chat_message(message["role"]):
|
|
|
|
| 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."
|
| 239 |
)
|
| 240 |
|
| 241 |
elif st.session_state.submitted == False:
|
| 242 |
pass
|
| 243 |
|
| 244 |
+
elif st.session_state.inserted > 1:
|
| 245 |
+
st.markdown("## Copy your code!")
|
| 246 |
+
st.markdown('**Your completion code is:**')
|
| 247 |
+
st.markdown(f'## {st.session_state.user_id}')
|
| 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("""
|