Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -984,46 +984,88 @@ def warmup_agent():
|
|
| 984 |
except Exception as e:
|
| 985 |
logger.error(f"Warmup failed: {e}")
|
| 986 |
|
| 987 |
-
def
|
| 988 |
-
"""
|
| 989 |
if not message.strip():
|
| 990 |
-
return
|
| 991 |
|
| 992 |
-
# Add
|
| 993 |
-
|
| 994 |
|
| 995 |
-
#
|
| 996 |
-
|
| 997 |
|
| 998 |
-
|
| 999 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1000 |
|
| 1001 |
-
|
| 1002 |
-
|
| 1003 |
-
history.append({"role": "assistant", "content": thinking_html})
|
| 1004 |
-
yield history, ""
|
| 1005 |
|
| 1006 |
try:
|
| 1007 |
-
#
|
| 1008 |
full_response = ""
|
| 1009 |
-
|
|
|
|
| 1010 |
full_response = chunk
|
| 1011 |
-
|
| 1012 |
-
|
| 1013 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1014 |
|
| 1015 |
except Exception as e:
|
| 1016 |
-
logger.error(f"Error in
|
| 1017 |
-
|
| 1018 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1019 |
|
| 1020 |
-
def
|
| 1021 |
-
"""
|
| 1022 |
-
return [],
|
| 1023 |
|
| 1024 |
# --- UI: Interface Creation ---
|
| 1025 |
def create_interface():
|
| 1026 |
-
"""Creates and configures the complete Gradio interface."""
|
| 1027 |
start_create_interface_time = time.perf_counter()
|
| 1028 |
current_time = datetime.now()
|
| 1029 |
|
|
@@ -1046,7 +1088,9 @@ def create_interface():
|
|
| 1046 |
# Add head content
|
| 1047 |
gr.HTML(html_head_content)
|
| 1048 |
gr.HTML(force_light_mode)
|
| 1049 |
-
|
|
|
|
|
|
|
| 1050 |
|
| 1051 |
with gr.Column(elem_classes=["main-container"]):
|
| 1052 |
# Title Section
|
|
@@ -1064,9 +1108,10 @@ def create_interface():
|
|
| 1064 |
elem_id="main-chatbot",
|
| 1065 |
scale=1,
|
| 1066 |
height="70vh",
|
|
|
|
| 1067 |
latex_delimiters=[
|
| 1068 |
-
{"left": "$$", "right": "$$", "display": True},
|
| 1069 |
-
{"left": "$", "right": "$", "display": False},
|
| 1070 |
]
|
| 1071 |
)
|
| 1072 |
|
|
@@ -1085,12 +1130,50 @@ def create_interface():
|
|
| 1085 |
send = gr.Button("Send", elem_classes=["send-button"], size="sm")
|
| 1086 |
clear = gr.Button("Clear", elem_classes=["clear-button"], size="sm")
|
| 1087 |
|
| 1088 |
-
#
|
| 1089 |
-
msg.submit(
|
| 1090 |
-
|
| 1091 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1092 |
|
| 1093 |
-
# Apply CSS
|
| 1094 |
gr.HTML(f'<style>{custom_css}</style>')
|
| 1095 |
|
| 1096 |
end_create_interface_time = time.perf_counter()
|
|
|
|
| 984 |
except Exception as e:
|
| 985 |
logger.error(f"Warmup failed: {e}")
|
| 986 |
|
| 987 |
+
def add_user_message(message, chat_history, conversation_state):
|
| 988 |
+
"""Add user message to state and display immediately"""
|
| 989 |
if not message.strip():
|
| 990 |
+
return "", chat_history, conversation_state
|
| 991 |
|
| 992 |
+
# Add to conversation state
|
| 993 |
+
conversation_state.append({"role": "user", "content": message})
|
| 994 |
|
| 995 |
+
# Update chat display
|
| 996 |
+
chat_history.append({"role": "user", "content": message})
|
| 997 |
|
| 998 |
+
return "", chat_history, conversation_state
|
| 999 |
+
|
| 1000 |
+
def add_thinking_indicator(chat_history, conversation_state):
|
| 1001 |
+
"""Add thinking indicator to chat display"""
|
| 1002 |
+
if not conversation_state:
|
| 1003 |
+
return chat_history, conversation_state
|
| 1004 |
+
|
| 1005 |
+
# Get the last user message for context-aware thinking indicator
|
| 1006 |
+
last_message = conversation_state[-1]["content"] if conversation_state else ""
|
| 1007 |
+
thinking_html = get_thinking_indicator_by_query(last_message)
|
| 1008 |
+
|
| 1009 |
+
# Add thinking indicator to chat display (not permanent state)
|
| 1010 |
+
chat_history.append({"role": "assistant", "content": thinking_html})
|
| 1011 |
+
|
| 1012 |
+
return chat_history, conversation_state
|
| 1013 |
+
|
| 1014 |
+
def generate_response(chat_history, conversation_state):
|
| 1015 |
+
"""Generate streaming response from the agent"""
|
| 1016 |
+
if not conversation_state:
|
| 1017 |
+
return chat_history, conversation_state
|
| 1018 |
+
|
| 1019 |
+
# Get the last user message
|
| 1020 |
+
last_user_message = ""
|
| 1021 |
+
for msg in reversed(conversation_state):
|
| 1022 |
+
if msg["role"] == "user":
|
| 1023 |
+
last_user_message = msg["content"]
|
| 1024 |
+
break
|
| 1025 |
|
| 1026 |
+
if not last_user_message:
|
| 1027 |
+
return chat_history, conversation_state
|
|
|
|
|
|
|
| 1028 |
|
| 1029 |
try:
|
| 1030 |
+
# Stream the response
|
| 1031 |
full_response = ""
|
| 1032 |
+
|
| 1033 |
+
for chunk in agent.stream_query(last_user_message):
|
| 1034 |
full_response = chunk
|
| 1035 |
+
|
| 1036 |
+
# Update the last message in chat display (replace thinking indicator)
|
| 1037 |
+
if chat_history and chat_history[-1]["role"] == "assistant":
|
| 1038 |
+
chat_history[-1]["content"] = full_response
|
| 1039 |
+
else:
|
| 1040 |
+
chat_history.append({"role": "assistant", "content": full_response})
|
| 1041 |
+
|
| 1042 |
+
yield chat_history, conversation_state
|
| 1043 |
+
|
| 1044 |
+
# Add final response to permanent conversation state
|
| 1045 |
+
conversation_state.append({"role": "assistant", "content": full_response})
|
| 1046 |
+
yield chat_history, conversation_state
|
| 1047 |
|
| 1048 |
except Exception as e:
|
| 1049 |
+
logger.error(f"Error in generate_response: {e}")
|
| 1050 |
+
error_msg = f"I encountered an error: {str(e)}"
|
| 1051 |
+
|
| 1052 |
+
# Update display
|
| 1053 |
+
if chat_history and chat_history[-1]["role"] == "assistant":
|
| 1054 |
+
chat_history[-1]["content"] = error_msg
|
| 1055 |
+
else:
|
| 1056 |
+
chat_history.append({"role": "assistant", "content": error_msg})
|
| 1057 |
+
|
| 1058 |
+
# Add to permanent state
|
| 1059 |
+
conversation_state.append({"role": "assistant", "content": error_msg})
|
| 1060 |
+
yield chat_history, conversation_state
|
| 1061 |
|
| 1062 |
+
def reset_conversation():
|
| 1063 |
+
"""Reset both chat display and conversation state"""
|
| 1064 |
+
return [], []
|
| 1065 |
|
| 1066 |
# --- UI: Interface Creation ---
|
| 1067 |
def create_interface():
|
| 1068 |
+
"""Creates and configures the complete Gradio interface with proper state management."""
|
| 1069 |
start_create_interface_time = time.perf_counter()
|
| 1070 |
current_time = datetime.now()
|
| 1071 |
|
|
|
|
| 1088 |
# Add head content
|
| 1089 |
gr.HTML(html_head_content)
|
| 1090 |
gr.HTML(force_light_mode)
|
| 1091 |
+
|
| 1092 |
+
# State management - this is the key addition
|
| 1093 |
+
conversation_state = gr.State([]) # Persistent conversation memory
|
| 1094 |
|
| 1095 |
with gr.Column(elem_classes=["main-container"]):
|
| 1096 |
# Title Section
|
|
|
|
| 1108 |
elem_id="main-chatbot",
|
| 1109 |
scale=1,
|
| 1110 |
height="70vh",
|
| 1111 |
+
value=[], # Initialize with empty list
|
| 1112 |
latex_delimiters=[
|
| 1113 |
+
{"left": "$$", "right": "$$", "display": True},
|
| 1114 |
+
{"left": "$", "right": "$", "display": False},
|
| 1115 |
]
|
| 1116 |
)
|
| 1117 |
|
|
|
|
| 1130 |
send = gr.Button("Send", elem_classes=["send-button"], size="sm")
|
| 1131 |
clear = gr.Button("Clear", elem_classes=["clear-button"], size="sm")
|
| 1132 |
|
| 1133 |
+
# event chaining with state management
|
| 1134 |
+
submit_event = msg.submit(
|
| 1135 |
+
add_user_message,
|
| 1136 |
+
inputs=[msg, chatbot, conversation_state],
|
| 1137 |
+
outputs=[msg, chatbot, conversation_state],
|
| 1138 |
+
show_progress="hidden"
|
| 1139 |
+
).then(
|
| 1140 |
+
add_thinking_indicator,
|
| 1141 |
+
inputs=[chatbot, conversation_state],
|
| 1142 |
+
outputs=[chatbot, conversation_state],
|
| 1143 |
+
show_progress="hidden"
|
| 1144 |
+
).then(
|
| 1145 |
+
generate_response,
|
| 1146 |
+
inputs=[chatbot, conversation_state],
|
| 1147 |
+
outputs=[chatbot, conversation_state],
|
| 1148 |
+
show_progress="hidden"
|
| 1149 |
+
)
|
| 1150 |
+
|
| 1151 |
+
send_event = send.click(
|
| 1152 |
+
add_user_message,
|
| 1153 |
+
inputs=[msg, chatbot, conversation_state],
|
| 1154 |
+
outputs=[msg, chatbot, conversation_state],
|
| 1155 |
+
show_progress="hidden"
|
| 1156 |
+
).then(
|
| 1157 |
+
add_thinking_indicator,
|
| 1158 |
+
inputs=[chatbot, conversation_state],
|
| 1159 |
+
outputs=[chatbot, conversation_state],
|
| 1160 |
+
show_progress="hidden"
|
| 1161 |
+
).then(
|
| 1162 |
+
generate_response,
|
| 1163 |
+
inputs=[chatbot, conversation_state],
|
| 1164 |
+
outputs=[chatbot, conversation_state],
|
| 1165 |
+
show_progress="hidden"
|
| 1166 |
+
)
|
| 1167 |
+
|
| 1168 |
+
# Clear button
|
| 1169 |
+
clear.click(
|
| 1170 |
+
reset_conversation,
|
| 1171 |
+
inputs=None,
|
| 1172 |
+
outputs=[chatbot, conversation_state],
|
| 1173 |
+
show_progress="hidden"
|
| 1174 |
+
)
|
| 1175 |
|
| 1176 |
+
# Apply CSS
|
| 1177 |
gr.HTML(f'<style>{custom_css}</style>')
|
| 1178 |
|
| 1179 |
end_create_interface_time = time.perf_counter()
|