Spaces:
Sleeping
Sleeping
Commit
Β·
e706c90
1
Parent(s):
a625aba
feat: add main menu options and scripts
Browse files- app.py +40 -5
- requirements.txt +1 -0
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import sys
|
|
| 5 |
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
|
| 6 |
|
| 7 |
import re
|
|
|
|
| 8 |
import numpy as np
|
| 9 |
import pandas as pd
|
| 10 |
import streamlit as st
|
|
@@ -12,6 +13,7 @@ import requests
|
|
| 12 |
|
| 13 |
from googletrans import Translator
|
| 14 |
from langdetect import detect
|
|
|
|
| 15 |
|
| 16 |
translator = Translator()
|
| 17 |
|
|
@@ -27,6 +29,28 @@ TEXT_2_SQL_API = os.environ.get(
|
|
| 27 |
)
|
| 28 |
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
@st.cache_data
|
| 31 |
def ask_text2sql(question, context):
|
| 32 |
if detect(question) != "en":
|
|
@@ -62,11 +86,11 @@ def introduction():
|
|
| 62 |
"π This app allows you to explore the ability of Text to SQL model. The model is CodeLlama-13b finetuned using QLoRA on NSText2SQL dataset."
|
| 63 |
)
|
| 64 |
st.write(
|
| 65 |
-
"π The NSText2SQL dataset contains more than 290.000 training samples. Then, the model is evaluated on Spider and
|
| 66 |
)
|
| 67 |
st.write("π The other pages in this app include:")
|
| 68 |
st.write(
|
| 69 |
-
" - π EDA Page: This page includes several visualizations to help you understand the two dataset: Spider and
|
| 70 |
)
|
| 71 |
st.write(
|
| 72 |
" - π° Text2SQL Page: This page allows you to generate SQL query from a given question and context."
|
|
@@ -159,7 +183,10 @@ def examples():
|
|
| 159 |
if example_btns[idx]:
|
| 160 |
st.markdown("##### SQL query:")
|
| 161 |
query = ask_text2sql(row["question"], row["context"])
|
| 162 |
-
st.code(query, language="sql")
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
|
| 165 |
# Define a function for the Stock Prediction page
|
|
@@ -174,8 +201,13 @@ def interactive_demo():
|
|
| 174 |
question_placeholder = st.empty()
|
| 175 |
context = context_placeholder.text_area(
|
| 176 |
"##### Context",
|
| 177 |
-
"CREATE TABLE
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
key="context",
|
|
|
|
| 179 |
)
|
| 180 |
question = question_placeholder.text_input(
|
| 181 |
"##### Question",
|
|
@@ -189,7 +221,10 @@ def interactive_demo():
|
|
| 189 |
query = ask_text2sql(question, context)
|
| 190 |
st.write("The SQL query generated by the model is:")
|
| 191 |
# Display the SQL query in a code block
|
| 192 |
-
st.code(query, language="sql")
|
|
|
|
|
|
|
|
|
|
| 193 |
|
| 194 |
|
| 195 |
# Define a function for the About page
|
|
|
|
| 5 |
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
|
| 6 |
|
| 7 |
import re
|
| 8 |
+
import sqlite3
|
| 9 |
import numpy as np
|
| 10 |
import pandas as pd
|
| 11 |
import streamlit as st
|
|
|
|
| 13 |
|
| 14 |
from googletrans import Translator
|
| 15 |
from langdetect import detect
|
| 16 |
+
from sql_formatter.core import format_sql
|
| 17 |
|
| 18 |
translator = Translator()
|
| 19 |
|
|
|
|
| 29 |
)
|
| 30 |
|
| 31 |
|
| 32 |
+
@st.cache_resource
|
| 33 |
+
def load_database():
|
| 34 |
+
db_conn = sqlite3.connect("resources/ai_app.db")
|
| 35 |
+
with open("resources/schema.sql", "r") as f:
|
| 36 |
+
db_conn.executescript(f.read())
|
| 37 |
+
|
| 38 |
+
return db_conn
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
db_conn = load_database()
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def execute_sql(sql_query):
|
| 45 |
+
try:
|
| 46 |
+
cursor = db_conn.cursor()
|
| 47 |
+
cursor.execute(sql_query)
|
| 48 |
+
st.success("SQL query executed successfully!")
|
| 49 |
+
return cursor.fetchall()
|
| 50 |
+
except Exception as e:
|
| 51 |
+
st.info("Database is not supported")
|
| 52 |
+
|
| 53 |
+
|
| 54 |
@st.cache_data
|
| 55 |
def ask_text2sql(question, context):
|
| 56 |
if detect(question) != "en":
|
|
|
|
| 86 |
"π This app allows you to explore the ability of Text to SQL model. The model is CodeLlama-13b finetuned using QLoRA on NSText2SQL dataset."
|
| 87 |
)
|
| 88 |
st.write(
|
| 89 |
+
"π The NSText2SQL dataset contains more than 290.000 training samples. Then, the model is evaluated on Spider and vMLP datasets."
|
| 90 |
)
|
| 91 |
st.write("π The other pages in this app include:")
|
| 92 |
st.write(
|
| 93 |
+
" - π EDA Page: This page includes several visualizations to help you understand the two dataset: Spider and vMLP."
|
| 94 |
)
|
| 95 |
st.write(
|
| 96 |
" - π° Text2SQL Page: This page allows you to generate SQL query from a given question and context."
|
|
|
|
| 183 |
if example_btns[idx]:
|
| 184 |
st.markdown("##### SQL query:")
|
| 185 |
query = ask_text2sql(row["question"], row["context"])
|
| 186 |
+
st.code(format_sql(query), language="sql")
|
| 187 |
+
result = execute_sql(query)
|
| 188 |
+
st.write("The result of the SQL query is:")
|
| 189 |
+
st.dataframe(pd.DataFrame(result))
|
| 190 |
|
| 191 |
|
| 192 |
# Define a function for the Stock Prediction page
|
|
|
|
| 201 |
question_placeholder = st.empty()
|
| 202 |
context = context_placeholder.text_area(
|
| 203 |
"##### Context",
|
| 204 |
+
"""CREATE TABLE customer (id number, name text, gender text, age number, district_id number;
|
| 205 |
+
CREATE TABLE registration (customer_id number, product_id number);
|
| 206 |
+
CREATE TABLE district (id number, name text, prefix text, province_id number);
|
| 207 |
+
CREATE TABLE province (id number, name text, code text)
|
| 208 |
+
CREATE TABLE product (id number, category text, name text, description text, price number, duration number, data_amount number, voice_amount number, sms_amount number);""",
|
| 209 |
key="context",
|
| 210 |
+
height=150,
|
| 211 |
)
|
| 212 |
question = question_placeholder.text_input(
|
| 213 |
"##### Question",
|
|
|
|
| 221 |
query = ask_text2sql(question, context)
|
| 222 |
st.write("The SQL query generated by the model is:")
|
| 223 |
# Display the SQL query in a code block
|
| 224 |
+
st.code(format_sql(query), language="sql")
|
| 225 |
+
result = execute_sql(query)
|
| 226 |
+
st.write("The result of the SQL query is:")
|
| 227 |
+
st.dataframe(pd.DataFrame(result))
|
| 228 |
|
| 229 |
|
| 230 |
# Define a function for the About page
|
requirements.txt
CHANGED
|
@@ -5,3 +5,4 @@ pandas
|
|
| 5 |
googletrans==3.1.0a0
|
| 6 |
langdetect
|
| 7 |
requests
|
|
|
|
|
|
| 5 |
googletrans==3.1.0a0
|
| 6 |
langdetect
|
| 7 |
requests
|
| 8 |
+
sql-formatter
|