Spaces:
Paused
Paused
| import streamlit as st | |
| from query_graph import query_graph | |
| # Helper for <details> | |
| def format_citations_html(chunks): | |
| html = [] | |
| for idx, (hdr, sc, txt, citation) in enumerate(chunks, start=1): | |
| preamble = ( | |
| f"<p style='font-size:0.9em;'><strong>Preamble:</strong> " | |
| f"The text in the following detail is reproduced from [{citation}]. " | |
| f"It had a cosine similarity of {sc:.2f} with the user question, " | |
| f"and it ranked {idx} among the text chunks in our graph database.</p>" | |
| ) | |
| body = txt.replace("\n", "<br>") | |
| html.append( | |
| f"<details>" | |
| f"<summary>{hdr} (cosine similarity: {sc:.2f})</summary>" | |
| f"<div style='font-size:0.9em; margin-top:0.5em;'>" | |
| f"<strong>Preamble:</strong> The text below is reproduced from {citation}. " | |
| f"</div>" | |
| f"<div style='font-size:0.7em; margin-left:1em; margin-top:0.5em;'>{body}</div>" | |
| f"</details><br><br>" | |
| ) | |
| return "<br>".join(html) | |
| # Sidebar configuration | |
| st.sidebar.title("About") | |
| st.sidebar.markdown("**Authors:** [The SIGHT Project Team](https://sites.miamioh.edu/sight/)") | |
| st.sidebar.markdown("**Version:** V. 0.0.2") | |
| st.sidebar.markdown("**Date:** July 24, 2025") | |
| st.sidebar.markdown("**Model:** gpt4o") | |
| st.sidebar.markdown("---") | |
| st.sidebar.markdown( | |
| "**Funding:** SIGHT is funded by [OHBWC WSIC](https://info.bwc.ohio.gov/for-employers/safety-services/workplace-safety-innovation-center/wsic-overview)" | |
| ) | |
| # Main interface | |
| st.set_page_config(page_title="Miami University's SIGHT Chatbot") | |
| st.title("Chat with SIGHT") | |
| st.write("Ask questions about machine safeguarding, LOTO, and hazard prevention based on OSHA/CFR's corpus.") | |
| # Example questions toggled in main window | |
| with st.expander("Example Questions", expanded=False): | |
| st.markdown( | |
| "- What are general machine guarding requirements? \n" | |
| "- How do I perform lockout/tagout? \n" | |
| "- Summarize the definition of machine guarding from 29 CFR 1910.211" | |
| ) | |
| # Initialize chat history | |
| if 'history' not in st.session_state: | |
| st.session_state.history = [] | |
| # User input | |
| query = st.text_input("Your question:") | |
| if st.button("Send") and query: | |
| answer, sources, chunks = query_graph(query) | |
| st.session_state.history.append({ | |
| 'query': query, | |
| 'answer': answer, | |
| 'sources': sources, | |
| 'chunks': chunks | |
| }) | |
| # Display chat history | |
| for entry in st.session_state.history[::-1]: | |
| st.markdown(f"**You:** {entry['query']}") | |
| st.markdown(f"**Assistant:** {entry['answer']}") | |
| st.markdown(format_citations_html(entry['chunks']), unsafe_allow_html=True) | |
| # Footer | |
| st.markdown("---") | |
| st.markdown( | |
| "**Disclaimer:** *Powered by a Graph RAG to reduce hallucinations; please verify as it can still make mistakes.*" | |
| ) | |
| st.markdown( | |
| "**Funding:** *We are thankful for [Ohio BWC/WSIC](https://info.bwc.ohio.gov/for-employers/safety-services/workplace-safety-innovation-center/wsic-overview)'s funding that made this chat bot possible.*" | |
| ) |