Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -34,8 +34,7 @@ if "follow_up" not in st.session_state:
|
|
| 34 |
st.session_state.follow_up = ""
|
| 35 |
|
| 36 |
# β
Initialize Hugging Face Model (Explicitly Set to CPU/GPU)
|
| 37 |
-
def get_llm_hf_inference(model_id="meta-llama/Llama-2-7b-chat-hf", max_new_tokens=800, temperature=0.
|
| 38 |
-
#mistralai/Mistral-7B-Instruct-v0.3
|
| 39 |
return HuggingFaceEndpoint(
|
| 40 |
repo_id=model_id,
|
| 41 |
max_new_tokens=max_new_tokens,
|
|
@@ -91,7 +90,7 @@ def generate_follow_up(user_text):
|
|
| 91 |
"Ensure it's concise and structured exactly as requested without extra commentary."
|
| 92 |
)
|
| 93 |
|
| 94 |
-
hf = get_llm_hf_inference(max_new_tokens=30, temperature=0.
|
| 95 |
output = hf.invoke(input=prompt_text).strip()
|
| 96 |
|
| 97 |
# β
Extract the relevant part using regex to remove unwanted symbols or truncations
|
|
@@ -109,12 +108,7 @@ def get_response(system_message, user_text, max_new_tokens=800):
|
|
| 109 |
Generates a response from the chatbot, ensures conversation history is updated, and includes a follow-up question.
|
| 110 |
"""
|
| 111 |
|
| 112 |
-
# β
|
| 113 |
-
if "chat_history" not in st.session_state:
|
| 114 |
-
st.session_state.chat_history = [{"role": "assistant", "content": "Hello! How can I assist you today?"}]
|
| 115 |
-
|
| 116 |
-
# β
Get Chat History Reference
|
| 117 |
-
chat_history = st.session_state.chat_history
|
| 118 |
|
| 119 |
# β
Detect Intent (NASA query vs General AI chat)
|
| 120 |
action = predict_action(user_text)
|
|
@@ -129,11 +123,8 @@ def get_response(system_message, user_text, max_new_tokens=800):
|
|
| 129 |
chat_history.append({'role': 'user', 'content': user_text})
|
| 130 |
chat_history.append({'role': 'assistant', 'content': response})
|
| 131 |
chat_history.append({'role': 'assistant', 'content': follow_up})
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
st.session_state.chat_history = chat_history
|
| 135 |
-
|
| 136 |
-
return response, follow_up, chat_history, nasa_url # β
Always return 4 values
|
| 137 |
|
| 138 |
# β
Format Conversation History for Model Input
|
| 139 |
formatted_chat_history = "\n".join(f"{msg['role']}: {msg['content']}" for msg in chat_history)
|
|
@@ -154,58 +145,26 @@ def get_response(system_message, user_text, max_new_tokens=800):
|
|
| 154 |
# β
Generate AI Response
|
| 155 |
chat = prompt | hf.bind(skip_prompt=True) | StrOutputParser(output_key='content')
|
| 156 |
response = chat.invoke(input=dict(system_message=system_message, user_text=user_text, chat_history=formatted_chat_history))
|
| 157 |
-
|
| 158 |
-
# β
Extract and Clean Response
|
| 159 |
response = response.split("HAL:")[-1].strip() if "HAL:" in response else response.strip()
|
| 160 |
|
| 161 |
-
# β
Ensure Response is in English
|
| 162 |
response = ensure_english(response)
|
| 163 |
|
| 164 |
-
# β
Fallback Response Handling
|
| 165 |
if not response:
|
| 166 |
response = "I'm sorry, but I couldn't generate a response. Can you rephrase your question?"
|
| 167 |
|
| 168 |
-
# β
Generate Follow-Up Question
|
| 169 |
follow_up = generate_follow_up(user_text)
|
| 170 |
|
| 171 |
-
# β
Append
|
| 172 |
chat_history.append({'role': 'user', 'content': user_text})
|
| 173 |
chat_history.append({'role': 'assistant', 'content': response})
|
| 174 |
chat_history.append({'role': 'assistant', 'content': follow_up})
|
|
|
|
| 175 |
|
| 176 |
-
|
| 177 |
-
st.session_state.chat_history = chat_history
|
| 178 |
-
|
| 179 |
-
return response, follow_up, chat_history, None # β
Ensure 4 values are returned
|
| 180 |
-
|
| 181 |
|
| 182 |
# β
Streamlit UI
|
| 183 |
st.title("π HAL - NASA AI Assistant")
|
| 184 |
|
| 185 |
-
# β
Justify all chatbot responses
|
| 186 |
-
st.markdown("""
|
| 187 |
-
<style>
|
| 188 |
-
.user-msg, .assistant-msg {
|
| 189 |
-
padding: 10px;
|
| 190 |
-
border-radius: 10px;
|
| 191 |
-
margin-bottom: 5px;
|
| 192 |
-
width: fit-content;
|
| 193 |
-
max-width: 80%;
|
| 194 |
-
text-align: justify;
|
| 195 |
-
}
|
| 196 |
-
.user-msg { background-color: #696969; color: white; }
|
| 197 |
-
.assistant-msg { background-color: #333333; color: white; }
|
| 198 |
-
.container { display: flex; flex-direction: column; align-items: flex-start; }
|
| 199 |
-
@media (max-width: 600px) { .user-msg, .assistant-msg { font-size: 16px; max-width: 100%; } }
|
| 200 |
-
</style>
|
| 201 |
-
""", unsafe_allow_html=True)
|
| 202 |
-
|
| 203 |
-
# β
Reset Chat Button
|
| 204 |
-
if st.sidebar.button("Reset Chat"):
|
| 205 |
-
st.session_state.chat_history = [{"role": "assistant", "content": "Hello! How can I assist you today?"}]
|
| 206 |
-
st.session_state.response_ready = False
|
| 207 |
-
st.session_state.follow_up = ""
|
| 208 |
-
|
| 209 |
# β
Display Chat History
|
| 210 |
st.markdown("<div class='container'>", unsafe_allow_html=True)
|
| 211 |
for message in st.session_state.chat_history:
|
|
@@ -215,16 +174,11 @@ for message in st.session_state.chat_history:
|
|
| 215 |
st.markdown(f"<div class='assistant-msg'><strong>HAL:</strong> {message['content']}</div>", unsafe_allow_html=True)
|
| 216 |
st.markdown("</div>", unsafe_allow_html=True)
|
| 217 |
|
| 218 |
-
|
| 219 |
-
# β
Chat UI
|
| 220 |
user_input = st.chat_input("Type your message here...")
|
| 221 |
|
| 222 |
if user_input:
|
| 223 |
-
response, follow_up,
|
| 224 |
-
system_message="You are a helpful AI assistant.",
|
| 225 |
-
user_text=user_input,
|
| 226 |
-
chat_history=st.session_state.chat_history
|
| 227 |
-
)
|
| 228 |
|
| 229 |
if response:
|
| 230 |
st.markdown(f"<div class='assistant-msg'><strong>HAL:</strong> {response}</div>", unsafe_allow_html=True)
|
|
@@ -235,9 +189,4 @@ if user_input:
|
|
| 235 |
if image_url:
|
| 236 |
st.image(image_url, caption="NASA Image of the Day")
|
| 237 |
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
if st.session_state.response_ready and st.session_state.follow_up:
|
| 241 |
-
st.markdown(f"<div class='assistant-msg'><strong>HAL:</strong> {st.session_state.follow_up}</div>", unsafe_allow_html=True)
|
| 242 |
-
st.session_state.response_ready = False
|
| 243 |
-
|
|
|
|
| 34 |
st.session_state.follow_up = ""
|
| 35 |
|
| 36 |
# β
Initialize Hugging Face Model (Explicitly Set to CPU/GPU)
|
| 37 |
+
def get_llm_hf_inference(model_id="meta-llama/Llama-2-7b-chat-hf", max_new_tokens=800, temperature=0.8):
|
|
|
|
| 38 |
return HuggingFaceEndpoint(
|
| 39 |
repo_id=model_id,
|
| 40 |
max_new_tokens=max_new_tokens,
|
|
|
|
| 90 |
"Ensure it's concise and structured exactly as requested without extra commentary."
|
| 91 |
)
|
| 92 |
|
| 93 |
+
hf = get_llm_hf_inference(max_new_tokens=30, temperature=0.8) # π₯ Lower temp for consistency
|
| 94 |
output = hf.invoke(input=prompt_text).strip()
|
| 95 |
|
| 96 |
# β
Extract the relevant part using regex to remove unwanted symbols or truncations
|
|
|
|
| 108 |
Generates a response from the chatbot, ensures conversation history is updated, and includes a follow-up question.
|
| 109 |
"""
|
| 110 |
|
| 111 |
+
chat_history = st.session_state.chat_history # β
Get Chat History Reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
# β
Detect Intent (NASA query vs General AI chat)
|
| 114 |
action = predict_action(user_text)
|
|
|
|
| 123 |
chat_history.append({'role': 'user', 'content': user_text})
|
| 124 |
chat_history.append({'role': 'assistant', 'content': response})
|
| 125 |
chat_history.append({'role': 'assistant', 'content': follow_up})
|
| 126 |
+
st.session_state.chat_history = chat_history # β
Update Session History
|
| 127 |
+
return response, follow_up, nasa_url
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
# β
Format Conversation History for Model Input
|
| 130 |
formatted_chat_history = "\n".join(f"{msg['role']}: {msg['content']}" for msg in chat_history)
|
|
|
|
| 145 |
# β
Generate AI Response
|
| 146 |
chat = prompt | hf.bind(skip_prompt=True) | StrOutputParser(output_key='content')
|
| 147 |
response = chat.invoke(input=dict(system_message=system_message, user_text=user_text, chat_history=formatted_chat_history))
|
|
|
|
|
|
|
| 148 |
response = response.split("HAL:")[-1].strip() if "HAL:" in response else response.strip()
|
| 149 |
|
|
|
|
| 150 |
response = ensure_english(response)
|
| 151 |
|
|
|
|
| 152 |
if not response:
|
| 153 |
response = "I'm sorry, but I couldn't generate a response. Can you rephrase your question?"
|
| 154 |
|
|
|
|
| 155 |
follow_up = generate_follow_up(user_text)
|
| 156 |
|
| 157 |
+
# β
Append to Chat History
|
| 158 |
chat_history.append({'role': 'user', 'content': user_text})
|
| 159 |
chat_history.append({'role': 'assistant', 'content': response})
|
| 160 |
chat_history.append({'role': 'assistant', 'content': follow_up})
|
| 161 |
+
st.session_state.chat_history = chat_history # β
Persist History
|
| 162 |
|
| 163 |
+
return response, follow_up, None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
# β
Streamlit UI
|
| 166 |
st.title("π HAL - NASA AI Assistant")
|
| 167 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
# β
Display Chat History
|
| 169 |
st.markdown("<div class='container'>", unsafe_allow_html=True)
|
| 170 |
for message in st.session_state.chat_history:
|
|
|
|
| 174 |
st.markdown(f"<div class='assistant-msg'><strong>HAL:</strong> {message['content']}</div>", unsafe_allow_html=True)
|
| 175 |
st.markdown("</div>", unsafe_allow_html=True)
|
| 176 |
|
| 177 |
+
# β
Chat Input
|
|
|
|
| 178 |
user_input = st.chat_input("Type your message here...")
|
| 179 |
|
| 180 |
if user_input:
|
| 181 |
+
response, follow_up, image_url = get_response("You are a helpful AI assistant.", user_input)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
if response:
|
| 184 |
st.markdown(f"<div class='assistant-msg'><strong>HAL:</strong> {response}</div>", unsafe_allow_html=True)
|
|
|
|
| 189 |
if image_url:
|
| 190 |
st.image(image_url, caption="NASA Image of the Day")
|
| 191 |
|
| 192 |
+
st.session_state.response_ready = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|