Spaces:
Sleeping
Sleeping
Commit
·
f10aa93
1
Parent(s):
a754153
feat: add main menu options and scripts
Browse files- resources/examples.csv +12 -0
- st_app.py +288 -0
- st_requirements.txt +7 -0
resources/examples.csv
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
context,question
|
2 |
+
"CREATE TABLE head (name VARCHAR, born_state VARCHAR, age VARCHAR)","List the name, born state and age of the heads of departments ordered by age."
|
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);
|
6 |
+
CREATE TABLE province (id number, name text, code text)
|
7 |
+
CREATE TABLE product (id number, category text, name text, description text, price number, duration number, data_amount number, voice_amount number, sms_amount number);",Có bao nhiêu khách hàng sử dụng gói cước có tên ST70?
|
8 |
+
"CREATE TABLE customer (id number, name text, gender text, age number, district_id number;
|
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);",Số lượng khách hàng có độ tuổi từ 15 đến 20 tuổi
|
st_app.py
ADDED
@@ -0,0 +1,288 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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__)))
|
6 |
+
|
7 |
+
import re
|
8 |
+
import numpy as np
|
9 |
+
import pandas as pd
|
10 |
+
import streamlit as st
|
11 |
+
import requests
|
12 |
+
|
13 |
+
from googletrans import Translator
|
14 |
+
from langdetect import detect
|
15 |
+
|
16 |
+
translator = Translator()
|
17 |
+
|
18 |
+
st.set_page_config(
|
19 |
+
layout="wide",
|
20 |
+
page_title="Text To SQL",
|
21 |
+
page_icon="📊",
|
22 |
+
)
|
23 |
+
|
24 |
+
# TEXT_2_SQL_API = "http://83.219.197.235:40172/api/text2sql/ask"
|
25 |
+
TEXT_2_SQL_API = os.environ.get(
|
26 |
+
"TEXT_2_SQL_API", "http://localhost:8501/api/text2sql/ask"
|
27 |
+
)
|
28 |
+
|
29 |
+
|
30 |
+
@st.cache_data
|
31 |
+
def ask_text2sql(question, context):
|
32 |
+
if detect(question) != "en":
|
33 |
+
question = translate_question(question)
|
34 |
+
st.write("The question is translated to Vietnamese:")
|
35 |
+
st.code(question, language="en")
|
36 |
+
|
37 |
+
r = requests.post(
|
38 |
+
TEXT_2_SQL_API,
|
39 |
+
json={
|
40 |
+
"context": context,
|
41 |
+
"question": question,
|
42 |
+
},
|
43 |
+
)
|
44 |
+
return r.json()["answers"][0]
|
45 |
+
|
46 |
+
|
47 |
+
@st.cache_data
|
48 |
+
def translate_question(question):
|
49 |
+
return translator.translate(question, dest="en").text
|
50 |
+
|
51 |
+
|
52 |
+
@st.cache_data
|
53 |
+
def load_example_df():
|
54 |
+
example_df = pd.read_csv("resources/examples.csv")
|
55 |
+
return example_df
|
56 |
+
|
57 |
+
|
58 |
+
def introduction():
|
59 |
+
st.title("📊 Introduction")
|
60 |
+
st.write("👋 Welcome to the Text to SQL app!")
|
61 |
+
st.write(
|
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 VMLP datasets."
|
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 VMLP."
|
70 |
+
)
|
71 |
+
st.write(
|
72 |
+
" - 💰 Text2SQL Page: This page allows you to generate SQL query from a given question and context."
|
73 |
+
)
|
74 |
+
st.write(
|
75 |
+
" - 🧑💻 About Page: This page provides information about the app and its creators."
|
76 |
+
)
|
77 |
+
st.write(
|
78 |
+
" - 📚 Reference Page: This page lists the references used in building this app."
|
79 |
+
)
|
80 |
+
|
81 |
+
|
82 |
+
# Define a function for the EDA page
|
83 |
+
def eda():
|
84 |
+
st.title("📊 Dataset Exploration")
|
85 |
+
|
86 |
+
# st.subheader("Candlestick Chart")
|
87 |
+
# fig = go.Figure(
|
88 |
+
# data=[
|
89 |
+
# go.Candlestick(
|
90 |
+
# x=df["date"],
|
91 |
+
# open=df["open"],
|
92 |
+
# high=df["high"],
|
93 |
+
# low=df["low"],
|
94 |
+
# close=df["close"],
|
95 |
+
# increasing_line_color="green",
|
96 |
+
# decreasing_line_color="red",
|
97 |
+
# )
|
98 |
+
# ],
|
99 |
+
# layout=go.Layout(
|
100 |
+
# title="Tesla Stock Price",
|
101 |
+
# xaxis_title="Date",
|
102 |
+
# yaxis_title="Price (USD)",
|
103 |
+
# xaxis_rangeslider_visible=True,
|
104 |
+
# ),
|
105 |
+
# )
|
106 |
+
# st.plotly_chart(fig)
|
107 |
+
|
108 |
+
# st.subheader("Line Chart")
|
109 |
+
# # Plot the closing price over time
|
110 |
+
# plot_column = st.selectbox(
|
111 |
+
# "Select a column", ["open", "close", "low", "high"], index=0
|
112 |
+
# )
|
113 |
+
# fig = px.line(
|
114 |
+
# df, x="date", y=plot_column, title=f"Tesla {plot_column} Price Over Time"
|
115 |
+
# )
|
116 |
+
# st.plotly_chart(fig)
|
117 |
+
|
118 |
+
# st.subheader("Distribution of Closing Price")
|
119 |
+
# # Plot the distribution of the closing price
|
120 |
+
# closing_price_hist = px.histogram(
|
121 |
+
# df, x="close", nbins=30, title="Distribution of Tesla Closing Price"
|
122 |
+
# )
|
123 |
+
# st.plotly_chart(closing_price_hist)
|
124 |
+
|
125 |
+
# st.subheader("Raw Data")
|
126 |
+
# st.write("You can see the raw data below.")
|
127 |
+
# # Display the dataset
|
128 |
+
# st.dataframe(df)
|
129 |
+
|
130 |
+
|
131 |
+
def preprocess_context(context):
|
132 |
+
context = context.replace("\n", " ").replace("\t", " ").replace("\r", " ")
|
133 |
+
|
134 |
+
# Remove multiple spaces
|
135 |
+
context = re.sub(" +", " ", context)
|
136 |
+
|
137 |
+
return context
|
138 |
+
|
139 |
+
|
140 |
+
def examples():
|
141 |
+
st.title("Examples")
|
142 |
+
st.write(
|
143 |
+
"This page uses CodeLlama-13b finetuned using QLoRA on NSText2SQL dataset to generate SQL query from a given question and context.\nThe examples are listed below"
|
144 |
+
)
|
145 |
+
|
146 |
+
example_df = load_example_df()
|
147 |
+
example_tabs = st.tabs([f"Example {i+1}" for i in range(len(example_df))])
|
148 |
+
example_btns = []
|
149 |
+
|
150 |
+
for idx, row in example_df.iterrows():
|
151 |
+
with example_tabs[idx]:
|
152 |
+
st.markdown("##### Context:")
|
153 |
+
st.code(row["context"], language="sql")
|
154 |
+
st.markdown("##### Question:")
|
155 |
+
st.text(row["question"])
|
156 |
+
|
157 |
+
example_btns.append(st.button("Generate SQL query", key=f"exp-btn-{idx}"))
|
158 |
+
|
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
|
166 |
+
def interactive_demo():
|
167 |
+
st.title("Text to SQL using CodeLlama-13b")
|
168 |
+
st.write(
|
169 |
+
"This page uses CodeLlama-13b finetuned using QLoRA on NSText2SQL dataset to generate SQL query from a given question and context."
|
170 |
+
)
|
171 |
+
|
172 |
+
st.subheader("Input")
|
173 |
+
context_placeholder = st.empty()
|
174 |
+
question_placeholder = st.empty()
|
175 |
+
context = context_placeholder.text_area(
|
176 |
+
"##### Context",
|
177 |
+
"CREATE TABLE head (name VARCHAR, born_state VARCHAR, age VARCHAR)",
|
178 |
+
key="context",
|
179 |
+
)
|
180 |
+
question = question_placeholder.text_input(
|
181 |
+
"##### Question",
|
182 |
+
"List the name, born state and age of the heads of departments ordered by age.",
|
183 |
+
key="question",
|
184 |
+
)
|
185 |
+
get_sql_button = st.button("Generate SQL query")
|
186 |
+
|
187 |
+
if get_sql_button:
|
188 |
+
st.markdown("##### Output")
|
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
|
196 |
+
def about():
|
197 |
+
st.title("🧑💻 About")
|
198 |
+
st.write(
|
199 |
+
"This Streamlit app allows you to explore stock prices and make predictions using an LSTM model."
|
200 |
+
)
|
201 |
+
|
202 |
+
st.header("Author")
|
203 |
+
st.write(
|
204 |
+
"This app was developed by Minh Nam. You can contact the author at trminhnam20082002@gmail.com."
|
205 |
+
)
|
206 |
+
|
207 |
+
st.header("Data Sources")
|
208 |
+
st.markdown(
|
209 |
+
"The Spider dataset was sourced from [Spider](https://yale-lily.github.io/spider)."
|
210 |
+
)
|
211 |
+
st.markdown("The vMLP dataset is a private dataset from Viettel.")
|
212 |
+
|
213 |
+
st.header("Acknowledgments")
|
214 |
+
st.write(
|
215 |
+
"The author would like to thank Dr. Nguyen Van Nam for his proper guidance, Mr. Nguyen Chi Dong for his support."
|
216 |
+
)
|
217 |
+
|
218 |
+
st.header("License")
|
219 |
+
st.write(
|
220 |
+
# "This app is licensed under the MIT License. See LICENSE.txt for more information."
|
221 |
+
"N/A"
|
222 |
+
)
|
223 |
+
|
224 |
+
|
225 |
+
def references():
|
226 |
+
st.title("📚 References")
|
227 |
+
st.header(
|
228 |
+
"References for Text to SQL project using foundation model - CodeLlama-13b"
|
229 |
+
)
|
230 |
+
|
231 |
+
st.subheader("1. 'Project for time-series data' by AI VIET NAM, et al.")
|
232 |
+
st.write(
|
233 |
+
"This organization provides a tutorial on how to build a stock price prediction model using LSTM in the AIO2022 course."
|
234 |
+
)
|
235 |
+
st.write("Link: https://www.facebook.com/aivietnam.edu.vn")
|
236 |
+
|
237 |
+
st.subheader(
|
238 |
+
"2. 'PyTorch LSTMs for time series forecasting of Indian Stocks' by Vinayak Nayak"
|
239 |
+
)
|
240 |
+
st.write(
|
241 |
+
"This blog post describes how to build a stock price prediction model using LSTM, RNN and CNN-sliding window model."
|
242 |
+
)
|
243 |
+
st.write(
|
244 |
+
"Link: https://medium.com/analytics-vidhya/pytorch-lstms-for-time-series-forecasting-of-indian-stocks-8a49157da8b9#b052"
|
245 |
+
)
|
246 |
+
|
247 |
+
st.header("References for Streamlit")
|
248 |
+
|
249 |
+
st.subheader("1. Streamlit Documentation")
|
250 |
+
st.write(
|
251 |
+
"The official documentation for Streamlit provides detailed information about how to use the library and build Streamlit apps."
|
252 |
+
)
|
253 |
+
st.write("Link: https://docs.streamlit.io/")
|
254 |
+
|
255 |
+
st.subheader("2. Streamlit Community")
|
256 |
+
st.write(
|
257 |
+
"The Streamlit community includes a forum and a GitHub repository with examples and resources for building Streamlit apps."
|
258 |
+
)
|
259 |
+
st.write(
|
260 |
+
"Link: https://discuss.streamlit.io/ and https://github.com/streamlit/streamlit/"
|
261 |
+
)
|
262 |
+
|
263 |
+
|
264 |
+
# Create the sidebar
|
265 |
+
st.sidebar.title("Menu")
|
266 |
+
pages = [
|
267 |
+
"Introduction",
|
268 |
+
"Datasets",
|
269 |
+
"Examples",
|
270 |
+
"Interactive Demo",
|
271 |
+
"About",
|
272 |
+
"References",
|
273 |
+
]
|
274 |
+
selected_page = st.sidebar.radio("Go to", pages)
|
275 |
+
|
276 |
+
# Show the appropriate page based on the selection
|
277 |
+
if selected_page == "Introduction":
|
278 |
+
introduction()
|
279 |
+
elif selected_page == "EDA":
|
280 |
+
eda()
|
281 |
+
elif selected_page == "Examples":
|
282 |
+
examples()
|
283 |
+
elif selected_page == "Interactive Demo":
|
284 |
+
interactive_demo()
|
285 |
+
elif selected_page == "About":
|
286 |
+
about()
|
287 |
+
elif selected_page == "References":
|
288 |
+
references()
|
st_requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
googletrans
|
2 |
+
streamlit
|
3 |
+
numpy
|
4 |
+
pandas
|
5 |
+
googletrans==3.1.0a0
|
6 |
+
langdetect
|
7 |
+
requests
|