Spaces:
Runtime error
Runtime error
Commit
·
74fa8e5
1
Parent(s):
beecf3e
Update app.py
Browse files
app.py
CHANGED
@@ -1,57 +1,45 @@
|
|
1 |
-
import os
|
2 |
import streamlit as st
|
3 |
-
|
4 |
|
|
|
|
|
|
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
genai.configure(api_key=os.environ['GOOGLE_API_KEY'])
|
12 |
|
13 |
-
#
|
14 |
-
|
15 |
|
16 |
-
#
|
17 |
-
if 'chat_history' not in st.session_state:
|
18 |
-
st.session_state.chat_history = []
|
19 |
-
|
20 |
-
# Display chat history
|
21 |
-
for message in st.session_state.chat_history:
|
22 |
-
if message['role'] == 'user':
|
23 |
-
st.markdown(f"**You:** {message['content']}")
|
24 |
-
else:
|
25 |
-
st.markdown(f"**Gemini Bot:** {message['content']}")
|
26 |
-
|
27 |
-
# User input
|
28 |
user_input = st.text_input("You")
|
29 |
|
30 |
-
#
|
31 |
if user_input:
|
|
|
|
|
|
|
32 |
# Add user message to chat history
|
33 |
-
|
34 |
-
'role': 'user',
|
35 |
-
'content': user_input
|
36 |
-
})
|
37 |
|
38 |
-
# Display user message
|
39 |
st.markdown(f"**You:** {user_input}")
|
40 |
|
41 |
-
#
|
42 |
-
|
|
|
43 |
|
44 |
-
#
|
45 |
-
|
46 |
|
47 |
-
#
|
48 |
-
|
49 |
|
50 |
-
#
|
51 |
-
st.
|
52 |
-
'role': 'assistant',
|
53 |
-
'content': bot_message
|
54 |
-
})
|
55 |
|
56 |
-
#
|
57 |
-
st.
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from google.cloud.generativeai import GenerativeModel, Part, Content
|
3 |
|
4 |
+
# Configure API key
|
5 |
+
api_key = "AIzaSyC70u1sN87IkoxOoIj4XCAPw97ae2LZwNM"
|
6 |
+
model_name = "gemini-pro"
|
7 |
|
8 |
+
# Create model object
|
9 |
+
model = GenerativeModel(api_key=api_key, model_name=model_name)
|
10 |
|
11 |
+
# Create chatbot interface
|
12 |
+
st.title("Gemini API Chatbot")
|
|
|
13 |
|
14 |
+
# Get chat history from session state
|
15 |
+
chat_history = st.session_state.get("chat_history", [])
|
16 |
|
17 |
+
# Get user input from text box
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
user_input = st.text_input("You")
|
19 |
|
20 |
+
# Check if user input is not empty
|
21 |
if user_input:
|
22 |
+
# Create user message object
|
23 |
+
user_message = Content(parts=[Part(text=user_input)], role="user")
|
24 |
+
|
25 |
# Add user message to chat history
|
26 |
+
chat_history.append(user_message)
|
|
|
|
|
|
|
27 |
|
28 |
+
# Display user message with markdown
|
29 |
st.markdown(f"**You:** {user_input}")
|
30 |
|
31 |
+
# Get model response with start_chat method
|
32 |
+
with st.spinner("Thinking..."):
|
33 |
+
response = model.generate_content(chat_history)
|
34 |
|
35 |
+
# Get response text from response object
|
36 |
+
response_text = response.contents[-1].parts[0].text
|
37 |
|
38 |
+
# Add response message to chat history
|
39 |
+
chat_history.append(Content(parts=[Part(text=response_text)], role="assistant"))
|
40 |
|
41 |
+
# Display response message with markdown
|
42 |
+
st.markdown(f"**Gemini Bot:** {response_text}")
|
|
|
|
|
|
|
43 |
|
44 |
+
# Update session state with chat history
|
45 |
+
st.session_state["chat_history"] = chat_history
|