CCockrum commited on
Commit
f77b42d
Β·
verified Β·
1 Parent(s): ac55e19

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -10
app.py CHANGED
@@ -105,7 +105,7 @@ def get_response(system_message, chat_history, user_text, max_new_tokens=512):
105
  """
106
  sentiment = analyze_sentiment(user_text)
107
  action = predict_action(user_text)
108
-
109
  # Extract style instruction if present
110
  style_instruction = ""
111
  lower_text = user_text.lower()
@@ -141,32 +141,34 @@ def get_response(system_message, chat_history, user_text, max_new_tokens=512):
141
  "AI: Please provide a detailed explanation in depth. "
142
  "Ensure your response covers the topic thoroughly and is written in a friendly, conversational style, "
143
  "starting with a phrase like 'Certainly!', 'Of course!', or 'Great question!'."
144
- "Answer exclusively in English, and do not include extra commentary."+ style_clause +
145
  "\nHAL:"
146
  )
147
  )
148
 
149
  chat = prompt | hf.bind(skip_prompt=True) | StrOutputParser(output_key='content')
150
  response = chat.invoke(input=dict(system_message=system_message, user_text=user_text, chat_history=filtered_history))
151
- # Remove any extra markers if present.
152
  response = response.split("HAL:")[-1].strip()
153
-
154
  # Fallback in case the generated answer is empty
155
  if not response:
156
  response = "Certainly, here is an in-depth explanation: [Fallback explanation]."
157
-
158
  chat_history.append({'role': 'user', 'content': user_text})
159
  chat_history.append({'role': 'assistant', 'content': response})
160
-
 
161
  if sentiment == "NEGATIVE" and not user_text.strip().endswith("?"):
162
  response = "I'm sorry you're feeling this way. I'm here to help. What can I do to assist you further?"
163
  chat_history[-1]['content'] = response
164
-
165
- follow_up = generate_follow_up(user_text)
166
- chat_history.append({'role': 'assistant', 'content': follow_up})
167
-
 
168
  return response, follow_up, chat_history, None
169
 
 
170
  # --- Chat UI ---
171
  st.title("πŸš€ HAL - Your NASA AI Assistant")
172
  st.markdown("🌌 *Ask me about space, NASA, and beyond!*")
 
105
  """
106
  sentiment = analyze_sentiment(user_text)
107
  action = predict_action(user_text)
108
+
109
  # Extract style instruction if present
110
  style_instruction = ""
111
  lower_text = user_text.lower()
 
141
  "AI: Please provide a detailed explanation in depth. "
142
  "Ensure your response covers the topic thoroughly and is written in a friendly, conversational style, "
143
  "starting with a phrase like 'Certainly!', 'Of course!', or 'Great question!'."
144
+ "Answer exclusively in English, and do not include extra commentary." + style_clause +
145
  "\nHAL:"
146
  )
147
  )
148
 
149
  chat = prompt | hf.bind(skip_prompt=True) | StrOutputParser(output_key='content')
150
  response = chat.invoke(input=dict(system_message=system_message, user_text=user_text, chat_history=filtered_history))
 
151
  response = response.split("HAL:")[-1].strip()
152
+
153
  # Fallback in case the generated answer is empty
154
  if not response:
155
  response = "Certainly, here is an in-depth explanation: [Fallback explanation]."
156
+
157
  chat_history.append({'role': 'user', 'content': user_text})
158
  chat_history.append({'role': 'assistant', 'content': response})
159
+
160
+ # πŸ” FIX: Only override if strongly negative and NOT a question
161
  if sentiment == "NEGATIVE" and not user_text.strip().endswith("?"):
162
  response = "I'm sorry you're feeling this way. I'm here to help. What can I do to assist you further?"
163
  chat_history[-1]['content'] = response
164
+ follow_up = None # 🚨 Don't generate follow-up if negative sentiment triggers a different message
165
+ else:
166
+ follow_up = generate_follow_up(user_text)
167
+ chat_history.append({'role': 'assistant', 'content': follow_up})
168
+
169
  return response, follow_up, chat_history, None
170
 
171
+
172
  # --- Chat UI ---
173
  st.title("πŸš€ HAL - Your NASA AI Assistant")
174
  st.markdown("🌌 *Ask me about space, NASA, and beyond!*")