File size: 1,183 Bytes
db92763
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from utils import Chain
from utils import DB
st.set_page_config(page_title="💬 Chat_to_DB")

# st.title(":red[Chat] to :red[Database]")
st.markdown("<h1 style='text-align: center;'>Chat to Database</h1>", unsafe_allow_html=True)

st.sidebar.subheader("See table")
row = st.sidebar.number_input("Enter Number of rows", min_value=5,step=1)
st.sidebar.write(DB().see_table(rows = row))

if "messages" not in st.session_state.keys():
    st.session_state.messages = [{"role": "assistant", "content": "How may I help you?"}]

for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.write(message["content"])

if prompt := st.chat_input():
    st.session_state.messages.append({"role": "user", "content": prompt})
    with st.chat_message("user"):
        st.write(prompt)

if st.session_state.messages[-1]["role"] != "assistant":
    with st.chat_message("assistant"):
        with st.spinner("Thinking..."):
            response = Chain().final_sql(prompt) 
            st.write(response) 
    message = {"role": "assistant", "content": response}
    st.session_state.messages.append(message)