-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamlit_app.py
More file actions
71 lines (56 loc) · 2.19 KB
/
streamlit_app.py
File metadata and controls
71 lines (56 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import streamlit as st
from random import choice
import vertexai
from vertexai.preview.generative_models import GenerativeModel, SafetySetting, Part
my_api_key = st.secrets["api_key"]
def multiturn_generate_content(prompt):
vertexai.init(api_key=my_api_key)
model = GenerativeModel(
"gemini-1.5-flash-002",
system_instruction=[textsi_1]
)
chat = model.start_chat()
for chunk in chat.send_message(prompt, stream=True):
yield chunk.text
textsi_1 = """You are a friendly fun greeting bot! Someone will say a greeting to you,
you will give them back a fun but greeting with the same theme!
If they say anything except a greeting, you will tell them off in a fun way!"""
generation_config = {
"max_output_tokens": 8192,
"temperature": 1,
"top_p": 0.95,
}
safety_settings = [
SafetySetting(
category=SafetySetting.HarmCategory.HARM_CATEGORY_HATE_SPEECH,
threshold=SafetySetting.HarmBlockThreshold.OFF
),
SafetySetting(
category=SafetySetting.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
threshold=SafetySetting.HarmBlockThreshold.OFF
),
SafetySetting(
category=SafetySetting.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
threshold=SafetySetting.HarmBlockThreshold.OFF
),
SafetySetting(
category=SafetySetting.HarmCategory.HARM_CATEGORY_HARASSMENT,
threshold=SafetySetting.HarmBlockThreshold.OFF
),
]
st.header("Welcome to greeting bot!")
st.write("Give me a greeting and I'll give you one back!")
if "messages" not in st.session_state:
st.session_state['messages'] = []
# load our history!!!!!!!!!!!!!!!!!!!!!
for message in st.session_state['messages']:
with st.chat_message(message["role"]):
st.write(message['content'])
user_input = st.chat_input("What's your favourite greeting?!")
if user_input:
with st.chat_message("user"):
st.write(user_input)
st.session_state['messages'].append({'role':'user', 'content':user_input})
with st.chat_message("assistant"):
response = st.write_stream(multiturn_generate_content(user_input))
st.session_state['messages'].append({'role':'assistant', 'content':response})