Spaces:
Sleeping
Sleeping
Commit
Β·
cb9c774
1
Parent(s):
0c2e6b9
updates
Browse files- app.py +31 -8
- tinysql_dataset_viewer.py +137 -40
app.py
CHANGED
|
@@ -1,13 +1,36 @@
|
|
| 1 |
from tinysql_model_demo import model_demo
|
| 2 |
-
from tinysql_dataset_viewer import dataset_viewer
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from tinysql_model_demo import model_demo
|
| 2 |
+
from tinysql_dataset_viewer import dataset_viewer
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
+
custom_css = """
|
| 6 |
+
:root {
|
| 7 |
+
--martian-orange: #FF6B4A;
|
| 8 |
+
--martian-black: #0A0A0A;
|
| 9 |
+
}
|
| 10 |
|
| 11 |
+
.gradio-container {
|
| 12 |
+
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
|
| 13 |
+
background-color: var(--martian-black) !important;
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
.tab-nav button {
|
| 17 |
+
font-size: 1.1rem !important;
|
| 18 |
+
font-weight: 600 !important;
|
| 19 |
+
padding: 0.75rem 1.5rem !important;
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
.tab-nav button.selected {
|
| 23 |
+
border-bottom: 3px solid var(--martian-orange) !important;
|
| 24 |
+
color: var(--martian-orange) !important;
|
| 25 |
+
}
|
| 26 |
+
"""
|
| 27 |
|
| 28 |
+
with gr.Blocks(css=custom_css, title="TinySQL Demo") as demo:
|
| 29 |
+
with gr.Tabs():
|
| 30 |
+
with gr.Tab("Model Demo"):
|
| 31 |
+
model_demo()
|
| 32 |
+
with gr.Tab("Dataset Viewer"):
|
| 33 |
+
dataset_viewer()
|
| 34 |
+
|
| 35 |
+
if __name__ == "__main__":
|
| 36 |
+
demo.launch()
|
tinysql_dataset_viewer.py
CHANGED
|
@@ -12,11 +12,10 @@ DATASETS = {
|
|
| 12 |
"CS4 Synonyms": "withmartian/cs4_dataset_synonyms",
|
| 13 |
}
|
| 14 |
|
| 15 |
-
# Columns to show
|
| 16 |
COLUMNS = ["create_statement", "english_prompt", "sql_statement"]
|
| 17 |
|
| 18 |
-
# Load small preview of dataset (first 500 rows)
|
| 19 |
def load_preview(dataset_name):
|
|
|
|
| 20 |
try:
|
| 21 |
ds = load_dataset(DATASETS[dataset_name], split="train")
|
| 22 |
df = pd.DataFrame(ds)[COLUMNS].head(500)
|
|
@@ -24,7 +23,18 @@ def load_preview(dataset_name):
|
|
| 24 |
except Exception as e:
|
| 25 |
return pd.DataFrame({"Error": [str(e)]})
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
custom_css = """
|
| 29 |
:root {
|
| 30 |
--martian-orange: #FF6B4A;
|
|
@@ -32,34 +42,34 @@ custom_css = """
|
|
| 32 |
--martian-gray-dark: #1A1A1A;
|
| 33 |
--martian-gray-medium: #2A2A2A;
|
| 34 |
--martian-gray-light: #3A3A3A;
|
| 35 |
-
--martian-bg: #0A0A0A;
|
| 36 |
}
|
| 37 |
|
| 38 |
.gradio-container {
|
| 39 |
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
|
| 40 |
-
background-color: var(--martian-
|
| 41 |
color: #E0E0E0 !important;
|
| 42 |
}
|
| 43 |
|
| 44 |
.header-section {
|
| 45 |
text-align: center;
|
| 46 |
-
padding:
|
| 47 |
background: linear-gradient(135deg, var(--martian-gray-dark) 0%, var(--martian-gray-medium) 100%);
|
| 48 |
-
border-radius:
|
| 49 |
-
margin-bottom:
|
| 50 |
color: white;
|
|
|
|
| 51 |
}
|
| 52 |
|
| 53 |
.header-section h1 {
|
| 54 |
-
font-size: 2rem;
|
| 55 |
font-weight: 700;
|
| 56 |
-
margin-bottom: 0.
|
| 57 |
}
|
| 58 |
|
| 59 |
.header-section .subtitle {
|
| 60 |
-
font-size: 1rem;
|
| 61 |
-
opacity: 0.
|
| 62 |
-
line-height: 1.
|
| 63 |
}
|
| 64 |
|
| 65 |
.orange-accent {
|
|
@@ -67,14 +77,35 @@ custom_css = """
|
|
| 67 |
font-weight: 600;
|
| 68 |
}
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
button.primary {
|
| 71 |
background: var(--martian-orange) !important;
|
| 72 |
border: none !important;
|
| 73 |
color: white !important;
|
|
|
|
| 74 |
}
|
| 75 |
|
| 76 |
button.primary:hover {
|
| 77 |
background: #FF5733 !important;
|
|
|
|
|
|
|
| 78 |
}
|
| 79 |
|
| 80 |
input, select, textarea {
|
|
@@ -83,9 +114,16 @@ input, select, textarea {
|
|
| 83 |
color: #E0E0E0 !important;
|
| 84 |
}
|
| 85 |
|
| 86 |
-
.dataframe
|
| 87 |
background: var(--martian-gray-dark) !important;
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
}
|
| 90 |
"""
|
| 91 |
|
|
@@ -100,45 +138,104 @@ def dataset_viewer():
|
|
| 100 |
</p>
|
| 101 |
</div>
|
| 102 |
""")
|
| 103 |
-
|
| 104 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
with gr.Row():
|
| 106 |
with gr.Column(scale=1):
|
|
|
|
| 107 |
dataset_dropdown = gr.Dropdown(
|
| 108 |
choices=list(DATASETS.keys()),
|
| 109 |
value="CS1",
|
| 110 |
-
label="
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
# Button to open Model Demo in new tab
|
| 114 |
-
gr.Button(
|
| 115 |
-
"Try in Model Demo",
|
| 116 |
-
variant="primary",
|
| 117 |
-
elem_id="open_model_demo"
|
| 118 |
-
).click(
|
| 119 |
-
lambda: "https://huggingface.co/spaces/abir-hr196/tinysql-demo",
|
| 120 |
-
None,
|
| 121 |
-
None,
|
| 122 |
-
_js="(url)=>{ window.open(url,'_blank'); }"
|
| 123 |
)
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
with gr.Column(scale=3):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
df_display = gr.Dataframe(
|
| 127 |
headers=COLUMNS,
|
| 128 |
datatype=["str", "str", "str"],
|
| 129 |
interactive=False,
|
| 130 |
-
|
| 131 |
-
max_rows=20
|
|
|
|
| 132 |
)
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
inputs=dataset_dropdown,
|
| 138 |
-
outputs=df_display
|
| 139 |
)
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
return viewer
|
| 142 |
|
| 143 |
if __name__ == "__main__":
|
| 144 |
-
dataset_viewer().launch()
|
|
|
|
| 12 |
"CS4 Synonyms": "withmartian/cs4_dataset_synonyms",
|
| 13 |
}
|
| 14 |
|
|
|
|
| 15 |
COLUMNS = ["create_statement", "english_prompt", "sql_statement"]
|
| 16 |
|
|
|
|
| 17 |
def load_preview(dataset_name):
|
| 18 |
+
"""Load first 500 rows of selected dataset"""
|
| 19 |
try:
|
| 20 |
ds = load_dataset(DATASETS[dataset_name], split="train")
|
| 21 |
df = pd.DataFrame(ds)[COLUMNS].head(500)
|
|
|
|
| 23 |
except Exception as e:
|
| 24 |
return pd.DataFrame({"Error": [str(e)]})
|
| 25 |
|
| 26 |
+
def filter_dataframe(df, search_query):
|
| 27 |
+
"""Filter dataframe by search query across all columns"""
|
| 28 |
+
if not search_query or df.empty or "Error" in df.columns:
|
| 29 |
+
return df
|
| 30 |
+
|
| 31 |
+
mask = df.astype(str).apply(
|
| 32 |
+
lambda row: row.str.contains(search_query, case=False, na=False).any(),
|
| 33 |
+
axis=1
|
| 34 |
+
)
|
| 35 |
+
return df[mask]
|
| 36 |
+
|
| 37 |
+
# CSS styling
|
| 38 |
custom_css = """
|
| 39 |
:root {
|
| 40 |
--martian-orange: #FF6B4A;
|
|
|
|
| 42 |
--martian-gray-dark: #1A1A1A;
|
| 43 |
--martian-gray-medium: #2A2A2A;
|
| 44 |
--martian-gray-light: #3A3A3A;
|
|
|
|
| 45 |
}
|
| 46 |
|
| 47 |
.gradio-container {
|
| 48 |
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
|
| 49 |
+
background-color: var(--martian-black) !important;
|
| 50 |
color: #E0E0E0 !important;
|
| 51 |
}
|
| 52 |
|
| 53 |
.header-section {
|
| 54 |
text-align: center;
|
| 55 |
+
padding: 2.5rem 1.5rem;
|
| 56 |
background: linear-gradient(135deg, var(--martian-gray-dark) 0%, var(--martian-gray-medium) 100%);
|
| 57 |
+
border-radius: 16px;
|
| 58 |
+
margin-bottom: 2rem;
|
| 59 |
color: white;
|
| 60 |
+
box-shadow: 0 4px 6px rgba(0,0,0,0.3);
|
| 61 |
}
|
| 62 |
|
| 63 |
.header-section h1 {
|
| 64 |
+
font-size: 2.2rem;
|
| 65 |
font-weight: 700;
|
| 66 |
+
margin-bottom: 0.75rem;
|
| 67 |
}
|
| 68 |
|
| 69 |
.header-section .subtitle {
|
| 70 |
+
font-size: 1.1rem;
|
| 71 |
+
opacity: 0.9;
|
| 72 |
+
line-height: 1.6;
|
| 73 |
}
|
| 74 |
|
| 75 |
.orange-accent {
|
|
|
|
| 77 |
font-weight: 600;
|
| 78 |
}
|
| 79 |
|
| 80 |
+
.info-box {
|
| 81 |
+
background: var(--martian-gray-dark);
|
| 82 |
+
border-radius: 12px;
|
| 83 |
+
padding: 1.5rem;
|
| 84 |
+
margin: 1.5rem 0;
|
| 85 |
+
border-left: 4px solid var(--martian-orange);
|
| 86 |
+
color: #E0E0E0;
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
.dataset-guide {
|
| 90 |
+
background: var(--martian-gray-dark);
|
| 91 |
+
border-radius: 8px;
|
| 92 |
+
padding: 1rem;
|
| 93 |
+
margin-top: 1rem;
|
| 94 |
+
font-size: 0.9rem;
|
| 95 |
+
color: #D0D0D0;
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
button.primary {
|
| 99 |
background: var(--martian-orange) !important;
|
| 100 |
border: none !important;
|
| 101 |
color: white !important;
|
| 102 |
+
font-weight: 600 !important;
|
| 103 |
}
|
| 104 |
|
| 105 |
button.primary:hover {
|
| 106 |
background: #FF5733 !important;
|
| 107 |
+
transform: translateY(-1px);
|
| 108 |
+
box-shadow: 0 4px 8px rgba(255, 107, 74, 0.3);
|
| 109 |
}
|
| 110 |
|
| 111 |
input, select, textarea {
|
|
|
|
| 114 |
color: #E0E0E0 !important;
|
| 115 |
}
|
| 116 |
|
| 117 |
+
.dataframe {
|
| 118 |
background: var(--martian-gray-dark) !important;
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
label {
|
| 122 |
+
color: #D0D0D0 !important;
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
.label-wrap span {
|
| 126 |
+
color: var(--martian-orange) !important;
|
| 127 |
}
|
| 128 |
"""
|
| 129 |
|
|
|
|
| 138 |
</p>
|
| 139 |
</div>
|
| 140 |
""")
|
| 141 |
+
|
| 142 |
+
# Info box
|
| 143 |
+
gr.HTML("""
|
| 144 |
+
<div class="info-box">
|
| 145 |
+
<strong>Preview Mode:</strong> Showing first 500 rows of each dataset. Use search to filter results in real-time.
|
| 146 |
+
</div>
|
| 147 |
+
""")
|
| 148 |
+
|
| 149 |
with gr.Row():
|
| 150 |
with gr.Column(scale=1):
|
| 151 |
+
gr.Markdown("### Dataset Selection")
|
| 152 |
dataset_dropdown = gr.Dropdown(
|
| 153 |
choices=list(DATASETS.keys()),
|
| 154 |
value="CS1",
|
| 155 |
+
label="Choose Dataset",
|
| 156 |
+
info="Select a dataset to preview"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
)
|
| 158 |
+
|
| 159 |
+
gr.HTML("""
|
| 160 |
+
<div class="dataset-guide">
|
| 161 |
+
<strong>Complexity Levels:</strong><br><br>
|
| 162 |
+
<strong>CS1:</strong> Basic SELECT-FROM<br>
|
| 163 |
+
<strong>CS2:</strong> Adds ORDER BY<br>
|
| 164 |
+
<strong>CS3:</strong> Aggregations<br>
|
| 165 |
+
<strong>CS4:</strong> Adds WHERE filters<br><br>
|
| 166 |
+
<strong>Synonyms:</strong> Natural language variations
|
| 167 |
+
</div>
|
| 168 |
+
""")
|
| 169 |
+
|
| 170 |
+
load_btn = gr.Button("Load Dataset", variant="primary", size="lg")
|
| 171 |
+
|
| 172 |
+
gr.HTML("<br>")
|
| 173 |
+
|
| 174 |
+
demo_btn = gr.Button("π Try Model Demo", variant="primary")
|
| 175 |
+
|
| 176 |
with gr.Column(scale=3):
|
| 177 |
+
gr.Markdown("### Dataset Preview (First 500 Rows)")
|
| 178 |
+
|
| 179 |
+
search_box = gr.Textbox(
|
| 180 |
+
label="Search",
|
| 181 |
+
placeholder="Search across all columns...",
|
| 182 |
+
lines=1
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
df_display = gr.Dataframe(
|
| 186 |
headers=COLUMNS,
|
| 187 |
datatype=["str", "str", "str"],
|
| 188 |
interactive=False,
|
| 189 |
+
wrap=True,
|
| 190 |
+
max_rows=20,
|
| 191 |
+
label="Results"
|
| 192 |
)
|
| 193 |
+
|
| 194 |
+
stats_display = gr.Markdown("Click 'Load Dataset' to begin")
|
| 195 |
+
|
| 196 |
+
# Store the loaded dataframe
|
| 197 |
+
df_state = gr.State(value=pd.DataFrame())
|
| 198 |
+
|
| 199 |
+
# Load dataset
|
| 200 |
+
def load_and_display(dataset_name):
|
| 201 |
+
df = load_preview(dataset_name)
|
| 202 |
+
if "Error" in df.columns:
|
| 203 |
+
return df, df, "β Error loading dataset"
|
| 204 |
+
stats = f"**Loaded:** {len(df)} rows | **Columns:** {', '.join(COLUMNS)}"
|
| 205 |
+
return df, df, stats
|
| 206 |
+
|
| 207 |
+
load_btn.click(
|
| 208 |
+
fn=load_and_display,
|
| 209 |
inputs=dataset_dropdown,
|
| 210 |
+
outputs=[df_state, df_display, stats_display]
|
| 211 |
)
|
| 212 |
+
|
| 213 |
+
# Search functionality
|
| 214 |
+
def search_and_display(df, query):
|
| 215 |
+
if df.empty:
|
| 216 |
+
return df, "Load a dataset first"
|
| 217 |
+
|
| 218 |
+
filtered_df = filter_dataframe(df, query)
|
| 219 |
+
stats = f"**Showing:** {len(filtered_df)} of {len(df)} rows"
|
| 220 |
+
if query:
|
| 221 |
+
stats += f" | **Search:** '{query}'"
|
| 222 |
+
return filtered_df, stats
|
| 223 |
+
|
| 224 |
+
search_box.change(
|
| 225 |
+
fn=search_and_display,
|
| 226 |
+
inputs=[df_state, search_box],
|
| 227 |
+
outputs=[df_display, stats_display]
|
| 228 |
+
)
|
| 229 |
+
|
| 230 |
+
# Open model demo
|
| 231 |
+
demo_btn.click(
|
| 232 |
+
lambda: None,
|
| 233 |
+
None,
|
| 234 |
+
None,
|
| 235 |
+
_js="()=>{ window.open('https://huggingface.co/spaces/abir-hr196/tinysql-demo','_blank'); }"
|
| 236 |
+
)
|
| 237 |
+
|
| 238 |
return viewer
|
| 239 |
|
| 240 |
if __name__ == "__main__":
|
| 241 |
+
dataset_viewer().launch()
|