File size: 3,828 Bytes
192f447 67c0b3c 5875608 8e0a273 5875608 192f447 b037f61 8e0a273 192f447 5875608 192f447 c8236a7 67c0b3c c8236a7 192f447 5875608 67c0b3c 5875608 192f447 c8236a7 67c0b3c c8236a7 192f447 8e0a273 192f447 8e0a273 5875608 67c0b3c 8e0a273 192f447 8e0a273 67c0b3c |
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
import streamlit as st
import numpy as np
from llm import load_llm, response_generator
from sql import csv_to_sqlite, run_sql_query
repo_id = "Qwen/Qwen2.5-Coder-1.5B-Instruct-GGUF"
filename = "qwen2.5-coder-1.5b-instruct-q8_0.gguf"
# repo_id = "Qwen/Qwen2.5-0.5B-Instruct-GGUF"
# filename = "qwen2.5-0.5b-instruct-q8_0.gguf"
llm = load_llm(repo_id, filename)
st.title("CSV TO SQL")
st.write("To start, Upload your CSV below 👇")
if st.button("Example prompt"):
st.session_state.csv_file = "./data/sales.csv"
st.session_state.db_name = "sales"
st.session_state.table_name = "sales"
csv_to_sqlite("./data/sales.csv", "sales", "sales")
prompt = "What is the sum, count and average sales?"
st.session_state.messages.append({"role": "user", "content": prompt})
response_sql = response_generator(
db_name=st.session_state.db_name,
table_name=st.session_state.table_name,
llm=llm,
messages=st.session_state.messages,
question=prompt,
)
result = run_sql_query(db_name=st.session_state.db_name, query=response_sql)
st.session_state.messages.append({"role": "assistant", "content": response_sql})
st.session_state.messages.append(
{"role": "assistant", "content": str(result), "result": result}
)
with st.expander("Upload CSV"):
csv_file = st.file_uploader(
"CSV",
)
db_name = st.text_input("DB Name")
table_name = st.text_input("Table Name")
if st.button("Save"):
if csv_file and db_name and table_name:
st.session_state.csv_file = csv_file
st.session_state.db_name = db_name
st.session_state.table_name = table_name
csv_to_sqlite(csv_file, db_name, table_name)
st.write("Saved ✅")
else:
st.write("Please enter all values")
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Display chat messages from history on app rerun
for message in st.session_state.messages:
with st.chat_message(message["role"]):
if "content" in message:
if message["role"] == "user":
st.markdown(message["content"])
else:
st.code(message["content"])
if "result" in message:
st.dataframe(message["result"])
# Accept user input
if prompt := st.chat_input(
"What is up?",
disabled=(
not "db_name" in st.session_state or not "table_name" in st.session_state
),
):
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
# Display user message in chat message container
with st.chat_message("user"):
st.markdown(prompt)
# Display assistant response in chat message container
with st.chat_message("assistant"):
response_sql = response_generator(
db_name=st.session_state.db_name,
table_name=st.session_state.table_name,
llm=llm,
messages=st.session_state.messages,
question=prompt,
)
response = st.code(response_sql)
result = run_sql_query(db_name=st.session_state.db_name, query=response_sql)
st.markdown(result)
st.table(result)
# Add assistant response to chat history
st.session_state.messages.append({"role": "assistant", "content": response_sql})
with st.sidebar:
st.title("Data Previewer")
st.write("You can see you CSV file content here")
if (
"csv_file" in st.session_state
and "db_name" in st.session_state
and "table_name" in st.session_state
):
result = run_sql_query(
db_name=st.session_state.db_name,
query=f"SELECT * FROM {st.session_state.table_name}",
)
st.dataframe(result)
|