Spaces:
bridging
/
Runtime error

YaraKyrychenko commited on
Commit
1cc7f47
·
verified ·
1 Parent(s): 668d9ab

Change to new setup

Browse files
Files changed (1) hide show
  1. app.py +44 -47
app.py CHANGED
@@ -28,22 +28,17 @@ st.markdown(
28
  ### Setting up the session state
29
 
30
  if 'inserted' not in st.session_state:
31
- with open('base.txt', 'r') as file:
32
- st.session_state.base_text = file.read()
33
- with open('bridging.txt', 'r') as file:
34
- st.session_state.bridging_text = file.read()
35
- with open('personalization.txt', 'r') as file:
36
- st.session_state.personalization_text = file.read()
37
- with open('both.txt', 'r') as file:
38
- st.session_state.both_text = file.read()
39
-
40
  # web app state
41
- st.session_state["model"] = "openai/gpt-5.2-chat"
42
  st.session_state.max_messages = 50
43
  st.session_state.messages = []
 
 
 
44
  st.session_state.user_data = {}
 
 
 
45
  st.session_state.user_data["user_id"] = str(random.randint(100000, 999999))
46
- st.session_state.user_data["model"] = st.session_state["model"]
47
  st.session_state.user_data["start_time"] = datetime.now()
48
  st.session_state.inserted = 1
49
 
@@ -61,30 +56,36 @@ if 'id' not in st.query_params:
61
  st.query_params['id'] = st.session_state.user_data["user_id"]
62
 
63
  def setup_messages():
64
- # 1 = base,
65
- # 2 = bridging,
66
- # 3 = personalization,
67
- # 4 = bridging + personalization
68
- # 5 = true control
69
 
70
  if st.query_params["p"] == "1":
71
- st.session_state.system_message = st.session_state.base_text
 
72
  elif st.query_params["p"] == "2":
73
- st.session_state.system_message = st.session_state.bridging_text
 
 
 
74
  elif st.query_params["p"] == "3":
75
- st.session_state.system_message = format_personalization(st.session_state.personalization_text)
 
 
 
76
  elif st.query_params["p"] == "4":
77
- st.session_state.system_message = format_personalization(st.session_state.both_text)
78
- elif st.query_params["p"] == "5":
79
- st.session_state.system_message = st.session_state.base_text
 
80
 
81
- st.session_state.messages = [{ "role": "system", "content": st.session_state.system_message}]
82
  st.session_state.convo_start_time = datetime.now()
83
  st.session_state.user_data["random_pid"] = st.query_params['id']
84
  st.session_state.user_data["condition"] = st.query_params['p']
85
- st.session_state.setup = True
86
 
87
- client = OpenAI(base_url="https://openrouter.ai/api/v1", api_key=st.secrets["OPENROUTER_API_KEY"])
 
88
 
89
  if st.session_state.setup == False:
90
  setup_messages()
@@ -95,23 +96,17 @@ if len(st.session_state.messages) == 1 and st.session_state.inserted < 2:
95
  ### App interface
96
  with st.sidebar:
97
  st.markdown("# Let's talk!")
98
- st.markdown(f"""
99
- {"☑" if st.session_state.submitted else "☐"} **Step 1. Complete a form**
100
-
101
- {"☑" if len(st.session_state.messages) > 0 else "☐"} **Step 2. Type in the chat box to start a conversation**""")
102
 
103
  st.success("Ask, request, or talk to the chatbot about something you consider **politically polarizing** or something that people from different US political parties might disagree about.", icon='🎯')
104
 
105
  st.markdown("🚫 Please avoid greetings and start the conversation with a question or a statement about a politically polarizing topic. **Note: the chatbot's knowledge only goes up to late August 2025.**")
106
 
107
- st.markdown(f"""
108
- {"☑" if st.session_state.inserted > 1 else "☐"} **Step 3. Use the *Submit Interaction* button to get your chatbot word**
109
 
110
  ⚠️ You must respond **at least 5 times** before you will see a *Submit Interaction* button. You can continue before submitting, but **you must Submit Interaction and enter your chatbot word to proceed with the survey**.
111
 
112
  ❗ Do not share any personal information (e.g., name or address). Do not use AI tools to write your responses. If you encounter any technical issues, please let us know. It might sometimes take 30 seconds or more to generate a response, so please be patient.
113
-
114
- {"🎉 **All done! Please enter your WORD in the survey and press *Next*.**" if st.session_state.inserted > 1 else ""}
115
  """)
116
 
117
  for message in st.session_state.messages:
@@ -127,7 +122,7 @@ if len(st.session_state.messages) >= st.session_state.max_messages:
127
  elif st.session_state.inserted > 1:
128
  st.markdown("## Copy your WORD!")
129
  st.markdown('**Your chatbot WORD is:**')
130
- st.markdown(f'## POTATOES')
131
  st.markdown('**Please copy the WORD and enter it into the survey field below.**')
132
 
133
  elif prompt := st.chat_input("Type to ask a question or respond..."):
@@ -153,7 +148,7 @@ elif prompt := st.chat_input("Type to ask a question or respond..."):
153
  with st.chat_message("user"):
154
  st.markdown(prompt)
155
 
156
- if st.query_params["p"] == "5":
157
  rate_limit_message = "Thank you for your question! You have been randomly assigned to a condition without a chatbot. **Please submit your interaction anyway** to get your chatbot word and proceed with the survey. Do not worry, this will not influence your compensation."
158
  st.session_state.messages.append(
159
  {"role": "assistant", "content": rate_limit_message}
@@ -164,20 +159,22 @@ elif prompt := st.chat_input("Type to ask a question or respond..."):
164
  else:
165
  with st.chat_message("assistant"):
166
  try:
167
- stream = client.chat.completions.create(
168
- model=st.session_state["model"],
169
- messages=[
170
- {"role": m["role"], "content": m["content"]}
171
- for m in st.session_state.messages
172
- ],
173
- stream=True
174
- )
175
-
176
- response = st.write_stream(stream)
177
-
 
 
178
  st.session_state.messages.append(
179
  {"role": "assistant", "content": response}
180
- )
181
  except:
182
  rate_limit_message = "An error has occurred or you've reached the maximum conversation length. Please submit the conversation."
183
  st.session_state.messages.append(
 
28
  ### Setting up the session state
29
 
30
  if 'inserted' not in st.session_state:
 
 
 
 
 
 
 
 
 
31
  # web app state
 
32
  st.session_state.max_messages = 50
33
  st.session_state.messages = []
34
+ st.session_state.API = ""
35
+ st.session_state.client = ""
36
+
37
  st.session_state.user_data = {}
38
+ st.session_state.user_data["BASE_URL"] = ""
39
+ st.session_state.user_data["MODEL_PATH"] = ""
40
+ st.session_state.user_data["url_id"] = True
41
  st.session_state.user_data["user_id"] = str(random.randint(100000, 999999))
 
42
  st.session_state.user_data["start_time"] = datetime.now()
43
  st.session_state.inserted = 1
44
 
 
56
  st.query_params['id'] = st.session_state.user_data["user_id"]
57
 
58
  def setup_messages():
59
+ # 1 = true control,
60
+ # 2 = base,
61
+ # 3 = bridging,
62
+ # 4 = GPT
 
63
 
64
  if st.query_params["p"] == "1":
65
+ pass
66
+
67
  elif st.query_params["p"] == "2":
68
+ st.session_state.user_data["BASE_URL"] = "https://openrouter.ai/api/v1"
69
+ st.session_state.user_data["MODEL_PATH"] = "meta-llama/llama-3.3-70b-instruct"
70
+ st.session_state.API = "OPENROUTER_API_KEY"
71
+
72
  elif st.query_params["p"] == "3":
73
+ st.session_state.user_data["BASE_URL"] = "https://tinker.thinkingmachines.dev/services/tinker-prod/oai/api/v1"
74
+ st.session_state.user_data["MODEL_PATH"] = "tinker://808e4f02-e847-54ae-bc75-f14ee885ce5a:train:0/sampler_weights/final_sampler"
75
+ st.session_state.API = "TINKER_API_KEY"
76
+
77
  elif st.query_params["p"] == "4":
78
+ st.session_state.user_data["BASE_URL"] = "https://openrouter.ai/api/v1"
79
+ st.session_state.user_data["MODEL_PATH"] = "openai/gpt-5.4"
80
+ st.session_state.API = "OPENROUTER_API_KEY"
81
+
82
 
 
83
  st.session_state.convo_start_time = datetime.now()
84
  st.session_state.user_data["random_pid"] = st.query_params['id']
85
  st.session_state.user_data["condition"] = st.query_params['p']
 
86
 
87
+ st.session_state.client = OpenAI(base_url=st.session_state.user_data["BASE_URL"], api_key=st.secrets[st.session_state.API])
88
+ st.session_state.setup = True
89
 
90
  if st.session_state.setup == False:
91
  setup_messages()
 
96
  ### App interface
97
  with st.sidebar:
98
  st.markdown("# Let's talk!")
99
+ st.markdown(f"""# **Step 1. Type in the chat box to start a conversation**""")
 
 
 
100
 
101
  st.success("Ask, request, or talk to the chatbot about something you consider **politically polarizing** or something that people from different US political parties might disagree about.", icon='🎯')
102
 
103
  st.markdown("🚫 Please avoid greetings and start the conversation with a question or a statement about a politically polarizing topic. **Note: the chatbot's knowledge only goes up to late August 2025.**")
104
 
105
+ st.markdown(f"""# **Step 2. Use the *Submit Interaction* button to get your chatbot word**
 
106
 
107
  ⚠️ You must respond **at least 5 times** before you will see a *Submit Interaction* button. You can continue before submitting, but **you must Submit Interaction and enter your chatbot word to proceed with the survey**.
108
 
109
  ❗ Do not share any personal information (e.g., name or address). Do not use AI tools to write your responses. If you encounter any technical issues, please let us know. It might sometimes take 30 seconds or more to generate a response, so please be patient.
 
 
110
  """)
111
 
112
  for message in st.session_state.messages:
 
122
  elif st.session_state.inserted > 1:
123
  st.markdown("## Copy your WORD!")
124
  st.markdown('**Your chatbot WORD is:**')
125
+ st.markdown(f'## TOMATOES')
126
  st.markdown('**Please copy the WORD and enter it into the survey field below.**')
127
 
128
  elif prompt := st.chat_input("Type to ask a question or respond..."):
 
148
  with st.chat_message("user"):
149
  st.markdown(prompt)
150
 
151
+ if st.query_params["p"] == "1":
152
  rate_limit_message = "Thank you for your question! You have been randomly assigned to a condition without a chatbot. **Please submit your interaction anyway** to get your chatbot word and proceed with the survey. Do not worry, this will not influence your compensation."
153
  st.session_state.messages.append(
154
  {"role": "assistant", "content": rate_limit_message}
 
159
  else:
160
  with st.chat_message("assistant"):
161
  try:
162
+ with st.spinner("Typing..."):
163
+ completion = st.session_state.client.chat.completions.create(
164
+ model=st.session_state.user_data["MODEL_PATH"],
165
+ messages=[
166
+ {"role": m["role"], "content": m["content"]}
167
+ for m in st.session_state.messages
168
+ ],
169
+ stream=False,
170
+ )
171
+
172
+ response = completion.choices[0].message.content or ""
173
+ st.markdown(response)
174
+
175
  st.session_state.messages.append(
176
  {"role": "assistant", "content": response}
177
+
178
  except:
179
  rate_limit_message = "An error has occurred or you've reached the maximum conversation length. Please submit the conversation."
180
  st.session_state.messages.append(