Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- backend/main.py +17 -14
- tests/test_api.py +25 -0
backend/main.py
CHANGED
|
@@ -157,26 +157,29 @@ def download_dataset():
|
|
| 157 |
|
| 158 |
@app.get("/dataset")
|
| 159 |
def get_dataset(
|
| 160 |
-
page: int =
|
| 161 |
-
limit: int =
|
| 162 |
disease: Optional[str] = None,
|
| 163 |
search: Optional[str] = None,
|
| 164 |
all: bool = False
|
| 165 |
):
|
| 166 |
-
"""
|
| 167 |
if ml_engine and os.path.exists(ml_engine.dataset_path):
|
| 168 |
try:
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
)
|
|
|
|
|
|
|
|
|
|
| 180 |
except Exception as e:
|
| 181 |
return {"error": f"Failed to read dataset: {str(e)}"}
|
| 182 |
return {"error": "Dataset not found"}
|
|
|
|
| 157 |
|
| 158 |
@app.get("/dataset")
|
| 159 |
def get_dataset(
|
| 160 |
+
page: Optional[int] = None,
|
| 161 |
+
limit: Optional[int] = None,
|
| 162 |
disease: Optional[str] = None,
|
| 163 |
search: Optional[str] = None,
|
| 164 |
all: bool = False
|
| 165 |
):
|
| 166 |
+
"""Telemetry records explorer supporting unparameterized list & paginated queries."""
|
| 167 |
if ml_engine and os.path.exists(ml_engine.dataset_path):
|
| 168 |
try:
|
| 169 |
+
# If explicit pagination is requested
|
| 170 |
+
if page is not None or limit is not None or disease is not None or search is not None:
|
| 171 |
+
return get_paginated_rows(
|
| 172 |
+
ml_engine.dataset_path,
|
| 173 |
+
page=page or 1,
|
| 174 |
+
limit=min(limit or 25, 100),
|
| 175 |
+
disease=disease,
|
| 176 |
+
search=search
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
# Unparameterized call (backward compatibility with API consumers & tests)
|
| 180 |
+
df = pd.read_csv(ml_engine.dataset_path, encoding='utf-8')
|
| 181 |
+
df.columns = [c.strip() for c in df.columns]
|
| 182 |
+
return df.to_dict(orient="records")
|
| 183 |
except Exception as e:
|
| 184 |
return {"error": f"Failed to read dataset: {str(e)}"}
|
| 185 |
return {"error": "Dataset not found"}
|
tests/test_api.py
CHANGED
|
@@ -21,6 +21,31 @@ def test_dataset_endpoint(client):
|
|
| 21 |
assert response.status_code == 200
|
| 22 |
assert isinstance(response.json(), list)
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
def test_export_tinyml_random_forest(client):
|
| 25 |
response = client.get("/export_tinyml?model_name=rf")
|
| 26 |
|
|
|
|
| 21 |
assert response.status_code == 200
|
| 22 |
assert isinstance(response.json(), list)
|
| 23 |
|
| 24 |
+
def test_dataset_paginated_endpoint(client):
|
| 25 |
+
response = client.get("/dataset?page=1&limit=25")
|
| 26 |
+
assert response.status_code == 200
|
| 27 |
+
data = response.json()
|
| 28 |
+
assert isinstance(data, dict)
|
| 29 |
+
assert "total" in data
|
| 30 |
+
assert "rows" in data
|
| 31 |
+
assert isinstance(data["rows"], list)
|
| 32 |
+
assert len(data["rows"]) <= 25
|
| 33 |
+
|
| 34 |
+
def test_dataset_summary_endpoint(client):
|
| 35 |
+
response = client.get("/dataset/summary")
|
| 36 |
+
assert response.status_code == 200
|
| 37 |
+
data = response.json()
|
| 38 |
+
assert isinstance(data, dict)
|
| 39 |
+
assert "summary" in data
|
| 40 |
+
assert data["summary"]["total_records"] == 60000
|
| 41 |
+
assert "target_distribution" in data
|
| 42 |
+
assert "correlation_matrix" in data
|
| 43 |
+
|
| 44 |
+
def test_dataset_download_endpoint(client):
|
| 45 |
+
response = client.get("/dataset/download")
|
| 46 |
+
assert response.status_code == 200
|
| 47 |
+
assert "text/csv" in response.headers.get("content-type", "")
|
| 48 |
+
|
| 49 |
def test_export_tinyml_random_forest(client):
|
| 50 |
response = client.get("/export_tinyml?model_name=rf")
|
| 51 |
|