Spaces:
Sleeping
Sleeping
Commit
Β·
11a11b6
1
Parent(s):
aa77f68
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,6 @@ st.set_page_config(page_title="π¦π¬ Llama 2 Chatbot")
|
|
| 8 |
# Replicate Credentials
|
| 9 |
with st.sidebar:
|
| 10 |
st.title('π¦π¬ Llama 2 Chatbot')
|
| 11 |
-
st.write('This chatbot is created using the open-source Llama 2 LLM model from Meta.')
|
| 12 |
if 'REPLICATE_API_TOKEN' in st.secrets:
|
| 13 |
st.success('API key already provided!', icon='β
')
|
| 14 |
replicate_api = st.secrets['REPLICATE_API_TOKEN']
|
|
@@ -18,18 +17,23 @@ with st.sidebar:
|
|
| 18 |
st.warning('Please enter your credentials!', icon='β οΈ')
|
| 19 |
else:
|
| 20 |
st.success('Proceed to entering your prompt message!', icon='π')
|
| 21 |
-
os.environ['REPLICATE_API_TOKEN'] = replicate_api
|
| 22 |
|
|
|
|
| 23 |
st.subheader('Models and parameters')
|
| 24 |
-
selected_model = st.sidebar.selectbox('Choose a Llama2 model', ['Llama2-7B', 'Llama2-13B'], key='selected_model')
|
| 25 |
if selected_model == 'Llama2-7B':
|
| 26 |
llm = 'a16z-infra/llama7b-v2-chat:4f0a4744c7295c024a1de15e1a63c880d3da035fa1f49bfd344fe076074c8eea'
|
| 27 |
elif selected_model == 'Llama2-13B':
|
| 28 |
llm = 'a16z-infra/llama13b-v2-chat:df7690f1994d94e96ad9d568eac121aecf50684a0b0963b25a41cc40061269e5'
|
|
|
|
|
|
|
|
|
|
| 29 |
temperature = st.sidebar.slider('temperature', min_value=0.01, max_value=5.0, value=0.1, step=0.01)
|
| 30 |
top_p = st.sidebar.slider('top_p', min_value=0.01, max_value=1.0, value=0.9, step=0.01)
|
| 31 |
-
max_length = st.sidebar.slider('max_length', min_value=
|
|
|
|
| 32 |
st.markdown('π Learn how to build this app in this [blog](https://blog.streamlit.io/how-to-build-a-llama-2-chatbot/)!')
|
|
|
|
| 33 |
|
| 34 |
# Store LLM generated responses
|
| 35 |
if "messages" not in st.session_state.keys():
|
|
@@ -44,7 +48,7 @@ def clear_chat_history():
|
|
| 44 |
st.session_state.messages = [{"role": "assistant", "content": "How may I assist you today?"}]
|
| 45 |
st.sidebar.button('Clear Chat History', on_click=clear_chat_history)
|
| 46 |
|
| 47 |
-
# Function for generating LLaMA2 response
|
| 48 |
def generate_llama2_response(prompt_input):
|
| 49 |
string_dialogue = "You are a helpful assistant. You do not respond as 'User' or pretend to be 'User'. You only respond once as 'Assistant'."
|
| 50 |
for dict_message in st.session_state.messages:
|
|
@@ -52,7 +56,7 @@ def generate_llama2_response(prompt_input):
|
|
| 52 |
string_dialogue += "User: " + dict_message["content"] + "\n\n"
|
| 53 |
else:
|
| 54 |
string_dialogue += "Assistant: " + dict_message["content"] + "\n\n"
|
| 55 |
-
output = replicate.run(
|
| 56 |
input={"prompt": f"{string_dialogue} {prompt_input} Assistant: ",
|
| 57 |
"temperature":temperature, "top_p":top_p, "max_length":max_length, "repetition_penalty":1})
|
| 58 |
return output
|
|
|
|
| 8 |
# Replicate Credentials
|
| 9 |
with st.sidebar:
|
| 10 |
st.title('π¦π¬ Llama 2 Chatbot')
|
|
|
|
| 11 |
if 'REPLICATE_API_TOKEN' in st.secrets:
|
| 12 |
st.success('API key already provided!', icon='β
')
|
| 13 |
replicate_api = st.secrets['REPLICATE_API_TOKEN']
|
|
|
|
| 17 |
st.warning('Please enter your credentials!', icon='β οΈ')
|
| 18 |
else:
|
| 19 |
st.success('Proceed to entering your prompt message!', icon='π')
|
|
|
|
| 20 |
|
| 21 |
+
# Refactored from https://github.com/a16z-infra/llama2-chatbot
|
| 22 |
st.subheader('Models and parameters')
|
| 23 |
+
selected_model = st.sidebar.selectbox('Choose a Llama2 model', ['Llama2-7B', 'Llama2-13B', 'Llama2-70B'], key='selected_model')
|
| 24 |
if selected_model == 'Llama2-7B':
|
| 25 |
llm = 'a16z-infra/llama7b-v2-chat:4f0a4744c7295c024a1de15e1a63c880d3da035fa1f49bfd344fe076074c8eea'
|
| 26 |
elif selected_model == 'Llama2-13B':
|
| 27 |
llm = 'a16z-infra/llama13b-v2-chat:df7690f1994d94e96ad9d568eac121aecf50684a0b0963b25a41cc40061269e5'
|
| 28 |
+
else:
|
| 29 |
+
llm = 'replicate/llama70b-v2-chat:e951f18578850b652510200860fc4ea62b3b16fac280f83ff32282f87bbd2e48'
|
| 30 |
+
|
| 31 |
temperature = st.sidebar.slider('temperature', min_value=0.01, max_value=5.0, value=0.1, step=0.01)
|
| 32 |
top_p = st.sidebar.slider('top_p', min_value=0.01, max_value=1.0, value=0.9, step=0.01)
|
| 33 |
+
max_length = st.sidebar.slider('max_length', min_value=64, max_value=4096, value=512, step=8)
|
| 34 |
+
|
| 35 |
st.markdown('π Learn how to build this app in this [blog](https://blog.streamlit.io/how-to-build-a-llama-2-chatbot/)!')
|
| 36 |
+
os.environ['REPLICATE_API_TOKEN'] = replicate_api
|
| 37 |
|
| 38 |
# Store LLM generated responses
|
| 39 |
if "messages" not in st.session_state.keys():
|
|
|
|
| 48 |
st.session_state.messages = [{"role": "assistant", "content": "How may I assist you today?"}]
|
| 49 |
st.sidebar.button('Clear Chat History', on_click=clear_chat_history)
|
| 50 |
|
| 51 |
+
# Function for generating LLaMA2 response
|
| 52 |
def generate_llama2_response(prompt_input):
|
| 53 |
string_dialogue = "You are a helpful assistant. You do not respond as 'User' or pretend to be 'User'. You only respond once as 'Assistant'."
|
| 54 |
for dict_message in st.session_state.messages:
|
|
|
|
| 56 |
string_dialogue += "User: " + dict_message["content"] + "\n\n"
|
| 57 |
else:
|
| 58 |
string_dialogue += "Assistant: " + dict_message["content"] + "\n\n"
|
| 59 |
+
output = replicate.run(llm,
|
| 60 |
input={"prompt": f"{string_dialogue} {prompt_input} Assistant: ",
|
| 61 |
"temperature":temperature, "top_p":top_p, "max_length":max_length, "repetition_penalty":1})
|
| 62 |
return output
|