Spaces:
Runtime error
Runtime error
Commit
·
0bba5f4
1
Parent(s):
554234c
Update app.py
Browse files
app.py
CHANGED
@@ -33,12 +33,11 @@ safety_settings = [
|
|
33 |
]
|
34 |
|
35 |
# Initialize the chat history and the model instance using session state
|
36 |
-
if
|
37 |
-
|
38 |
-
|
39 |
-
st.session_state.model = genai.GenerativeModel(model_name="gemini-pro",
|
40 |
generation_config=generation_config,
|
41 |
-
safety_settings=safety_settings)
|
42 |
|
43 |
# Create a streamlit interface with a title, a description, and a chat container
|
44 |
st.title("Gemini API") # the title of the interface
|
@@ -46,7 +45,7 @@ st.markdown("A simple interface for Gemini API that explains code snippets in na
|
|
46 |
chat_container = st.empty() # the chat container to display chat messages
|
47 |
|
48 |
# Display chat messages from history on app rerun
|
49 |
-
for message in
|
50 |
role = message["role"]
|
51 |
content = message["content"]
|
52 |
with chat_container.chat_message(role):
|
@@ -58,18 +57,19 @@ user_input, attachment = st.chat_input("Enter a message or a code snippet")
|
|
58 |
if user_input or attachment:
|
59 |
# Add user message to chat history
|
60 |
if user_input:
|
61 |
-
|
62 |
# Display user message in chat container
|
63 |
with chat_container.chat_message("user"):
|
64 |
st.markdown(user_input)
|
65 |
if attachment:
|
66 |
# You can process the attachment here or save it as needed
|
67 |
# For example, you can save it to a temporary folder and store the file path in the chat history
|
|
|
68 |
attachment_path = f"attachments/{attachment.name}"
|
69 |
with open(attachment_path, "wb") as f:
|
70 |
f.write(attachment.read())
|
71 |
# Add attachment message to chat history
|
72 |
-
|
73 |
# Display attachment message in chat container
|
74 |
with chat_container.chat_message("user"):
|
75 |
st.markdown(f"Attachment: {attachment.name}")
|
@@ -78,7 +78,7 @@ if user_input or attachment:
|
|
78 |
with chat_container.chat_message("assistant"):
|
79 |
with st.spinner("Generating a response..."):
|
80 |
# Start a chat session with the model
|
81 |
-
convo =
|
82 |
# Send the user input to the model
|
83 |
convo.send_message(user_input)
|
84 |
# Get the response from the model
|
@@ -86,4 +86,4 @@ if user_input or attachment:
|
|
86 |
# Display the response in the chat container
|
87 |
st.markdown(response)
|
88 |
# Add assistant message to chat history
|
89 |
-
|
|
|
33 |
]
|
34 |
|
35 |
# Initialize the chat history and the model instance using session state
|
36 |
+
# Use the get function to avoid KeyError if the key does not exist
|
37 |
+
chat_history = st.session_state.get("chat_history", [])
|
38 |
+
model = st.session_state.get("model", genai.GenerativeModel(model_name="gemini-pro",
|
|
|
39 |
generation_config=generation_config,
|
40 |
+
safety_settings=safety_settings))
|
41 |
|
42 |
# Create a streamlit interface with a title, a description, and a chat container
|
43 |
st.title("Gemini API") # the title of the interface
|
|
|
45 |
chat_container = st.empty() # the chat container to display chat messages
|
46 |
|
47 |
# Display chat messages from history on app rerun
|
48 |
+
for message in chat_history:
|
49 |
role = message["role"]
|
50 |
content = message["content"]
|
51 |
with chat_container.chat_message(role):
|
|
|
57 |
if user_input or attachment:
|
58 |
# Add user message to chat history
|
59 |
if user_input:
|
60 |
+
chat_history.append({"role": "user", "content": user_input})
|
61 |
# Display user message in chat container
|
62 |
with chat_container.chat_message("user"):
|
63 |
st.markdown(user_input)
|
64 |
if attachment:
|
65 |
# You can process the attachment here or save it as needed
|
66 |
# For example, you can save it to a temporary folder and store the file path in the chat history
|
67 |
+
# Use the attachment.name attribute to get the file name
|
68 |
attachment_path = f"attachments/{attachment.name}"
|
69 |
with open(attachment_path, "wb") as f:
|
70 |
f.write(attachment.read())
|
71 |
# Add attachment message to chat history
|
72 |
+
chat_history.append({"role": "user", "content": f"Attachment: {attachment.name}"})
|
73 |
# Display attachment message in chat container
|
74 |
with chat_container.chat_message("user"):
|
75 |
st.markdown(f"Attachment: {attachment.name}")
|
|
|
78 |
with chat_container.chat_message("assistant"):
|
79 |
with st.spinner("Generating a response..."):
|
80 |
# Start a chat session with the model
|
81 |
+
convo = model.start_chat(history=[])
|
82 |
# Send the user input to the model
|
83 |
convo.send_message(user_input)
|
84 |
# Get the response from the model
|
|
|
86 |
# Display the response in the chat container
|
87 |
st.markdown(response)
|
88 |
# Add assistant message to chat history
|
89 |
+
chat_history.append({"role": "assistant", "content": response})
|