Spaces:
Sleeping
Sleeping
Download inference.py from hackrock18/cloud-data-janitor: direct link, hf CLI and curl.
- Browser
- Download file 3.77 kB
-
https://huggingface.co/spaces/hackrock18/cloud-data-janitor/resolve/main/inference.py
- Command line
-
hf download hf://spaces/hackrock18/cloud-data-janitor/inference.py
-
curl -L -o inference.py https://huggingface.co/spaces/hackrock18/cloud-data-janitor/resolve/main/inference.py
3.77 kB
| import json | |
| import os | |
| from environment.cloud_env import CloudDataJanitorEnv | |
| from openai import OpenAI | |
| def create_client(): | |
| try: | |
| api_key = os.getenv("API_KEY", "") | |
| base_url = os.getenv( | |
| "API_BASE_URL", | |
| "https://router.huggingface.co/v1" | |
| ) | |
| return OpenAI( | |
| api_key=api_key, | |
| base_url=base_url | |
| ) | |
| except Exception as e: | |
| print("OpenAI initialization failed:", str(e)) | |
| return None | |
| API_BASE_URL = os.environ.get( | |
| "API_BASE_URL", | |
| "https://router.huggingface.co/v1" | |
| ) | |
| API_KEY = os.environ.get( | |
| "API_KEY", | |
| "" | |
| ) | |
| MODEL_NAME = os.environ.get( | |
| "MODEL_NAME", | |
| "Qwen/Qwen2.5-72B-Instruct" | |
| ) | |
| MAX_STEPS = 10 | |
| # IMPORTANT — run ALL tasks automatically | |
| TASKS = ["easy", "medium", "hard"] | |
| client = create_client() | |
| class BaselineLLMAgent: | |
| def choose_action(self, state): | |
| if client is None: | |
| return {"action": "done"} | |
| prompt = f""" | |
| You are a Data Janitor managing a cloud drive. | |
| Current State: | |
| {state} | |
| Choose ONE action. | |
| Reply with ONLY valid JSON. | |
| Valid formats: | |
| {{"action": "move_file", "filename": "report_q1.pdf", "destination": "Documents"}} | |
| {{"action": "delete_file", "filename": "duplicate.pdf"}} | |
| {{"action": "zip_folder", "folder_name": "OldFiles"}} | |
| {{"action": "done"}} | |
| """ | |
| try: | |
| response = client.chat.completions.create( | |
| model=MODEL_NAME, | |
| messages=[ | |
| { | |
| "role": "user", | |
| "content": prompt | |
| } | |
| ], | |
| temperature=0.1 | |
| ) | |
| raw_output = ( | |
| response | |
| .choices[0] | |
| .message | |
| .content | |
| .strip("` \n") | |
| ) | |
| if raw_output.startswith("json"): | |
| raw_output = raw_output[4:] | |
| return json.loads(raw_output) | |
| except Exception: | |
| return {"action": "done"} | |
| agent = BaselineLLMAgent() | |
| # RUN ALL TASKS | |
| for TASK_NAME in TASKS: | |
| env = CloudDataJanitorEnv( | |
| task_name=TASK_NAME | |
| ) | |
| rewards = [] | |
| success = False | |
| try: | |
| print( | |
| "[START] " | |
| f"task={TASK_NAME} " | |
| "env=cloud-data-janitor " | |
| "model=baseline-llm" | |
| ) | |
| state = env.reset() | |
| for step in range(1, MAX_STEPS + 1): | |
| action = agent.choose_action(state) | |
| state, reward, done, info = env.step(action) | |
| rewards.append(reward) | |
| error = info.get( | |
| "last_action_error" | |
| ) | |
| print( | |
| "[STEP] " | |
| f"step={step} " | |
| f"action={action} " | |
| f"reward={reward:.2f} " | |
| f"done={str(done).lower()} " | |
| f"error={error if error else 'null'}" | |
| ) | |
| if done: | |
| success = True | |
| break | |
| if not success and rewards: | |
| success = done | |
| except Exception as e: | |
| print( | |
| "[STEP] " | |
| "step=0 " | |
| "action=error " | |
| "reward=0.00 " | |
| "done=true " | |
| f"error={str(e)}" | |
| ) | |
| finally: | |
| env.close() | |
| if rewards: | |
| score = rewards[-1] | |
| rewards_str = ",".join( | |
| f"{r:.2f}" | |
| for r in rewards | |
| ) | |
| else: | |
| score = 0.0 | |
| rewards_str = "0.00" | |
| print( | |
| "[END] " | |
| f"success={str(success).lower()} " | |
| f"steps={len(rewards)} " | |
| f"score={score:.2f} " | |
| f"rewards={rewards_str}" | |
| ) |