CCockrum commited on
Commit
af3b056
Β·
verified Β·
1 Parent(s): c53c955

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -56
app.py CHANGED
@@ -8,52 +8,6 @@ from langchain_core.prompts import PromptTemplate
8
  from langchain_core.output_parsers import StrOutputParser
9
  from transformers import pipeline
10
 
11
- st.set_page_config(page_title="HAL - NASA ChatBot", page_icon="πŸš€")
12
-
13
- # Appearance Settings: Allow user to adjust UI appearance via sidebar.
14
- user_bg_color = st.sidebar.color_picker("User Message Background", "#0078D7")
15
- assistant_bg_color = st.sidebar.color_picker("Assistant Message Background", "#333333")
16
- text_color = st.sidebar.color_picker("Message Text Color", "#FFFFFF")
17
- font_choice = st.sidebar.selectbox("Font Family", ["sans serif", "serif", "monospace"])
18
-
19
- # Inject custom CSS for appearance
20
- custom_css = f"""
21
- <style>
22
- .user-msg {{
23
- background-color: {user_bg_color};
24
- color: {text_color};
25
- padding: 10px;
26
- border-radius: 10px;
27
- margin-bottom: 5px;
28
- width: fit-content;
29
- max-width: 80%;
30
- font-family: {font_choice};
31
- }}
32
- .assistant-msg {{
33
- background-color: {assistant_bg_color};
34
- color: {text_color};
35
- padding: 10px;
36
- border-radius: 10px;
37
- margin-bottom: 5px;
38
- width: fit-content;
39
- max-width: 80%;
40
- font-family: {font_choice};
41
- }}
42
- .container {{
43
- display: flex;
44
- flex-direction: column;
45
- align-items: flex-start;
46
- }}
47
- @media (max-width: 600px) {{
48
- .user-msg, .assistant-msg {{
49
- font-size: 16px;
50
- max-width: 100%;
51
- }}
52
- }}
53
- </style>
54
- """
55
- st.markdown(custom_css, unsafe_allow_html=True)
56
-
57
  # Use environment variables for keys
58
  HF_TOKEN = os.getenv("HF_TOKEN")
59
  if HF_TOKEN is None:
@@ -110,10 +64,14 @@ def predict_action(user_text):
110
  return "general_query"
111
 
112
  def generate_follow_up(user_text):
 
 
 
 
113
  prompt_text = (
114
  f"Based on the user's question: '{user_text}', generate two concise, friendly follow-up questions "
115
- "that invite further discussion (e.g., one might ask, 'Would you like to know more about the six types of quarks?' "
116
- "and another might ask, 'Would you like to explore something else?'). Do not include extra meta commentary."
117
  )
118
  hf = get_llm_hf_inference(max_new_tokens=80, temperature=0.9)
119
  output = hf.invoke(input=prompt_text).strip()
@@ -124,8 +82,15 @@ def generate_follow_up(user_text):
124
  return random.choice(cleaned)
125
 
126
  def get_response(system_message, chat_history, user_text, max_new_tokens=256):
 
 
 
 
 
127
  sentiment = analyze_sentiment(user_text)
128
  action = predict_action(user_text)
 
 
129
  style_instruction = ""
130
  lower_text = user_text.lower()
131
  if "in the voice of" in lower_text or "speaking as" in lower_text:
@@ -133,6 +98,7 @@ def get_response(system_message, chat_history, user_text, max_new_tokens=256):
133
  if match:
134
  style_instruction = match.group(2).strip().capitalize()
135
  style_instruction = f" Please respond in the voice of {style_instruction}."
 
136
  if action == "nasa_info":
137
  nasa_url, nasa_title, nasa_explanation = get_nasa_apod()
138
  response = f"**{nasa_title}**\n\n{nasa_explanation}"
@@ -141,36 +107,50 @@ def get_response(system_message, chat_history, user_text, max_new_tokens=256):
141
  follow_up = generate_follow_up(user_text)
142
  chat_history.append({'role': 'assistant', 'content': follow_up})
143
  return response, follow_up, chat_history, nasa_url
 
144
  hf = get_llm_hf_inference(max_new_tokens=max_new_tokens, temperature=0.9)
145
  filtered_history = ""
146
  for message in chat_history:
147
  if message["role"] == "assistant" and message["content"].strip() == "Hello! How can I assist you today?":
148
  continue
149
  filtered_history += f"{message['role']}: {message['content']}\n"
 
150
  style_clause = style_instruction if style_instruction else ""
 
 
