Ravi theja K commited on
Commit
d12ffe7
1 Parent(s): bf32291

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -109
app.py CHANGED
@@ -3,58 +3,43 @@ import replicate
3
  import os
4
  from transformers import AutoTokenizer
5
 
 
 
 
6
  # App title
7
  st.set_page_config(page_title="Snowflake Arctic")
8
 
9
- def main():
10
- """Execution starts here."""
11
- get_replicate_api_token()
12
- display_sidebar_ui()
13
- init_chat_history()
14
- display_chat_messages()
15
- get_and_process_prompt()
16
-
17
- def get_replicate_api_token():
18
- os.environ['REPLICATE_API_TOKEN'] = st.secrets['REPLICATE_API_TOKEN']
19
-
20
- def display_sidebar_ui():
21
- with st.sidebar:
22
- st.title('Snowflake Arctic_Enu')
23
- st.subheader("Adjust model parameters")
24
- st.slider('temperature', min_value=0.01, max_value=5.0, value=0.3,
25
- step=0.01, key="temperature")
26
- st.slider('top_p', min_value=0.01, max_value=1.0, value=0.9, step=0.01,
27
- key="top_p")
28
-
29
- st.button('Clear chat history', on_click=clear_chat_history)
30
-
31
- st.sidebar.caption('Build your own app powered by Arctic and [enter to win](https://arctic-streamlit-hackathon.devpost.com/) $10k in prizes.')
32
-
33
- st.subheader("About")
34
- st.caption('Built by [Snowflake](https://snowflake.com/) to demonstrate [Snowflake Arctic](https://www.snowflake.com/blog/arctic-open-and-efficient-foundation-language-models-snowflake). App hosted on [Streamlit Community Cloud](https://streamlit.io/cloud). Model hosted by [Replicate](https://replicate.com/snowflake/snowflake-arctic-instruct).')
35
 
36
- # # # Uncomment to show debug info
37
- # st.subheader("Debug")
38
- # st.write(st.session_state)
 
39
 
40
  def clear_chat_history():
41
  st.session_state.messages = [{"role": "assistant", "content": "Hi. I'm Arctic, a new, efficient, intelligent, and truly open language model created by Snowflake AI Research. Ask me anything."}]
42
- st.session_state.chat_aborted = False
43
-
44
- def init_chat_history():
45
- """Create a st.session_state.messages list to store chat messages"""
46
- if "messages" not in st.session_state:
47
- clear_chat_history()
48
- check_safety()
49
-
50
- def display_chat_messages():
51
- # Set assistant icon to Snowflake logo
52
- icons = {"assistant": "./Snowflake_Logomark_blue.svg", "user": "⛷️"}
53
 
54
- # Display the messages
55
- for message in st.session_state.messages:
56
- with st.chat_message(message["role"], avatar=icons[message["role"]]):
57
- st.write(message["content"])
58
 
59
  @st.cache_resource(show_spinner=False)
60
  def get_tokenizer():
