File size: 2,876 Bytes
2929135
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
67
68
69
70
71
72
73
74
75
76
77
78
import streamlit as st
from typing import Optional, Dict, Callable
from datetime import datetime

class ChatComponent:
    def __init__(self, process_message_callback: Callable):
        """
        Initialize the chat component
        
        Args:
            process_message_callback: Callback function to process messages
        """
        self.process_message = process_message_callback
        
        # Initialize session state for messages if not exists
        if 'messages' not in st.session_state:
            st.session_state.messages = []
            
    def _display_message(self, role: str, content: str, timestamp: Optional[datetime] = None):
        """Display a single chat message"""
        avatar = "πŸ€–" if role == "assistant" else "πŸ‘€"
        with st.chat_message(role, avatar=avatar):
            st.markdown(content)
            if timestamp:
                st.caption(f":clock2: {timestamp.strftime('%H:%M')}")

    def render(self):
        """Render the chat interface"""
        st.markdown("### πŸ’¬ Healthcare Operations Chat")
        
        # Display chat messages
        for message in st.session_state.messages:
            self._display_message(
                role=message["role"],
                content=message["content"],
                timestamp=message.get("timestamp")
            )
        
        # Chat input
        if prompt := st.chat_input(
            "Ask about patient flow, resources, quality metrics, or staff scheduling..."
        ):
            # Add user message
            current_time = datetime.now()
            st.session_state.messages.append({
                "role": "user",
                "content": prompt,
                "timestamp": current_time
            })
            
            # Display user message
            self._display_message("user", prompt, current_time)
            
            # Process message and get response
            with st.spinner("Processing your request... πŸ”„"):
                try:
                    response = self.process_message(prompt)
                    
                    # Add and display assistant response
                    st.session_state.messages.append({
                        "role": "assistant",
                        "content": response["response"],
                        "timestamp": datetime.now()
                    })
                    
                    self._display_message(
                        "assistant",
                        response["response"],
                        datetime.now()
                    )
                    
                except Exception as e:
                    st.error(f"Error processing your request: {str(e)} ❌")

    def clear_chat(self):
        """Clear the chat history"""
        st.session_state.messages = []
        st.success("Chat history cleared! 🧹")