Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -113,44 +113,43 @@ def page1():
|
|
| 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 |
-
|
| 120 |
# Add message to the thread
|
| 121 |
client.beta.threads.messages.create(
|
| 122 |
thread_id=st.session_state.thread.id,
|
| 123 |
role="user",
|
| 124 |
content=question
|
| 125 |
)
|
| 126 |
-
|
| 127 |
# Create a new run to process the messages in the thread
|
| 128 |
st.session_state.run = client.beta.threads.runs.create(
|
| 129 |
thread_id=st.session_state.thread.id,
|
| 130 |
assistant_id=st.session_state.assistant.id,
|
| 131 |
)
|
| 132 |
-
|
| 133 |
-
# Wait for the run to complete
|
| 134 |
while not hasattr(st.session_state.run, 'status') or st.session_state.run.status not in ["completed", "failed"]:
|
| 135 |
time.sleep(1)
|
| 136 |
st.session_state.run = client.beta.threads.runs.retrieve(
|
| 137 |
thread_id=st.session_state.thread.id,
|
| 138 |
run_id=st.session_state.run.id,
|
| 139 |
)
|
| 140 |
-
|
| 141 |
if st.session_state.run.status == "completed":
|
| 142 |
# Retrieve the list of messages
|
| 143 |
st.session_state.messages = client.beta.threads.messages.list(
|
| 144 |
thread_id=st.session_state.thread.id
|
| 145 |
)
|
| 146 |
-
|
| 147 |
-
#
|
| 148 |
last_assistant_message = next((message for message in reversed(st.session_state.messages.data) if message.role == "assistant"), None)
|
| 149 |
if last_assistant_message:
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
|
| 155 |
# Example query buttons
|
| 156 |
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 and chat history
|
| 117 |
st.session_state.run = None
|
| 118 |
+
st.session_state.chat_history = [("User", question)] # Start with the current user question
|
| 119 |
+
|
| 120 |
# Add message to the thread
|
| 121 |
client.beta.threads.messages.create(
|
| 122 |
thread_id=st.session_state.thread.id,
|
| 123 |
role="user",
|
| 124 |
content=question
|
| 125 |
)
|
| 126 |
+
|
| 127 |
# Create a new run to process the messages in the thread
|
| 128 |
st.session_state.run = client.beta.threads.runs.create(
|
| 129 |
thread_id=st.session_state.thread.id,
|
| 130 |
assistant_id=st.session_state.assistant.id,
|
| 131 |
)
|
| 132 |
+
|
| 133 |
+
# Wait for the run to complete
|
| 134 |
while not hasattr(st.session_state.run, 'status') or st.session_state.run.status not in ["completed", "failed"]:
|
| 135 |
time.sleep(1)
|
| 136 |
st.session_state.run = client.beta.threads.runs.retrieve(
|
| 137 |
thread_id=st.session_state.thread.id,
|
| 138 |
run_id=st.session_state.run.id,
|
| 139 |
)
|
| 140 |
+
|
| 141 |
if st.session_state.run.status == "completed":
|
| 142 |
# Retrieve the list of messages
|
| 143 |
st.session_state.messages = client.beta.threads.messages.list(
|
| 144 |
thread_id=st.session_state.thread.id
|
| 145 |
)
|
| 146 |
+
|
| 147 |
+
# Get the latest message from the assistant
|
| 148 |
last_assistant_message = next((message for message in reversed(st.session_state.messages.data) if message.role == "assistant"), None)
|
| 149 |
if last_assistant_message:
|
| 150 |
+
# Update chat history with the assistant's response
|
| 151 |
+
st.session_state.chat_history.append(("Assistant", last_assistant_message.content[0].text.value))
|
| 152 |
+
|
|
|
|
| 153 |
|
| 154 |
# Example query buttons
|
| 155 |
col3, col4 = st.columns(2) # Create two columns for buttons
|