Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -112,4 +112,83 @@ def get_response(system_message, chat_history, user_text, max_new_tokens=256):
|
|
112 |
|
113 |
# --- Chat UI ---
|
114 |
st.title("π HAL - Your NASA AI Assistant")
|
115 |
-
st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
|
113 |
# --- Chat UI ---
|
114 |
st.title("π HAL - Your NASA AI Assistant")
|
115 |
+
st.markdown("π *Ask me about space, NASA, and beyond!*")
|
116 |
+
|
117 |
+
# Sidebar: Reset Chat
|
118 |
+
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.experimental_rerun()
|
123 |
+
|
124 |
+
# Custom Chat Styling
|
125 |
+
st.markdown("""
|
126 |
+
<style>
|
127 |
+
.user-msg {
|
128 |
+
background-color: #0078D7;
|
129 |
+
color: white;
|
130 |
+
padding: 10px;
|
131 |
+
border-radius: 10px;
|
132 |
+
margin-bottom: 5px;
|
133 |
+
width: fit-content;
|
134 |
+
max-width: 80%;
|
135 |
+
}
|
136 |
+
.assistant-msg {
|
137 |
+
background-color: #333333;
|
138 |
+
color: white;
|
139 |
+
padding: 10px;
|
140 |
+
border-radius: 10px;
|
141 |
+
margin-bottom: 5px;
|
142 |
+
width: fit-content;
|
143 |
+
max-width: 80%;
|
144 |
+
}
|
145 |
+
.container {
|
146 |
+
display: flex;
|
147 |
+
flex-direction: column;
|
148 |
+
align-items: flex-start;
|
149 |
+
}
|
150 |
+
@media (max-width: 600px) {
|
151 |
+
.user-msg, .assistant-msg { font-size: 16px; max-width: 100%; }
|
152 |
+
}
|
153 |
+
</style>
|
154 |
+
""", unsafe_allow_html=True)
|
155 |
+
|
156 |
+
# --- Chat History Display (Ensures All Messages Are Visible) ---
|
157 |
+
st.markdown("<div class='container'>", unsafe_allow_html=True)
|
158 |
+
|
159 |
+
for message in st.session_state.chat_history:
|
160 |
+
if message["role"] == "user":
|
161 |
+
st.markdown(f"<div class='user-msg'><strong>You:</strong> {message['content']}</div>", unsafe_allow_html=True)
|
162 |
+
else:
|
163 |
+
st.markdown(f"<div class='assistant-msg'><strong>HAL:</strong> {message['content']}</div>", unsafe_allow_html=True)
|
164 |
+
|
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...") # Uses Enter to submit
|
169 |
+
|
170 |
+
if user_input:
|
171 |
+
# Save user message in chat history
|
172 |
+
st.session_state.chat_history.append({'role': 'user', 'content': user_input})
|
173 |
+
|
174 |
+
# Generate HAL's response
|
175 |
+
response, follow_up, st.session_state.chat_history, image_url = get_response(
|
176 |
+
system_message="You are a helpful AI assistant.",
|
177 |
+
user_text=user_input,
|
178 |
+
chat_history=st.session_state.chat_history
|
179 |
+
)
|
180 |
+
|
181 |
+
st.session_state.chat_history.append({'role': 'assistant', 'content': response})
|
182 |
+
|
183 |
+
st.markdown(f"<div class='assistant-msg'><strong>HAL:</strong> {response}</div>", unsafe_allow_html=True)
|
184 |
+
|
185 |
+
if image_url:
|
186 |
+
st.image(image_url, caption="NASA Image of the Day")
|
187 |
+
|
188 |
+
st.session_state.follow_up = follow_up
|
189 |
+
st.session_state.response_ready = True # Enables follow-up response cycle
|
190 |
+
|
191 |
+
if st.session_state.response_ready and st.session_state.follow_up:
|
192 |
+
st.session_state.chat_history.append({'role': 'assistant', 'content': st.session_state.follow_up})
|
193 |
+
st.markdown(f"<div class='assistant-msg'><strong>HAL:</strong> {st.session_state.follow_up}</div>", unsafe_allow_html=True)
|
194 |
+
st.session_state.response_ready = False
|