@@ -63,63 +48,14 @@ def get_tokenizer():
63
  """
64
  return AutoTokenizer.from_pretrained("huggyllama/llama-7b")
65
 
66
- @st.cache_resource(show_spinner=False)
67
- def get_llamaguard_deployment():
68
- return replicate.deployments.get("snowflake/llamaguard")
69
-
70
- def check_safety(disable=False) -> bool:
71
- if disable:
72
- return True
73
-
74
- # with st.spinner("Checking prompt safety ..."):
75
- deployment = get_llamaguard_deployment()
76
- conversation_history = st.session_state.messages
77
- prediction = deployment.predictions.create(
78
- input = { "prompt": f"[INST] Task: Check if there is unsafe content in 'Agent' messages in conversations according our safety policy with the below categories.\n\n<BEGIN UNSAFE CONTENT CATEGORIES>\nO1: Violence and Hate.\nShould not\n- Help people plan or engage in violence.\n- Encourage people to engage in violence.\n- Express hateful, derogatory, or demeaning sentiments against people based on sensitive personal characteristics like their race, color, religion, national origin, sexual orientation, gender, gender identity, or disability.\n- Encourage discrimination against people based on those characteristics and should not use slurs or create insults based on those characteristics.\nCan\n- Provide information on violence and discrimination.\n- Discuss the topics of hate and violence, and can discuss historical events involving violence.\nO2: Sexual Content.\nShould not\n- Engage in sexually explicit (i.e., erotic) conversations and should not encourage people to engage in sexual activities.\nCan\n- Discuss topics related to sexuality and sexual education.\n- Provide general information regarding sexual activities.\n- Provide guidance on topics related to sexual health.\nO3: Criminal Planning.\nShould not\n- Help people plan or engage in criminal activities like theft, kidnapping, or financial crimes, especially when the person indicates that they may act on the information provided to them.\n- Encourage people to engage in criminal activities or condone criminal behavior.\nCan\n- Help people understand how crimes are committed, provided that the response is not so detailed that it could be used to provide a roadmap for future crimes.\n- Help people understand how systems work that are used to prevent crimes, provided those descriptions do not provide detailed information on how to defeat those systems.\nO4: Guns and Illegal Weapons.\nShould not\n- Assist in the planning or execution of firearm or illegal weapons crimes, especially when the person indicates that they may act on the information provided to them.\n- Encourage people to engage in firearm or illegal weapons crimes or condone such behavior.\nCan\n- Discuss firearms and the arguments for and against firearm ownership.\nO5: Regulated or Controlled Substances.\nShould not\n- Encourage or assist people in the trafficking, consumption, or use of regulated or controlled substances, including but not limited to illegal drugs, tobacco, alcohol, and cannabis.\n- Assist or encourage people to create such substances when it is illegal to do so.\nCan\n- Discuss the use of regulated or controlled substances, and can provide general information on those substances (ex: what their chemical composition is and what their effects are).\n- Provide information on how those substances are manufactured, provided that the information is not so detailed as to provide step-by-step instructions for their creation.\nO6: Self-Harm.\nShould not\n- Encourage people to harm themselves.\n- Romanticize or condone self-harm or suicide.\n- Provide information regarding the methods of suicide or self-harm.\n- Provide any information or advice, other than information regarding appropriate health resources, to people who express or imply that they wish to harm themselves.\nCan\n- Provide general information regarding self-harm and suicide (ex: statistics, the mental health challenges faced by those who take these actions).\nShould\n- Provide information regarding appropriate health resources to people who imply or express the desire to harm themselves.\n<END UNSAFE CONTENT CATEGORIES>\n\n<BEGIN CONVERSATION>\n\n{conversation_history}\n\n<END CONVERSATION>\n\nProvide your safety assessment for Agent in the above conversation:\n- First line must read 'safe' or 'unsafe'.\n- If unsafe, a second line must include a comma-separated list of violated categories. [/INST]" }
79
- )
80
- prediction.wait()
81
- output = prediction.output
82
-
83
- if output is not None and "unsafe" in output:
84
- return False
85
- else:
86
- return True
87
-
88
  def get_num_tokens(prompt):
89
  """Get the number of tokens in a given prompt"""
90
  tokenizer = get_tokenizer()
91
  tokens = tokenizer.tokenize(prompt)
92
  return len(tokens)
93
 
94
- def abort_chat(error_message: str):
95
- """Display an error message requiring the chat to be cleared.
96
- Forces a rerun of the app."""
97
- assert error_message, "Error message must be provided."
98
- error_message = f":red[{error_message}]"
99
- if st.session_state.messages[-1]["role"] != "assistant":
100
- st.session_state.messages.append({"role": "assistant", "content": error_message})
101
- else:
102
- st.session_state.messages[-1]["content"] = error_message
103
- st.session_state.chat_aborted = True
104
- st.rerun()
105
-
106
- def get_and_process_prompt():
107
- """Get the user prompt and process it"""
108
- # Generate a new response if last message is not from assistant
109
- if st.session_state.messages[-1]["role"] != "assistant":
110
- with st.chat_message("assistant", avatar="./Snowflake_Logomark_blue.svg"):
111
- response = generate_arctic_response()
112
- st.write_stream(response)
113
-
114
- if st.session_state.chat_aborted:
115
- st.button('Reset chat', on_click=clear_chat_history, key="clear_chat_history")
116
- st.chat_input(disabled=True)
117
- elif prompt := st.chat_input():
118
- st.session_state.messages.append({"role": "user", "content": prompt})
119
- st.rerun()
120
-
121
  def generate_arctic_response():
122
- """String generator for the Snowflake Arctic response."""
123
  prompt = []
124
  for dict_message in st.session_state.messages:
125
  if dict_message["role"] == "user":
@@ -130,23 +66,30 @@ def generate_arctic_response():
130
  prompt.append("<|im_start|>assistant")
131
  prompt.append("")
132
  prompt_str = "\n".join(prompt)
133
-
134
- num_tokens = get_num_tokens(prompt_str)
135
- max_tokens = 1500
136
-
137
- if num_tokens >= max_tokens:
138
- abort_chat(f"Conversation length too long. Please keep it under {max_tokens} tokens.")
139
 
140
- st.session_state.messages.append({"role": "assistant", "content": ""})
 
 
 
 
141
  for event in replicate.stream("snowflake/snowflake-arctic-instruct",
142
  input={"prompt": prompt_str,
143
  "prompt_template": r"{prompt}",
144
- "temperature": st.session_state.temperature,
145
- "top_p": st.session_state.top_p,
146
  }):
147
- print(str(event), end="")
148
-
149
-
150
-
151
- if __name__ == "__main__":
152
- main()
 
 
 
 
 
 
 
 
 
 
3
  import os
4
  from transformers import AutoTokenizer
5
 
6
+ # Set assistant icon to Snowflake logo
7
+ icons = {"assistant": "./Snowflake_Logomark_blue.svg", "user": "⛷️"}
8
+
9
  # App title
10
  st.set_page_config(page_title="Snowflake Arctic")
11
 
12
+ # Replicate Credentials
13
+ with st.sidebar:
14
+ st.title('Snowflake Arctic')
15
+ if 'REPLICATE_API_TOKEN' in st.secrets:
16
+ replicate_api = st.secrets['REPLICATE_API_TOKEN']
17
+ else:
18
+ replicate_api = st.text_input('Enter Replicate API token:', type='password')
19
+ if not (replicate_api.startswith('r8_') and len(replicate_api)==40):
20
+ st.warning('Please enter your Replicate API token.', icon='⚠️')
21
+ st.markdown("**Don't have an API token?** Head over to [Replicate](https://replicate.com) to sign up for one.")
22
+
23
+ os.environ['REPLICATE_API_TOKEN'] = replicate_api
24
+ st.subheader("Adjust model parameters")
25
+ temperature = st.sidebar.slider('temperature', min_value=0.01, max_value=5.0, value=0.3, step=0.01)
26
+ top_p = st.sidebar.slider('top_p', min_value=0.01, max_value=1.0, value=0.9, step=0.01)
27
+
28
+ # Store LLM-generated responses
29
+ if "messages" not in st.session_state.keys():
30
+ st.session_state.messages = [{"role": "assistant", "content": "Hi. I'm Arctic, a new, efficient, intelligent, and truly open language model created by Snowflake AI Research. Ask me anything."}]
 
 
 
 
 
 
 
31
 
32
+ # Display or clear chat messages
33
+ for message in st.session_state.messages:
34
+ with st.chat_message(message["role"], avatar=icons[message["role"]]):
35
+ st.write(message["content"])
36
 
37
  def clear_chat_history():
38
  st.session_state.messages = [{"role": "assistant", "content": "Hi. I'm Arctic, a new, efficient, intelligent, and truly open language model created by Snowflake AI Research. Ask me anything."}]
 
 
 
 
 
 
 
 
 
 
 
39
 
40
+ st.sidebar.button('Clear chat history', on_click=clear_chat_history)
41
+ st.sidebar.caption('Built by [Snowflake](https://snowflake.com/) to demonstrate [Snowflake Arctic](https://www.snowflake.com/blog/arctic-open-and-efficient-foundation-language-models-snowflake). App hosted on [Streamlit Community Cloud](https://streamlit.io/cloud). Model hosted by [Replicate](https://replicate.com/snowflake/snowflake-arctic-instruct).')
42
+ st.sidebar.caption('Build your own app powered by Arctic and [enter to win](https://arctic-streamlit-hackathon.devpost.com/) $10k in prizes.')
 
43
 
44
  @st.cache_resource(show_spinner=False)
45
  def get_tokenizer():
 
48
  """
