Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -60,8 +60,6 @@ def get_parquet_path(config: str, shard: int = 0) -> str:
|
|
| 60 |
def query_parquet(config: str, sql: str, params: dict = None) -> list:
|
| 61 |
"""Execute SQL query on parquet file."""
|
| 62 |
path = get_parquet_path(config)
|
| 63 |
-
print(f"Querying parquet: {path}")
|
| 64 |
-
print(f"SQL: {sql}")
|
| 65 |
try:
|
| 66 |
conn = duckdb.connect(":memory:")
|
| 67 |
conn.execute(f"CREATE VIEW data AS SELECT * FROM read_parquet('{path}')")
|
|
@@ -72,12 +70,31 @@ def query_parquet(config: str, sql: str, params: dict = None) -> list:
|
|
| 72 |
result = conn.execute(sql).fetchdf()
|
| 73 |
|
| 74 |
conn.close()
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
except Exception as e:
|
| 79 |
-
|
| 80 |
-
raise HTTPException(status_code=500, detail=f"Query error: {str(e)}")
|
| 81 |
|
| 82 |
|
| 83 |
@app.get("/")
|
|
|
|
| 60 |
def query_parquet(config: str, sql: str, params: dict = None) -> list:
|
| 61 |
"""Execute SQL query on parquet file."""
|
| 62 |
path = get_parquet_path(config)
|
|
|
|
|
|
|
| 63 |
try:
|
| 64 |
conn = duckdb.connect(":memory:")
|
| 65 |
conn.execute(f"CREATE VIEW data AS SELECT * FROM read_parquet('{path}')")
|
|
|
|
| 70 |
result = conn.execute(sql).fetchdf()
|
| 71 |
|
| 72 |
conn.close()
|
| 73 |
+
|
| 74 |
+
# Convert to JSON-safe format
|
| 75 |
+
import json
|
| 76 |
+
import numpy as np
|
| 77 |
+
|
| 78 |
+
def clean_value(v):
|
| 79 |
+
if v is None:
|
| 80 |
+
return None
|
| 81 |
+
if isinstance(v, float) and (np.isnan(v) or np.isinf(v)):
|
| 82 |
+
return None
|
| 83 |
+
if isinstance(v, (np.integer, np.int64)):
|
| 84 |
+
return int(v)
|
| 85 |
+
if isinstance(v, (np.floating, np.float64)):
|
| 86 |
+
return float(v)
|
| 87 |
+
return v
|
| 88 |
+
|
| 89 |
+
records = []
|
| 90 |
+
for _, row in result.iterrows():
|
| 91 |
+
record = {k: clean_value(v) for k, v in row.items()}
|
| 92 |
+
records.append(record)
|
| 93 |
+
|
| 94 |
+
return records
|
| 95 |
except Exception as e:
|
| 96 |
+
import traceback
|
| 97 |
+
raise HTTPException(status_code=500, detail=f"Query error: {str(e)}. Traceback: {traceback.format_exc()}")
|
| 98 |
|
| 99 |
|
| 100 |
@app.get("/")
|