syedabdullah32 commited on
Commit
ba3f0c1
Β·
1 Parent(s): d5e6c75

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ 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']
15
+ else:
16
+ replicate_api = st.text_input('Enter Replicate API token:', type='password')
17
+ if not (replicate_api.startswith('r8_') and len(replicate_api)==40):
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, max_value=128, value=120, step=8)
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():
36
+ st.session_state.messages = [{"role": "assistant", "content": "How may I assist you today?"}]
37
+
38
+ # Display or clear chat messages
39
+ for message in st.session_state.messages:
40
+ with st.chat_message(message["role"]):
41
+ st.write(message["content"])
42
+
43
+ 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. Refactored from https://github.com/a16z-infra/llama2-chatbot
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:
51
+ if dict_message["role"] == "user":
52
+ string_dialogue += "User: " + dict_message["content"] + "\n\n"
53
+ else:
54
+ string_dialogue += "Assistant: " + dict_message["content"] + "\n\n"
55
+ output = replicate.run('a16z-infra/llama13b-v2-chat:df7690f1994d94e96ad9d568eac121aecf50684a0b0963b25a41cc40061269e5',
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
59
+
60
+ # User-provided prompt
61
+ if prompt := st.chat_input(disabled=not replicate_api):
62
+ st.session_state.messages.append({"role": "user", "content": prompt})
63
+ with st.chat_message("user"):
64
+ st.write(prompt)
65
+
66
+ # Generate a new response if last message is not from assistant
67
+ if st.session_state.messages[-1]["role"] != "assistant":
68
+ with st.chat_message("assistant"):
69
+ with st.spinner("Thinking..."):
70
+ response = generate_llama2_response(prompt)
71
+ placeholder = st.empty()
72
+ full_response = ''
73
+ for item in response:
74
+ full_response += item
75
+ placeholder.markdown(full_response)
76
+ placeholder.markdown(full_response)
77
+ message = {"role": "assistant", "content": full_response}
78
+ st.session_state.messages.append(message)