Upload 5 files
Browse files- .gitignore +19 -0
- 1_Gemini_Pro.py +75 -0
- README.md +17 -13
- requirements.txt +2 -0
- utils.py +30 -0
.gitignore
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.venv
|
| 2 |
+
.pytest_cache
|
| 3 |
+
.mypy_cache
|
| 4 |
+
.coverage
|
| 5 |
+
.coverage.*
|
| 6 |
+
.cache
|
| 7 |
+
.cache.*
|
| 8 |
+
.pytest_cache
|
| 9 |
+
.pytest_cache.*
|
| 10 |
+
.hypothesis
|
| 11 |
+
.hypothesis.*
|
| 12 |
+
.hypothesis_cache
|
| 13 |
+
.hypothesis_cache.*
|
| 14 |
+
.hypothesis_profile
|
| 15 |
+
.hypothesis_profile.*
|
| 16 |
+
.hypothesis_display
|
| 17 |
+
.hypothesis_display.*
|
| 18 |
+
.streamlit/secrets.toml
|
| 19 |
+
__pycache__
|
1_Gemini_Pro.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
|
README.md
CHANGED
|
@@ -1,13 +1,17 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
## Gemini ChatBot
|
| 3 |
+
Chatbot powered by Google Gemini-Pro, Gemini-Pro-Vision, and UI.
|
| 4 |
+
|
| 5 |
+
https://geminix.streamlit.app/
|
| 6 |
+
|
| 7 |
+
## Running Locally
|
| 8 |
+
```python
|
| 9 |
+
pip install -r requirements.txt
|
| 10 |
+
streamlit run 1_Gemini_Pro.py
|
| 11 |
+
```
|
| 12 |
+
Get your API KEY from https://makersuite.google.com
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
# Credits:
|
| 16 |
+
|
| 17 |
+
The repository was forked from: https://github.com/hiliuxg/geminichat
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit==1.29.0
|
| 2 |
+
google-generativeai==0.3.1
|
utils.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
"""https://ai.google.dev/api/rest/v1beta/HarmCategory"""
|
| 4 |
+
|
| 5 |
+
SAFETY_SETTTINGS = [
|
| 6 |
+
{
|
| 7 |
+
"category": "HARM_CATEGORY_SEXUAL",
|
| 8 |
+
"threshold": "BLOCK_ONLY_HIGH",
|
| 9 |
+
},
|
| 10 |
+
{
|
| 11 |
+
"category": "HARM_CATEGORY_DANGEROUS",
|
| 12 |
+
"threshold": "BLOCK_ONLY_HIGH",
|
| 13 |
+
},
|
| 14 |
+
{
|
| 15 |
+
"category": "HARM_CATEGORY_HARASSMENT",
|
| 16 |
+
"threshold": "BLOCK_ONLY_HIGH",
|
| 17 |
+
},
|
| 18 |
+
{
|
| 19 |
+
"category": "HARM_CATEGORY_HATE_SPEECH",
|
| 20 |
+
"threshold": "BLOCK_ONLY_HIGH",
|
| 21 |
+
},
|
| 22 |
+
{
|
| 23 |
+
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
| 24 |
+
"threshold": "BLOCK_ONLY_HIGH",
|
| 25 |
+
},
|
| 26 |
+
{
|
| 27 |
+
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
| 28 |
+
"threshold": "BLOCK_ONLY_HIGH",
|
| 29 |
+
},
|
| 30 |
+
]
|