Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release-1.0 #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "Python 3",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/python:1-3.11-bullseye",
"customizations": {
"codespaces": {
"openFiles": [
"README.md",
"1_Gemini_Pro.py"
]
},
"vscode": {
"settings": {},
"extensions": [
"ms-python.python",
"ms-python.vscode-pylance"
]
}
},
"updateContentCommand": "[ -f packages.txt ] && sudo apt update && sudo apt upgrade -y && sudo xargs apt install -y <packages.txt; [ -f requirements.txt ] && pip3 install --user -r requirements.txt; pip3 install --user streamlit; echo '✅ Packages installed and Requirements met'",
"postAttachCommand": {
"server": "streamlit run 1_Gemini_Pro.py --server.enableCORS false --server.enableXsrfProtection false --ui.hideSidebarNav true"
},
"portsAttributes": {
"8501": {
"label": "Application",
"onAutoForward": "openPreview"
}
},
"forwardPorts": [
8501
]
}
121 changes: 73 additions & 48 deletions 1_Gemini_Pro.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@


import requests
import re
import google.generativeai as genai
import streamlit as st
import time
Expand All @@ -8,68 +9,92 @@


st.set_page_config(
page_title="Chat To XYthing",
page_title="Chat To ZenBot",
page_icon="🔥",
menu_items={
'About': "# Make By hiliuxg"
}
initial_sidebar_state="collapsed"
)

st.title("Chat To XYthing")
st.caption("a chatbot, powered by google gemini pro.")
st.title("Chat To ZenBot")
st.caption("a chatbot, based on zendesk data.")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2NOACyFq1zy1Ape-wwQXQYaCMFSzdxQyherMGSyqQrVcD1-MgbbDvkOq9c9oUPrINoSagHMKQIJfH2YyLdzlZ-NyC8IrRY26FHCIBjdNmKrPT55-ulHOYFRgpeRqDPvOv_TwbHe1sn5jutBnkSzxla6yn8zuGEqRdipK4RVg0Nfbit0dRnGfewXxu1zUUk4g",
"Content-Type": "application/json",
}

if "app_key" not in st.session_state:
app_key = st.text_input("Your Gemini App Key", type='password')
app_key = st.secrets["gemini_key"]
if app_key:
st.session_state.app_key = app_key

if "history" not in st.session_state:
st.session_state.history = []

try:
genai.configure(api_key = st.session_state.app_key)
except AttributeError as e:
st.warning("Please Put Your Gemini App Key First.")

model = genai.GenerativeModel('gemini-pro')
chat = model.start_chat(history = st.session_state.history)


with st.sidebar:
if st.button("Clear Chat Window", use_container_width = True, type="primary"):
st.session_state.history = []
st.session_state.messages = []
st.rerun()


# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []

# Display chat messages from history on app rerun
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])

# React to user input
if prompt := st.chat_input("Ask something"):
# Display user message in chat message container
st.chat_message("user").markdown(prompt)
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})

casualFlag = 0
if ( prompt.lower()=="hi" or prompt.lower()=="hello" or prompt.lower()=="greetings" or prompt.lower()=="yes" or prompt.lower()=="no" or prompt.lower()=="ok" or prompt.lower()=="okay" or prompt.lower()=="thanks" or prompt.lower()=="thank you" or prompt.lower()=="bye" ) :
casualFlag = 1

for message in chat.history:
role = "assistant" if message.role == "model" else message.role
with st.chat_message(role):
st.markdown(message.parts[0].text)
epoch_time = int(time.time())
data = {
"v": 2,
"chatSession": {
"type": "join",
"chatId": "cha_1oNtB90HT9T6bW8imTNyp"
},
"history": [
{
"author": "uplaceholder",
"msg": prompt,
"time": epoch_time
}
]
}
response = requests.post('https://chat-pr4yueoqha-ue.a.run.app/', headers=headers, json=data)
content = "Couldn't Connect to Cloud"

if "app_key" in st.session_state:
if prompt := st.chat_input(""):
prompt = prompt.replace('\n', ' \n')
with st.chat_message("user"):
st.markdown(prompt)
if response.status_code == 200:
content = response.content.decode("utf-8")
print(content)

if content != "":
content = content.split("\n",1)[1]

x = re.findall(icon_pattern, content)
content = icon_pattern.sub("", content)

#if len(x) == 0 and casualFlag == 0 :
#content = "I apologize, but I cannot provide an answer to your question as the information provided in my knowledgebase is not enough."

with st.chat_message("assistant"):
message_placeholder = st.empty()
message_placeholder.markdown("Thinking...")
try:
full_response = ""
for chunk in chat.send_message(prompt, stream=True, safety_settings = SAFETY_SETTTINGS):
word_count = 0
random_int = random.randint(5, 10)
for word in chunk.text:
full_response += word
word_count += 1
if word_count == random_int:
time.sleep(0.05)
message_placeholder.markdown(full_response + "_")
word_count = 0
random_int = random.randint(5, 10)
message_placeholder.markdown(full_response)
except genai.types.generation_types.BlockedPromptException as e:
st.exception(e)
except Exception as e:
st.exception(e)
st.session_state.history = chat.history
response = content
# Display assistant response in chat message container
with st.chat_message("assistant"):
st.markdown(response)
# Add assistant response to chat history
st.session_state.messages.append({"role": "assistant", "content": response})
10 changes: 5 additions & 5 deletions pages/2_Gemino_Pro_Vision.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
from utils import SAFETY_SETTTINGS

st.set_page_config(
page_title="Chat To XYthing",
page_title="Chat To ZenBot",
page_icon="🔥",
menu_items={
'About': "# Make by hiliuxg"
}
)

st.title('Upload Image And Ask')
st.title('Upload PDF')

if "app_key" not in st.session_state:
app_key = st.text_input("Your Gemini App Key", type='password')
Expand Down Expand Up @@ -61,7 +61,7 @@ def clear_state():

image = None
if "app_key" in st.session_state:
uploaded_file = st.file_uploader("choose a pic...", type=["jpg", "png", "jpeg", "gif"], label_visibility='collapsed', on_change = clear_state)
uploaded_file = st.file_uploader("choose a file...", type=["pdf"], label_visibility='collapsed', on_change = clear_state)
if uploaded_file is not None:
image = Image.open(uploaded_file)
width, height = image.size
Expand All @@ -74,9 +74,9 @@ def clear_state():
st.markdown(item["text"])

if "app_key" in st.session_state:
if prompt := st.chat_input("desc this picture"):
if prompt := st.chat_input(" "):
if image is None:
st.warning("Please upload an image first", icon="⚠️")
st.warning("Please upload first", icon="⚠️")
else:
prompt = prompt.replace('\n', ' \n')
with st.chat_message("user"):
Expand Down