Spaces:
Sleeping
Sleeping
Upload client/client.py with huggingface_hub
Browse files- client/client.py +51 -13
client/client.py
CHANGED
|
@@ -1,35 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import requests
|
|
|
|
| 2 |
|
| 3 |
|
| 4 |
class DataCentricClient:
|
| 5 |
-
"""
|
| 6 |
-
OpenEnv client for DataCentric-Env.
|
| 7 |
-
Communicates via HTTP only — never imports from server/.
|
| 8 |
-
"""
|
| 9 |
def __init__(self, base_url: str):
|
| 10 |
self.base_url = base_url.rstrip("/")
|
|
|
|
| 11 |
|
| 12 |
def reset(self, difficulty: str = None) -> dict:
|
| 13 |
-
payload = {
|
|
|
|
|
|
|
| 14 |
r = requests.post(f"{self.base_url}/reset", json=payload, timeout=30)
|
| 15 |
r.raise_for_status()
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
def step(self, action:
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
r.raise_for_status()
|
| 21 |
return r.json()
|
| 22 |
|
| 23 |
def state(self) -> dict:
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
r.raise_for_status()
|
| 26 |
return r.json()
|
| 27 |
|
| 28 |
|
| 29 |
-
# Usage
|
| 30 |
if __name__ == "__main__":
|
| 31 |
client = DataCentricClient("http://localhost:8000")
|
|
|
|
|
|
|
| 32 |
obs = client.reset(difficulty="easy")
|
| 33 |
-
print("
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
client/client.py — OpenEnv client for DataCentric-Env v0.3.
|
| 3 |
+
Communicates via HTTP only — never imports from server/.
|
| 4 |
+
"""
|
| 5 |
import requests
|
| 6 |
+
from typing import Optional
|
| 7 |
|
| 8 |
|
| 9 |
class DataCentricClient:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def __init__(self, base_url: str):
|
| 11 |
self.base_url = base_url.rstrip("/")
|
| 12 |
+
self.session_id: Optional[str] = None
|
| 13 |
|
| 14 |
def reset(self, difficulty: str = None) -> dict:
|
| 15 |
+
payload = {}
|
| 16 |
+
if difficulty:
|
| 17 |
+
payload["difficulty"] = difficulty
|
| 18 |
r = requests.post(f"{self.base_url}/reset", json=payload, timeout=30)
|
| 19 |
r.raise_for_status()
|
| 20 |
+
data = r.json()
|
| 21 |
+
self.session_id = data.get("session_id")
|
| 22 |
+
return data
|
| 23 |
|
| 24 |
+
def step(self, action: str, rec_id: str = None, target_class: int = None) -> dict:
|
| 25 |
+
if not self.session_id:
|
| 26 |
+
raise RuntimeError("Call reset() first to get a session_id.")
|
| 27 |
+
payload = {"session_id": self.session_id, "action": action}
|
| 28 |
+
if rec_id:
|
| 29 |
+
payload["rec_id"] = rec_id
|
| 30 |
+
if target_class is not None:
|
| 31 |
+
payload["target_class"] = target_class
|
| 32 |
+
r = requests.post(f"{self.base_url}/step", json=payload, timeout=30)
|
| 33 |
r.raise_for_status()
|
| 34 |
return r.json()
|
| 35 |
|
| 36 |
def state(self) -> dict:
|
| 37 |
+
if not self.session_id:
|
| 38 |
+
raise RuntimeError("Call reset() first.")
|
| 39 |
+
r = requests.get(f"{self.base_url}/state/{self.session_id}", timeout=30)
|
| 40 |
+
r.raise_for_status()
|
| 41 |
+
return r.json()
|
| 42 |
+
|
| 43 |
+
def metrics(self) -> dict:
|
| 44 |
+
r = requests.get(f"{self.base_url}/metrics", timeout=10)
|
| 45 |
+
r.raise_for_status()
|
| 46 |
+
return r.json()
|
| 47 |
+
|
| 48 |
+
def health(self) -> dict:
|
| 49 |
+
r = requests.get(f"{self.base_url}/health", timeout=10)
|
| 50 |
r.raise_for_status()
|
| 51 |
return r.json()
|
| 52 |
|
| 53 |
|
|
|
|
| 54 |
if __name__ == "__main__":
|
| 55 |
client = DataCentricClient("http://localhost:8000")
|
| 56 |
+
|
| 57 |
+
# Demo episode
|
| 58 |
obs = client.reset(difficulty="easy")
|
| 59 |
+
print(f"Reset: session={obs['session_id']}, acc={obs['current_accuracy']}, target={obs['target_accuracy']}")
|
| 60 |
+
|
| 61 |
+
result = client.step("query_analyst")
|
| 62 |
+
plan = result.get("query_result", {}).get("action_plan", [])
|
| 63 |
+
print(f"Analyst plan: {[p['action'] for p in plan]}")
|
| 64 |
+
|
| 65 |
+
result = client.step("query_cleaner")
|
| 66 |
+
recs = list(result.get("observation", {}).get("pending_recommendations", {}).keys())
|
| 67 |
+
print(f"Cleaner recs: {recs}")
|
| 68 |
+
|
| 69 |
+
if recs:
|
| 70 |
+
result = client.step("apply", rec_id=recs[0])
|
| 71 |
+
print(f"Apply: acc {result['info']['prev_accuracy']} -> {result['info']['new_accuracy']} | reward={result['reward']}")
|
| 72 |
+
|
| 73 |
+
print(f"Metrics: {client.metrics()['sessions']}")
|