Spaces:
Sleeping
Sleeping
Commit
Β·
6b333e0
1
Parent(s):
57c2a16
try catch
Browse files
app.py
CHANGED
@@ -21,19 +21,25 @@ def remove_citation(text: str) -> str:
|
|
21 |
pattern = r"γ\d+β \w+γ"
|
22 |
return re.sub(pattern, "π", text)
|
23 |
|
24 |
-
# Initialize session state
|
25 |
if "messages" not in st.session_state:
|
26 |
st.session_state["messages"] = []
|
27 |
if "thread_id" not in st.session_state:
|
28 |
st.session_state["thread_id"] = None
|
|
|
|
|
|
|
29 |
|
30 |
st.title("Solution Specifier A")
|
31 |
|
32 |
def predict(user_input: str) -> str:
|
33 |
"""
|
34 |
This function calls our OpenAIAssistantRunnable to get a response.
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
37 |
"""
|
38 |
try:
|
39 |
if st.session_state["thread_id"] is None:
|
@@ -41,19 +47,19 @@ def predict(user_input: str) -> str:
|
|
41 |
response = extractor_llm.invoke({"content": user_input})
|
42 |
st.session_state["thread_id"] = response.thread_id
|
43 |
else:
|
44 |
-
# Continue
|
45 |
response = extractor_llm.invoke(
|
46 |
{"content": user_input, "thread_id": st.session_state["thread_id"]}
|
47 |
)
|
|
|
48 |
output = response.return_values["output"]
|
49 |
return remove_citation(output)
|
50 |
|
51 |
except openai.error.BadRequestError as e:
|
52 |
-
# If
|
53 |
-
|
54 |
-
if "while a run" in err_msg:
|
55 |
st.session_state["thread_id"] = None
|
56 |
-
#
|
57 |
try:
|
58 |
response = extractor_llm.invoke({"content": user_input})
|
59 |
st.session_state["thread_id"] = response.thread_id
|
@@ -63,11 +69,10 @@ def predict(user_input: str) -> str:
|
|
63 |
st.error(f"Error after resetting thread: {e2}")
|
64 |
return ""
|
65 |
else:
|
66 |
-
# Some other
|
67 |
-
st.error(
|
68 |
return ""
|
69 |
except Exception as e:
|
70 |
-
# Catch-all for any other error
|
71 |
st.error(str(e))
|
72 |
return ""
|
73 |
|
@@ -80,11 +85,16 @@ for msg in st.session_state["messages"]:
|
|
80 |
with st.chat_message("assistant"):
|
81 |
st.write(msg["content"])
|
82 |
|
83 |
-
#
|
84 |
user_input = st.chat_input("Type your message here...")
|
85 |
|
86 |
-
#
|
87 |
-
|
|
|
|
|
|
|
|
|
|
|
88 |
# Add the user message to session state
|
89 |
st.session_state["messages"].append({"role": "user", "content": user_input})
|
90 |
|
@@ -92,12 +102,15 @@ if user_input:
|
|
92 |
with st.chat_message("user"):
|
93 |
st.write(user_input)
|
94 |
|
95 |
-
# Get
|
96 |
response_text = predict(user_input)
|
97 |
|
98 |
-
# Add
|
99 |
st.session_state["messages"].append({"role": "assistant", "content": response_text})
|
100 |
|
101 |
-
# Display
|
102 |
with st.chat_message("assistant"):
|
103 |
-
st.write(response_text)
|
|
|
|
|
|
|
|
21 |
pattern = r"γ\d+β \w+γ"
|
22 |
return re.sub(pattern, "π", text)
|
23 |
|
24 |
+
# Initialize session state
|
25 |
if "messages" not in st.session_state:
|
26 |
st.session_state["messages"] = []
|
27 |
if "thread_id" not in st.session_state:
|
28 |
st.session_state["thread_id"] = None
|
29 |
+
# A flag to indicate if a request is in progress
|
30 |
+
if "is_in_request" not in st.session_state:
|
31 |
+
st.session_state["is_in_request"] = False
|
32 |
|
33 |
st.title("Solution Specifier A")
|
34 |
|
35 |
def predict(user_input: str) -> str:
|
36 |
"""
|
37 |
This function calls our OpenAIAssistantRunnable to get a response.
|
38 |
+
If st.session_state["thread_id"] is None, we start a new thread.
|
39 |
+
Otherwise, we continue the existing thread.
|
40 |
+
|
41 |
+
If a concurrency error occurs ("Can't add messages to thread..."), we reset
|
42 |
+
the thread_id and try again once on a fresh thread.
|
43 |
"""
|
44 |
try:
|
45 |
if st.session_state["thread_id"] is None:
|
|
|
47 |
response = extractor_llm.invoke({"content": user_input})
|
48 |
st.session_state["thread_id"] = response.thread_id
|
49 |
else:
|
50 |
+
# Continue existing thread
|
51 |
response = extractor_llm.invoke(
|
52 |
{"content": user_input, "thread_id": st.session_state["thread_id"]}
|
53 |
)
|
54 |
+
|
55 |
output = response.return_values["output"]
|
56 |
return remove_citation(output)
|
57 |
|
58 |
except openai.error.BadRequestError as e:
|
59 |
+
# If we get the specific concurrency error, reset thread and try once more
|
60 |
+
if "while a run" in str(e):
|
|
|
61 |
st.session_state["thread_id"] = None
|
62 |
+
# Now create a new thread for the same user input
|
63 |
try:
|
64 |
response = extractor_llm.invoke({"content": user_input})
|
65 |
st.session_state["thread_id"] = response.thread_id
|
|
|
69 |
st.error(f"Error after resetting thread: {e2}")
|
70 |
return ""
|
71 |
else:
|
72 |
+
# Some other 400 error
|
73 |
+
st.error(str(e))
|
74 |
return ""
|
75 |
except Exception as e:
|
|
|
76 |
st.error(str(e))
|
77 |
return ""
|
78 |
|
|
|
85 |
with st.chat_message("assistant"):
|
86 |
st.write(msg["content"])
|
87 |
|
88 |
+
# Chat input at the bottom of the page
|
89 |
user_input = st.chat_input("Type your message here...")
|
90 |
|
91 |
+
# Process the user input only if:
|
92 |
+
# 1) There is some text, and
|
93 |
+
# 2) We are not already handling a request (is_in_request == False)
|
94 |
+
if user_input and not st.session_state["is_in_request"]:
|
95 |
+
# Lock to prevent duplicate requests
|
96 |
+
st.session_state["is_in_request"] = True
|
97 |
+
|
98 |
# Add the user message to session state
|
99 |
st.session_state["messages"].append({"role": "user", "content": user_input})
|
100 |
|
|
|
102 |
with st.chat_message("user"):
|
103 |
st.write(user_input)
|
104 |
|
105 |
+
# Get assistant response
|
106 |
response_text = predict(user_input)
|
107 |
|
108 |
+
# Add assistant response to session state
|
109 |
st.session_state["messages"].append({"role": "assistant", "content": response_text})
|
110 |
|
111 |
+
# Display assistant response
|
112 |
with st.chat_message("assistant"):
|
113 |
+
st.write(response_text)
|
114 |
+
|
115 |
+
# Release the lock
|
116 |
+
st.session_state["is_in_request"] = False
|