49
  return AutoTokenizer.from_pretrained("huggyllama/llama-7b")
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  def get_num_tokens(prompt):
52
  """Get the number of tokens in a given prompt"""
53
  tokenizer = get_tokenizer()
54
  tokens = tokenizer.tokenize(prompt)
55
  return len(tokens)
56
 
57
+ # Function for generating Snowflake Arctic response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  def generate_arctic_response():
 
59
  prompt = []
60
  for dict_message in st.session_state.messages:
61
  if dict_message["role"] == "user":
 
66
  prompt.append("<|im_start|>assistant")
67
  prompt.append("")
68
  prompt_str = "\n".join(prompt)
 
 
 
 
 
 
69
 
70
+ if get_num_tokens(prompt_str) >= 3072:
71
+ st.error("Conversation length too long. Please keep it under 3072 tokens.")
72
+ st.button('Clear chat history', on_click=clear_chat_history, key="clear_chat_history")
73
+ st.stop()
74
+
75
  for event in replicate.stream("snowflake/snowflake-arctic-instruct",
76
  input={"prompt": prompt_str,
77
  "prompt_template": r"{prompt}",
78
+ "temperature": temperature,
79
+ "top_p": top_p,
80
  }):
81
+ yield str(event)
82
+
83
+ # User-provided prompt
84
+ if prompt := st.chat_input(disabled=not replicate_api):
85
+ st.session_state.messages.append({"role": "user", "content": prompt})
86
+ with st.chat_message("user", avatar="⛷️"):
87
+ st.write(prompt)
88
+
89
+ # Generate a new response if last message is not from assistant
90
+ if st.session_state.messages[-1]["role"] != "assistant":
91
+ with st.chat_message("assistant", avatar="./Snowflake_Logomark_blue.svg"):
92
+ response = generate_arctic_response()
93
+ full_response = st.write_stream(response)
94
+ message = {"role": "assistant", "content": full_response}
95
+ st.session_state.messages.append(message)