Download inference.py from Harshalll007/sql-data-janitor-openenv: direct link, hf CLI and curl.
- Browser
- Download file 2.26 kB
-
https://huggingface.co/spaces/Harshalll007/sql-data-janitor-openenv/resolve/main/inference.py
- Command line
-
hf download hf://spaces/Harshalll007/sql-data-janitor-openenv/inference.py
-
curl -L -o inference.py https://huggingface.co/spaces/Harshalll007/sql-data-janitor-openenv/resolve/main/inference.py
2.26 kB
| import os | |
| import re | |
| from openai import OpenAI | |
| # 1. Config (Required by OpenEnv checklist) | |
| API_BASE_URL = os.getenv("API_BASE_URL", "http://127.0.0.1:7860") | |
| MODEL_NAME = os.getenv("MODEL_NAME", "gpt-4") # Or whichever model you are using | |
| HF_TOKEN = os.getenv("HF_TOKEN", "") | |
| # 2. Setup the Client | |
| # Note: We point base_url to the environment itself if testing locally, | |
| # but the script uses OpenAI client for the LLM logic as requested. | |
| client = OpenAI(base_url="https://router.huggingface.co/v1", api_key=HF_TOKEN) | |
| def get_action_from_llm(task_description, observation): | |
| prompt = f""" | |
| You are a SQL Data Janitor. Your goal is: {task_description} | |
| Current Schema: {observation.get('schema_info')} | |
| Last Query Results: {observation.get('query_result')} | |
| Last Error: {observation.get('error_message')} | |
| Provide EXACTLY one SQL query to help solve the task. | |
| Respond only with the SQL query string. | |
| """ | |
| response = client.chat.completions.create( | |
| model=MODEL_NAME, | |
| messages=[{"role": "user", "content": prompt}] | |
| ) | |
| return response.choices[0].message.content.strip() | |
| def main(): | |
| # We use httpx to talk to our own environment endpoints | |
| import httpx | |
| tasks = ["task_1_null_fixer", "task_2_price_standardizer", "task_3_duplicate_merger"] | |
| for task_id in tasks: | |
| print(f"\n--- Starting Task: {task_id} ---") | |
| # Reset Environment for specific task | |
| res = httpx.post(f"{API_BASE_URL}/reset?task_id={task_id}") | |
| obs = res.json()["observation"] | |
| for step in range(5): # Allow up to 5 attempts per task | |
| sql_query = get_action_from_llm(task_id, obs) | |
| print(f"Step {step+1} | AI Query: {sql_query}") | |
| # Send action to environment | |
| step_res = httpx.post(f"{API_BASE_URL}/step", json={"query": sql_query}) | |
| data = step_res.json() | |
| obs = data["observation"] | |
| reward = data["reward"] | |
| done = data["done"] | |
| print(f"Reward: {reward} | Done: {done}") | |
| if done: | |
| print(f"SUCCESS: {task_id} completed!") | |
| break | |
| if __name__ == "__main__": | |
| main() |