Spaces:
Sleeping
Sleeping
Commit ·
7795384
1
Parent(s): e706c90
feat: add main menu options and scripts
Browse files- app.py +46 -16
- resources/ai_app.db +0 -0
- resources/examples.csv +6 -2
- resources/schema.sql +0 -0
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import sys
|
|
|
|
| 3 |
|
| 4 |
# insert current directory to sys.path
|
| 5 |
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
|
|
@@ -25,7 +26,7 @@ st.set_page_config(
|
|
| 25 |
|
| 26 |
# TEXT_2_SQL_API = "http://83.219.197.235:40172/api/text2sql/ask"
|
| 27 |
TEXT_2_SQL_API = os.environ.get(
|
| 28 |
-
"TEXT_2_SQL_API", "http://
|
| 29 |
)
|
| 30 |
|
| 31 |
|
|
@@ -49,14 +50,15 @@ def execute_sql(sql_query):
|
|
| 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":
|
| 57 |
question = translate_question(question)
|
| 58 |
-
st.write("The question is translated to Vietnamese:")
|
| 59 |
-
st.code(question, language="en")
|
| 60 |
|
| 61 |
r = requests.post(
|
| 62 |
TEXT_2_SQL_API,
|
|
@@ -182,11 +184,26 @@ def examples():
|
|
| 182 |
|
| 183 |
if example_btns[idx]:
|
| 184 |
st.markdown("##### SQL query:")
|
| 185 |
-
|
| 186 |
-
st.
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
|
| 192 |
# Define a function for the Stock Prediction page
|
|
@@ -218,13 +235,26 @@ CREATE TABLE product (id number, category text, name text, description text, pri
|
|
| 218 |
|
| 219 |
if get_sql_button:
|
| 220 |
st.markdown("##### Output")
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
|
| 230 |
# Define a function for the About page
|
|
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
+
import time
|
| 4 |
|
| 5 |
# insert current directory to sys.path
|
| 6 |
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
|
|
|
|
| 26 |
|
| 27 |
# TEXT_2_SQL_API = "http://83.219.197.235:40172/api/text2sql/ask"
|
| 28 |
TEXT_2_SQL_API = os.environ.get(
|
| 29 |
+
"TEXT_2_SQL_API", "http://213.181.122.2:40057/api/text2sql/ask"
|
| 30 |
)
|
| 31 |
|
| 32 |
|
|
|
|
| 50 |
return cursor.fetchall()
|
| 51 |
except Exception as e:
|
| 52 |
st.info("Database is not supported")
|
| 53 |
+
return None
|
| 54 |
|
| 55 |
|
| 56 |
+
# @st.cache_data
|
| 57 |
def ask_text2sql(question, context):
|
| 58 |
if detect(question) != "en":
|
| 59 |
question = translate_question(question)
|
| 60 |
+
# st.write("The question is translated to Vietnamese:")
|
| 61 |
+
# st.code(question, language="en")
|
| 62 |
|
| 63 |
r = requests.post(
|
| 64 |
TEXT_2_SQL_API,
|
|
|
|
| 184 |
|
| 185 |
if example_btns[idx]:
|
| 186 |
st.markdown("##### SQL query:")
|
| 187 |
+
tries = 3
|
| 188 |
+
with st.spinner("Generating SQL query..."):
|
| 189 |
+
while tries > 0:
|
| 190 |
+
start_time = time.time()
|
| 191 |
+
query = ask_text2sql(row["question"], row["context"])
|
| 192 |
+
end_time = time.time()
|
| 193 |
+
st.write(
|
| 194 |
+
"The SQL query generated by the model in {:.2f}s is:".format(
|
| 195 |
+
end_time - start_time
|
| 196 |
+
)
|
| 197 |
+
)
|
| 198 |
+
st.code(format_sql(query), language="sql")
|
| 199 |
+
result = execute_sql(query)
|
| 200 |
+
|
| 201 |
+
st.write("Executing the SQL query yields the following result:")
|
| 202 |
+
st.dataframe(pd.DataFrame(result), hide_index=True)
|
| 203 |
+
if result is not None:
|
| 204 |
+
break
|
| 205 |
+
else:
|
| 206 |
+
tries -= 1
|
| 207 |
|
| 208 |
|
| 209 |
# Define a function for the Stock Prediction page
|
|
|
|
| 235 |
|
| 236 |
if get_sql_button:
|
| 237 |
st.markdown("##### Output")
|
| 238 |
+
tries = 3
|
| 239 |
+
while tries > 0:
|
| 240 |
+
start_time = time.time()
|
| 241 |
+
query = ask_text2sql(question, context)
|
| 242 |
+
end_time = time.time()
|
| 243 |
+
|
| 244 |
+
st.write(
|
| 245 |
+
"The SQL query generated by the model in {:.2f}s is:".format(
|
| 246 |
+
end_time - start_time
|
| 247 |
+
)
|
| 248 |
+
)
|
| 249 |
+
# Display the SQL query in a code block
|
| 250 |
+
st.code(format_sql(query), language="sql")
|
| 251 |
+
result = execute_sql(query)
|
| 252 |
+
st.write("Executing the SQL query yields the following result:")
|
| 253 |
+
st.dataframe(pd.DataFrame(result), hide_index=True)
|
| 254 |
+
if result is not None:
|
| 255 |
+
break
|
| 256 |
+
else:
|
| 257 |
+
tries -= 1
|
| 258 |
|
| 259 |
|
| 260 |
# Define a function for the About page
|
resources/ai_app.db
ADDED
|
Binary file (61.4 kB). View file
|
|
|
resources/examples.csv
CHANGED
|
@@ -1,5 +1,9 @@
|
|
| 1 |
context,question
|
| 2 |
-
"CREATE TABLE
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
"CREATE TABLE customer (id number, name text, gender text, age number, district_id number;
|
| 4 |
CREATE TABLE registration (customer_id number, product_id number);
|
| 5 |
CREATE TABLE district (id number, name text, prefix text, province_id number);
|
|
@@ -9,4 +13,4 @@ CREATE TABLE product (id number, category text, name text, description text, pri
|
|
| 9 |
CREATE TABLE registration (customer_id number, product_id number);
|
| 10 |
CREATE TABLE district (id number, name text, prefix text, province_id number);
|
| 11 |
CREATE TABLE province (id number, name text, code text)
|
| 12 |
-
CREATE TABLE product (id number, category text, name text, description text, price number, duration number, data_amount number, voice_amount number, sms_amount number);",
|
|
|
|
| 1 |
context,question
|
| 2 |
+
"CREATE TABLE customer (id number, name text, gender text, age number, district_id number;
|
| 3 |
+
CREATE TABLE registration (customer_id number, product_id number);
|
| 4 |
+
CREATE TABLE district (id number, name text, prefix text, province_id number);
|
| 5 |
+
CREATE TABLE province (id number, name text, code text)
|
| 6 |
+
CREATE TABLE product (id number, category text, name text, description text, price number, duration number, data_amount number, voice_amount number, sms_amount number);",Số lượng khách hàng có độ tuổi từ 15 đến 20 tuổi
|
| 7 |
"CREATE TABLE customer (id number, name text, gender text, age number, district_id number;
|
| 8 |
CREATE TABLE registration (customer_id number, product_id number);
|
| 9 |
CREATE TABLE district (id number, name text, prefix text, province_id number);
|
|
|
|
| 13 |
CREATE TABLE registration (customer_id number, product_id number);
|
| 14 |
CREATE TABLE district (id number, name text, prefix text, province_id number);
|
| 15 |
CREATE TABLE province (id number, name text, code text)
|
| 16 |
+
CREATE TABLE product (id number, category text, name text, description text, price number, duration number, data_amount number, voice_amount number, sms_amount number);", Liệt kê số khách hàng của từng tỉnh
|
resources/schema.sql
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|