Download app.py from Harshalll007/sql-data-janitor-openenv: direct link, hf CLI and curl.
- Browser
- Download file 2.58 kB
-
https://huggingface.co/spaces/Harshalll007/sql-data-janitor-openenv/resolve/main/app.py
- Command line
-
hf download hf://spaces/Harshalll007/sql-data-janitor-openenv/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Harshalll007/sql-data-janitor-openenv/resolve/main/app.py
2.58 kB
| from fastapi import FastAPI, HTTPException | |
| from pydantic import BaseModel | |
| from typing import List, Any, Dict, Optional | |
| import database_logic as db # Importing our messy database logic | |
| app = FastAPI(title="SQL Data Janitor OpenEnv") | |
| # --- Internal State Store --- | |
| # In a real app, you'd use a session, but for this hackathon, | |
| # a global variable is fine for a single-agent environment. | |
| current_conn = db.get_messy_db() | |
| active_task_id = "task_1_null_fixer" | |
| # --- OpenEnv Pydantic Models --- | |
| class Action(BaseModel): | |
| query: str | |
| class Observation(BaseModel): | |
| query_result: List[Dict[str, Any]] | |
| error_message: str | |
| schema_info: str | |
| class StepResponse(BaseModel): | |
| observation: Observation | |
| reward: float | |
| done: bool | |
| info: Dict[str, Any] | |
| # --- Helper to get Schema --- | |
| def get_schema_hint(): | |
| return "Tables: users (id, name, email), products (id, item, price), leads (email, interactions)" | |
| # --- Required OpenEnv Endpoints --- | |
| def health_check(): | |
| return {"status": "ready", "environment": "sql-data-janitor"} | |
| def reset(task_id: Optional[str] = "task_1_null_fixer"): | |
| global current_conn, active_task_id | |
| active_task_id = task_id | |
| current_conn = db.get_messy_db() # Refresh the messy data | |
| obs = Observation( | |
| query_result=[], | |
| error_message="", | |
| schema_info=get_schema_hint() | |
| ) | |
| return {"observation": obs.model_dump()} | |
| def step(action: Action): | |
| global current_conn, active_task_id | |
| # 1. Execute the AI's SQL query | |
| results, error = db.run_query(current_conn, action.query) | |
| # 2. Check if they fixed the problem (Grading) | |
| reward = 0.0 | |
| done = False | |
| if active_task_id == "task_1_null_fixer": | |
| reward = db.grade_task_1(current_conn) | |
| elif active_task_id == "task_2_price_standardizer": | |
| reward = db.grade_task_2(current_conn) | |
| elif active_task_id == "task_3_duplicate_merger": | |
| reward = db.grade_task_3(current_conn) | |
| if reward >= 1.0: | |
| done = True # Task complete! | |
| obs = Observation( | |
| query_result=results[:10], # Limit results so we don't blow up the LLM context | |
| error_message=error, | |
| schema_info=get_schema_hint() | |
| ) | |
| return StepResponse( | |
| observation=obs, | |
| reward=reward, | |
| done=done, | |
| info={"active_task": active_task_id} | |
| ) | |
| def state(): | |
| return { | |
| "active_task": active_task_id, | |
| "database_status": "connected" | |
| } |