Spaces:
Build error
Build error
Upload folder using huggingface_hub
Browse files- inference.py +36 -22
inference.py
CHANGED
|
@@ -8,15 +8,16 @@ Required env vars: API_BASE_URL, MODEL_NAME, HF_TOKEN
|
|
| 8 |
import asyncio
|
| 9 |
import json
|
| 10 |
import os
|
| 11 |
-
from typing import List
|
| 12 |
|
| 13 |
from openai import OpenAI
|
| 14 |
|
| 15 |
API_BASE_URL = os.getenv("API_BASE_URL", "https://api.openai.com/v1")
|
| 16 |
MODEL_NAME = os.getenv("MODEL_NAME", "gpt-4o-mini")
|
| 17 |
API_KEY = os.getenv("HF_TOKEN")
|
| 18 |
-
|
| 19 |
|
|
|
|
| 20 |
BENCHMARK = "doc_edit_game_v2"
|
| 21 |
TASKS = ["legal_easy", "legal_medium", "legal_hard", "pharma_easy", "pharma_hard"]
|
| 22 |
SUCCESS_THRESHOLD = 0.90
|
|
@@ -25,9 +26,9 @@ SUCCESS_THRESHOLD = 0.90
|
|
| 25 |
def log_start(task: str, env: str, model: str):
|
| 26 |
print(f"[START] task={task} env={env} model={model}", flush=True)
|
| 27 |
|
| 28 |
-
def log_step(step: int, action:
|
| 29 |
error_val = error if error else "null"
|
| 30 |
-
print(f"[STEP] step={step} action={
|
| 31 |
|
| 32 |
def log_end(success: bool, steps: int, score: float, rewards: List[float]):
|
| 33 |
rewards_str = ",".join(f"{r:.2f}" for r in rewards)
|
|
@@ -62,12 +63,6 @@ Rules:
|
|
| 62 |
- ONE tool call per response, as valid JSON (no markdown fences)
|
| 63 |
- Use EXACT text from the document for the target parameter
|
| 64 |
- Fix the most impactful corruption first (highest similarity improvement)
|
| 65 |
-
- For spelling: target = misspelled word, content = correct word
|
| 66 |
-
- For case: use replace to fix capitalization
|
| 67 |
-
- For formatting: use format_text to add bold/italic/underline tags
|
| 68 |
-
- For alignment/spacing: use set_alignment/set_spacing with the line index
|
| 69 |
-
- For PDF artifacts: use merge_runs with the line index
|
| 70 |
-
- For junk chars: use clean_junk_chars
|
| 71 |
"""
|
| 72 |
|
| 73 |
|
|
@@ -102,11 +97,24 @@ def get_model_action(client: OpenAI, chunk: str, instruction: str, similarity: f
|
|
| 102 |
return {"tool": "replace", "params": {"target": "", "content": ""}}
|
| 103 |
|
| 104 |
|
| 105 |
-
async def
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
|
| 109 |
-
env = await DocEditGameV2Env.from_docker_image(IMAGE_NAME)
|
| 110 |
|
| 111 |
history: List[str] = []
|
| 112 |
rewards: List[float] = []
|
|
@@ -137,7 +145,7 @@ async def run_task(task_name: str) -> dict:
|
|
| 137 |
rewards.append(reward)
|
| 138 |
steps_taken = step
|
| 139 |
|
| 140 |
-
log_step(step=step, action=action_dict, reward=reward, done=result.done)
|
| 141 |
history.append(f"Step {step}: {action_dict.get('tool')} success={obs.last_tool_success} sim={obs.similarity:.3f}")
|
| 142 |
|
| 143 |
if result.done:
|
|
@@ -146,22 +154,28 @@ async def run_task(task_name: str) -> dict:
|
|
| 146 |
score = obs.similarity
|
| 147 |
success = score >= SUCCESS_THRESHOLD
|
| 148 |
|
|
|
|
|
|
|
| 149 |
finally:
|
| 150 |
-
try:
|
| 151 |
-
await env.close()
|
| 152 |
-
except Exception:
|
| 153 |
-
pass
|
| 154 |
log_end(success=success, steps=steps_taken, score=score, rewards=rewards)
|
| 155 |
|
| 156 |
return {"task": task_name, "score": score, "success": success, "steps": steps_taken}
|
| 157 |
|
| 158 |
|
| 159 |
async def main():
|
|
|
|
| 160 |
results = []
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
print(f"\n{'='*60}")
|
| 167 |
print("SUMMARY")
|
|
|
|
| 8 |
import asyncio
|
| 9 |
import json
|
| 10 |
import os
|
| 11 |
+
from typing import List, Optional
|
| 12 |
|
| 13 |
from openai import OpenAI
|
| 14 |
|
| 15 |
API_BASE_URL = os.getenv("API_BASE_URL", "https://api.openai.com/v1")
|
| 16 |
MODEL_NAME = os.getenv("MODEL_NAME", "gpt-4o-mini")
|
| 17 |
API_KEY = os.getenv("HF_TOKEN")
|
| 18 |
+
LOCAL_IMAGE_NAME = os.getenv("LOCAL_IMAGE_NAME")
|
| 19 |
|
| 20 |
+
HF_REPO_ID = "sanjuhs/doc_edit_v3"
|
| 21 |
BENCHMARK = "doc_edit_game_v2"
|
| 22 |
TASKS = ["legal_easy", "legal_medium", "legal_hard", "pharma_easy", "pharma_hard"]
|
| 23 |
SUCCESS_THRESHOLD = 0.90
|
|
|
|
| 26 |
def log_start(task: str, env: str, model: str):
|
| 27 |
print(f"[START] task={task} env={env} model={model}", flush=True)
|
| 28 |
|
| 29 |
+
def log_step(step: int, action: str, reward: float, done: bool, error: Optional[str] = None):
|
| 30 |
error_val = error if error else "null"
|
| 31 |
+
print(f"[STEP] step={step} action={action} reward={reward:.2f} done={str(done).lower()} error={error_val}", flush=True)
|
| 32 |
|
| 33 |
def log_end(success: bool, steps: int, score: float, rewards: List[float]):
|
| 34 |
rewards_str = ",".join(f"{r:.2f}" for r in rewards)
|
|
|
|
| 63 |
- ONE tool call per response, as valid JSON (no markdown fences)
|
| 64 |
- Use EXACT text from the document for the target parameter
|
| 65 |
- Fix the most impactful corruption first (highest similarity improvement)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
"""
|
| 67 |
|
| 68 |
|
|
|
|
| 97 |
return {"tool": "replace", "params": {"target": "", "content": ""}}
|
| 98 |
|
| 99 |
|
| 100 |
+
async def create_env():
|
| 101 |
+
"""Create environment client — tries multiple strategies."""
|
| 102 |
+
from doc_edit_game_v2 import DocEditGameV2Env
|
| 103 |
+
|
| 104 |
+
# Strategy 1: local Docker image (if explicitly provided)
|
| 105 |
+
if LOCAL_IMAGE_NAME:
|
| 106 |
+
print(f"[DEBUG] Using local Docker image: {LOCAL_IMAGE_NAME}", flush=True)
|
| 107 |
+
return await DocEditGameV2Env.from_docker_image(LOCAL_IMAGE_NAME)
|
| 108 |
+
|
| 109 |
+
# Strategy 2: pull from HF Docker registry via from_env
|
| 110 |
+
print(f"[DEBUG] Pulling from HF registry: {HF_REPO_ID}", flush=True)
|
| 111 |
+
return await DocEditGameV2Env.from_env(HF_REPO_ID)
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
async def run_task(env, task_name: str) -> dict:
|
| 115 |
+
from doc_edit_game_v2 import DocEditAction
|
| 116 |
|
| 117 |
client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
|
|
|
|
| 118 |
|
| 119 |
history: List[str] = []
|
| 120 |
rewards: List[float] = []
|
|
|
|
| 145 |
rewards.append(reward)
|
| 146 |
steps_taken = step
|
| 147 |
|
| 148 |
+
log_step(step=step, action=json.dumps(action_dict), reward=reward, done=result.done)
|
| 149 |
history.append(f"Step {step}: {action_dict.get('tool')} success={obs.last_tool_success} sim={obs.similarity:.3f}")
|
| 150 |
|
| 151 |
if result.done:
|
|
|
|
| 154 |
score = obs.similarity
|
| 155 |
success = score >= SUCCESS_THRESHOLD
|
| 156 |
|
| 157 |
+
except Exception as exc:
|
| 158 |
+
print(f"[DEBUG] Task {task_name} error: {exc}", flush=True)
|
| 159 |
finally:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
log_end(success=success, steps=steps_taken, score=score, rewards=rewards)
|
| 161 |
|
| 162 |
return {"task": task_name, "score": score, "success": success, "steps": steps_taken}
|
| 163 |
|
| 164 |
|
| 165 |
async def main():
|
| 166 |
+
env = await create_env()
|
| 167 |
results = []
|
| 168 |
+
|
| 169 |
+
try:
|
| 170 |
+
for task in TASKS:
|
| 171 |
+
r = await run_task(env, task)
|
| 172 |
+
results.append(r)
|
| 173 |
+
print(f"\n{'='*60}", flush=True)
|
| 174 |
+
finally:
|
| 175 |
+
try:
|
| 176 |
+
await env.close()
|
| 177 |
+
except Exception as e:
|
| 178 |
+
print(f"[DEBUG] env.close() error: {e}", flush=True)
|
| 179 |
|
| 180 |
print(f"\n{'='*60}")
|
| 181 |
print("SUMMARY")
|