krishujeniya's picture
Update app.py
362d829 verified
import streamlit as st
from bot import chat
import nltk
# Ensure required NLTK data is downloaded
nltk.download('punkt')
nltk.download('wordnet')
# Streamlit App Configuration
st.set_page_config(
page_title="MedicBot",
page_icon="🩺",
layout="centered",
initial_sidebar_state="expanded",
)
# Function to send message and get response with error handling
def send_message():
user_input = st.session_state.user_input
if user_input:
st.session_state.chat_history.append(("You", user_input))
try:
response = chat(user_input)
if not response:
raise ValueError("Received empty response")
st.session_state.chat_history.append(("MedicBot", response))
except Exception as e:
st.session_state.chat_history.append(("MedicBot", f"Error: {str(e)}"))
st.session_state.user_input = ""
# Function to clear chat history
def clear_chat():
st.session_state.chat_history = []
# Initialize chat history
if 'chat_history' not in st.session_state:
st.session_state.chat_history = []
# UI Elements
st.title("🩺 MedicBot")
st.write("Welcome to MedicBot! Your virtual medical assistant.")
# Chat history display
chat_container = st.container()
with chat_container:
for sender, message in st.session_state.chat_history:
if sender == "You":
st.markdown(f"**{sender}:** {message}", unsafe_allow_html=True)
else:
st.markdown(f"**<span style='color: #4CAF50;'>{sender}</span>:** {message}", unsafe_allow_html=True)
# User input field
st.text_input("Type your message:", key='user_input', on_change=send_message)
# Buttons
col1, col2 = st.columns(2)
with col1:
st.button("Send", on_click=send_message)
with col2:
st.button("Clear Chat", on_click=clear_chat)
st.write("Developed by Krish Ujeniya")