Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ from langchain_core.prompts import PromptTemplate
|
|
| 6 |
from langchain_core.output_parsers import StrOutputParser
|
| 7 |
from langchain_community.document_loaders import YoutubeLoader, WebBaseLoader
|
| 8 |
|
| 9 |
-
model_id="
|
| 10 |
|
| 11 |
model = pipeline(
|
| 12 |
"text-generation",
|
|
@@ -45,209 +45,209 @@ You are provided with a context and a user query. Your task is to answer the use
|
|
| 45 |
"""
|
| 46 |
|
| 47 |
|
| 48 |
-
|
| 49 |
-
def get_llm_hf_inference(model_id=model_id, max_new_tokens=128, temperature=0.1, top_k=50, top_p=0.9):
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
# Configure the Streamlit app
|
| 74 |
-
st.set_page_config(page_title="HuggingFace ChatBot", page_icon="π€")
|
| 75 |
-
st.title("Personal HuggingFace ChatBot")
|
| 76 |
-
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}.*")
|
| 77 |
-
|
| 78 |
-
# Initialize session state for avatars
|
| 79 |
-
if "avatars" not in st.session_state:
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
# Initialize session state for user text input
|
| 83 |
-
if 'user_text' not in st.session_state:
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
# Initialize session state for model parameters
|
| 87 |
-
if "max_response_length" not in st.session_state:
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
if "top_k" not in st.session_state:
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
if "top_p" not in st.session_state:
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
if "temperature" not in st.session_state:
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
if "system_message" not in st.session_state:
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
if "starter_message" not in st.session_state:
|
| 103 |
-
|
| 104 |
|
| 105 |
|
| 106 |
-
# Sidebar for settings
|
| 107 |
-
with st.sidebar:
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
|
| 163 |
-
# Initialize or reset chat history
|
| 164 |
-
if "chat_history" not in st.session_state or reset_history:
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
def get_response(system_message, chat_history, user_text,
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
# Chat interface
|
| 215 |
-
chat_interface = st.container(border=True)
|
| 216 |
-
with chat_interface:
|
| 217 |
-
|
| 218 |
-
|
| 219 |
|
| 220 |
-
# Display chat messages
|
| 221 |
-
with output_container:
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
|
| 233 |
-
|
| 234 |
-
|
| 235 |
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
|
|
|
| 6 |
from langchain_core.output_parsers import StrOutputParser
|
| 7 |
from langchain_community.document_loaders import YoutubeLoader, WebBaseLoader
|
| 8 |
|
| 9 |
+
model_id="unsloth/Llama-3.2-1B-Instruct"
|
| 10 |
|
| 11 |
model = pipeline(
|
| 12 |
"text-generation",
|
|
|
|
| 45 |
"""
|
| 46 |
|
| 47 |
|
| 48 |
+
# model_id="mistralai/Mistral-7B-Instruct-v0.3"
|
| 49 |
+
# def get_llm_hf_inference(model_id=model_id, max_new_tokens=128, temperature=0.1, top_k=50, top_p=0.9):
|
| 50 |
+
# """
|
| 51 |
+
# Returns a language model for HuggingFace inference.
|
| 52 |
+
|
| 53 |
+
# Parameters:
|
| 54 |
+
# - model_id (str): The ID of the HuggingFace model repository.
|
| 55 |
+
# - max_new_tokens (int): The maximum number of new tokens to generate.
|
| 56 |
+
# - temperature (float): The temperature for sampling from the model.
|
| 57 |
+
# - top_k (int): The number of highest probability tokens to consider.
|
| 58 |
+
# - top_p (float): The cumulative probability threshold for token selection.
|
| 59 |
+
|
| 60 |
+
# Returns:
|
| 61 |
+
# - llm (HuggingFaceEndpoint): The language model for HuggingFace inference.
|
| 62 |
+
# """
|
| 63 |
+
# llm = HuggingFaceEndpoint(
|
| 64 |
+
# repo_id=model_id,
|
| 65 |
+
# max_new_tokens=max_new_tokens,
|
| 66 |
+
# temperature=temperature,
|
| 67 |
+
# top_k=top_k,
|
| 68 |
+
# top_p=top_p,
|
| 69 |
+
# token=os.getenv("HF_TOKEN")
|
| 70 |
+
# )
|
| 71 |
+
# return llm
|
| 72 |
+
|
| 73 |
+
# # Configure the Streamlit app
|
| 74 |
+
# st.set_page_config(page_title="HuggingFace ChatBot", page_icon="π€")
|
| 75 |
+
# st.title("Personal HuggingFace ChatBot")
|
| 76 |
+
# 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}.*")
|
| 77 |
+
|
| 78 |
+
# # Initialize session state for avatars
|
| 79 |
+
# if "avatars" not in st.session_state:
|
| 80 |
+
# st.session_state.avatars = {'user': None, 'assistant': None}
|
| 81 |
+
|
| 82 |
+
# # Initialize session state for user text input
|
| 83 |
+
# if 'user_text' not in st.session_state:
|
| 84 |
+
# st.session_state.user_text = None
|
| 85 |
+
|
| 86 |
+
# # Initialize session state for model parameters
|
| 87 |
+
# if "max_response_length" not in st.session_state:
|
| 88 |
+
# st.session_state.max_response_length = 256
|
| 89 |
+
|
| 90 |
+
# if "top_k" not in st.session_state:
|
| 91 |
+
# st.session_state.top_k = 1.0
|
| 92 |
+
|
| 93 |
+
# if "top_p" not in st.session_state:
|
| 94 |
+
# st.session_state.top_p = 0.95
|
| 95 |
+
|
| 96 |
+
# if "temperature" not in st.session_state:
|
| 97 |
+
# st.session_state.temperature = 1.0
|
| 98 |
+
|
| 99 |
+
# if "system_message" not in st.session_state:
|
| 100 |
+
# st.session_state.system_message = "friendly AI conversing with a human user"
|
| 101 |
+
|
| 102 |
+
# if "starter_message" not in st.session_state:
|
| 103 |
+
# st.session_state.starter_message = "Hello, there! How can I help you today?"
|
| 104 |
|
| 105 |
|
| 106 |
+
# # Sidebar for settings
|
| 107 |
+
# with st.sidebar:
|
| 108 |
+
# st.header("System Settings")
|
| 109 |
+
|
| 110 |
+
# # AI Settings
|
| 111 |
+
# st.session_state.system_message = st.text_area(
|
| 112 |
+
# "System Message", value="You are a friendly AI conversing with a human user."
|
| 113 |
+
# )
|
| 114 |
+
# st.session_state.starter_message = st.text_area(
|
| 115 |
+
# 'First AI Message', value="Hello, there! How can I help you today?"
|
| 116 |
+
# )
|
| 117 |
+
|
| 118 |
+
# # Model Settings
|
| 119 |
+
# st.session_state.max_response_length = st.number_input(
|
| 120 |
+
# "Max Response Length", value=128
|
| 121 |
+
# )
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
# selected_option = st.selectbox("Choose parametrs", ("Pricise", "Balanced", "Creative", "Custom"))
|
| 125 |
+
|
| 126 |
+
# if selected_option == "Precise":
|
| 127 |
+
# st.session_state.temperature = 0.1
|
| 128 |
+
# st.session_state.top_k = 10.0
|
| 129 |
+
# st.session_state.top_p = 0.8
|
| 130 |
+
# elif selected_option == "Balanced":
|
| 131 |
+
# st.session_state.temperature = 1.0
|
| 132 |
+
# st.session_state.top_k = 100.0
|
| 133 |
+
# st.session_state.top_p = 0.9
|
| 134 |
+
# elif selected_option == "Creative":
|
| 135 |
+
# st.session_state.temperature = 10.0
|
| 136 |
+
# st.session_state.top_k = 1500.0
|
| 137 |
+
# st.session_state.top_p = 0.99
|
| 138 |
+
# elif selected_option == "Custom":
|
| 139 |
+
# st.session_state.temperature = st.number_input(
|
| 140 |
+
# "Temperature", value=st.session_state.temperature, min_value=0.0
|
| 141 |
+
# )
|
| 142 |
+
# st.session_state.top_k = st.number_input(
|
| 143 |
+
# "Top sK", value=st.session_state.top_k, min_value=1.0, step=1.0
|
| 144 |
+
# )
|
| 145 |
+
# st.session_state.top_p = st.number_input(
|
| 146 |
+
# "Top sP", value=st.session_state.top_p, min_value=0.0, max_value=1.0
|
| 147 |
+
# )
|
| 148 |
|
| 149 |
+
# # Avatar Selection
|
| 150 |
+
# st.markdown("*Select Avatars:*")
|
| 151 |
+
# col1, col2 = st.columns(2)
|
| 152 |
+
# with col1:
|
| 153 |
+
# st.session_state.avatars['assistant'] = st.selectbox(
|
| 154 |
+
# "AI Avatar", options=["π€", "π¬", "π€"], index=0
|
| 155 |
+
# )
|
| 156 |
+
# with col2:
|
| 157 |
+
# st.session_state.avatars['user'] = st.selectbox(
|
| 158 |
+
# "User Avatar", options=["π€", "π±ββοΈ", "π¨πΎ", "π©", "π§πΎ"], index=0
|
| 159 |
+
# )
|
| 160 |
+
# # Reset Chat History
|
| 161 |
+
# reset_history = st.button("Reset Chat History")
|
| 162 |
|
| 163 |
+
# # Initialize or reset chat history
|
| 164 |
+
# if "chat_history" not in st.session_state or reset_history:
|
| 165 |
+
# st.session_state.chat_history = [{"role": "assistant", "content": st.session_state.starter_message}]
|
| 166 |
+
|
| 167 |
+
# def get_response(system_message, chat_history, user_text,
|
| 168 |
+
# eos_token_id=['User'], max_new_tokens=256, get_llm_hf_kws={}):
|
| 169 |
+
# """
|
| 170 |
+
# Generates a response from the chatbot model.
|
| 171 |
+
|
| 172 |
+
# Args:
|
| 173 |
+
# system_message (str): The system message for the conversation.
|
| 174 |
+
# chat_history (list): The list of previous chat messages.
|
| 175 |
+
# user_text (str): The user's input text.
|
| 176 |
+
# eos_token_id (list, optional): The list of end-of-sentence token IDs.
|
| 177 |
+
# max_new_tokens (int, optional): The maximum number of new tokens to generate.
|
| 178 |
+
# get_llm_hf_kws (dict, optional): Additional keyword arguments for the get_llm_hf function.
|
| 179 |
+
|
| 180 |
+
# Returns:
|
| 181 |
+
# tuple: A tuple containing the generated response and the updated chat history.
|
| 182 |
+
# """
|
| 183 |
+
# # Set up the model
|
| 184 |
+
# hf = get_llm_hf_inference(
|
| 185 |
+
# model_id=model_id,
|
| 186 |
+
# max_new_tokens=max_new_tokens,
|
| 187 |
+
# temperature=st.session_state.temperature,
|
| 188 |
+
# top_k=st.session_state.top_k,
|
| 189 |
+
# top_p=st.session_state.top_p
|
| 190 |
+
# )
|
| 191 |
+
|
| 192 |
+
# # Create the prompt template
|
| 193 |
+
# prompt = PromptTemplate.from_template(
|
| 194 |
+
# (
|
| 195 |
+
# "[INST] {system_message}"
|
| 196 |
+
# "\nCurrent Conversation:\n{chat_history}\n\n"
|
| 197 |
+
# "\nUser: {user_text}.\n [/INST]"
|
| 198 |
+
# "\nAI:"
|
| 199 |
+
# )
|
| 200 |
+
# )
|
| 201 |
+
# # Make the chain and bind the prompt
|
| 202 |
+
# chat = prompt | hf.bind(skip_prompt=True) | StrOutputParser(output_key='content')
|
| 203 |
+
|
| 204 |
+
# # Generate the response
|
| 205 |
+
# response = chat.invoke(input=dict(system_message=system_message, user_text=user_text, chat_history=chat_history))
|
| 206 |
+
# response = response.split("AI:")[-1]
|
| 207 |
+
|
| 208 |
+
# # Update the chat history
|
| 209 |
+
# chat_history.append({'role': 'user', 'content': user_text})
|
| 210 |
+
# chat_history.append({'role': 'assistant', 'content': response})
|
| 211 |
+
# return response, chat_history
|
| 212 |
+
|
| 213 |
+
|
| 214 |
+
# # Chat interface
|
| 215 |
+
# chat_interface = st.container(border=True)
|
| 216 |
+
# with chat_interface:
|
| 217 |
+
# output_container = st.container()
|
| 218 |
+
# st.session_state.user_text = st.chat_input(placeholder="Enter your text here.")
|
| 219 |
|
| 220 |
+
# # Display chat messages
|
| 221 |
+
# with output_container:
|
| 222 |
+
# # For every message in the history
|
| 223 |
+
# for message in st.session_state.chat_history:
|
| 224 |
+
# # Skip the system message
|
| 225 |
+
# if message['role'] == 'system':
|
| 226 |
+
# continue
|
| 227 |
|
| 228 |
+
# # Display the chat message using the correct avatar
|
| 229 |
+
# with st.chat_message(message['role'],
|
| 230 |
+
# avatar=st.session_state['avatars'][message['role']]):
|
| 231 |
+
# st.markdown(message['content'])
|
| 232 |
|
| 233 |
+
# # When the user enter new text:
|
| 234 |
+
# if st.session_state.user_text:
|
| 235 |
|
| 236 |
+
# # Display the user's new message immediately
|
| 237 |
+
# with st.chat_message("user",
|
| 238 |
+
# avatar=st.session_state.avatars['user']):
|
| 239 |
+
# st.markdown(st.session_state.user_text)
|
| 240 |
|
| 241 |
+
# # Display a spinner status bar while waiting for the response
|
| 242 |
+
# with st.chat_message("assistant",
|
| 243 |
+
# avatar=st.session_state.avatars['assistant']):
|
| 244 |
+
|
| 245 |
+
# with st.spinner("Thinking..."):
|
| 246 |
+
# # Call the Inference API with the system_prompt, user text, and history
|
| 247 |
+
# response, st.session_state.chat_history = get_response(
|
| 248 |
+
# system_message=st.session_state.system_message,
|
| 249 |
+
# user_text=st.session_state.user_text,
|
| 250 |
+
# chat_history=st.session_state.chat_history,
|
| 251 |
+
# max_new_tokens=st.session_state.max_response_length,
|
| 252 |
+
# )
|
| 253 |
+
# st.markdown(response)
|