File size: 1,672 Bytes
4cbe4e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ddedbff
4cbe4e9
 
 
 
 
 
 
 
 
 
 
 
95b17be
 
4cbe4e9
 
 
 
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
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)