File size: 1,834 Bytes
4180c11
af733da
 
4180c11
 
af733da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
import streamlit as st
from lib import Text2SQLRAG
from utils import execute_query_and_return_df


st.set_page_config(page_title="Text2SQLRAG")
st.title("Text2SQLRAG")

# Create an instance of Text2SQLRAG
text2sql = Text2SQLRAG()

# Initialize session state for storing chat messages
if "messages" not in st.session_state:
    st.session_state.messages = []
    
    # Display conversation history from session state
for message in st.session_state.messages:
    role = message.get("role", "assistant")
    with st.chat_message(role):
        if "output" in message:
            with st.expander("Reasoning", expanded=True):
                st.markdown(message["reasoning"])
        if "sql_query" in message and message["sql_query"]:
            with st.expander("SQL Query", expanded=True):
                st.code(message["sql_query"])

# Get user input
input_text = st.chat_input("Chat with your bot here...")

if input_text:
    # Display user input
    with st.chat_message("user"):
        st.markdown(input_text)

    # Add user input to chat history
    st.session_state.chat_history.append({"role": "user", "text": input_text})

    # Get chatbot response
    response = text2sql.run(input_text)
    sql_query = response.query
    reasoning = response.reasoning
    df = execute_query_and_return_df(sql_query)

    if sql_query:
        with st.expander("SQL Query", expanded=True):
            st.code(sql_query)

        with st.expander("Reasoning", expanded=True):
            st.write(reasoning)

    if df is not None:
        st.dataframe(df)
    else:
        st.error("Error executing query")
    
    # Append assistant response to session state
    st.session_state.messages.append(
        {
            "role": "assistant",
            "reasoning": reasoning,
            "sql_query": sql_query,
        }
    )