Vlad Bastina commited on
Commit
9809102
·
1 Parent(s): 9f66c70

style changes

Browse files
Files changed (2) hide show
  1. app.py +50 -35
  2. style.css +1 -1
app.py CHANGED
@@ -82,31 +82,25 @@ if "messages" not in st.session_state:
82
  if "question_input" not in st.session_state:
83
  st.session_state.question_input = ""
84
 
 
 
 
85
 
86
- # --- Callback Functions ---
87
  def handle_ask_ai():
88
  """Callback function for the 'Ask AI' button."""
89
- question = st.session_state.question_input # Get question from state
90
- if question: # Proceed only if there is a question
91
- # Append user question
92
  st.session_state.messages.append({"role": "user", "content": question})
93
 
94
- # Generate AI response
95
- # Use a spinner *outside* the callback if needed, or understand
96
- # the UI might briefly freeze if generation is long.
97
- # For simplicity here, we generate directly.
98
  try:
99
  answer = chatbot.answer_question(question)
100
- # Append AI response
101
  st.session_state.messages.append({"role": "assistant", "content": answer})
102
  except Exception as e:
103
  st.error(f"Error getting answer from chatbot: {e}")
104
- # Optionally remove the user message if the AI failed
105
- # st.session_state.messages.pop()
106
 
107
- # ***** Clear the input state variable AFTER processing *****
108
- st.session_state.question_input = ""
109
- # No explicit st.rerun() needed here, on_click handles the rerun
110
 
111
 
112
  def handle_clear_chat():
@@ -157,24 +151,45 @@ def display_message(role, content):
157
  for message in st.session_state.messages:
158
  display_message(message["role"], message["content"])
159
 
160
- # --- User Input ---
161
- # This widget's value is controlled by st.session_state.question_input
162
- st.text_area(
163
- "Ask a question about Zega AI:",
164
- height=100,
165
- key="question_input", # Links to st.session_state.question_input
166
- on_change=handle_ask_ai
167
- )
168
-
169
- # --- Chat Actions ---
170
- col1, col2 = st.columns([3, 1])
171
- with col1:
172
- # Attach the callback function to the button's on_click parameter
173
- st.button("💬 Ask AI", on_click=handle_ask_ai)
174
- with col2:
175
- # Attach the callback function to the button's on_click parameter
176
- st.button("🗑️ Clear Chat", on_click=handle_clear_chat)
177
-
178
- # --- No processing logic needed here anymore ---
179
- # The callbacks handle the logic when the buttons are clicked.
180
- # Streamlit automatically reruns after a callback, updating the UI.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  if "question_input" not in st.session_state:
83
  st.session_state.question_input = ""
84
 
85
+ # --- Initialize session state if not already present ---
86
+ if "asked_ai" not in st.session_state:
87
+ st.session_state.asked_ai = False
88
 
 
89
  def handle_ask_ai():
90
  """Callback function for the 'Ask AI' button."""
91
+ question = st.session_state.question_input # Get question from state
92
+ if question:
93
+
94
  st.session_state.messages.append({"role": "user", "content": question})
95
 
 
 
 
 
96
  try:
97
  answer = chatbot.answer_question(question)
 
98
  st.session_state.messages.append({"role": "assistant", "content": answer})
99
  except Exception as e:
100
  st.error(f"Error getting answer from chatbot: {e}")
 
 
101
 
102
+ st.session_state.question_input = "" # Clear input after processing
103
+
 
104
 
105
 
106
  def handle_clear_chat():
 
151
  for message in st.session_state.messages:
152
  display_message(message["role"], message["content"])
153
 
154
+ # --- Chat Input using st.chat_input (Handles Enter automatically) ---
155
+ prompt = st.chat_input("Ask a question about Zega AI:")
156
+ if prompt:
157
+ # 1. Add user message to chat history immediately
158
+ st.session_state.messages.append({"role": "user", "content": prompt})
159
+
160
+ # 2. Display the user message (using your existing function)
161
+ display_message("user", prompt)
162
+
163
+ # 3. Get and display AI response
164
+ try:
165
+ with st.spinner("Thinking..."): # Optional: Add spinner for feedback
166
+ answer = chatbot.answer_question(prompt)
167
+ st.session_state.messages.append({"role": "assistant", "content": answer})
168
+ # Display the assistant message immediately after getting it
169
+ display_message("assistant", answer)
170
+ except Exception as e:
171
+ st.error(f"Error getting answer from chatbot: {e}")
172
+ # Optionally add an error message to the chat history
173
+ # st.session_state.messages.append({"role": "assistant", "content": f"Sorry, an error occurred: {e}"})
174
+ # display_message("assistant", f"Sorry, an error occurred: {e}")
175
+
176
+ # No need to manually clear st.session_state.question_input
177
+ # st.chat_input handles its state internally upon submission.
178
+ # Streamlit automatically reruns after processing the input block.
179
+
180
+ # --- Clear Chat Button (Keep this separate) ---
181
+ # Place it where you want it, maybe in the sidebar or below the chat input
182
+ st.markdown("""
183
+ <style>
184
+ .stButton > button {
185
+ color: white !important;
186
+ background-color: #ff4b4b; /* optional: red background for the trash button */
187
+ }
188
+ </style>
189
+ """, unsafe_allow_html=True)
190
+ if st.sidebar.button("🗑️ Clear Chat"):
191
+ st.session_state.messages = []
192
+ if 'chatbot' in globals() and hasattr(chatbot, 'clear_conv_history'):
193
+ chatbot.clear_conv_history()
194
+ # st.session_state.question_input is no longer used, so no need to clear it
195
+ st.rerun() # Rerun to reflect the cleared chat visually
style.css CHANGED
@@ -73,4 +73,4 @@ button:focus,
73
  /* Other fonts */
74
  [class^="st-emotion-cache-"] {
75
  font-family: 'Inter Tight', Arial, Helvetica, sans-serif !important;
76
- }
 
73
  /* Other fonts */
74
  [class^="st-emotion-cache-"] {
75
  font-family: 'Inter Tight', Arial, Helvetica, sans-serif !important;
76
+ }