Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from bot import chat
|
3 |
+
|
4 |
+
# Streamlit App Configuration
|
5 |
+
st.set_page_config(
|
6 |
+
page_title="MedicBot",
|
7 |
+
page_icon="🩺",
|
8 |
+
layout="centered",
|
9 |
+
initial_sidebar_state="expanded",
|
10 |
+
)
|
11 |
+
|
12 |
+
# Function to send message and get response with error handling
|
13 |
+
def send_message():
|
14 |
+
user_input = st.session_state.user_input
|
15 |
+
if user_input:
|
16 |
+
st.session_state.chat_history.append(("You", user_input))
|
17 |
+
try:
|
18 |
+
response = chat(user_input)
|
19 |
+
if not response:
|
20 |
+
raise ValueError("Received empty response")
|
21 |
+
st.session_state.chat_history.append(("MedicBot", response))
|
22 |
+
except Exception as e:
|
23 |
+
st.session_state.chat_history.append(("MedicBot", f"Error: {str(e)}"))
|
24 |
+
st.session_state.user_input = ""
|
25 |
+
|
26 |
+
# Function to clear chat history
|
27 |
+
def clear_chat():
|
28 |
+
st.session_state.chat_history = []
|
29 |
+
|
30 |
+
# Initialize chat history
|
31 |
+
if 'chat_history' not in st.session_state:
|
32 |
+
st.session_state.chat_history = []
|
33 |
+
|
34 |
+
# UI Elements
|
35 |
+
st.title("🩺 MedicBot")
|
36 |
+
st.write("Welcome to MedicBot! Your virtual medical assistant.")
|
37 |
+
|
38 |
+
# Chat history display
|
39 |
+
chat_container = st.container()
|
40 |
+
with chat_container:
|
41 |
+
for sender, message in st.session_state.chat_history:
|
42 |
+
if sender == "You":
|
43 |
+
st.markdown(f"**{sender}:** {message}", unsafe_allow_html=True)
|
44 |
+
else:
|
45 |
+
st.markdown(f"**<span style='color: #4CAF50;'>{sender}</span>:** {message}", unsafe_allow_html=True)
|
46 |
+
|
47 |
+
# User input field
|
48 |
+
st.text_input("Type your message:", key='user_input', on_change=send_message)
|
49 |
+
|
50 |
+
# Buttons
|
51 |
+
col1, col2 = st.columns(2)
|
52 |
+
with col1:
|
53 |
+
st.button("Send", on_click=send_message)
|
54 |
+
with col2:
|
55 |
+
st.button("Clear Chat", on_click=clear_chat)
|
56 |
+
|
57 |
+
|
58 |
+
|
59 |
+
st.write("Developed by Krish Ujeniya")
|