import streamlit as st import logging import asyncio from contextlib import asynccontextmanager from app import QueryRequest # Import the request model # Configure logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) # Set page config st.set_page_config(page_title="Certification Chat", layout="centered") st.title("🎓 Certification Chat Assistant") # Create a function to handle the async call async def async_query(query_text): from app import handle_query # Import here to avoid circular imports request = QueryRequest(query=query_text) return await handle_query(request) # Function to run async code in Streamlit def run_async(coroutine): try: loop = asyncio.get_event_loop() except RuntimeError: loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) return loop.run_until_complete(coroutine) # User input user_input = st.text_area("💬 Enter your prompt:", height=150) if user_input: st.markdown("## 🧠 Response") try: # Use try-except to handle errors with st.spinner("Processing your query..."): # Run the async function result = run_async(async_query(user_input)) # Display output st.write("**Certification:**", result["certification"]) st.write("**Answer from '.' chunking method:**", result["certif_index"]) st.write("**Answer from hybrid chunking method:**", result["certification_index"]) except Exception as e: st.error(f"An error occurred: {str(e)}") logger.error(f"Error processing query: {e}", exc_info=True)