acadiaway/gemini_nl2sql
1
1import streamlit as st2from pipeline import text_to_sql3 4st.title("SQLCoder Text-to-SQL App")5st.write("Powered by defog/sqlcoder-7b-2 ๐")6 7# Sample queries for user guidance8st.sidebar.header("Sample Queries")9sample_queries = [10 "List 11 names of ships type schooner",11 "Show me the 5 oldest ships",12 "What are the different types of vessels?",13 "Count the number of ships by type",14 "Show ships built after 1900"15]16 17selected_sample = st.sidebar.selectbox("Choose a sample query:", [""] + sample_queries)18 19# Main input20nl_query = st.text_input(21 "Enter your natural language query:", 22 value=selected_sample if selected_sample else "List 11 names of ships type schooner",23 help="Ask questions about your database in plain English"24)25 26if st.button("๐ Generate & Execute SQL"):27 if nl_query.strip():28 with st.spinner("Generating SQL and executing query..."):29 try:30 sql, results = text_to_sql(nl_query)31 32 # Display results33 st.success("Query executed successfully!")34 35 # Show generated SQL36 st.subheader("Generated SQL:")37 st.code(sql, language="sql")38 39 # Show results40 st.subheader("Results:")41 if results:42 # Convert results to a more readable format43 if isinstance(results[0], tuple):44 # If results are tuples, display as table45 st.write(f"Found {len(results)} rows:")46 for i, row in enumerate(results[:50]): # Show first 50 rows47 st.write(f"Row {i+1}: {row}")48 if len(results) > 50:49 st.info(f"Showing first 50 rows out of {len(results)} total results.")50 else:51 st.write(results)52 else:53 st.info("Query executed successfully but returned no results.")54 55 except Exception as e:56 st.error(f"Error: {str(e)}")57 st.write("Please try rephrasing your query or check if the requested data exists in the database.")58 else:59 st.warning("Please enter a query to proceed.")