Spaces:
Sleeping
Sleeping
feat: enhance model client configuration and local image handling in inference module
Browse files- inference.py +12 -13
inference.py
CHANGED
|
@@ -26,6 +26,10 @@ PRESETS = [
|
|
| 26 |
PROJECT_DIR = Path(__file__).resolve().parent
|
| 27 |
IMAGE_NAME = "workflow-arena-inference:latest"
|
| 28 |
DOCKERFILE_PATH = PROJECT_DIR / "server" / "Dockerfile"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
DEFAULT_BASE_URL = os.getenv("WORKFLOW_ARENA_BASE_URL", "http://localhost:8000")
|
| 30 |
TEMPERATURE = 0.0
|
| 31 |
MAX_STEPS = 256
|
|
@@ -178,21 +182,15 @@ def action_to_log_string(action: WorkflowArenaAction) -> str:
|
|
| 178 |
|
| 179 |
|
| 180 |
def resolve_model_client() -> tuple[OpenAI | None, str]:
|
| 181 |
-
api_base_url = os.getenv("API_BASE_URL")
|
| 182 |
-
model_name = os.getenv("MODEL_NAME")
|
| 183 |
api_key = (
|
| 184 |
os.getenv("API_KEY")
|
| 185 |
-
or
|
| 186 |
or os.getenv("OPENAI_API_KEY")
|
| 187 |
)
|
| 188 |
missing = []
|
| 189 |
|
| 190 |
-
if not api_base_url:
|
| 191 |
-
missing.append("API_BASE_URL")
|
| 192 |
-
if not model_name:
|
| 193 |
-
missing.append("MODEL_NAME")
|
| 194 |
if not api_key:
|
| 195 |
-
missing.append("API_KEY")
|
| 196 |
|
| 197 |
if missing:
|
| 198 |
log_warning(
|
|
@@ -203,7 +201,7 @@ def resolve_model_client() -> tuple[OpenAI | None, str]:
|
|
| 203 |
return None, "heuristic"
|
| 204 |
|
| 205 |
try:
|
| 206 |
-
return OpenAI(base_url=
|
| 207 |
except Exception as exc: # pragma: no cover - defensive initialization fallback
|
| 208 |
log_warning(
|
| 209 |
f"Failed to initialize model client: {exc}. Falling back to heuristic policy."
|
|
@@ -235,9 +233,10 @@ class EpisodeResult:
|
|
| 235 |
|
| 236 |
|
| 237 |
def ensure_local_image() -> None:
|
|
|
|
| 238 |
try:
|
| 239 |
inspect_result = subprocess.run(
|
| 240 |
-
["docker", "image", "inspect",
|
| 241 |
cwd=PROJECT_DIR,
|
| 242 |
stdout=subprocess.DEVNULL,
|
| 243 |
stderr=subprocess.DEVNULL,
|
|
@@ -251,7 +250,7 @@ def ensure_local_image() -> None:
|
|
| 251 |
|
| 252 |
try:
|
| 253 |
build_result = subprocess.run(
|
| 254 |
-
["docker", "build", "-t",
|
| 255 |
cwd=PROJECT_DIR,
|
| 256 |
capture_output=True,
|
| 257 |
text=True,
|
|
@@ -263,7 +262,7 @@ def ensure_local_image() -> None:
|
|
| 263 |
if build_result.returncode != 0:
|
| 264 |
raise RuntimeError(
|
| 265 |
"Failed to build Docker image for inference.\n"
|
| 266 |
-
f"Command: docker build -t {
|
| 267 |
f"Exit code: {build_result.returncode}\n"
|
| 268 |
f"Stdout: {build_result.stdout}\n"
|
| 269 |
f"Stderr: {build_result.stderr}"
|
|
@@ -283,7 +282,7 @@ async def managed_env():
|
|
| 283 |
)
|
| 284 |
|
| 285 |
ensure_local_image()
|
| 286 |
-
env = await WorkflowArenaEnv.from_docker_image(IMAGE_NAME)
|
| 287 |
try:
|
| 288 |
yield env
|
| 289 |
finally:
|
|
|
|
| 26 |
PROJECT_DIR = Path(__file__).resolve().parent
|
| 27 |
IMAGE_NAME = "workflow-arena-inference:latest"
|
| 28 |
DOCKERFILE_PATH = PROJECT_DIR / "server" / "Dockerfile"
|
| 29 |
+
API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
|
| 30 |
+
MODEL_NAME = os.getenv("MODEL_NAME", "qwen/qwen3.5-9b")
|
| 31 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 32 |
+
LOCAL_IMAGE_NAME = os.getenv("LOCAL_IMAGE_NAME")
|
| 33 |
DEFAULT_BASE_URL = os.getenv("WORKFLOW_ARENA_BASE_URL", "http://localhost:8000")
|
| 34 |
TEMPERATURE = 0.0
|
| 35 |
MAX_STEPS = 256
|
|
|
|
| 182 |
|
| 183 |
|
| 184 |
def resolve_model_client() -> tuple[OpenAI | None, str]:
|
|
|
|
|
|
|
| 185 |
api_key = (
|
| 186 |
os.getenv("API_KEY")
|
| 187 |
+
or HF_TOKEN
|
| 188 |
or os.getenv("OPENAI_API_KEY")
|
| 189 |
)
|
| 190 |
missing = []
|
| 191 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
if not api_key:
|
| 193 |
+
missing.append("API_KEY or HF_TOKEN")
|
| 194 |
|
| 195 |
if missing:
|
| 196 |
log_warning(
|
|
|
|
| 201 |
return None, "heuristic"
|
| 202 |
|
| 203 |
try:
|
| 204 |
+
return OpenAI(base_url=API_BASE_URL, api_key=api_key), MODEL_NAME
|
| 205 |
except Exception as exc: # pragma: no cover - defensive initialization fallback
|
| 206 |
log_warning(
|
| 207 |
f"Failed to initialize model client: {exc}. Falling back to heuristic policy."
|
|
|
|
| 233 |
|
| 234 |
|
| 235 |
def ensure_local_image() -> None:
|
| 236 |
+
local_image_name = LOCAL_IMAGE_NAME or IMAGE_NAME
|
| 237 |
try:
|
| 238 |
inspect_result = subprocess.run(
|
| 239 |
+
["docker", "image", "inspect", local_image_name],
|
| 240 |
cwd=PROJECT_DIR,
|
| 241 |
stdout=subprocess.DEVNULL,
|
| 242 |
stderr=subprocess.DEVNULL,
|
|
|
|
| 250 |
|
| 251 |
try:
|
| 252 |
build_result = subprocess.run(
|
| 253 |
+
["docker", "build", "-t", local_image_name, "-f", str(DOCKERFILE_PATH), "."],
|
| 254 |
cwd=PROJECT_DIR,
|
| 255 |
capture_output=True,
|
| 256 |
text=True,
|
|
|
|
| 262 |
if build_result.returncode != 0:
|
| 263 |
raise RuntimeError(
|
| 264 |
"Failed to build Docker image for inference.\n"
|
| 265 |
+
f"Command: docker build -t {local_image_name} -f {DOCKERFILE_PATH} .\n"
|
| 266 |
f"Exit code: {build_result.returncode}\n"
|
| 267 |
f"Stdout: {build_result.stdout}\n"
|
| 268 |
f"Stderr: {build_result.stderr}"
|
|
|
|
| 282 |
)
|
| 283 |
|
| 284 |
ensure_local_image()
|
| 285 |
+
env = await WorkflowArenaEnv.from_docker_image(LOCAL_IMAGE_NAME or IMAGE_NAME)
|
| 286 |
try:
|
| 287 |
yield env
|
| 288 |
finally:
|