Spaces:
Sleeping
Sleeping
File size: 1,917 Bytes
c6312ec 71249b1 86e92a3 c6312ec 86e92a3 c6312ec 86e92a3 c6312ec 86e92a3 | 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 | import os
import streamlit as st
import google.generativeai as genai
# Configure the API key
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
genai.configure(api_key=GOOGLE_API_KEY)
# Initialize the Generative Model
model = genai.GenerativeModel('gemini-2.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"] = []
# Form at the top
with st.form(key="chat_form", clear_on_submit=True):
user_input = st.text_input("", max_chars=2000, placeholder="Type your message here...")
submit_button = st.form_submit_button("Send")
if submit_button and user_input:
response = get_chatbot_response(user_input)
st.session_state.history.append((user_input, response))
# Display chat history AFTER form processing
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) |