151
  prompt = PromptTemplate.from_template(
152
  (
153
  "[INST] {system_message}\n\nCurrent Conversation:\n{chat_history}\n\n"
154
  "User: {user_text}.\n [/INST]\n"
155
- "AI: Please answer the user's question in depth and in a friendly, conversational tone, "
 
156
  "starting with a phrase like 'Certainly!', 'Of course!', or 'Great question!'." + style_clause +
157
  "\nHAL:"
158
  )
159
  )
 
160
  chat = prompt | hf.bind(skip_prompt=True) | StrOutputParser(output_key='content')
161
  response = chat.invoke(input=dict(system_message=system_message, user_text=user_text, chat_history=filtered_history))
 
162
  response = response.split("HAL:")[-1].strip()
 
 
163
  if not response:
164
  response = "Certainly, here is an in-depth explanation: [Fallback explanation]."
 
165
  chat_history.append({'role': 'user', 'content': user_text})
166
  chat_history.append({'role': 'assistant', 'content': response})
 
167
  if sentiment == "NEGATIVE" and not user_text.strip().endswith("?"):
168
  response = "I'm sorry you're feeling this way. I'm here to help. What can I do to assist you further?"
169
  chat_history[-1]['content'] = response
 
170
  follow_up = generate_follow_up(user_text)
171
  chat_history.append({'role': 'assistant', 'content': follow_up})
 
172
  return response, follow_up, chat_history, None
173
 
 
174
  st.title("πŸš€ HAL - Your NASA AI Assistant")
175
  st.markdown("🌌 *Ask me about space, NASA, and beyond!*")
176
 
@@ -180,13 +160,36 @@ if st.sidebar.button("Reset Chat"):
180
  st.session_state.follow_up = ""
181
  st.experimental_rerun()
182
 
