Shanulhaq's picture
Update app.py
b699e1f verified
raw
history blame
No virus
5.19 kB
import streamlit as st
import google.generativeai as genai
#from streamlit_chat import message
import os
# Securely configure API Key (set this in the deployment environment)
#GOOGLE_API_KEY = st.secrets["GOOGLE_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-1.5-flash',
system_instruction=(
"Persona: You are Dr. Assad Siddiqui, a heart specialist. Only provide information related to heart health, symptoms, and advice. "
"Ask users about their heart-related symptoms and provide consultation and guidance based on their input. "
"Always provide brief answers. If the inquiry is not related to heart health, politely say that you can only provide heart-related information. "
"Responses should be in Urdu written in English and in English."
)
)
# Function to get response from the chatbot
def get_chatbot_response(user_input):
response = model.generate_content(user_input)
return response.text.strip()
# Streamlit page configuration
st.set_page_config(
page_title="Heart Health Chatbot",
page_icon="πŸ‘¨β€βš•οΈ",
layout="centered",
initial_sidebar_state="collapsed",
)
# Background image and custom CSS
st.markdown("""
<style>
.stApp {
background-image: url('https://cdn.wallpapersafari.com/29/34/8Ak1Sf.png');
background-size: cover;
background-position: center;
background-attachment: fixed;
}
.chat-bubble {
background-color: #128c7E;
color: white;
padding: 10px;
border-radius: 10px;
max-width: 70%;
}
.user-bubble {
background-color: #075e54;
color: white;
padding: 10px;
border-radius: 10px;
max-width: 70%;
align-self: flex-end;
}
img.avatar {
width: 50px;
height: 50px;
border-radius: 50%;
}
</style>
""", unsafe_allow_html=True)
# Load and display a custom header
def load_header():
st.markdown("""
<div style="padding:10px;text-align:center;color:white;">
<h1>Heart Health Chatbot πŸ«€</h1>
<p>Ask me anything about heart diseases!</p>
</div>
""", unsafe_allow_html=True)
# Initialize session state for chat history
if "history" not in st.session_state:
st.session_state.history = []
user_avatar_url = "https://img.freepik.com/free-photo/sad-cartoon-anatomical-heart_23-2149767987.jpg?t=st=1725263300~exp=1725266900~hmac=3763e175a896a554720d54c6d774dc645dd73078c952913accec719977d50b48&w=740"
bot_avatar_url = "https://img.freepik.com/premium-photo/3d-render-man-doctor-avatar-round-sticker-with-cartoon-character-face-user-id-thumbnail-modern-69_1181551-3160.jpg?w=740"
# Function to display chat history
def display_chat_history():
for chat in st.session_state.history:
if chat["role"] == "user":
st.markdown(f"""
<div style="display: flex; justify-content: flex-end; align-items: center; margin-bottom: 10px;">
<div class="user-bubble">
<p><b>You:</b> {chat['content']}</p>
</div>
<img src="{user_avatar_url}" class="avatar"/>
</div>
""", unsafe_allow_html=True)
else:
st.markdown(f"""
<div style="display: flex; align-items: center; margin-bottom: 10px;">
<img src="{bot_avatar_url}" class="avatar">
<div class="chat-bubble">
<p><b>Bot:</b> {chat['content']}</p>
</div>
</div>
""", unsafe_allow_html=True)
# Main application layout
def main():
load_header()
st.write("") # Add spacing
with st.container():
display_chat_history()
# User input area
with st.form(key="user_input_form", clear_on_submit=True):
user_input = st.text_input(
label="Type your message...",
placeholder="Ask about heart health...",
max_chars=500
)
submit_button = st.form_submit_button(label="Send")
if submit_button and user_input.strip():
with st.spinner("Thinking..."):
bot_response = get_chatbot_response(user_input)
# Update chat history
st.session_state.history.append({"role": "user", "content": user_input})
st.session_state.history.append({"role": "bot", "content": bot_response})
# Refresh chat display
display_chat_history()
# Footer with provided link
st.markdown("""
<p style="text-align:center; color:white; margin-top:50px;">
Check out the <a href="https://live-appointment-chatbot20.zapier.app/" target="_blank" style="color:#34c759;">Live Appointment</a>.
</p>
""", unsafe_allow_html=True)
# Run the app
if __name__ == "__main__":
main()