Spaces:
Sleeping
Sleeping
| """ | |
| Streamlit app for NL to SQL agent | |
| """ | |
| import os | |
| import streamlit as st | |
| from dotenv import load_dotenv | |
| from galileo import galileo_context | |
| from nl_to_sql_agent import NLToSQLAgent | |
| import pandas as pd | |
| # Load environment variables | |
| load_dotenv() | |
| # Set page config | |
| st.set_page_config( | |
| page_title="HR Database Query Assistant", | |
| page_icon="π", | |
| layout="centered", | |
| initial_sidebar_state="collapsed" | |
| ) | |
| # Title and description | |
| st.title("HR Database Query Assistant") | |
| st.markdown(""" | |
| Convert natural language to SQL queries for an HR database. | |
| Simply describe what information you need, and the AI will generate the appropriate SQL query and execute it. | |
| """) | |
| if "agent" not in st.session_state: | |
| st.session_state.agent = NLToSQLAgent() | |
| # Sidebar for settings | |
| with st.sidebar: | |
| st.toggle("Galileo Protect", key="protect_enabled", help="Enable content protection with Galileo") | |
| # Function to run the agent | |
| def generate_and_execute_sql(query): | |
| """Generate SQL from natural language using the agent and execute it.""" | |
| agent = st.session_state.agent | |
| run_protect = st.session_state.protect_enabled | |
| return agent.generate_and_execute_sql(query, run_protect) | |
| # Query input | |
| with st.form(key="query_form"): | |
| query = st.text_area( | |
| "Enter your query in natural language:", | |
| placeholder="e.g., Show me all employees in the Marketing department", | |
| height=100 | |
| ) | |
| submit_button = st.form_submit_button(label="Generate and Execute Query") | |
| # Process the query when the form is submitted | |
| if submit_button and query: | |
| with st.spinner("Processing your query..."): | |
| try: | |
| # Run the function with Galileo logging | |
| with galileo_context(project=os.getenv("GALILEO_PROJECT"), log_stream=os.getenv("GALILEO_LOG_STREAM")): | |
| sql_query, results = generate_and_execute_sql(query) | |
| # Display the SQL query | |
| st.subheader("Generated SQL Query") | |
| st.code(sql_query, language="sql") | |
| # Add a copy button for SQL | |
| st.button( | |
| "Copy SQL", | |
| help="Copy the SQL query to clipboard", | |
| on_click=lambda: st.write("SQL query copied to clipboard!") | |
| ) | |
| # Display the results | |
| st.subheader("Query Results") | |
| if results: | |
| # Convert results to DataFrame for display | |
| df = pd.DataFrame(results) | |
| st.dataframe(df) | |
| # Add download button | |
| csv = df.to_csv(index=False).encode('utf-8') | |
| st.download_button( | |
| label="Download Results as CSV", | |
| data=csv, | |
| file_name="query_results.csv", | |
| mime="text/csv" | |
| ) | |
| else: | |
| st.info("No results returned from the query.") | |
| except Exception as e: | |
| st.error(f"Error processing query: {e}") | |
| # Display example queries | |
| with st.expander("Example Queries"): | |
| st.markdown(""" | |
| Here are some example queries you can try: | |
| - Show me all employees in the Marketing department | |
| - List the top 5 highest paid employees | |
| - Find employees who haven't taken any training courses | |
| - Show me the average salary by department | |
| - Which employees have been with the company for more than 3 years? | |
| - Show me the performance reviews for employees in the Engineering department | |
| """) | |
| # Footer | |
| st.markdown("---") | |
| st.markdown( | |
| "Powered by OpenAI GPT-4o-mini | HR Database Assistant" | |
| ) |