Spaces:
Runtime error
Runtime error
Update pages/5_Gemini-Chat.py
Browse files- pages/5_Gemini-Chat.py +4 -9
pages/5_Gemini-Chat.py
CHANGED
|
@@ -8,7 +8,7 @@ import google.generativeai as genai
|
|
| 8 |
from io import BytesIO
|
| 9 |
|
| 10 |
# Set your API key
|
| 11 |
-
api_key = "AIzaSyC70u1sN87IkoxOoIj4XCAPw97ae2LZwNM" # Replace
|
| 12 |
genai.configure(api_key=api_key)
|
| 13 |
|
| 14 |
# Configure the generative AI model
|
|
@@ -42,8 +42,6 @@ if 'chat_history' not in st.session_state:
|
|
| 42 |
st.session_state.chat_history = []
|
| 43 |
if 'file_uploader_key' not in st.session_state:
|
| 44 |
st.session_state.file_uploader_key = str(uuid.uuid4())
|
| 45 |
-
if 'uploaded_files' not in st.session_state: # Initialize uploaded_files in session state
|
| 46 |
-
st.session_state.uploaded_files = []
|
| 47 |
|
| 48 |
st.title("Gemini Chatbot")
|
| 49 |
|
|
@@ -82,7 +80,7 @@ def get_chat_history_str():
|
|
| 82 |
)
|
| 83 |
return chat_history_str
|
| 84 |
|
| 85 |
-
#
|
| 86 |
def send_message():
|
| 87 |
user_input = st.session_state.user_input
|
| 88 |
prompts = []
|
|
@@ -93,8 +91,7 @@ def send_message():
|
|
| 93 |
st.session_state.chat_history.append({"role": "user", "parts": [{"text": user_input}]})
|
| 94 |
prompt_parts.append({"text": user_input})
|
| 95 |
|
| 96 |
-
# Handling uploaded files
|
| 97 |
-
uploaded_files = st.session_state.uploaded_files
|
| 98 |
if uploaded_files:
|
| 99 |
for uploaded_file in uploaded_files:
|
| 100 |
base64_image = get_image_base64(Image.open(uploaded_file))
|
|
@@ -130,13 +127,11 @@ def send_message():
|
|
| 130 |
st.audio(tts_file, format='audio/mp3')
|
| 131 |
|
| 132 |
st.session_state.user_input = ''
|
| 133 |
-
st.session_state.uploaded_files = []
|
| 134 |
-
st.session_state.file_uploader_key = str(uuid.uuid4())
|
| 135 |
display_chat_history()
|
| 136 |
|
| 137 |
# UI components for user input, file uploader, send and clear buttons
|
| 138 |
user_input = st.text_area("Enter your message here:", value="", key="user_input")
|
| 139 |
-
uploaded_files = st.file_uploader("Upload images:", type=["png", "jpg", "jpeg"], accept_multiple_files=True, key="file_uploader_key"
|
| 140 |
send_button = st.button("Send", on_click=send_message)
|
| 141 |
clear_button = st.button("Clear Conversation", on_click=clear_conversation)
|
| 142 |
|
|
|
|
| 8 |
from io import BytesIO
|
| 9 |
|
| 10 |
# Set your API key
|
| 11 |
+
api_key = "AIzaSyC70u1sN87IkoxOoIj4XCAPw97ae2LZwNM" # Replace with your actual API key
|
| 12 |
genai.configure(api_key=api_key)
|
| 13 |
|
| 14 |
# Configure the generative AI model
|
|
|
|
| 42 |
st.session_state.chat_history = []
|
| 43 |
if 'file_uploader_key' not in st.session_state:
|
| 44 |
st.session_state.file_uploader_key = str(uuid.uuid4())
|
|
|
|
|
|
|
| 45 |
|
| 46 |
st.title("Gemini Chatbot")
|
| 47 |
|
|
|
|
| 80 |
)
|
| 81 |
return chat_history_str
|
| 82 |
|
| 83 |
+
# Function to send messages
|
| 84 |
def send_message():
|
| 85 |
user_input = st.session_state.user_input
|
| 86 |
prompts = []
|
|
|
|
| 91 |
st.session_state.chat_history.append({"role": "user", "parts": [{"text": user_input}]})
|
| 92 |
prompt_parts.append({"text": user_input})
|
| 93 |
|
| 94 |
+
# Handling uploaded files directly
|
|
|
|
| 95 |
if uploaded_files:
|
| 96 |
for uploaded_file in uploaded_files:
|
| 97 |
base64_image = get_image_base64(Image.open(uploaded_file))
|
|
|
|
| 127 |
st.audio(tts_file, format='audio/mp3')
|
| 128 |
|
| 129 |
st.session_state.user_input = ''
|
|
|
|
|
|
|
| 130 |
display_chat_history()
|
| 131 |
|
| 132 |
# UI components for user input, file uploader, send and clear buttons
|
| 133 |
user_input = st.text_area("Enter your message here:", value="", key="user_input")
|
| 134 |
+
uploaded_files = st.file_uploader("Upload images:", type=["png", "jpg", "jpeg"], accept_multiple_files=True, key="file_uploader_key")
|
| 135 |
send_button = st.button("Send", on_click=send_message)
|
| 136 |
clear_button = st.button("Clear Conversation", on_click=clear_conversation)
|
| 137 |
|