Anne31415 commited on
Commit
b2e5352
·
1 Parent(s): 2d30d9c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -8
app.py CHANGED
@@ -113,23 +113,42 @@ def page1():
113
 
114
  # Function to process the question
115
  def process_question(question):
 
 
 
116
  # Add message to the thread
117
- st.session_state.messages = client.beta.threads.messages.create(
118
  thread_id=st.session_state.thread.id,
119
  role="user",
120
  content=question
121
  )
122
-
123
- # Do a run to process the messages in the thread
124
  st.session_state.run = client.beta.threads.runs.create(
125
  thread_id=st.session_state.thread.id,
126
  assistant_id=st.session_state.assistant.id,
127
  )
128
-
129
- # Wait and rerun logic
130
- if st.session_state.retry_error < 3:
131
- time.sleep(1) # Wait 1 second before checking run status
132
- st.rerun()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
 
134
  # Example query buttons
135
  col3, col4 = st.columns(2) # Create two columns for buttons
 
113
 
114
  # Function to process the question
115
  def process_question(question):
116
+ # Clear the previous run status
117
+ st.session_state.run = None
118
+
119
  # Add message to the thread
120
+ client.beta.threads.messages.create(
121
  thread_id=st.session_state.thread.id,
122
  role="user",
123
  content=question
124
  )
125
+
126
+ # Create a new run to process the messages in the thread
127
  st.session_state.run = client.beta.threads.runs.create(
128
  thread_id=st.session_state.thread.id,
129
  assistant_id=st.session_state.assistant.id,
130
  )
131
+
132
+ # Wait for the run to complete and then refresh the app
133
+ while not hasattr(st.session_state.run, 'status') or st.session_state.run.status not in ["completed", "failed"]:
134
+ time.sleep(1)
135
+ st.session_state.run = client.beta.threads.runs.retrieve(
136
+ thread_id=st.session_state.thread.id,
137
+ run_id=st.session_state.run.id,
138
+ )
139
+
140
+ if st.session_state.run.status == "completed":
141
+ # Retrieve the list of messages
142
+ st.session_state.messages = client.beta.threads.messages.list(
143
+ thread_id=st.session_state.thread.id
144
+ )
145
+
146
+ # Display the latest message from the assistant
147
+ last_assistant_message = next((message for message in reversed(st.session_state.messages.data) if message.role == "assistant"), None)
148
+ if last_assistant_message:
149
+ # Add assistant's response to chat history
150
+ st.session_state.chat_history.append(("Assistant", last_assistant_message.content[0].text.value))
151
+
152
 
153
  # Example query buttons
154
  col3, col4 = st.columns(2) # Create two columns for buttons