183
- st.markdown("<div class='container'>", unsafe_allow_html=True)
184
- for message in st.session_state.chat_history:
185
- if message["role"] == "user":
186
- st.markdown(f"<div class='user-msg'><strong>You:</strong> {message['content']}</div>", unsafe_allow_html=True)
187
- else:
188
- st.markdown(f"<div class='assistant-msg'><strong>HAL:</strong> {message['content']}</div>", unsafe_allow_html=True)
189
- st.markdown("</div>", unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
190
 
191
  user_input = st.chat_input("Type your message here...")
192
 
@@ -200,3 +203,11 @@ if user_input:
200
  st.image(image_url, caption="NASA Image of the Day")
201
  st.session_state.follow_up = follow_up
202
  st.session_state.response_ready = True
 
 
 
 
 
 
 
 
 
8
  from langchain_core.output_parsers import StrOutputParser
9
  from transformers import pipeline
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  # Use environment variables for keys
12
  HF_TOKEN = os.getenv("HF_TOKEN")
13
  if HF_TOKEN is None:
 
64
  return "general_query"
65
 
66
  def generate_follow_up(user_text):
67
+ """
68
+ Generates two variant follow-up questions and randomly selects one.
69
+ It also cleans up any unwanted quotation marks or extra meta commentary.
70
+ """
71
  prompt_text = (
72
  f"Based on the user's question: '{user_text}', generate two concise, friendly follow-up questions "
73
+ "that invite further discussion. For example, one might be 'Would you like to know more about the six types of quarks?' "
74
+ "and another might be 'Would you like to explore another aspect of quantum physics?' Do not include extra commentary."
75
  )
76
  hf = get_llm_hf_inference(max_new_tokens=80, temperature=0.9)
77
  output = hf.invoke(input=prompt_text).strip()
 
82
  return random.choice(cleaned)
83
 
84
  def get_response(system_message, chat_history, user_text, max_new_tokens=256):
85
+ """
86
+ Generates HAL's answer with depth and a follow-up question.
87
+ The prompt instructs the model to provide a detailed explanation and then generate a follow-up.
88
+ If the answer comes back empty, a fallback answer is used.
89
+ """
90
  sentiment = analyze_sentiment(user_text)
91
  action = predict_action(user_text)
92
+
93
+ # Extract style instruction if present
94
  style_instruction = ""
95
  lower_text = user_text.lower()
96
  if "in the voice of" in lower_text or "speaking as" in lower_text:
 
98
  if match:
99
  style_instruction = match.group(2).strip().capitalize()
100
  style_instruction = f" Please respond in the voice of {style_instruction}."
101
+
102
  if action == "nasa_info":
103
  nasa_url, nasa_title, nasa_explanation = get_nasa_apod()
104
  response = f"**{nasa_title}**\n\n{nasa_explanation}"
 
107
  follow_up = generate_follow_up(user_text)
108
  chat_history.append({'role': 'assistant', 'content': follow_up})
109
  return response, follow_up, chat_history, nasa_url
110
+
111
  hf = get_llm_hf_inference(max_new_tokens=max_new_tokens, temperature=0.9)
112
  filtered_history = ""
113
  for message in chat_history:
114
  if message["role"] == "assistant" and message["content"].strip() == "Hello! How can I assist you today?":
115
  continue
116
  filtered_history += f"{message['role']}: {message['content']}\n"
117
+
118
  style_clause = style_instruction if style_instruction else ""
119
+
120
+ # Instruct the model to generate a detailed, in-depth answer.
121
  prompt = PromptTemplate.from_template(
122
  (
123
  "[INST] {system_message}\n\nCurrent Conversation:\n{chat_history}\n\n"
124
  "User: {user_text}.\n [/INST]\n"
125
+ "AI: Please provide a detailed explanation in depth. "
126
+ "Ensure your response covers the topic thoroughly and is written in a friendly, conversational style, "
127
  "starting with a phrase like 'Certainly!', 'Of course!', or 'Great question!'." + style_clause +
128
  "\nHAL:"
129
  )
130
  )
131
+
132
  chat = prompt | hf.bind(skip_prompt=True) | StrOutputParser(output_key='content')
133
  response = chat.invoke(input=dict(system_message=system_message, user_text=user_text, chat_history=filtered_history))
134
+ # Remove any extra markers if present.
135
  response = response.split("HAL:")[-1].strip()
136
+
137
+ # Fallback in case the generated answer is empty
138
  if not response:
139
  response = "Certainly, here is an in-depth explanation: [Fallback explanation]."
140
+
141
  chat_history.append({'role': 'user', 'content': user_text})
142
  chat_history.append({'role': 'assistant', 'content': response})
143
+
144
  if sentiment == "NEGATIVE" and not user_text.strip().endswith("?"):
145
  response = "I'm sorry you're feeling this way. I'm here to help. What can I do to assist you further?"
146
  chat_history[-1]['content'] = response
147
+
148
  follow_up = generate_follow_up(user_text)
149
  chat_history.append({'role': 'assistant', 'content': follow_up})
150
+
151
  return response, follow_up, chat_history, None
152
 
153
+ # --- Chat UI ---
154
  st.title("πŸš€ HAL - Your NASA AI Assistant")
155
  st.markdown("🌌 *Ask me about space, NASA, and beyond!*")
156
 
 
160
  st.session_state.follow_up = ""
161
  st.experimental_rerun()
162
 
163
+ st.markdown("""
164
+ <style>
165
+ .user-msg {
166
+ background-color: #0078D7;
167
+ color: white;
168
+ padding: 10px;
169
+ border-radius: 10px;
170
+ margin-bottom: 5px;
171
+ width: fit-content;
172
+ max-width: 80%;
173
+ }
174
+ .assistant-msg {
175
+ background-color: #333333;
176
+ color: white;
177
+ padding: 10px;
178
+ border-radius: 10px;
179
+ margin-bottom: 5px;
180
+ width: fit-content;
181
+ max-width: 80%;
182
+ }
183
+ .container {
184
+ display: flex;
185
+ flex-direction: column;
186
+ align-items: flex-start;
187
+ }
188
+ @media (max-width: 600px) {
189
+ .user-msg, .assistant-msg { font-size: 16px; max-width: 100%; }
190
+ }
191
+ </style>
192
+ """, unsafe_allow_html=True)
193
 
194
  user_input = st.chat_input("Type your message here...")
195
 
 
203
  st.image(image_url, caption="NASA Image of the Day")
204
  st.session_state.follow_up = follow_up
205
  st.session_state.response_ready = True
206
+
207
+ st.markdown("<div class='container'>", unsafe_allow_html=True)
208
+ for message in st.session_state.chat_history:
209
+ if message["role"] == "user":
210
+ st.markdown(f"<div class='user-msg'><strong>You:</strong> {message['content']}</div>", unsafe_allow_html=True)
211
+ else:
212
+ st.markdown(f"<div class='assistant-msg'><strong>HAL:</strong> {message['content']}</div>", unsafe_allow_html=True)
213
+ st.markdown("</div>", unsafe_allow_html=True)