Spaces:
Runtime error
Runtime error
Commit
·
beecf3e
1
Parent(s):
ef05528
Update app.py
Browse files
app.py
CHANGED
@@ -1,77 +1,57 @@
|
|
|
|
1 |
import streamlit as st
|
2 |
import google.generativeai as genai
|
3 |
|
4 |
-
# Configure API key
|
5 |
-
genai.configure(api_key="AIzaSyC70u1sN87IkoxOoIj4XCAPw97ae2LZwNM")
|
6 |
|
7 |
-
#
|
8 |
-
|
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 |
-
#
|
35 |
-
|
|
|
36 |
|
37 |
-
#
|
38 |
-
|
39 |
|
40 |
-
#
|
41 |
-
|
|
|
42 |
|
43 |
-
#
|
44 |
-
for message in chat_history:
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
48 |
|
49 |
-
|
50 |
-
chat_container.markdown(f"**{role}:** {text}")
|
51 |
-
|
52 |
-
# Get user input from text box
|
53 |
user_input = st.text_input("You")
|
54 |
|
55 |
-
#
|
56 |
if user_input:
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
59 |
|
60 |
-
|
61 |
-
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
response = model.start_chat({'user_input': user_input})
|
66 |
|
67 |
-
|
68 |
-
|
69 |
|
70 |
-
|
71 |
-
|
72 |
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
75 |
|
76 |
-
|
77 |
-
|
|
|
1 |
+
import os
|
2 |
import streamlit as st
|
3 |
import google.generativeai as genai
|
4 |
|
|
|
|
|
5 |
|
6 |
+
# Title for the application
|
7 |
+
st.title("Chat with Gemini Bot")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
# Set your Google API Key as an environment variable
|
10 |
+
os.environ['GOOGLE_API_KEY'] = "AIzaSyC70u1sN87IkoxOoIj4XCAPw97ae2LZwNM"
|
11 |
+
genai.configure(api_key=os.environ['GOOGLE_API_KEY'])
|
12 |
|
13 |
+
# Create the model
|
14 |
+
model = genai.GenerativeModel('gemini-pro')
|
15 |
|
16 |
+
# Initialize chat history
|
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 |
+
# If user input is not empty
|
31 |
if user_input:
|
32 |
+
# Add user message to chat history
|
33 |
+
st.session_state.chat_history.append({
|
34 |
+
'role': 'user',
|
35 |
+
'content': user_input
|
36 |
+
})
|
37 |
|
38 |
+
# Display user message
|
39 |
+
st.markdown(f"**You:** {user_input}")
|
40 |
|
41 |
+
# Create a message for the Gemini model
|
42 |
+
message = [{'role': 'user', 'content': msg['content']} for msg in st.session_state.chat_history]
|
|
|
43 |
|
44 |
+
# Generate response from the model
|
45 |
+
response = model.generate_content(message)
|
46 |
|
47 |
+
# Extract bot's message from the response
|
48 |
+
bot_message = response['choices'][0]['message']['content']
|
49 |
|
50 |
+
# Add bot message to chat history
|
51 |
+
st.session_state.chat_history.append({
|
52 |
+
'role': 'assistant',
|
53 |
+
'content': bot_message
|
54 |
+
})
|
55 |
|
56 |
+
# Display bot message
|
57 |
+
st.markdown(f"**Gemini Bot:** {bot_message}")
|