|
import streamlit as st |
|
import logging |
|
import asyncio |
|
from contextlib import asynccontextmanager |
|
from app import QueryRequest |
|
|
|
|
|
logging.basicConfig(level=logging.INFO) |
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
st.set_page_config(page_title="Certification Chat", layout="wide") |
|
st.title("π Hydrogen Certification Chat Assistant") |
|
|
|
|
|
async def async_query(query_text): |
|
from app import handle_query |
|
request = QueryRequest(query=query_text) |
|
return await handle_query(request) |
|
|
|
|
|
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 = st.text_area("π¬ Enter your question about hydrogen certification:", height=100) |
|
|
|
if user_input: |
|
try: |
|
|
|
with st.spinner("Processing your query..."): |
|
|
|
result = run_async(async_query(user_input)) |
|
|
|
|
|
st.markdown("## π§ Response") |
|
|
|
|
|
st.subheader(f"Detected Certification: {result['certification']}") |
|
|
|
|
|
tab1, tab2, tab3, tab4 = st.tabs([ |
|
"Answers", |
|
"Context Details", |
|
"Raw Context (Dot Chunking)", |
|
"Raw Context (Hybrid Chunking)" |
|
]) |
|
|
|
with tab1: |
|
st.markdown("### Basic Chunking Answer:") |
|
st.write(result["certif_index"]) |
|
|
|
st.markdown("### Hybrid Chunking Answer:") |
|
st.write(result["certification_index"]) |
|
|
|
with tab2: |
|
col1, col2 = st.columns(2) |
|
|
|
with col1: |
|
st.markdown("### Basic Chunking Context Sources:") |
|
for i, context_item in enumerate(result["context_certif"]): |
|
with st.expander(f"Source {i+1}"): |
|
st.write(context_item) |
|
|
|
with col2: |
|
st.markdown("### Hybrid Chunking Context Sources:") |
|
for i, context_item in enumerate(result["context_certifications"]): |
|
with st.expander(f"Source {i+1}"): |
|
st.write(context_item) |
|
|
|
with tab3: |
|
st.markdown("### Raw Context (Dot Chunking):") |
|
for i, chunk in enumerate(result["context_certif"]): |
|
st.text_area(f"Chunk {i+1}", chunk, height=150) |
|
|
|
with tab4: |
|
st.markdown("### Raw Context (Hybrid Chunking):") |
|
for i, chunk in enumerate(result["context_certifications"]): |
|
st.text_area(f"Chunk {i+1}", chunk, height=150) |
|
|
|
|
|
st.markdown("---") |
|
st.markdown("### Feedback") |
|
feedback = st.radio( |
|
"How helpful was this response?", |
|
["Very helpful", "Somewhat helpful", "Not helpful"] |
|
) |
|
feedback_text = st.text_area("Additional feedback (optional):", height=100) |
|
if st.button("Submit Feedback"): |
|
st.success("Thank you for your feedback!") |
|
|
|
except Exception as e: |
|
st.error(f"An error occurred: {str(e)}") |
|
logger.error(f"Error processing query: {e}", exc_info=True) |
|
else: |
|
st.info("π Enter your question about hydrogen certifications above to get started!") |
|
|
|
|
|
with st.sidebar: |
|
st.markdown("## About") |
|
st.markdown(""" |
|
This tool helps answer questions about hydrogen certification standards using |
|
a Retrieval-Augmented Generation (RAG) system. |
|
|
|
The system: |
|
1. Classifies which certification your question is about |
|
2. Optimizes your query |
|
3. Retrieves relevant information |
|
4. Generates a precise answer |
|
""") |
|
|
|
st.markdown("## Available Certifications") |
|
try: |
|
from app import list_certifications |
|
certifications = run_async(list_certifications()) |
|
for cert in certifications: |
|
st.markdown(f"- {cert}") |
|
except Exception as e: |
|
st.warning("Could not load certification list") |