File size: 1,905 Bytes
d58e80d 362d829 4d0ea91 41ce0fc 4d0ea91 41ce0fc 4d0ea91 d58e80d 6546bc3 |
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 66 |
import streamlit as st
from bot import chat
import nltk
# Ensure required NLTK data is downloaded
nltk.data.clear_cache()
nltk.download('punkt')
nltk.data.clear_cache()
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 Gurunam Lohia")
|