Swayambhoo Jain commited on
Commit
ac9e7e2
1 Parent(s): b4c995f

Adding Streamlit App

Browse files
Files changed (1) hide show
  1. app.py +88 -0
app.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import json
3
+ import streamlit as st
4
+ import sys
5
+
6
+ st.set_page_config(page_title="Samba-CoE_v0.1 Chatbot")
7
+
8
+ if 'expert_sampling_details' not in st.session_state.keys():
9
+ st.session_state['temperature'] = 0.7
10
+ st.session_state['top_p'] = 0.1
11
+ st.session_state['max_length'] = 512
12
+ st.session_state['history_buffer_length'] = 3
13
+ st.session_state['top_k'] = 40
14
+
15
+ with st.sidebar:
16
+ st.sidebar.info("Samba-CoE_v0.1 Chatbot")
17
+ with st.sidebar.form("Chatbot Settings"):
18
+ temperature = st.sidebar.slider('temperature', min_value=0.01, max_value=1.0, value=0.7, step=0.01)
19
+ top_p = st.sidebar.slider('top_p', min_value=0.01, max_value=1.0, value=0.1, step=0.01)
20
+ max_length = st.sidebar.slider('max_length', min_value=32, max_value=1024, value=512, step=8)
21
+ submitted = st.form_submit_button("Submit")
22
+
23
+ if submitted:
24
+ st.session_state['max_length'] = max_length
25
+ st.session_state['temperature'] = temperature
26
+ st.session_state['top_p'] = top_p
27
+ st.sidebar.success("Generation parameters:\n" + f'\n * temperature: {st.session_state.temperature}\n' + f'\n * top_p: {st.session_state.top_p}\n' + f'\n * max_tokens: {st.session_state.max_length}\n' + f'\n * skip_special_tokens: True\n'+ f'\n * do_sample: True\n' )
28
+
29
+ def clear_chat_history():
30
+ st.session_state.messages = [{"role": "assistant", "content": "How may I assist you today?"}]
31
+
32
+ st.sidebar.button('Clear Chat History', on_click=clear_chat_history)
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 message
39
+ for message in st.session_state.messages:
40
+ with st.chat_message(message["role"]):
41
+ st.write(message["content"])
42
+
43
+ def generate_response(prompt_input):
44
+ history_buffer_length = st.session_state.history_buffer_length
45
+ message_history = st.session_state.messages[-history_buffer_length:]
46
+ string_dialogue = "<s> [INST] You are a helpful assistant developed by SambaNova Systems as part of its Composition of Expert (CoE) effort. Always assist with care, respect, and truth. Respond with utmost utility yet securely and professionally. Avoid harmful, unethical, prejudiced, or negative content. Ensure replies promote fairness, positivity and an engaging conversation. [/INST] \n"
47
+ for dict_message in message_history:
48
+ if dict_message["role"] == "user":
49
+ string_dialogue += '[INST]' + dict_message["content"] + '[/INST]' + '\n'
50
+ else:
51
+ string_dialogue += dict_message["content"] + "</s>" + '\n'
52
+
53
+ payload = {'prompt': string_dialogue,
54
+ 'max_tokens': st.session_state['max_length'],
55
+ 'n': 1,
56
+ 'do_sample': True,
57
+ 'temperature': st.session_state['temperature'],
58
+ 'top_p': st.session_state['top_p'],
59
+ 'top_k': st.session_state['top_k'],
60
+ 'skip_special_token': True,
61
+ 'repition_penalty': 1.15,
62
+ 'stop_sequences': ['INST', '[INST]', '[/INST]']
63
+ }
64
+
65
+ r = requests.post(st.secrets["backend_url"], json=payload)
66
+ response = r.json()['choices'][0]['text']
67
+
68
+ return response
69
+
70
+ # User-provided prompt
71
+ if prompt := st.chat_input():
72
+ st.session_state.messages.append({"role": "user", "content": prompt})
73
+ with st.chat_message("user"):
74
+ st.write(prompt)
75
+
76
+ # Generate a new response if last message is not from assistant
77
+ if st.session_state.messages[-1]["role"] != "assistant":
78
+ with st.chat_message("assistant"):
79
+ with st.spinner("Thinking..."):
80
+ response = generate_response(prompt)
81
+ placeholder = st.empty()
82
+ full_response = ''
83
+ for item in response:
84
+ full_response += item
85
+ placeholder.markdown(full_response)
86
+
87
+ message = {"role": "assistant", "content": full_response}
88
+ st.session_state.messages.append(message)