CCockrum commited on
Commit
2a239ae
·
verified ·
1 Parent(s): 053ce9b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +133 -1
app.py CHANGED
@@ -63,4 +63,136 @@ def generate_follow_up(user_text):
63
  f"Given the user's question: '{user_text}', generate a single friendly follow-up question. "
64
  "Make it short, conversational, and natural—like a human would ask. "
65
  "Example: If the user asks 'What is a quark?', respond with something like "
66
- "'Would
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  f"Given the user's question: '{user_text}', generate a single friendly follow-up question. "
64
  "Make it short, conversational, and natural—like a human would ask. "
65
  "Example: If the user asks 'What is a quark?', respond with something like "
66
+ "'Would you like to learn about the six types of quarks?' "
67
+ "Do NOT include phrases like 'A natural follow-up question could be'."
68
+ )
69
+
70
+ hf = get_llm_hf_inference(max_new_tokens=32, temperature=0.7)
71
+ return hf.invoke(input=prompt_text).strip()
72
+
73
+ def get_response(system_message, chat_history, user_text, max_new_tokens=256):
74
+ """
75
+ Generates HAL's response, making it more conversational and engaging.
76
+ """
77
+ sentiment = analyze_sentiment(user_text)
78
+ action = predict_action(user_text)
79
+
80
+ if action == "nasa_info":
81
+ nasa_url, nasa_title, nasa_explanation = get_nasa_apod()
82
+ response = f"**{nasa_title}**\n\n{nasa_explanation}"
83
+ chat_history.append({'role': 'user', 'content': user_text})
84
+ chat_history.append({'role': 'assistant', 'content': response})
85
+
86
+ follow_up = generate_follow_up(user_text)
87
+ chat_history.append({'role': 'assistant', 'content': follow_up})
88
+ return response, follow_up, chat_history, nasa_url
89
+
90
+ hf = get_llm_hf_inference(max_new_tokens=max_new_tokens, temperature=0.9)
91
+
92
+ prompt = PromptTemplate.from_template(
93
+ (
94
+ "[INST] {system_message}"
95
+ "\nCurrent Conversation:\n{chat_history}\n\n"
96
+ "\nUser: {user_text}.\n [/INST]"
97
+ "\nAI: Keep responses conversational and engaging. Start with a friendly phrase like "
98
+ "'Certainly!', 'Of course!', or 'Great question!' before answering."
99
+ " Keep responses concise but engaging."
100
+ "\nHAL:"
101
+ )
102
+ )
103
+
104
+ chat = prompt | hf.bind(skip_prompt=True) | StrOutputParser(output_key='content')
105
+ response = chat.invoke(input=dict(system_message=system_message, user_text=user_text, chat_history=chat_history))
106
+ response = response.split("HAL:")[-1].strip()
107
+
108
+ chat_history.append({'role': 'user', 'content': user_text})
109
+ chat_history.append({'role': 'assistant', 'content': response})
110
+
111
+ follow_up = generate_follow_up(user_text)
112
+ chat_history.append({'role': 'assistant', 'content': follow_up})
113
+
114
+ return response, follow_up, chat_history, None
115
+
116
+ # --- Chat UI ---
117
+ st.title("🚀 HAL - Your NASA AI Assistant")
118
+ st.markdown("🌌 *Ask me about space, NASA, and beyond!*")
119
+
120
+ # Sidebar: Reset Chat
121
+ if st.sidebar.button("Reset Chat"):
122
+ st.session_state.chat_history = [{"role": "assistant", "content": "Hello! How can I assist you today?"}]
123
+ st.session_state.response_ready = False
124
+ st.session_state.follow_up = ""
125
+ st.session_state.last_topic = ""
126
+ st.rerun()
127
+
128
+ # Custom Chat Styling
129
+ st.markdown("""
130
+ <style>
131
+ .user-msg {
132
+ background-color: #0078D7;
133
+ color: white;
134
+ padding: 10px;
135
+ border-radius: 10px;
136
+ margin-bottom: 5px;
137
+ width: fit-content;
138
+ max-width: 80%;
139
+ }
140
+ .assistant-msg {
141
+ background-color: #333333;
142
+ color: white;
143
+ padding: 10px;
144
+ border-radius: 10px;
145
+ margin-bottom: 5px;
146
+ width: fit-content;
147
+ max-width: 80%;
148
+ }
149
+ .container {
150
+ display: flex;
151
+ flex-direction: column;
152
+ align-items: flex-start;
153
+ }
154
+ @media (max-width: 600px) {
155
+ .user-msg, .assistant-msg { font-size: 16px; max-width: 100%; }
156
+ }
157
+ </style>
158
+ """, unsafe_allow_html=True)
159
+
160
+ # --- Chat History Display (Ensures All Messages Are Visible) ---
161
+ st.markdown("<div class='container'>", unsafe_allow_html=True)
162
+
163
+ for message in st.session_state.chat_history:
164
+ if message["role"] == "user":
165
+ st.markdown(f"<div class='user-msg'><strong>You:</strong> {message['content']}</div>", unsafe_allow_html=True)
166
+ else:
167
+ st.markdown(f"<div class='assistant-msg'><strong>HAL:</strong> {message['content']}</div>", unsafe_allow_html=True)
168
+
169
+ st.markdown("</div>", unsafe_allow_html=True)
170
+
171
+ # --- Single Input Box for Both Initial and Follow-Up Messages ---
172
+ user_input = st.chat_input("Type your message here...") # Uses Enter to submit
173
+
174
+ if user_input:
175
+ # Save user message in chat history
176
+ st.session_state.chat_history.append({'role': 'user', 'content': user_input})
177
+
178
+ # Generate HAL's response
179
+ response, follow_up, st.session_state.chat_history, image_url = get_response(
180
+ system_message="You are a helpful AI assistant.",
181
+ user_text=user_input,
182
+ chat_history=st.session_state.chat_history
183
+ )
184
+
185
+ st.session_state.chat_history.append({'role': 'assistant', 'content': response})
186
+
187
+ st.markdown(f"<div class='assistant-msg'><strong>HAL:</strong> {response}</div>", unsafe_allow_html=True)
188
+
189
+ if image_url:
190
+ st.image(image_url, caption="NASA Image of the Day")
191
+
192
+ st.session_state.follow_up = follow_up
193
+ st.session_state.response_ready = True # Enables follow-up response cycle
194
+
195
+ if st.session_state.response_ready and st.session_state.follow_up:
196
+ st.session_state.chat_history.append({'role': 'assistant', 'content': st.session_state.follow_up})
197
+ st.markdown(f"<div class='assistant-msg'><strong>HAL:</strong> {st.session_state.follow_up}</div>", unsafe_allow_html=True)
198
+ st.session_state.response_ready = False