Spaces:
Runtime error
Runtime error
Update pages/10_Gemini-ChatOnly.py
Browse files- pages/10_Gemini-ChatOnly.py +21 -14
pages/10_Gemini-ChatOnly.py
CHANGED
|
@@ -2,6 +2,7 @@ import streamlit as st
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
|
| 4 |
# Set your API key
|
|
|
|
| 5 |
api_key = "AIzaSyC70u1sN87IkoxOoIj4XCAPw97ae2LZwNM" # Replace with your actual API key
|
| 6 |
|
| 7 |
genai.configure(api_key=api_key)
|
|
@@ -12,6 +13,7 @@ generation_config = genai.GenerationConfig(
|
|
| 12 |
max_output_tokens=3000
|
| 13 |
)
|
| 14 |
|
|
|
|
| 15 |
# Initialize session state for chat history
|
| 16 |
if 'chat_history' not in st.session_state:
|
| 17 |
st.session_state['chat_history'] = []
|
|
@@ -28,7 +30,6 @@ def clear_conversation():
|
|
| 28 |
st.session_state['chat_history'] = []
|
| 29 |
|
| 30 |
# Send message function
|
| 31 |
-
# Send message function with error handling
|
| 32 |
def send_message():
|
| 33 |
user_input = st.session_state.user_input
|
| 34 |
if user_input:
|
|
@@ -38,22 +39,28 @@ def send_message():
|
|
| 38 |
|
| 39 |
model = genai.GenerativeModel(
|
| 40 |
model_name='gemini-pro',
|
| 41 |
-
generation_config=generation_config
|
| 42 |
)
|
| 43 |
|
| 44 |
response = model.generate_content([{"role": "user", "parts": [{"text": chat_history_str}]}])
|
| 45 |
-
|
| 46 |
-
# Error handling for empty or blocked responses
|
| 47 |
-
if hasattr(response, 'prompt_feedback') and response.prompt_feedback:
|
| 48 |
-
st.error("The prompt was blocked or not processed correctly. Feedback: " + str(response.prompt_feedback))
|
| 49 |
-
elif not hasattr(response, 'parts') or not response.parts:
|
| 50 |
-
st.error("No response was generated. Please try again or modify your input.")
|
| 51 |
-
else:
|
| 52 |
-
# Assuming response.parts exists and has content
|
| 53 |
-
response_text = response.parts[0].text if hasattr(response.parts[0], "text") else "No response text found."
|
| 54 |
-
if response_text:
|
| 55 |
-
st.session_state['chat_history'].append({"role": "model", "parts":[{"text": response_text}]})
|
| 56 |
|
| 57 |
st.session_state.user_input = ''
|
| 58 |
|
| 59 |
-
display_chat_history()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
|
| 4 |
# Set your API key
|
| 5 |
+
|
| 6 |
api_key = "AIzaSyC70u1sN87IkoxOoIj4XCAPw97ae2LZwNM" # Replace with your actual API key
|
| 7 |
|
| 8 |
genai.configure(api_key=api_key)
|
|
|
|
| 13 |
max_output_tokens=3000
|
| 14 |
)
|
| 15 |
|
| 16 |
+
|
| 17 |
# Initialize session state for chat history
|
| 18 |
if 'chat_history' not in st.session_state:
|
| 19 |
st.session_state['chat_history'] = []
|
|
|
|
| 30 |
st.session_state['chat_history'] = []
|
| 31 |
|
| 32 |
# Send message function
|
|
|
|
| 33 |
def send_message():
|
| 34 |
user_input = st.session_state.user_input
|
| 35 |
if user_input:
|
|
|
|
| 39 |
|
| 40 |
model = genai.GenerativeModel(
|
| 41 |
model_name='gemini-pro',
|
| 42 |
+
generation_config=generation_config
|
| 43 |
)
|
| 44 |
|
| 45 |
response = model.generate_content([{"role": "user", "parts": [{"text": chat_history_str}]}])
|
| 46 |
+
response_text = response.text if hasattr(response, "text") else "No response text found."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
st.session_state.user_input = ''
|
| 49 |
|
| 50 |
+
display_chat_history()
|
| 51 |
+
|
| 52 |
+
# User input text area
|
| 53 |
+
user_input = st.text_area(
|
| 54 |
+
"Enter your message here:",
|
| 55 |
+
value="",
|
| 56 |
+
key="user_input"
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
# Send message button
|
| 60 |
+
send_button = st.button(
|
| 61 |
+
"Send",
|
| 62 |
+
on_click=send_message
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
# Clear conversation button
|
| 66 |
+
clear_button = st.button("Clear Conversation", on_click=clear_conversation)
|