elineve commited on
Commit
eb46588
Β·
verified Β·
1 Parent(s): 64865f3

Upload 4 files

Browse files
.devcontainer/devcontainer.json ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "Python 3",
3
+ // Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
4
+ "image": "mcr.microsoft.com/devcontainers/python:1-3.11-bullseye",
5
+ "customizations": {
6
+ "codespaces": {
7
+ "openFiles": [
8
+ "README.md",
9
+ "streamlit_app.py"
10
+ ]
11
+ },
12
+ "vscode": {
13
+ "settings": {},
14
+ "extensions": [
15
+ "ms-python.python",
16
+ "ms-python.vscode-pylance"
17
+ ]
18
+ }
19
+ },
20
+ "updateContentCommand": "[ -f packages.txt ] && sudo apt update && sudo apt upgrade -y && sudo xargs apt install -y <packages.txt; [ -f requirements.txt ] && pip3 install --user -r requirements.txt; pip3 install --user streamlit; echo 'βœ… Packages installed and Requirements met'",
21
+ "postAttachCommand": {
22
+ "server": "streamlit run streamlit_app.py --server.enableCORS false --server.enableXsrfProtection false"
23
+ },
24
+ "portsAttributes": {
25
+ "8501": {
26
+ "label": "Application",
27
+ "onAutoForward": "openPreview"
28
+ }
29
+ },
30
+ "forwardPorts": [
31
+ 8501
32
+ ]
33
+ }
.streamlit/config.toml ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ [theme]
2
+ primaryColor="#F63366"
3
+ backgroundColor="#FFFFFF"
4
+ secondaryBackgroundColor="#F0F2F6"
5
+ textColor="#262730"
6
+ font="sans serif"
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ streamlit
2
+ replicate
streamlit_app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ language = st.sidebar.selectbox('Language', ['English', 'Spanish'], index=0)
36
+
37
+ st.markdown('πŸ“– Learn how to build this app in this [blog](https://blog.streamlit.io/how-to-build-a-llama-2-chatbot/)!')
38
+ os.environ['REPLICATE_API_TOKEN'] = replicate_api
39
+
40
+ # Store LLM generated responses
41
+ if "messages" not in st.session_state.keys():
42
+ st.session_state.messages = [{"role": "assistant", "content": "How may I assist you today?"}]
43
+
44
+ # Display or clear chat messages
45
+ for message in st.session_state.messages:
46
+ with st.chat_message(message["role"]):
47
+ st.write(message["content"])
48
+
49
+ def clear_chat_history():
50
+ st.session_state.messages = [{"role": "assistant", "content": "How may I assist you today?"}]
51
+ st.sidebar.button('Clear Chat History', on_click=clear_chat_history)
52
+
53
+ # Function for generating LLaMA2 response
54
+ def generate_llama2_response(prompt_input, language='English'):
55
+ string_dialogue = "You are a helpful assistant. You do not respond as 'User' or pretend to be 'User'. You only respond once as 'Assistant'."
56
+ for dict_message in st.session_state.messages:
57
+ if dict_message["role"] == "user":
58
+ string_dialogue += "User: " + dict_message["content"] + "\n\n"
59
+ else:
60
+ string_dialogue += "Assistant: " + dict_message["content"] + "\n\n"
61
+ output = replicate.run(llm,
62
+ input={"prompt": f"{string_dialogue} {prompt_input} Assistant: ",
63
+ "temperature":temperature, "top_p":top_p, "max_length":max_length, "repetition_penalty":1, "language": language})
64
+ return output
65
+
66
+ # User-provided prompt
67
+ if prompt := st.chat_input(disabled=not replicate_api):
68
+ st.session_state.messages.append({"role": "user", "content": prompt})
69
+ with st.chat_message("user"):
70
+ st.write(prompt)
71
+
72
+ # Generate a new response if last message is not from assistant
73
+ if st.session_state.messages[-1]["role"] != "assistant":
74
+ with st.chat_message("assistant"):
75
+ with st.spinner("Thinking..."):
76
+ response = generate_llama2_response(prompt, language='Spanish')
77
+ placeholder = st.empty()
78
+ full_response = ''
79
+ for item in response:
80
+ full_response += item
81
+ placeholder.markdown(full_response)
82
+ placeholder.markdown(full_response)
83
+ message = {"role": "assistant", "content": full_response}
84
+ st.session_state.messages.append(message)