-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chatbot.py
65 lines (55 loc) · 1.93 KB
/
Chatbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import streamlit as st
import google.generativeai as genai
# Configure the API key
GOOGLE_API_KEY = "AIzaSyAhxWx3LRfWxHHUZ5Hembo-t9U7f2oEJUg"
genai.configure(api_key=GOOGLE_API_KEY)
# Initialize the Generative Model
model = genai.GenerativeModel('gemini-1.5-flash')
# Function to get response from the model
def get_chatbot_response(user_input):
response = model.generate_content(user_input)
return response.text
# Streamlit interface
st.set_page_config(page_title="Simple ChatBot", layout="centered")
st.title("✨ Simple ChatBot ✨")
st.write("Powered by Google Generative AI")
if "history" not in st.session_state:
st.session_state["history"] = []
# Display chat history
for user_message, bot_message in st.session_state.history:
st.markdown(f"""
<div style="
background-color: #d1d3e0;
border-radius: 15px;
padding: 10px 15px;
margin: 5px 0;
max-width: 70%;
text-align: left;
display: inline-block;
">
<p style="margin: 0; font-size: 16px; line-height: 1.5;"><b>You:</b> {user_message} 😊</p>
</div>
""", unsafe_allow_html=True)
st.markdown(f"""
<div style="
background-color: #e1ffc7;
border-radius: 15px;
padding: 10px 15px;
margin: 5px 0;
max-width: 70%;
text-align: left;
display: inline-block;
">
<p style="margin: 0; font-size: 16px; line-height: 1.5;"><b>Bot:</b> {bot_message} 🤖</p>
</div>
""", unsafe_allow_html=True)
# Form for user input
with st.form(key="chat_form", clear_on_submit=True):
user_input = st.text_input("Type your message here:", max_chars=2000) # Add a label
submit_button = st.form_submit_button("Send")
if submit_button:
if user_input:
response = get_chatbot_response(user_input)
st.session_state.history.append((user_input, response))
else:
st.warning("Please enter a prompt")