Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,10 @@ import os
|
|
| 3 |
from streamlit_extras.stylable_container import stylable_container
|
| 4 |
from PIL import Image
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Variables used Globally
|
| 8 |
path = "/data" #preset path for hugging face spaces for persistent storage and cannot be changed
|
|
@@ -122,9 +126,172 @@ with recycle_column:
|
|
| 122 |
st.button("Recycle", key='recycle')
|
| 123 |
|
| 124 |
# Main app goes below here -
|
| 125 |
-
|
| 126 |
|
| 127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
|
| 130 |
|
|
|
|
| 3 |
from streamlit_extras.stylable_container import stylable_container
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
+
from langchain_huggingface import HuggingFaceEndpoint
|
| 7 |
+
from langchain_core.prompts import PromptTemplate
|
| 8 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 9 |
+
|
| 10 |
|
| 11 |
# Variables used Globally
|
| 12 |
path = "/data" #preset path for hugging face spaces for persistent storage and cannot be changed
|
|
|
|
| 126 |
st.button("Recycle", key='recycle')
|
| 127 |
|
| 128 |
# Main app goes below here -
|
|
|
|
| 129 |
|
| 130 |
|
| 131 |
+
model_id="mistralai/Mistral-7B-Instruct-v0.3"
|
| 132 |
+
|
| 133 |
+
def get_llm_hf_inference(model_id=model_id, max_new_tokens=128, temperature=0.1):
|
| 134 |
+
"""
|
| 135 |
+
Returns a language model for HuggingFace inference.
|
| 136 |
+
|
| 137 |
+
Parameters:
|
| 138 |
+
- model_id (str): The ID of the HuggingFace model repository.
|
| 139 |
+
- max_new_tokens (int): The maximum number of new tokens to generate.
|
| 140 |
+
- temperature (float): The temperature for sampling from the model.
|
| 141 |
+
|
| 142 |
+
Returns:
|
| 143 |
+
- llm (HuggingFaceEndpoint): The language model for HuggingFace inference.
|
| 144 |
+
"""
|
| 145 |
+
llm = HuggingFaceEndpoint(
|
| 146 |
+
repo_id=model_id,
|
| 147 |
+
max_new_tokens=max_new_tokens,
|
| 148 |
+
temperature=temperature,
|
| 149 |
+
token = os.getenv("HF_TOKEN")
|
| 150 |
+
)
|
| 151 |
+
return llm
|
| 152 |
+
|
| 153 |
+
# Configure the Streamlit app
|
| 154 |
+
st.set_page_config(page_title="HuggingFace ChatBot", page_icon="π€")
|
| 155 |
+
st.title("Personal HuggingFace ChatBot")
|
| 156 |
+
st.markdown(f"*This is a simple chatbot that uses the HuggingFace transformers library to generate responses to your text input. It uses the {model_id}.*")
|
| 157 |
+
|
| 158 |
+
# Initialize session state for avatars
|
| 159 |
+
if "avatars" not in st.session_state:
|
| 160 |
+
st.session_state.avatars = {'user': None, 'assistant': None}
|
| 161 |
+
|
| 162 |
+
# Initialize session state for user text input
|
| 163 |
+
if 'user_text' not in st.session_state:
|
| 164 |
+
st.session_state.user_text = None
|
| 165 |
+
|
| 166 |
+
# Initialize session state for model parameters
|
| 167 |
+
if "max_response_length" not in st.session_state:
|
| 168 |
+
st.session_state.max_response_length = 256
|
| 169 |
+
|
| 170 |
+
if "system_message" not in st.session_state:
|
| 171 |
+
st.session_state.system_message = "friendly AI conversing with a human user"
|
| 172 |
+
|
| 173 |
+
if "starter_message" not in st.session_state:
|
| 174 |
+
st.session_state.starter_message = "Hello, there! How can I help you today?"
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
# Sidebar for settings
|
| 178 |
+
with st.sidebar:
|
| 179 |
+
st.header("System Settings")
|
| 180 |
+
|
| 181 |
+
# AI Settings
|
| 182 |
+
st.session_state.system_message = st.text_area(
|
| 183 |
+
"System Message", value="You are a friendly AI conversing with a human user."
|
| 184 |
+
)
|
| 185 |
+
st.session_state.starter_message = st.text_area(
|
| 186 |
+
'First AI Message', value="Hello, there! How can I help you today?"
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
+
# Model Settings
|
| 190 |
+
st.session_state.max_response_length = st.number_input(
|
| 191 |
+
"Max Response Length", value=128
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
# Avatar Selection
|
| 195 |
+
st.markdown("*Select Avatars:*")
|
| 196 |
+
col1, col2 = st.columns(2)
|
| 197 |
+
with col1:
|
| 198 |
+
st.session_state.avatars['assistant'] = st.selectbox(
|
| 199 |
+
"AI Avatar", options=["π€", "π¬", "π€"], index=0
|
| 200 |
+
)
|
| 201 |
+
with col2:
|
| 202 |
+
st.session_state.avatars['user'] = st.selectbox(
|
| 203 |
+
"User Avatar", options=["π€", "π±ββοΈ", "π¨πΎ", "π©", "π§πΎ"], index=0
|
| 204 |
+
)
|
| 205 |
+
# Reset Chat History
|
| 206 |
+
reset_history = st.button("Reset Chat History")
|
| 207 |
+
|
| 208 |
+
# Initialize or reset chat history
|
| 209 |
+
if "chat_history" not in st.session_state or reset_history:
|
| 210 |
+
st.session_state.chat_history = [{"role": "assistant", "content": st.session_state.starter_message}]
|
| 211 |
+
|
| 212 |
+
def get_response(system_message, chat_history, user_text,
|
| 213 |
+
eos_token_id=['User'], max_new_tokens=256, get_llm_hf_kws={}):
|
| 214 |
+
"""
|
| 215 |
+
Generates a response from the chatbot model.
|
| 216 |
+
|
| 217 |
+
Args:
|
| 218 |
+
system_message (str): The system message for the conversation.
|
| 219 |
+
chat_history (list): The list of previous chat messages.
|
| 220 |
+
user_text (str): The user's input text.
|
| 221 |
+
model_id (str, optional): The ID of the HuggingFace model to use.
|
| 222 |
+
eos_token_id (list, optional): The list of end-of-sentence token IDs.
|
| 223 |
+
max_new_tokens (int, optional): The maximum number of new tokens to generate.
|
| 224 |
+
get_llm_hf_kws (dict, optional): Additional keyword arguments for the get_llm_hf function.
|
| 225 |
+
|
| 226 |
+
Returns:
|
| 227 |
+
tuple: A tuple containing the generated response and the updated chat history.
|
| 228 |
+
"""
|
| 229 |
+
# Set up the model
|
| 230 |
+
hf = get_llm_hf_inference(max_new_tokens=max_new_tokens, temperature=0.1)
|
| 231 |
+
|
| 232 |
+
# Create the prompt template
|
| 233 |
+
prompt = PromptTemplate.from_template(
|
| 234 |
+
(
|
| 235 |
+
"[INST] {system_message}"
|
| 236 |
+
"\nCurrent Conversation:\n{chat_history}\n\n"
|
| 237 |
+
"\nUser: {user_text}.\n [/INST]"
|
| 238 |
+
"\nAI:"
|
| 239 |
+
)
|
| 240 |
+
)
|
| 241 |
+
# Make the chain and bind the prompt
|
| 242 |
+
chat = prompt | hf.bind(skip_prompt=True) | StrOutputParser(output_key='content')
|
| 243 |
+
|
| 244 |
+
# Generate the response
|
| 245 |
+
response = chat.invoke(input=dict(system_message=system_message, user_text=user_text, chat_history=chat_history))
|
| 246 |
+
response = response.split("AI:")[-1]
|
| 247 |
+
|
| 248 |
+
# Update the chat history
|
| 249 |
+
chat_history.append({'role': 'user', 'content': user_text})
|
| 250 |
+
chat_history.append({'role': 'assistant', 'content': response})
|
| 251 |
+
return response, chat_history
|
| 252 |
+
|
| 253 |
+
# Chat interface
|
| 254 |
+
chat_interface = st.container(border=True)
|
| 255 |
+
with chat_interface:
|
| 256 |
+
output_container = st.container()
|
| 257 |
+
st.session_state.user_text = st.chat_input(placeholder="Enter your text here.")
|
| 258 |
+
|
| 259 |
+
# Display chat messages
|
| 260 |
+
with output_container:
|
| 261 |
+
# For every message in the history
|
| 262 |
+
for message in st.session_state.chat_history:
|
| 263 |
+
# Skip the system message
|
| 264 |
+
if message['role'] == 'system':
|
| 265 |
+
continue
|
| 266 |
+
|
| 267 |
+
# Display the chat message using the correct avatar
|
| 268 |
+
with st.chat_message(message['role'],
|
| 269 |
+
avatar=st.session_state['avatars'][message['role']]):
|
| 270 |
+
st.markdown(message['content'])
|
| 271 |
+
|
| 272 |
+
# When the user enter new text:
|
| 273 |
+
if st.session_state.user_text:
|
| 274 |
+
|
| 275 |
+
# Display the user's new message immediately
|
| 276 |
+
with st.chat_message("user",
|
| 277 |
+
avatar=st.session_state.avatars['user']):
|
| 278 |
+
st.markdown(st.session_state.user_text)
|
| 279 |
+
|
| 280 |
+
# Display a spinner status bar while waiting for the response
|
| 281 |
+
with st.chat_message("assistant",
|
| 282 |
+
avatar=st.session_state.avatars['assistant']):
|
| 283 |
+
|
| 284 |
+
with st.spinner("Thinking..."):
|
| 285 |
+
# Call the Inference API with the system_prompt, user text, and history
|
| 286 |
+
response, st.session_state.chat_history = get_response(
|
| 287 |
+
system_message=st.session_state.system_message,
|
| 288 |
+
user_text=st.session_state.user_text,
|
| 289 |
+
chat_history=st.session_state.chat_history,
|
| 290 |
+
max_new_tokens=st.session_state.max_response_length,
|
| 291 |
+
)
|
| 292 |
+
st.markdown(response)
|
| 293 |
+
T
|
| 294 |
+
|
| 295 |
|
| 296 |
|
| 297 |
|