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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import transformers
|
| 3 |
+
import os
|
| 4 |
+
from langchain_huggingface import HuggingFaceEndpoint
|
| 5 |
+
import streamlit as st
|
| 6 |
+
from langchain_core.prompts import PromptTemplate
|
| 7 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 8 |
+
|
| 9 |
+
model_id = "GoToCompany/gemma2-9b-cpt-sahabatai-v1-instruct"
|
| 10 |
+
HF_TOKEN = os.getenv("HF") # Ensure this is set correctly
|
| 11 |
+
|
| 12 |
+
pipeline = transformers.pipeline(
|
| 13 |
+
"text-generation",
|
| 14 |
+
model=model_id,
|
| 15 |
+
model_kwargs={"torch_dtype": torch.bfloat16},
|
| 16 |
+
device_map="auto",
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
def get_llm_hf_inference(model_id=model_id, max_new_tokens=128, temperature=0.1):
|
| 20 |
+
"""
|
| 21 |
+
Returns a language model for HuggingFace inference.
|
| 22 |
+
|
| 23 |
+
Parameters:
|
| 24 |
+
- model_id (str): The ID of the HuggingFace model repository.
|
| 25 |
+
- max_new_tokens (int): The maximum number of new tokens to generate.
|
| 26 |
+
- temperature (float): The temperature for sampling from the model.
|
| 27 |
+
|
| 28 |
+
Returns:
|
| 29 |
+
- llm (HuggingFaceEndpoint): The language model for HuggingFace inference.
|
| 30 |
+
"""
|
| 31 |
+
# Make sure to specify the task
|
| 32 |
+
llm = HuggingFaceEndpoint(
|
| 33 |
+
repo_id=model_id,
|
| 34 |
+
task="text-generation", # Use the correct task for your model
|
| 35 |
+
huggingface_token=os.getenv("HF"), # Pass your API token
|
| 36 |
+
max_new_tokens= max_new_tokens,
|
| 37 |
+
temperature= temperature
|
| 38 |
+
|
| 39 |
+
)
|
| 40 |
+
return llm
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
# Configure the Streamlit app
|
| 44 |
+
st.set_page_config(page_title="HuggingFace ChatBot", page_icon="π€")
|
| 45 |
+
st.title("Personal HuggingFace ChatBot")
|
| 46 |
+
st.markdown(
|
| 47 |
+
f"*This is a simple chatbot that uses the HuggingFace transformers library to generate responses to your text input. It uses the {model_id}.*")
|
| 48 |
+
|
| 49 |
+
# Initialize session state for avatars
|
| 50 |
+
if "avatars" not in st.session_state:
|
| 51 |
+
st.session_state.avatars = {'user': None, 'assistant': None}
|
| 52 |
+
|
| 53 |
+
# Initialize session state for user text input
|
| 54 |
+
if 'user_text' not in st.session_state:
|
| 55 |
+
st.session_state.user_text = None
|
| 56 |
+
|
| 57 |
+
# Initialize session state for model parameters
|
| 58 |
+
if "max_response_length" not in st.session_state:
|
| 59 |
+
st.session_state.max_response_length = 256
|
| 60 |
+
|
| 61 |
+
if "system_message" not in st.session_state:
|
| 62 |
+
st.session_state.system_message = "You are a helpful assistant"
|
| 63 |
+
|
| 64 |
+
if "starter_message" not in st.session_state:
|
| 65 |
+
st.session_state.starter_message = "Hello, there! How can I help you today?"
|
| 66 |
+
|
| 67 |
+
# Sidebar for settings
|
| 68 |
+
with st.sidebar:
|
| 69 |
+
st.header("System Settings")
|
| 70 |
+
|
| 71 |
+
# AI Settings
|
| 72 |
+
st.session_state.system_message = st.text_area(
|
| 73 |
+
"System Message", value="You are a helpful assistant"
|
| 74 |
+
)
|
| 75 |
+
st.session_state.starter_message = st.text_area(
|
| 76 |
+
'First AI Message', value="Hello, there! How can I help you today?"
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
# Model Settings
|
| 80 |
+
st.session_state.max_response_length = st.number_input(
|
| 81 |
+
"Max Response Length", value=128
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
# Avatar Selection
|
| 85 |
+
st.markdown("*Select Avatars:*")
|
| 86 |
+
col1, col2 = st.columns(2)
|
| 87 |
+
with col1:
|
| 88 |
+
st.session_state.avatars['assistant'] = st.selectbox(
|
| 89 |
+
"AI Avatar", options=["π€", "π¬", "π€"], index=0
|
| 90 |
+
)
|
| 91 |
+
with col2:
|
| 92 |
+
st.session_state.avatars['user'] = st.selectbox(
|
| 93 |
+
"User Avatar", options=["π€", "π±ββοΈ", "π¨πΎ", "π©", "π§πΎ"], index=0
|
| 94 |
+
)
|
| 95 |
+
# Reset Chat History
|
| 96 |
+
reset_history = st.button("Reset Chat History")
|
| 97 |
+
|
| 98 |
+
# Initialize or reset chat history
|
| 99 |
+
if "chat_history" not in st.session_state or reset_history:
|
| 100 |
+
st.session_state.chat_history = [{"role": "assistant", "content": st.session_state.starter_message}]
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def get_response(system_message, chat_history, user_text,
|
| 104 |
+
max_new_tokens=256):
|
| 105 |
+
"""
|
| 106 |
+
Generates a response from the chatbot model.
|
| 107 |
+
|
| 108 |
+
Args:
|
| 109 |
+
system_message (str): The system message for the conversation.
|
| 110 |
+
chat_history (list): The list of previous chat messages.
|
| 111 |
+
user_text (str): The user's input text.
|
| 112 |
+
max_new_tokens (int, optional): The maximum number of new tokens to generate.
|
| 113 |
+
|
| 114 |
+
Returns:
|
| 115 |
+
tuple: A tuple containing the generated response and the updated chat history.
|
| 116 |
+
"""
|
| 117 |
+
# Set up the model
|
| 118 |
+
hf = get_llm_hf_inference(max_new_tokens=max_new_tokens, temperature=0.1)
|
| 119 |
+
|
| 120 |
+
# Create the prompt template
|
| 121 |
+
prompt = PromptTemplate.from_template(
|
| 122 |
+
(
|
| 123 |
+
"[INST] {system_message}"
|
| 124 |
+
"\nCurrent Conversation:\n{chat_history}\n\n"
|
| 125 |
+
"\nUser: {user_text}.\n [/INST]"
|
| 126 |
+
"\nAI:"
|
| 127 |
+
)
|
| 128 |
+
)
|
| 129 |
+
# Make the chain and bind the prompt
|
| 130 |
+
chat = prompt | hf.bind(skip_prompt=True) | StrOutputParser(output_key='content')
|
| 131 |
+
|
| 132 |
+
# Generate the response
|
| 133 |
+
response = chat.invoke(input=dict(system_message=system_message, user_text=user_text, chat_history=chat_history))
|
| 134 |
+
response = response.split("AI:")[-1]
|
| 135 |
+
|
| 136 |
+
# Update the chat history
|
| 137 |
+
chat_history.append({'role': 'user', 'content': user_text})
|
| 138 |
+
chat_history.append({'role': 'assistant', 'content': response})
|
| 139 |
+
return response, chat_history
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
# Chat interface
|
| 143 |
+
chat_interface = st.container()
|
| 144 |
+
with chat_interface:
|
| 145 |
+
output_container = st.container()
|
| 146 |
+
st.session_state.user_text = st.chat_input(placeholder="Enter your text here.")
|
| 147 |
+
|
| 148 |
+
# Display chat messages
|
| 149 |
+
with output_container:
|
| 150 |
+
# For every message in the history
|
| 151 |
+
for message in st.session_state.chat_history:
|
| 152 |
+
# Skip the system message
|
| 153 |
+
if message['role'] == 'system':
|
| 154 |
+
continue
|
| 155 |
+
|
| 156 |
+
# Display the chat message using the correct avatar
|
| 157 |
+
with st.chat_message(message['role'],
|
| 158 |
+
avatar=st.session_state['avatars'][message['role']]):
|
| 159 |
+
st.markdown(message['content'])
|
| 160 |
+
|
| 161 |
+
# When the user enters new text:
|
| 162 |
+
if st.session_state.user_text:
|
| 163 |
+
# Display the user's new message immediately
|
| 164 |
+
with st.chat_message("user",
|
| 165 |
+
avatar=st.session_state.avatars['user']):
|
| 166 |
+
st.markdown(st.session_state.user_text)
|
| 167 |
+
|
| 168 |
+
# Display a spinner status bar while waiting for the response
|
| 169 |
+
with st.chat_message("assistant",
|
| 170 |
+
avatar=st.session_state.avatars['assistant']):
|
| 171 |
+
with st.spinner("Thinking..."):
|
| 172 |
+
# Call the Inference API with the system_prompt, user text, and history
|
| 173 |
+
response, st.session_state.chat_history = get_response(
|
| 174 |
+
system_message=st.session_state.system_message,
|
| 175 |
+
user_text=st.session_state.user_text,
|
| 176 |
+
chat_history=st.session_state.chat_history,
|
| 177 |
+
max_new_tokens=st.session_state.max_response_length,
|
| 178 |
+
)
|
| 179 |
+
st.markdown(response)
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
huggingface_hub
|
| 3 |
+
streamlit
|
| 4 |
+
langchain_core
|
| 5 |
+
langchain_community
|
| 6 |
+
langchain_huggingface
|
| 7 |
+
langchain_text_splitters
|
| 8 |
+
accelerate
|
| 9 |
+
watchdog
|
| 10 |
+
tqdm
|