typesdigital commited on
Commit
3460474
β€’
1 Parent(s): 0ab79a1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -0
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import replicate
3
+ import os
4
+
5
+ # App title
6
+ st.set_page_config(page_title="πŸ¦™πŸ’¬ Llama 2 Chatbot")
7
+
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']
14
+ else:
15
+ replicate_api = st.text_input('Enter Replicate API token:', type='password')
16
+ if not (replicate_api.startswith('r8_') and len(replicate_api)==40):
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():
40
+ st.session_state.messages = [{"role": "assistant", "content": "How may I assist you today?"}]
41
+
42
+ # Display or clear chat messages
43
+ for message in st.session_state.messages:
44
+ with st.chat_message(message["role"]):
45
+ st.write(message["content"])
46
+
47
+ def clear_chat_history():
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:
55
+ if dict_message["role"] == "user":
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
63
+
64
+ # User-provided prompt
65
+ if prompt := st.chat_input(disabled=not replicate_api):
66
+ st.session_state.messages.append({"role": "user", "content": prompt})
67
+ with st.chat_message("user"):
68
+ st.write(prompt)
69
+
70
+ # Generate a new response if last message is not from assistant
71
+ if st.session_state.messages[-1]["role"] != "assistant":
72
+ with st.chat_message("assistant"):
73
+ with st.spinner("Thinking..."):
74
+ response = generate_llama2_response(prompt)
75
+ placeholder = st.empty()
76
+ full_response = ''
77
+ for item in response:
78
+ full_response += item
79
+ placeholder.markdown(full_response)
80
+ placeholder.markdown(full_response)
81
+ message = {"role": "assistant", "content": full_response}
82
+ st.session_state.messages.append(message)