TanujInsane commited on
Commit ·
99d66ea
1
Parent(s): 49234f8
fix: update inference.py with API_BASE_URL and HTTP client format
Browse files- .gitignore +0 -0
- inference.py +53 -0
.gitignore
CHANGED
|
Binary files a/.gitignore and b/.gitignore differ
|
|
|
inference.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
import time
|
| 4 |
+
|
| 5 |
+
# Default points to the direct HuggingFace Space API URL
|
| 6 |
+
API_BASE_URL = os.environ.get(
|
| 7 |
+
"API_BASE_URL",
|
| 8 |
+
"https://tanujinsane-document-classification-env.hf.space"
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
def run_inference(difficulty="easy", seed=42):
|
| 12 |
+
print(f"Connecting to OpenEnv environment at {API_BASE_URL}...")
|
| 13 |
+
|
| 14 |
+
# 1. Reset the environment
|
| 15 |
+
reset_payload = {"difficulty": difficulty, "seed": seed}
|
| 16 |
+
reset_response = requests.post(f"{API_BASE_URL}/reset", json=reset_payload)
|
| 17 |
+
reset_response.raise_for_status()
|
| 18 |
+
|
| 19 |
+
obs = reset_response.json()
|
| 20 |
+
|
| 21 |
+
done = False
|
| 22 |
+
total_reward = 0.0
|
| 23 |
+
start_time = time.time()
|
| 24 |
+
|
| 25 |
+
# 2. Step through the environment
|
| 26 |
+
while not done:
|
| 27 |
+
# Example agent logic (replace with your intelligent agent)
|
| 28 |
+
action = 0
|
| 29 |
+
|
| 30 |
+
step_payload = {"action": action}
|
| 31 |
+
step_response = requests.post(f"{API_BASE_URL}/step", json=step_payload)
|
| 32 |
+
step_response.raise_for_status()
|
| 33 |
+
|
| 34 |
+
result = step_response.json()
|
| 35 |
+
|
| 36 |
+
obs = result.get("observation")
|
| 37 |
+
reward = result.get("reward", 0.0)
|
| 38 |
+
done = result.get("done", False)
|
| 39 |
+
info = result.get("info", {})
|
| 40 |
+
|
| 41 |
+
total_reward += reward
|
| 42 |
+
|
| 43 |
+
elapsed = time.time() - start_time
|
| 44 |
+
|
| 45 |
+
return {
|
| 46 |
+
"difficulty": difficulty,
|
| 47 |
+
"score": total_reward,
|
| 48 |
+
"time": elapsed
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
if __name__ == "__main__":
|
| 52 |
+
result = run_inference()
|
| 53 |
+
print("Inference completed:", result)
|