CCockrum commited on
Commit
4520c6d
·
verified ·
1 Parent(s): 552697b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -18
app.py CHANGED
@@ -57,11 +57,10 @@ def generate_follow_up(user_text):
57
  Generates a concise and conversational follow-up question related to the user's input.
58
  """
59
  prompt_text = (
60
- f"Given the user's question: '{user_text}', generate a single friendly follow-up question. "
61
- "Make it short, conversational, and natural—like a human would ask. "
62
- "Example: If the user asks 'What is a quark?', respond with something like "
63
- "'Would you like to learn about the six types of quarks?' "
64
- "Do NOT include phrases like 'A natural follow-up question could be'."
65
  )
66
 
67
  hf = get_llm_hf_inference(max_new_tokens=32, temperature=0.7)
@@ -105,6 +104,9 @@ def get_response(system_message, chat_history, user_text, max_new_tokens=256):
105
  chat_history.append({'role': 'user', 'content': user_text})
106
  chat_history.append({'role': 'assistant', 'content': response})
107
 
 
 
 
108
  follow_up = generate_follow_up(user_text)
109
  chat_history.append({'role': 'assistant', 'content': follow_up})
110
 
@@ -119,8 +121,7 @@ if st.sidebar.button("Reset Chat"):
119
  st.session_state.chat_history = [{"role": "assistant", "content": "Hello! How can I assist you today?"}]
120
  st.session_state.response_ready = False
121
  st.session_state.follow_up = ""
122
- st.session_state.last_topic = ""
123
- st.rerun() #
124
 
125
  # Custom Chat Styling
126
  st.markdown("""
@@ -154,33 +155,25 @@ st.markdown("""
154
  </style>
155
  """, unsafe_allow_html=True)
156
 
157
- # --- Chat History Display (Ensures All Messages Are Visible) ---
158
  st.markdown("<div class='container'>", unsafe_allow_html=True)
159
-
160
  for message in st.session_state.chat_history:
161
  if message["role"] == "user":
162
  st.markdown(f"<div class='user-msg'><strong>You:</strong> {message['content']}</div>", unsafe_allow_html=True)
163
  else:
164
  st.markdown(f"<div class='assistant-msg'><strong>HAL:</strong> {message['content']}</div>", unsafe_allow_html=True)
165
-
166
  st.markdown("</div>", unsafe_allow_html=True)
167
 
168
  # --- Single Input Box for Both Initial and Follow-Up Messages ---
169
- user_input = st.chat_input("Type your message here...") # Uses Enter to submit
170
 
171
  if user_input:
172
- # Save user message in chat history
173
- st.session_state.chat_history.append({'role': 'user', 'content': user_input})
174
-
175
- # Generate HAL's response
176
  response, follow_up, st.session_state.chat_history, image_url = get_response(
177
  system_message="You are a helpful AI assistant.",
178
  user_text=user_input,
179
  chat_history=st.session_state.chat_history
180
  )
181
 
182
- st.session_state.chat_history.append({'role': 'assistant', 'content': response})
183
-
184
  st.markdown(f"<div class='assistant-msg'><strong>HAL:</strong> {response}</div>", unsafe_allow_html=True)
185
 
186
  if image_url:
@@ -189,7 +182,9 @@ if user_input:
189
  st.session_state.follow_up = follow_up
190
  st.session_state.response_ready = True # Enables follow-up response cycle
191
 
 
192
  if st.session_state.response_ready and st.session_state.follow_up:
193
- st.session_state.chat_history.append({'role': 'assistant', 'content': st.session_state.follow_up})
194
  st.markdown(f"<div class='assistant-msg'><strong>HAL:</strong> {st.session_state.follow_up}</div>", unsafe_allow_html=True)
 
 
195
  st.session_state.response_ready = False
 
57
  Generates a concise and conversational follow-up question related to the user's input.
58
  """
59
  prompt_text = (
60
+ f"Given the user's question: '{user_text}', generate a SHORT and SIMPLE follow-up question. "
61
+ "Make it conversational and friendly. Example: "
62
+ "'Would you like to learn more about the six types of quarks?' "
63
+ "Do NOT provide long explanations—just ask a friendly follow-up question."
 
64
  )
65
 
66
  hf = get_llm_hf_inference(max_new_tokens=32, temperature=0.7)
 
104
  chat_history.append({'role': 'user', 'content': user_text})
105
  chat_history.append({'role': 'assistant', 'content': response})
106
 
107
+ if sentiment == "NEGATIVE":
108
+ response = "I'm here to help. Let me know what I can do for you. 😊"
109
+
110
  follow_up = generate_follow_up(user_text)
111
  chat_history.append({'role': 'assistant', 'content': follow_up})
112
 
 
121
  st.session_state.chat_history = [{"role": "assistant", "content": "Hello! How can I assist you today?"}]
122
  st.session_state.response_ready = False
123
  st.session_state.follow_up = ""
124
+ st.experimental_rerun()
 
125
 
126
  # Custom Chat Styling
127
  st.markdown("""
 
155
  </style>
156
  """, unsafe_allow_html=True)
157
 
158
+ # Chat History Display
159
  st.markdown("<div class='container'>", unsafe_allow_html=True)
 
160
  for message in st.session_state.chat_history:
161
  if message["role"] == "user":
162
  st.markdown(f"<div class='user-msg'><strong>You:</strong> {message['content']}</div>", unsafe_allow_html=True)
163
  else:
164
  st.markdown(f"<div class='assistant-msg'><strong>HAL:</strong> {message['content']}</div>", unsafe_allow_html=True)
 
165
  st.markdown("</div>", unsafe_allow_html=True)
166
 
167
  # --- Single Input Box for Both Initial and Follow-Up Messages ---
168
+ user_input = st.chat_input("Type your message here...") # Only ONE chat_input()
169
 
170
  if user_input:
 
 
 
 
171
  response, follow_up, st.session_state.chat_history, image_url = get_response(
172
  system_message="You are a helpful AI assistant.",
173
  user_text=user_input,
174
  chat_history=st.session_state.chat_history
175
  )
176
 
 
 
177
  st.markdown(f"<div class='assistant-msg'><strong>HAL:</strong> {response}</div>", unsafe_allow_html=True)
178
 
179
  if image_url:
 
182
  st.session_state.follow_up = follow_up
183
  st.session_state.response_ready = True # Enables follow-up response cycle
184
 
185
+ # Display follow-up question inside chat if available
186
  if st.session_state.response_ready and st.session_state.follow_up:
 
187
  st.markdown(f"<div class='assistant-msg'><strong>HAL:</strong> {st.session_state.follow_up}</div>", unsafe_allow_html=True)
188
+
189
+ # Reset response state so user can type next input
190
  st.session_state.response_ready = False