-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
40 lines (31 loc) · 1.26 KB
/
main.py
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
import streamlit as st
import os
import google.generativeai as genai
# Initialize Gemini-Pro
genai.configure(api_key=st.secrets["GOOGLE_GEMINI_KEY"])
model = genai.GenerativeModel('gemini-pro')
# Gemini uses 'model' for assistant; Streamlit uses 'assistant'
def role_to_streamlit(role):
if role == "model":
return "assistant"
else:
return role
# Add a Gemini Chat history object to Streamlit session state
if "chat" not in st.session_state:
st.session_state.chat = model.start_chat(history = [])
# Display Form Title
st.set_page_config(page_title="AskME")
st.title("AskME: The Knowledge Well")
# Display chat messages from history above current input box
for message in st.session_state.chat.history:
with st.chat_message(role_to_streamlit(message.role)):
st.markdown(message.parts[0].text)
# Accept user's next message, add to context, resubmit context to Gemini
if prompt := st.chat_input("I possess a well of knowledge. What would you like to know?"):
# Display user's last message
st.chat_message("user").markdown(prompt)
# Send user entry to Gemini and read the response
response = st.session_state.chat.send_message(prompt)
# Display last
with st.chat_message("assistant"):
st.markdown(response.text)