Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
+
import streamlit as st
|
| 5 |
+
import time
|
| 6 |
+
import random
|
| 7 |
+
from utils import SAFETY_SETTTINGS
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
st.set_page_config(
|
| 11 |
+
page_title="Gemini-Pro Chat",
|
| 12 |
+
page_icon="🔥",
|
| 13 |
+
menu_items={
|
| 14 |
+
'About': "# Forked from https://github.com/hiliuxg/geminichat"
|
| 15 |
+
}
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
st.title("Gemini-Pro Chat")
|
| 19 |
+
st.caption("Chatbot, powered by Google Gemini Pro.")
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
if "app_key" not in st.session_state:
|
| 23 |
+
app_key = st.text_input("Your Gemini App Key", type='password')
|
| 24 |
+
if app_key:
|
| 25 |
+
st.session_state.app_key = app_key
|
| 26 |
+
|
| 27 |
+
if "history" not in st.session_state:
|
| 28 |
+
st.session_state.history = []
|
| 29 |
+
|
| 30 |
+
try:
|
| 31 |
+
genai.configure(api_key = st.session_state.app_key)
|
| 32 |
+
except AttributeError as e:
|
| 33 |
+
st.warning("Please Add Your Gemini App Key.")
|
| 34 |
+
|
| 35 |
+
model = genai.GenerativeModel('gemini-pro')
|
| 36 |
+
chat = model.start_chat(history = st.session_state.history)
|
| 37 |
+
|
| 38 |
+
with st.sidebar:
|
| 39 |
+
if st.button("Clear Chat Window", use_container_width = True, type="primary"):
|
| 40 |
+
st.session_state.history = []
|
| 41 |
+
st.rerun()
|
| 42 |
+
|
| 43 |
+
for message in chat.history:
|
| 44 |
+
role = "assistant" if message.role == "model" else message.role
|
| 45 |
+
with st.chat_message(role):
|
| 46 |
+
st.markdown(message.parts[0].text)
|
| 47 |
+
|
| 48 |
+
if "app_key" in st.session_state:
|
| 49 |
+
if prompt := st.chat_input(""):
|
| 50 |
+
prompt = prompt.replace('\n', ' \n')
|
| 51 |
+
with st.chat_message("user"):
|
| 52 |
+
st.markdown(prompt)
|
| 53 |
+
|
| 54 |
+
with st.chat_message("assistant"):
|
| 55 |
+
message_placeholder = st.empty()
|
| 56 |
+
message_placeholder.markdown("Thinking...")
|
| 57 |
+
try:
|
| 58 |
+
full_response = ""
|
| 59 |
+
for chunk in chat.send_message(prompt, stream=True, safety_settings = SAFETY_SETTTINGS):
|
| 60 |
+
word_count = 0
|
| 61 |
+
random_int = random.randint(5, 10)
|
| 62 |
+
for word in chunk.text:
|
| 63 |
+
full_response += word
|
| 64 |
+
word_count += 1
|
| 65 |
+
if word_count == random_int:
|
| 66 |
+
time.sleep(0.05)
|
| 67 |
+
message_placeholder.markdown(full_response + "_")
|
| 68 |
+
word_count = 0
|
| 69 |
+
random_int = random.randint(5, 10)
|
| 70 |
+
message_placeholder.markdown(full_response)
|
| 71 |
+
except genai.types.generation_types.BlockedPromptException as e:
|
| 72 |
+
st.exception(e)
|
| 73 |
+
except Exception as e:
|
| 74 |
+
st.exception(e)
|
| 75 |
+
st.session_state.history = chat.history
|