Spaces:
Runtime error
Runtime error
Commit
·
0195449
1
Parent(s):
180afa8
Update app.py
Browse files
app.py
CHANGED
@@ -4,68 +4,74 @@ import google.generativeai as genai
|
|
4 |
# Configure API key
|
5 |
genai.configure(api_key="AIzaSyC70u1sN87IkoxOoIj4XCAPw97ae2LZwNM")
|
6 |
|
7 |
-
# Create model
|
8 |
model = genai.GenerativeModel(
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
13 |
},
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
)
|
21 |
|
22 |
-
#
|
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 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
34 |
user_input = st.text_input("You")
|
35 |
|
|
|
36 |
if user_input:
|
|
|
|
|
37 |
|
38 |
-
|
39 |
-
|
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 |
-
|
48 |
-
|
|
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
convo = model.start_chat(chat_history)
|
53 |
-
response = convo.last
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
# Create response message
|
59 |
-
response_message = genai.Content(
|
60 |
-
parts=[genai.Part(text=response_text)],
|
61 |
-
role="assistant"
|
62 |
-
)
|
63 |
|
64 |
-
|
65 |
-
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
# Update session state
|
71 |
-
st.session_state["chat_history"] = chat_history
|
|
|
4 |
# Configure API key
|
5 |
genai.configure(api_key="AIzaSyC70u1sN87IkoxOoIj4XCAPw97ae2LZwNM")
|
6 |
|
7 |
+
# Create model object
|
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 |
+
{
|
16 |
+
"category": "HARM_CATEGORY_HARASSMENT",
|
17 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
18 |
},
|
19 |
+
{
|
20 |
+
"category": "HARM_CATEGORY_HATE_SPEECH",
|
21 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
22 |
+
},
|
23 |
+
{
|
24 |
+
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
25 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
26 |
+
},
|
27 |
+
{
|
28 |
+
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
29 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
30 |
+
}
|
31 |
+
]
|
32 |
)
|
33 |
|
34 |
+
# Create chatbot interface
|
35 |
st.title("Gemini API Chatbot")
|
36 |
|
37 |
+
# Get chat history from session state
|
38 |
chat_history = st.session_state.get("chat_history", [])
|
39 |
+
|
40 |
+
# Create container for chat messages
|
41 |
chat_container = st.container()
|
42 |
|
43 |
+
# Loop over chat history and display messages
|
44 |
for message in chat_history:
|
45 |
+
# Get message role and text
|
46 |
+
role = message.role
|
47 |
+
text = message.parts[0].text
|
48 |
+
|
49 |
+
# Display message with markdown
|
50 |
+
chat_container.markdown(f"**{role}:** {text}")
|
51 |
+
|
52 |
+
# Get user input from text box
|
53 |
user_input = st.text_input("You")
|
54 |
|
55 |
+
# Check if user input is not empty
|
56 |
if user_input:
|
57 |
+
# Add user message to chat history
|
58 |
+
chat_history.append(user_input)
|
59 |
|
60 |
+
# Display user message with markdown
|
61 |
+
chat_container.markdown(f"**user:** {user_input}")
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
+
# Get model response with start_chat method
|
64 |
+
with st.spinner("Thinking..."):
|
65 |
+
response = model.start_chat(user_input)
|
66 |
|
67 |
+
# Get response text from response object
|
68 |
+
response_text = response.parts[0].text
|
|
|
|
|
69 |
|
70 |
+
# Add response message to chat history
|
71 |
+
chat_history.append(response_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
+
# Display response message with markdown
|
74 |
+
chat_container.markdown(f"**assistant:** {response_text}")
|
75 |
|
76 |
+
# Update session state with chat history
|
77 |
+
st.session_state["chat_history"] = chat_history
|
|
|
|
|
|