Spaces:
Runtime error
Runtime error
Commit
·
49bb563
1
Parent(s):
a974cfa
Update app.py
Browse files
app.py
CHANGED
@@ -1,92 +1,71 @@
|
|
1 |
-
# Import modules
|
2 |
import streamlit as st
|
3 |
import google.generativeai as genai
|
4 |
|
5 |
# Configure API key
|
6 |
genai.configure(api_key="AIzaSyC70u1sN87IkoxOoIj4XCAPw97ae2LZwNM")
|
7 |
|
8 |
-
# Create model
|
9 |
model = genai.GenerativeModel(
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
},
|
15 |
-
safety_settings=[ # Set safety settings
|
16 |
-
{
|
17 |
-
"category": "HARM_CATEGORY_HARASSMENT",
|
18 |
-
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
19 |
},
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
27 |
-
},
|
28 |
-
{
|
29 |
-
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
30 |
-
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
31 |
-
}
|
32 |
-
]
|
33 |
)
|
34 |
|
35 |
-
#
|
36 |
-
st.title("Gemini API Chatbot")
|
37 |
|
38 |
-
# Get chat history from session state
|
39 |
chat_history = st.session_state.get("chat_history", [])
|
40 |
-
|
41 |
-
# Create container for chat messages
|
42 |
chat_container = st.container()
|
43 |
|
44 |
-
# Loop over chat history and display messages
|
45 |
for message in chat_history:
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
chat_container.markdown(f"**{role}:** {text}")
|
52 |
-
|
53 |
-
# Get user input from text box
|
54 |
user_input = st.text_input("You")
|
55 |
|
56 |
-
# Check if user input is not empty
|
57 |
if user_input:
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import google.generativeai as genai
|
3 |
|
4 |
# Configure API key
|
5 |
genai.configure(api_key="AIzaSyC70u1sN87IkoxOoIj4XCAPw97ae2LZwNM")
|
6 |
|
7 |
+
# Create model
|
8 |
model = genai.GenerativeModel(
|
9 |
+
model_name="gemini-pro",
|
10 |
+
generation_config={
|
11 |
+
"temperature": 0.9,
|
12 |
+
"max_output_tokens": 2048
|
|
|
|
|
|
|
|
|
|
|
13 |
},
|
14 |
+
safety_settings=[
|
15 |
+
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
|
16 |
+
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
|
17 |
+
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
|
18 |
+
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}
|
19 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
)
|
21 |
|
22 |
+
# Chatbot interface
|
23 |
+
st.title("Gemini API Chatbot")
|
24 |
|
|
|
25 |
chat_history = st.session_state.get("chat_history", [])
|
|
|
|
|
26 |
chat_container = st.container()
|
27 |
|
|
|
28 |
for message in chat_history:
|
29 |
+
role = message.role
|
30 |
+
text = message.parts[0].text
|
31 |
+
|
32 |
+
chat_container.markdown(f"**{role}:** {text}")
|
33 |
+
|
|
|
|
|
|
|
34 |
user_input = st.text_input("You")
|
35 |
|
|
|
36 |
if user_input:
|
37 |
|
38 |
+
# Create user message
|
39 |
+
user_message = genai.Content(
|
40 |
+
parts=[genai.Part(text=user_input)],
|
41 |
+
role="user"
|
42 |
+
)
|
43 |
+
|
44 |
+
# Add user message to chat history
|
45 |
+
chat_history.append(user_message)
|
46 |
+
|
47 |
+
# Display user message
|
48 |
+
chat_container.markdown(f"**user:** {user_input}")
|
49 |
+
|
50 |
+
# Get model response
|
51 |
+
with st.spinner("Thinking..."):
|
52 |
+
convo = model.start_chat(chat_history)
|
53 |
+
response = convo.last
|
54 |
+
|
55 |
+
# Get response text
|
56 |
+
response_text = response.parts[0].text
|
57 |
+
|
58 |
+
# Create response message
|
59 |
+
response_message = genai.Content(
|
60 |
+
parts=[genai.Part(text=response_text)],
|
61 |
+
role="assistant"
|
62 |
+
)
|
63 |
+
|
64 |
+
# Add response to chat history
|
65 |
+
chat_history.append(response_message)
|
66 |
+
|
67 |
+
# Display response
|
68 |
+
chat_container.markdown(f"**assistant:** {response_text}")
|
69 |
+
|
70 |
+
# Update session state
|
71 |
+
st.session_state["chat_history"] = chat_history
|