Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -112,7 +112,83 @@ def main():
|
|
| 112 |
st.session_state.messages = []
|
| 113 |
st.rerun() # Changed from experimental_rerun() to rerun()
|
| 114 |
|
| 115 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
if __name__ == "__main__":
|
| 118 |
main()
|
|
|
|
| 112 |
st.session_state.messages = []
|
| 113 |
st.rerun() # Changed from experimental_rerun() to rerun()
|
| 114 |
|
| 115 |
+
# Initialize or get chatbot from session state
|
| 116 |
+
if 'chatbot' not in st.session_state:
|
| 117 |
+
with st.spinner("Loading dataset and initializing model..."):
|
| 118 |
+
try:
|
| 119 |
+
st.session_state.chatbot = initialize_chatbot(
|
| 120 |
+
dataset_name, text_column, split, max_samples
|
| 121 |
+
)
|
| 122 |
+
# Display dataset info
|
| 123 |
+
info = st.session_state.chatbot.get_dataset_info()
|
| 124 |
+
st.success(f"Successfully loaded {info['name']} dataset")
|
| 125 |
+
st.info(f"Total documents: {info['total_documents']}\nTopics found: {info['topics_found']}")
|
| 126 |
+
except Exception as e:
|
| 127 |
+
st.error(f"Error initializing chatbot: {str(e)}")
|
| 128 |
+
return
|
| 129 |
+
|
| 130 |
+
chatbot = st.session_state.chatbot
|
| 131 |
+
|
| 132 |
+
# Create tabs for chat and metrics
|
| 133 |
+
chat_tab, metrics_tab = st.tabs(["Chat", "Metrics"])
|
| 134 |
+
|
| 135 |
+
with chat_tab:
|
| 136 |
+
# Initialize messages if not exists
|
| 137 |
+
if 'messages' not in st.session_state:
|
| 138 |
+
st.session_state.messages = []
|
| 139 |
+
|
| 140 |
+
# Display chat messages
|
| 141 |
+
for message in st.session_state.messages:
|
| 142 |
+
with st.chat_message(message["role"]):
|
| 143 |
+
st.markdown(message["content"])
|
| 144 |
+
|
| 145 |
+
# User input
|
| 146 |
+
if prompt := st.chat_input("Hãy nói gì đó..."):
|
| 147 |
+
# Add user message to chat history
|
| 148 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 149 |
+
|
| 150 |
+
# Display user message
|
| 151 |
+
with st.chat_message("user"):
|
| 152 |
+
st.markdown(prompt)
|
| 153 |
+
|
| 154 |
+
# Get chatbot response and metrics
|
| 155 |
+
response, metrics = chatbot.get_response(prompt)
|
| 156 |
+
|
| 157 |
+
# Display chatbot response
|
| 158 |
+
with st.chat_message("assistant"):
|
| 159 |
+
st.markdown(response)
|
| 160 |
+
with st.expander("Response Metrics"):
|
| 161 |
+
st.json(metrics)
|
| 162 |
+
|
| 163 |
+
# Add assistant message to chat history
|
| 164 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 165 |
+
|
| 166 |
+
with metrics_tab:
|
| 167 |
+
# Get and display visualizations
|
| 168 |
+
fig_similarity, fig_response_time, fig_tokens, fig_topics = chatbot.get_metrics_visualizations()
|
| 169 |
+
|
| 170 |
+
col1, col2 = st.columns(2)
|
| 171 |
+
with col1:
|
| 172 |
+
st.plotly_chart(fig_similarity, use_container_width=True)
|
| 173 |
+
st.plotly_chart(fig_tokens, use_container_width=True)
|
| 174 |
+
|
| 175 |
+
with col2:
|
| 176 |
+
st.plotly_chart(fig_response_time, use_container_width=True)
|
| 177 |
+
st.plotly_chart(fig_topics, use_container_width=True)
|
| 178 |
+
|
| 179 |
+
# Add overall statistics
|
| 180 |
+
st.subheader("Overall Statistics")
|
| 181 |
+
if len(chatbot.metrics_history['similarities']) > 0:
|
| 182 |
+
stats_col1, stats_col2, stats_col3 = st.columns(3)
|
| 183 |
+
with stats_col1:
|
| 184 |
+
st.metric("Avg Similarity",
|
| 185 |
+
f"{np.mean(list(chatbot.metrics_history['similarities'])):.3f}")
|
| 186 |
+
with stats_col2:
|
| 187 |
+
st.metric("Avg Response Time",
|
| 188 |
+
f"{np.mean(list(chatbot.metrics_history['response_times'])):.3f}s")
|
| 189 |
+
with stats_col3:
|
| 190 |
+
st.metric("Total Tokens Used",
|
| 191 |
+
sum(chatbot.metrics_history['token_counts']))
|
| 192 |
|
| 193 |
if __name__ == "__main__":
|
| 194 |
main()
|