File size: 3,877 Bytes
7c26280
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""Streamlit interface for the barber booking system."""

import streamlit as st
from c_langgraph_flow_new import run_booking_flow, SERVICE_MAPPING
import os
from dotenv import load_dotenv

# Load environment variables
load_dotenv()

def initialize_session():
    """Initialize the session state with default values."""
    if "messages" not in st.session_state:
        st.session_state.messages = []
        # Add initial greeting
        st.session_state.messages.append({
            "role": "assistant",
            "content": "Hello! Welcome to our barber shop. I'll help you book an appointment. Could you please tell me your name?"
        })
    
    if "booking_info" not in st.session_state:
        st.session_state.booking_info = {
            "current_node": "Greeting",
            "booking_info": {},
            "response": "",
            "messages": []
        }

def main():
    st.set_page_config(page_title="AI Barber Booking", page_icon="πŸ’‡β€β™‚οΈ")
    st.title("πŸ’‡β€β™‚οΈ AI Barber Booking Assistant")
    
    # Initialize session
    initialize_session()
    
    # Display current state (for debugging)
    with st.sidebar:
        st.subheader("Debug Information")
        st.write("Current State:", st.session_state.booking_info.get("current_node", "Unknown"))
        st.write("\nBooking Info:", st.session_state.booking_info.get("booking_info", {}))
        if st.checkbox("Show Full Debug Info"):
            st.json(st.session_state.booking_info)
    
    # Display service cards
    st.markdown("### Our Services")
    cols = st.columns(len(SERVICE_MAPPING))
    for col, (service_name, details) in zip(cols, SERVICE_MAPPING.items()):
        with col:
            st.markdown(
                f"""
                <div style="padding: 10px; border-radius: 10px; border: 1px solid #ddd; text-align: center;">
                    <h4>{service_name.title()}</h4>
                    <p>${details['price']}<br>{details['duration']} min</p>
                </div>
                """,
                unsafe_allow_html=True
            )
    
    # Chat interface
    st.markdown("### Chat with our Booking Assistant")
    
    # Display chat history
    for message in st.session_state.messages:
        with st.chat_message(message["role"]):
            st.markdown(message["content"])
    
    # Get user input
    if prompt := st.chat_input("Type your message here..."):
        if prompt.strip() == "":
            st.warning("Please enter a valid message.")
            return
        
        # Add user message to chat history
        st.session_state.messages.append({"role": "user", "content": prompt})
        with st.chat_message("user"):
            st.markdown(prompt)
        
        try:
            # Update booking_info messages
            st.session_state.booking_info["messages"] = st.session_state.messages.copy()
            
            # Process user input through LangGraph flow
            with st.spinner("Processing your request..."):
                new_state = run_booking_flow(prompt, st.session_state.booking_info)
                
                # Update session state
                st.session_state.booking_info = new_state
                
                # Add assistant response to chat history
                if new_state.get("response"):
                    st.session_state.messages.append({
                        "role": "assistant",
                        "content": new_state["response"]
                    })
                    with st.chat_message("assistant"):
                        st.markdown(new_state["response"])
            
            # Force streamlit to rerun
            st.rerun()
            
        except Exception as e:
            st.error(f"An error occurred: {str(e)}")
            print(f"Error in main loop: {str(e)}")

if __name__ == "__main__":
    main()