CCockrum commited on
Commit
d17524e
Β·
verified Β·
1 Parent(s): fdad8e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -2
app.py CHANGED
@@ -88,6 +88,24 @@ def get_response(system_message, chat_history, user_text, max_new_tokens=800):
88
  # βœ… Keep only last 10 exchanges to prevent unnecessary repetition
89
  return response, chat_history[-10:]
90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  # βœ… Streamlit UI
92
  st.title("πŸš€ HAL - NASA AI Assistant")
93
 
@@ -109,7 +127,7 @@ st.markdown("""
109
  background-color: #2196F3;
110
  border: none;
111
  color: white;
112
- padding: 5px 11px;
113
  text-align: center;
114
  text-decoration: none;
115
  display: inline-block;
@@ -347,4 +365,59 @@ st.markdown("""
347
 
348
  # Add voice status indicator
349
  st.markdown("""
350
- <div class="status
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
  # βœ… Keep only last 10 exchanges to prevent unnecessary repetition
89
  return response, chat_history[-10:]
90
 
91
+ # βœ… NASA API Function to get space data
92
+ def get_nasa_data(query):
93
+ try:
94
+ if "apod" in query.lower() or "picture of the day" in query.lower():
95
+ response = requests.get(f"https://api.nasa.gov/planetary/apod?api_key={NASA_API_KEY}")
96
+ if response.status_code == 200:
97
+ data = response.json()
98
+ return {
99
+ "title": data.get("title", "NASA Image"),
100
+ "date": data.get("date", ""),
101
+ "explanation": data.get("explanation", ""),
102
+ "url": data.get("url", "")
103
+ }
104
+ return None
105
+ except Exception as e:
106
+ print(f"Error fetching NASA data: {e}")
107
+ return None
108
+
109
  # βœ… Streamlit UI
110
  st.title("πŸš€ HAL - NASA AI Assistant")
111
 
 
127
  background-color: #2196F3;
128
  border: none;
129
  color: white;
130
+ padding: 5px 10px;
131
  text-align: center;
132
  text-decoration: none;
133
  display: inline-block;
 
365
 
366
  # Add voice status indicator
367
  st.markdown("""
368
+ <div class="status-bar">
369
+ <span id="voice-status">Initializing voice recognition...</span>
370
+ <span id="voice-indicator" class="voice-indicator" onclick="toggleVoiceRecognition()"></span>
371
+ </div>
372
+ """, unsafe_allow_html=True)
373
+
374
+ # Regular text input
375
+ user_input = st.chat_input("Type your message here or just speak...")
376
+
377
+ # Hidden input for speech results
378
+ speech_result = st.text_input("Speech Result", key="speech_input", label_visibility="collapsed")
379
+
380
+ # Hidden button to submit speech
381
+ st.markdown('<button id="submit-speech" style="display:none;">Submit Speech</button>', unsafe_allow_html=True)
382
+
383
+ # Auto-speak toggle
384
+ st.checkbox("Auto-speak responses", value=st.session_state.auto_speak, key="auto_speak_toggle",
385
+ on_change=lambda: setattr(st.session_state, "auto_speak", st.session_state.auto_speak_toggle))
386
+
387
+ # Display chat history
388
+ for i, msg in enumerate(st.session_state.chat_history):
389
+ if msg["role"] == "user":
390
+ st.markdown(f'<div class="container"><div class="user-msg">You: {msg["content"]}</div></div>', unsafe_allow_html=True)
391
+ else:
392
+ msg_id = f"msg-{i}"
393
+ st.markdown(f'<div class="container"><div class="assistant-msg">HAL: <span id="{msg_id}">{msg["content"]}</span> <button class="speak-button" onclick="speakText(document.getElementById(\'{msg_id}\').textContent)">πŸ”Š</button></div></div>', unsafe_allow_html=True)
394
+
395
+ # Process user input
396
+ if user_input or speech_result:
397
+ # Prioritize speech result if available
398
+ query = speech_result if speech_result else user_input
399
+
400
+ # Get NASA data if applicable
401
+ nasa_data = get_nasa_data(query)
402
+
403
+ # Generate response
404
+ system_message = "You are HAL, an AI assistant specialized in NASA and space knowledge. Provide concise, factual responses."
405
+ response, st.session_state.chat_history = get_response(system_message, st.session_state.chat_history, query)
406
+
407
+ # Display NASA image if available
408
+ if nasa_data:
409
+ st.image(nasa_data["url"], caption=f"{nasa_data['title']} - {nasa_data['date']}")
410
+ st.write(nasa_data["explanation"])
411
+
412
+ # Force a rerun to update the chat display
413
+ st.rerun()
414
+
415
+ # Add JavaScript to ensure the page scrolls to the bottom on new messages
416
+ st.markdown("""
417
+ <script>
418
+ // Scroll to bottom of page on load
419
+ window.onload = function() {
420
+ window.scrollTo(0, document.body.scrollHeight);
421
+ }
422
+ </script>
423
+ """, unsafe_allow_html=True)