Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -63,137 +63,4 @@ 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
|
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() # ✅ Correct method to reset the app in newer Streamlit versions
|
127 |
-
|
128 |
-
|
129 |
-
# Custom Chat Styling
|
130 |
-
st.markdown("""
|
131 |
-
<style>
|
132 |
-
.user-msg {
|
133 |
-
background-color: #0078D7;
|
134 |
-
color: white;
|
135 |
-
padding: 10px;
|
136 |
-
border-radius: 10px;
|
137 |
-
margin-bottom: 5px;
|
138 |
-
width: fit-content;
|
139 |
-
max-width: 80%;
|
140 |
-
}
|
141 |
-
.assistant-msg {
|
142 |
-
background-color: #333333;
|
143 |
-
color: white;
|
144 |
-
padding: 10px;
|
145 |
-
border-radius: 10px;
|
146 |
-
margin-bottom: 5px;
|
147 |
-
width: fit-content;
|
148 |
-
max-width: 80%;
|
149 |
-
}
|
150 |
-
.container {
|
151 |
-
display: flex;
|
152 |
-
flex-direction: column;
|
153 |
-
align-items: flex-start;
|
154 |
-
}
|
155 |
-
@media (max-width: 600px) {
|
156 |
-
.user-msg, .assistant-msg { font-size: 16px; max-width: 100%; }
|
157 |
-
}
|
158 |
-
</style>
|
159 |
-
""", unsafe_allow_html=True)
|
160 |
-
|
161 |
-
# --- Chat History Display (Ensures All Messages Are Visible) ---
|
162 |
-
st.markdown("<div class='container'>", unsafe_allow_html=True)
|
163 |
-
|
164 |
-
for message in st.session_state.chat_history:
|
165 |
-
if message["role"] == "user":
|
166 |
-
st.markdown(f"<div class='user-msg'><strong>You:</strong> {message['content']}</div>", unsafe_allow_html=True)
|
167 |
-
else:
|
168 |
-
st.markdown(f"<div class='assistant-msg'><strong>HAL:</strong> {message['content']}</div>", unsafe_allow_html=True)
|
169 |
-
|
170 |
-
st.markdown("</div>", unsafe_allow_html=True)
|
171 |
-
|
172 |
-
# --- Single Input Box for Both Initial and Follow-Up Messages ---
|
173 |
-
user_input = st.chat_input("Type your message here...") # Uses Enter to submit
|
174 |
-
|
175 |
-
if user_input:
|
176 |
-
# Save user message in chat history
|
177 |
-
st.session_state.chat_history.append({'role': 'user', 'content': user_input})
|
178 |
-
|
179 |
-
# Generate HAL's response
|
180 |
-
response, follow_up, st.session_state.chat_history, image_url = get_response(
|
181 |
-
system_message="You are a helpful AI assistant.",
|
182 |
-
user_text=user_input,
|
183 |
-
chat_history=st.session_state.chat_history
|
184 |
-
)
|
185 |
-
|
186 |
-
st.session_state.chat_history.append({'role': 'assistant', 'content': response})
|
187 |
-
|
188 |
-
st.markdown(f"<div class='assistant-msg'><strong>HAL:</strong> {response}</div>", unsafe_allow_html=True)
|
189 |
-
|
190 |
-
if image_url:
|
191 |
-
st.image(image_url, caption="NASA Image of the Day")
|
192 |
-
|
193 |
-
st.session_state.follow_up = follow_up
|
194 |
-
st.session_state.response_ready = True # Enables follow-up response cycle
|
195 |
-
|
196 |
-
if st.session_state.response_ready and st.session_state.follow_up:
|
197 |
-
st.session_state.chat_history.append({'role': 'assistant', 'content': st.session_state.follow_up})
|
198 |
-
st.markdown(f"<div class='assistant-msg'><strong>HAL:</strong> {st.session_state.follow_up}</div>", unsafe_allow_html=True)
|
199 |
-
st.session_state.response_ready = False
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|