import streamlit as st from chat import ChatSession, ChatSessionSchema, Role from app import react_agent from mongoengine import connect from config import settings import requests from tools.menu_tools_utils import get_data_store_id import speech_recognition as sr st.set_page_config(layout="wide") connect(settings.DB_NAME, host=settings.DB_URI, alias="default") print("Database connection established!!") def speech_to_text(): r = sr.Recognizer() with sr.Microphone() as mic: st.write("Listening for your query...") r.adjust_for_ambient_noise(mic, duration=1) audio = r.listen(mic) try: text = r.recognize_google(audio) st.success(f"Recognized: {text}") return text except sr.UnknownValueError: st.error("Sorry, I could not understand the audio.") return "" except sr.RequestError as e: st.error( f"Could not request results from Google Speech Recognition service; {e}" ) return "" col1, col2 = st.columns([1, 3]) with col2: st.title("Restaurant Order Bot") if "session_id" not in st.session_state: session = ChatSession() session.save() st.session_state["session_id"] = str(session.id) if "conversation" not in st.session_state: st.session_state["conversation"] = [] # Input: Either type in or use the speech-to-text button user_query = st.text_input("Ask something:") col3, col4, col5 = st.columns([3, 3, 3]) # New button for speech recognition with col3: if st.button("🎤 Speak your query"): recognized_text = speech_to_text() if recognized_text: user_query = recognized_text # Set recognized text to the user input st.session_state["conversation"].append( {"role": "user", "text": user_query} ) chat_schema = ChatSessionSchema( session_id=st.session_state["session_id"], query=user_query ) chat_session = ChatSession.objects(id=chat_schema.session_id).first() chat_history = chat_session.get_last_messages() response = react_agent.handle_query( session_id=chat_schema.session_id, query=chat_schema.query, chat_history=chat_history, ) chat_session.add_message_with_metadata( role=Role.USER.value, content=chat_schema.query ) chat_session.add_message_with_metadata( role=Role.MODEL.value, content=response ) if response: st.session_state["conversation"].append( {"role": "bot", "text": response} ) else: st.session_state["conversation"].append( { "role": "bot", "text": "Error: Could not get a response from the server.", } ) # Submit button for text input with col4: if st.button("Submit"): if user_query: st.session_state["conversation"].append( {"role": "user", "text": user_query} ) chat_schema = ChatSessionSchema( session_id=st.session_state["session_id"], query=user_query ) chat_session = ChatSession.objects(id=chat_schema.session_id).first() chat_history = chat_session.get_last_messages() response = react_agent.handle_query( session_id=chat_schema.session_id, query=chat_schema.query, chat_history=chat_history, ) chat_session.add_message_with_metadata( role=Role.USER.value, content=chat_schema.query ) chat_session.add_message_with_metadata( role=Role.MODEL.value, content=response ) if response: st.session_state["conversation"].append( {"role": "bot", "text": response} ) else: st.session_state["conversation"].append( { "role": "bot", "text": "Error: Could not get a response from the server.", } ) with col5: if st.button("Clear Chat"): st.session_state["conversation"] = [] # Display conversation user_message_style = """ background-color: #d1e7dd; padding: 10px; border-radius: 10px; margin-bottom: 10px; color: #0f5132; text-align: left; """ bot_message_style = """ background-color: #f8d7da; padding: 10px; border-radius: 10px; margin-bottom: 10px; color: #842029; text-align: left; """ for message in st.session_state["conversation"]: if message["role"] == "user": st.markdown( f"