File size: 2,333 Bytes
6b5f4d7
5356085
6b5f4d7
5356085
dab1d38
a474ea6
dab1d38
 
 
 
 
 
 
 
 
a474ea6
dab1d38
 
 
 
 
 
 
 
 
 
 
5356085
dab1d38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5356085
dab1d38
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
51
52
53
54
55
56
57
58
59
import streamlit as st
from pipeline import text_to_sql

st.title("SQLCoder Text-to-SQL App")
st.write("Powered by defog/sqlcoder-7b-2 πŸš€")

# Sample queries for user guidance
st.sidebar.header("Sample Queries")
sample_queries = [
    "List 11 names of ships type schooner",
    "Show me the 5 oldest ships",
    "What are the different types of vessels?",
    "Count the number of ships by type",
    "Show ships built after 1900"
]

selected_sample = st.sidebar.selectbox("Choose a sample query:", [""] + sample_queries)

# Main input
nl_query = st.text_input(
    "Enter your natural language query:", 
    value=selected_sample if selected_sample else "List 11 names of ships type schooner",
    help="Ask questions about your database in plain English"
)

if st.button("πŸ”„ Generate & Execute SQL"):
    if nl_query.strip():
        with st.spinner("Generating SQL and executing query..."):
            try:
                sql, results = text_to_sql(nl_query)
                
                # Display results
                st.success("Query executed successfully!")
                
                # Show generated SQL
                st.subheader("Generated SQL:")
                st.code(sql, language="sql")
                
                # Show results
                st.subheader("Results:")
                if results:
                    # Convert results to a more readable format
                    if isinstance(results[0], tuple):
                        # If results are tuples, display as table
                        st.write(f"Found {len(results)} rows:")
                        for i, row in enumerate(results[:50]):  # Show first 50 rows
                            st.write(f"Row {i+1}: {row}")
                        if len(results) > 50:
                            st.info(f"Showing first 50 rows out of {len(results)} total results.")
                    else:
                        st.write(results)
                else:
                    st.info("Query executed successfully but returned no results.")
                    
            except Exception as e:
                st.error(f"Error: {str(e)}")
                st.write("Please try rephrasing your query or check if the requested data exists in the database.")
    else:
        st.warning("Please enter a query to proceed.")