Sanchayt commited on
Commit
f4d88d9
·
1 Parent(s): 30fad71
Files changed (2) hide show
  1. app.py +71 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ from ctransformers import AutoModelForCausalLM
4
+
5
+ # App title
6
+ st.set_page_config(page_title="🦙💬 Llama 2 Chatbot")
7
+
8
+ @st.cache_resource()
9
+ def ChatModel(temperature, top_p):
10
+ return AutoModelForCausalLM.from_pretrained(
11
+ 'ggml-llama-2-7b-chat-q4_0.bin',
12
+ model_type='llama',
13
+ temperature=temperature,
14
+ top_p = top_p)
15
+
16
+ # Replicate Credentials
17
+ with st.sidebar:
18
+ st.title('🦙💬 Llama 2 Chatbot')
19
+
20
+ # Refactored from <https://github.com/a16z-infra/llama2-chatbot>
21
+ st.subheader('Models and parameters')
22
+
23
+ temperature = st.sidebar.slider('temperature', min_value=0.01, max_value=2.0, value=0.1, step=0.01)
24
+ top_p = st.sidebar.slider('top_p', min_value=0.01, max_value=1.0, value=0.9, step=0.01)
25
+ # max_length = st.sidebar.slider('max_length', min_value=64, max_value=4096, value=512, step=8)
26
+ chat_model =ChatModel(temperature, top_p)
27
+ # st.markdown('📖 Learn how to build this app in this [blog](#link-to-blog)!')
28
+
29
+ # Store LLM generated responses
30
+ if "messages" not in st.session_state.keys():
31
+ st.session_state.messages = [{"role": "assistant", "content": "How may I assist you today?"}]
32
+
33
+ # Display or clear chat messages
34
+ for message in st.session_state.messages:
35
+ with st.chat_message(message["role"]):
36
+ st.write(message["content"])
37
+
38
+ def clear_chat_history():
39
+ st.session_state.messages = [{"role": "assistant", "content": "How may I assist you today?"}]
40
+ st.sidebar.button('Clear Chat History', on_click=clear_chat_history)
41
+
42
+ # Function for generating LLaMA2 response
43
+ def generate_llama2_response(prompt_input):
44
+ string_dialogue = "You are a helpful assistant. You do not respond as 'User' or pretend to be 'User'. You only respond once as 'Assistant'."
45
+ for dict_message in st.session_state.messages:
46
+ if dict_message["role"] == "user":
47
+ string_dialogue += "User: " + dict_message["content"] + "\\n\\n"
48
+ else:
49
+ string_dialogue += "Assistant: " + dict_message["content"] + "\\n\\n"
50
+ output = chat_model(f"prompt {string_dialogue} {prompt_input} Assistant: ")
51
+ return output
52
+
53
+ # User-provided prompt
54
+ if prompt := st.chat_input():
55
+ st.session_state.messages.append({"role": "user", "content": prompt})
56
+ with st.chat_message("user"):
57
+ st.write(prompt)
58
+
59
+ # Generate a new response if last message is not from assistant
60
+ if st.session_state.messages[-1]["role"] != "assistant":
61
+ with st.chat_message("assistant"):
62
+ with st.spinner("Thinking..."):
63
+ response = generate_llama2_response(prompt)
64
+ placeholder = st.empty()
65
+ full_response = ''
66
+ for item in response:
67
+ full_response += item
68
+ placeholder.markdown(full_response)
69
+ placeholder.markdown(full_response)
70
+ message = {"role": "assistant", "content": full_response}
71
+ st.session_state.messages.append(message)
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ ctransformers
2
+ streamlit