Spaces:
Sleeping
Sleeping
File size: 3,168 Bytes
9181263 d1975ef 413ef52 d1975ef 9181263 a6c9f47 9181263 17a3dab 9181263 17a3dab 573d515 e11e9b5 9181263 47cfa38 9181263 94d2fd1 521767f 94d2fd1 9181263 47cfa38 9181263 d1975ef 9181263 152f485 e11e9b5 d1975ef 152f485 d1975ef 9181263 17a3dab 9181263 488a487 e11e9b5 9181263 17a3dab 9181263 152f485 9181263 a6c9f47 152f485 a6c9f47 573d515 a6c9f47 f205939 9181263 9b223bc 9181263 47cfa38 |
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
import streamlit as st
from transformers import pipeline
import sqlite3
import numpy as np
import pandas as pd
#initialize chat history
if "messages" not in st.session_state:
st.session_state.messages=[]
## Function to read SQL data
def read_sql_query(sql, db):
st.write("in FUnction")
conn = sqlite3.connect(db)
cur = conn.cursor()
cur.execute(sql)
rows = cur.fetchall()
#for row in rows:
#st.write(row)
conn.close()
output_df = pd.DataFrame(rows)
st.table(output_df)
#return output_df
i_pwd= st.sidebar.text_input("Enter password", type="password")
if i_pwd=='password':
translator = pipeline(model= "saikiranmaddukuri/chat_to_sql0.17", max_new_tokens= 100)
df= pd.read_csv("Car_sales.csv")
st.title("Conversational AI: Text-2-SQL")
column_info = df.dtypes.to_dict()
column_df = pd.DataFrame(column_info.items(), columns=['Column Name', 'Data Type'])
st.sidebar.table(column_df)
create_table_statement= "CREATE TABLE car_sales (Manufacturer text, Model text, Sales_in_thousands float, __year_resale_value float, Vehicle_type text, Price_in_thousands float, Engine_size float, Horsepower float, Wheelbase float, Width float, Length float, Curb_weight float, Fuel_capacity float, Fuel_efficiency float, Latest_Launch text, Power_perf_factor float)"
# display chat messages from history
for message in st.session_state.messages:
with st.chat_message(message['role']):
st.markdown(message['content'])
# Step 1: User input
#with st.chat_message(name= 'assistant'):
#st.markdown('Ask question and press Enter')
#st.session_state.messages.append({"role":"assistant", "content": 'Ask question and press Enter' })
user_query= st.chat_input('Ask question and press ENTER.')
if user_query:
with st.chat_message('user'):
st.markdown(user_query)
st.session_state.messages.append({"role":"user", "content": user_query })
#user_query = st.text_input("")
# Step 2: Backend processing on Enter
#if user_query:
text='question:{}. table:{}'.format(user_query,create_table_statement.replace("text", 'VARCHAR') )
with st.spinner('Generating SQL. Please wait...'):
generated_sql= translator([text])
generated_sql= generated_sql[0]['generated_text']
i_gen_query = st.text_area("AI Generated SQL (edit if required)", generated_sql)
with st.chat_message('assistant'):
st.markdown(i_gen_query)
st.session_state.messages.append({"role":"assistant", "content": i_gen_query })
# Step 4: Process edited text on Enter
if st.button("Show results") and i_gen_query:
st.write("Button worked!")
st.markdown(i_gen_query)
#st.session_state.messages.append({"role":"assistant", "content": i_gen_query })
read_sql_query(i_gen_query, "Cars_db.sqlite")
# Step 5: Display final output
#st.write("Final Output:", final_output)
else:
st.warning("Enter correct password!")
|