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