Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
# --------------------------------------------------------------
|
| 2 |
-
# gradio_s3_sql.py
|
| 3 |
# --------------------------------------------------------------
|
| 4 |
import boto3
|
| 5 |
import pandas as pd
|
|
@@ -14,7 +14,6 @@ ACCESS_KEY = "rNuPBAQetemqpEeBospZ"
|
|
| 14 |
SECRET_KEY = "BU4FccUYxzXVqiWjPSJM1CWEX1cNhBqbU9NeGidE"
|
| 15 |
BUCKET_NAME = "accusagas3"
|
| 16 |
|
| 17 |
-
# Initialize S3 client
|
| 18 |
s3 = boto3.client(
|
| 19 |
"s3",
|
| 20 |
endpoint_url=ENDPOINT_URL,
|
|
@@ -24,31 +23,29 @@ s3 = boto3.client(
|
|
| 24 |
|
| 25 |
|
| 26 |
def run_sql(path: str, sql: str) -> pd.DataFrame:
|
| 27 |
-
"""
|
| 28 |
-
# ---
|
| 29 |
try:
|
| 30 |
obj = s3.get_object(Bucket=BUCKET_NAME, Key=path)
|
| 31 |
-
|
| 32 |
-
df = pd.read_csv(StringIO(csv_content))
|
| 33 |
except ClientError as e:
|
| 34 |
if e.response["Error"]["Code"] == "NoSuchKey":
|
| 35 |
return pd.DataFrame({"error": [f"File not found: {path}"]})
|
| 36 |
-
return pd.DataFrame({"error": [f"S3 Error: {
|
| 37 |
except Exception as e:
|
| 38 |
-
return pd.DataFrame({"error": [f"CSV read failed: {
|
| 39 |
|
| 40 |
if df.empty:
|
| 41 |
return pd.DataFrame({"error": ["CSV is empty"]})
|
| 42 |
|
| 43 |
-
# ---
|
| 44 |
numeric_keywords = ["price", "amount", "value", "cost", "revenue", "total", "volume", "open", "high", "low", "close"]
|
| 45 |
for col in df.columns:
|
| 46 |
if any(kw in col.lower() for kw in numeric_keywords):
|
| 47 |
-
# Clean: remove $, %, commas
|
| 48 |
cleaned = df[col].astype(str).str.replace(r"[^\d.-]", "", regex=True)
|
| 49 |
df[col] = pd.to_numeric(cleaned, errors="coerce")
|
| 50 |
|
| 51 |
-
# ---
|
| 52 |
con = duckdb.connect(":memory:")
|
| 53 |
con.register("data", df)
|
| 54 |
|
|
@@ -60,7 +57,7 @@ def run_sql(path: str, sql: str) -> pd.DataFrame:
|
|
| 60 |
try:
|
| 61 |
result = con.execute(sql).df()
|
| 62 |
except Exception as e:
|
| 63 |
-
# Auto-fix
|
| 64 |
if "Cannot compare values of type VARCHAR" in str(e):
|
| 65 |
import re
|
| 66 |
match = re.search(r"column ([a-zA-Z0-9_]+)", str(e), re.I)
|
|
@@ -71,7 +68,7 @@ def run_sql(path: str, sql: str) -> pd.DataFrame:
|
|
| 71 |
result = con.execute(fixed_sql).df()
|
| 72 |
except Exception as e2:
|
| 73 |
con.close()
|
| 74 |
-
return pd.DataFrame({"error": [f"SQL failed even after
|
| 75 |
else:
|
| 76 |
con.close()
|
| 77 |
return pd.DataFrame({"error": [f"Type error: {e}"]})
|
|
@@ -81,19 +78,18 @@ def run_sql(path: str, sql: str) -> pd.DataFrame:
|
|
| 81 |
finally:
|
| 82 |
con.close()
|
| 83 |
|
| 84 |
-
# Limit to 10,000 rows
|
| 85 |
return result.head(10_000)
|
| 86 |
|
| 87 |
|
| 88 |
# --------------------------------------------------------------
|
| 89 |
-
# Gradio
|
| 90 |
# --------------------------------------------------------------
|
| 91 |
with gr.Blocks(title="S3 SQL Query (iDrive e2)") as demo:
|
| 92 |
gr.Markdown(
|
| 93 |
"""
|
| 94 |
# S3 CSV SQL Explorer
|
| 95 |
-
|
| 96 |
-
Table name: `data` | Auto-casts `Price`, `Amount`, etc.
|
| 97 |
"""
|
| 98 |
)
|
| 99 |
|
|
@@ -110,14 +106,30 @@ with gr.Blocks(title="S3 SQL Query (iDrive e2)") as demo:
|
|
| 110 |
)
|
| 111 |
|
| 112 |
run_btn = gr.Button("Run SQL", variant="primary")
|
| 113 |
-
|
|
|
|
|
|
|
| 114 |
label="Result",
|
| 115 |
interactive=False,
|
| 116 |
wrap=True,
|
| 117 |
-
height=500,
|
| 118 |
)
|
| 119 |
|
| 120 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
run_btn.click(
|
| 122 |
fn=run_sql,
|
| 123 |
inputs=[path_input, sql_input],
|
|
@@ -125,7 +137,6 @@ with gr.Blocks(title="S3 SQL Query (iDrive e2)") as demo:
|
|
| 125 |
show_progress=True,
|
| 126 |
)
|
| 127 |
|
| 128 |
-
# Examples
|
| 129 |
gr.Examples(
|
| 130 |
examples=[
|
| 131 |
[
|
|
@@ -136,23 +147,10 @@ with gr.Blocks(title="S3 SQL Query (iDrive e2)") as demo:
|
|
| 136 |
"vatsav_123/reports/Gold Futures Historical Data.csv",
|
| 137 |
"SELECT MIN(Price) AS min_price, MAX(Price) AS max_price FROM data"
|
| 138 |
],
|
| 139 |
-
[
|
| 140 |
-
"vatsav_123/reports/Gold Futures Historical Data.csv",
|
| 141 |
-
"SELECT * FROM data WHERE Volume > 1000000 LIMIT 5"
|
| 142 |
-
],
|
| 143 |
],
|
| 144 |
inputs=[path_input, sql_input],
|
| 145 |
)
|
| 146 |
|
| 147 |
-
gr.Markdown(
|
| 148 |
-
"""
|
| 149 |
-
**Tips**
|
| 150 |
-
- Use `data` as table name
|
| 151 |
-
- Columns like `Price`, `Volume`, `Amount` are auto-converted to numbers
|
| 152 |
-
- Invalid SQL β clear error message
|
| 153 |
-
"""
|
| 154 |
-
)
|
| 155 |
-
|
| 156 |
|
| 157 |
# --------------------------------------------------------------
|
| 158 |
# Launch
|
|
@@ -161,7 +159,6 @@ if __name__ == "__main__":
|
|
| 161 |
demo.launch(
|
| 162 |
server_name="0.0.0.0",
|
| 163 |
server_port=7860,
|
| 164 |
-
share=False,
|
| 165 |
debug=True,
|
| 166 |
-
mcp_server=True
|
| 167 |
)
|
|
|
|
| 1 |
# --------------------------------------------------------------
|
| 2 |
+
# gradio_s3_sql.py (Gradio-compatible, no 'height' error)
|
| 3 |
# --------------------------------------------------------------
|
| 4 |
import boto3
|
| 5 |
import pandas as pd
|
|
|
|
| 14 |
SECRET_KEY = "BU4FccUYxzXVqiWjPSJM1CWEX1cNhBqbU9NeGidE"
|
| 15 |
BUCKET_NAME = "accusagas3"
|
| 16 |
|
|
|
|
| 17 |
s3 = boto3.client(
|
| 18 |
"s3",
|
| 19 |
endpoint_url=ENDPOINT_URL,
|
|
|
|
| 23 |
|
| 24 |
|
| 25 |
def run_sql(path: str, sql: str) -> pd.DataFrame:
|
| 26 |
+
"""S3 β CSV β DuckDB β SQL β DataFrame (auto-cast + auto-fix)"""
|
| 27 |
+
# --- Load CSV ---
|
| 28 |
try:
|
| 29 |
obj = s3.get_object(Bucket=BUCKET_NAME, Key=path)
|
| 30 |
+
df = pd.read_csv(StringIO(obj["Body"].read().decode("utf-8")))
|
|
|
|
| 31 |
except ClientError as e:
|
| 32 |
if e.response["Error"]["Code"] == "NoSuchKey":
|
| 33 |
return pd.DataFrame({"error": [f"File not found: {path}"]})
|
| 34 |
+
return pd.DataFrame({"error": [f"S3 Error: {e}"]})
|
| 35 |
except Exception as e:
|
| 36 |
+
return pd.DataFrame({"error": [f"CSV read failed: {e}"]})
|
| 37 |
|
| 38 |
if df.empty:
|
| 39 |
return pd.DataFrame({"error": ["CSV is empty"]})
|
| 40 |
|
| 41 |
+
# --- Auto-convert numeric columns ---
|
| 42 |
numeric_keywords = ["price", "amount", "value", "cost", "revenue", "total", "volume", "open", "high", "low", "close"]
|
| 43 |
for col in df.columns:
|
| 44 |
if any(kw in col.lower() for kw in numeric_keywords):
|
|
|
|
| 45 |
cleaned = df[col].astype(str).str.replace(r"[^\d.-]", "", regex=True)
|
| 46 |
df[col] = pd.to_numeric(cleaned, errors="coerce")
|
| 47 |
|
| 48 |
+
# --- DuckDB ---
|
| 49 |
con = duckdb.connect(":memory:")
|
| 50 |
con.register("data", df)
|
| 51 |
|
|
|
|
| 57 |
try:
|
| 58 |
result = con.execute(sql).df()
|
| 59 |
except Exception as e:
|
| 60 |
+
# Auto-fix VARCHAR vs number
|
| 61 |
if "Cannot compare values of type VARCHAR" in str(e):
|
| 62 |
import re
|
| 63 |
match = re.search(r"column ([a-zA-Z0-9_]+)", str(e), re.I)
|
|
|
|
| 68 |
result = con.execute(fixed_sql).df()
|
| 69 |
except Exception as e2:
|
| 70 |
con.close()
|
| 71 |
+
return pd.DataFrame({"error": [f"SQL failed even after CAST: {e2}"]})
|
| 72 |
else:
|
| 73 |
con.close()
|
| 74 |
return pd.DataFrame({"error": [f"Type error: {e}"]})
|
|
|
|
| 78 |
finally:
|
| 79 |
con.close()
|
| 80 |
|
|
|
|
| 81 |
return result.head(10_000)
|
| 82 |
|
| 83 |
|
| 84 |
# --------------------------------------------------------------
|
| 85 |
+
# Gradio UI (no 'height' on Dataframe)
|
| 86 |
# --------------------------------------------------------------
|
| 87 |
with gr.Blocks(title="S3 SQL Query (iDrive e2)") as demo:
|
| 88 |
gr.Markdown(
|
| 89 |
"""
|
| 90 |
# S3 CSV SQL Explorer
|
| 91 |
+
Query any CSV in your iDrive e2 bucket using SQL
|
| 92 |
+
Table name: `data` | Auto-casts `Price`, `Amount`, etc.
|
| 93 |
"""
|
| 94 |
)
|
| 95 |
|
|
|
|
| 106 |
)
|
| 107 |
|
| 108 |
run_btn = gr.Button("Run SQL", variant="primary")
|
| 109 |
+
|
| 110 |
+
# Use DataGrid + CSS for fixed height
|
| 111 |
+
output = gr.DataGrid(
|
| 112 |
label="Result",
|
| 113 |
interactive=False,
|
| 114 |
wrap=True,
|
|
|
|
| 115 |
)
|
| 116 |
|
| 117 |
+
# Apply height via CSS
|
| 118 |
+
demo.load(
|
| 119 |
+
None,
|
| 120 |
+
None,
|
| 121 |
+
None,
|
| 122 |
+
_js=f"""
|
| 123 |
+
() => {{
|
| 124 |
+
setTimeout(() => {{
|
| 125 |
+
const grid = document.querySelector('.gradio-container .data-grid');
|
| 126 |
+
if (grid) grid.style.maxHeight = '500px';
|
| 127 |
+
if (grid) grid.style.overflowY = 'auto';
|
| 128 |
+
}}, 100);
|
| 129 |
+
}}
|
| 130 |
+
"""
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
run_btn.click(
|
| 134 |
fn=run_sql,
|
| 135 |
inputs=[path_input, sql_input],
|
|
|
|
| 137 |
show_progress=True,
|
| 138 |
)
|
| 139 |
|
|
|
|
| 140 |
gr.Examples(
|
| 141 |
examples=[
|
| 142 |
[
|
|
|
|
| 147 |
"vatsav_123/reports/Gold Futures Historical Data.csv",
|
| 148 |
"SELECT MIN(Price) AS min_price, MAX(Price) AS max_price FROM data"
|
| 149 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
],
|
| 151 |
inputs=[path_input, sql_input],
|
| 152 |
)
|
| 153 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
# --------------------------------------------------------------
|
| 156 |
# Launch
|
|
|
|
| 159 |
demo.launch(
|
| 160 |
server_name="0.0.0.0",
|
| 161 |
server_port=7860,
|
| 162 |
+
share=False,
|
| 163 |
debug=True,
|
|
|
|
| 164 |
)
|