File size: 10,205 Bytes
0446283
7889ae7
0446283
7889ae7
0446283
7889ae7
 
 
0446283
7889ae7
caa2181
7889ae7
 
 
 
 
caa2181
7889ae7
 
0446283
7889ae7
 
0446283
 
 
 
 
 
 
7889ae7
 
 
 
 
 
0446283
 
7889ae7
0446283
7889ae7
caa2181
 
7889ae7
0446283
 
7889ae7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0446283
 
7889ae7
0446283
 
7889ae7
 
0446283
 
 
7889ae7
 
0446283
7889ae7
 
0446283
 
 
 
 
7889ae7
0446283
 
7889ae7
0446283
 
 
 
 
 
 
 
 
 
 
7889ae7
0446283
7889ae7
0446283
 
 
7889ae7
 
0446283
7889ae7
0446283
 
 
 
 
7889ae7
0446283
 
 
 
7889ae7
 
0446283
 
7889ae7
0446283
7889ae7
0446283
 
 
 
 
 
 
 
 
caa2181
0446283
 
7889ae7
caa2181
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7889ae7
 
caa2181
 
 
 
0446283
 
7889ae7
caa2181
 
7889ae7
 
 
 
0446283
 
 
7889ae7
 
 
 
0446283
 
 
7889ae7
0446283
 
 
 
 
7889ae7
0446283
 
 
7889ae7
 
 
 
 
 
 
 
 
0446283
7889ae7
0446283
 
7889ae7
 
 
 
 
 
0446283
7889ae7
0446283
 
7889ae7
 
 
 
0446283
 
7889ae7
0446283
 
 
 
 
 
 
 
7889ae7
0446283
7889ae7
0446283
 
 
 
 
 
 
7889ae7
 
 
 
0446283
7889ae7
0446283
 
 
 
 
7889ae7
 
 
0446283
7889ae7
0446283
 
 
 
 
 
7889ae7
 
0446283
 
7889ae7
0446283
 
 
 
 
7889ae7
0446283
 
7889ae7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
caa2181
 
 
 
 
7889ae7
 
0446283
7889ae7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
"""
FitScript inference.py — required entry point for hackathon evaluation.

Runs all 3 tasks sequentially and emits structured stdout logs per spec.

LOCAL USAGE (no Docker — start the server first in a separate terminal):
    cd FitScript
    uvicorn server.app:app --host 0.0.0.0 --port 8000

    Then in another terminal:
    USE_DOCKER=false HF_TOKEN=hf_... python inference.py

SINGLE TASK (local):
    FITSCRIPT_TASK=basic_plan USE_DOCKER=false python inference.py

DOCKER USAGE (spins up the container automatically):
    USE_DOCKER=true LOCAL_IMAGE_NAME=fitscript-env:latest HF_TOKEN=hf_... python inference.py

STDOUT FORMAT (exact hackathon spec):
    [START] task=<task> env=fitscript_env model=<model>
    [STEP] step=<N> action=<text> reward=<R:.2f> done=<true|false> error=<null|msg>
    [END] success=<true|false> steps=<N> score=<score:.2f> rewards=<r1:.2f,...>
"""

import asyncio
import json
import os
import sys

# Optional: load .env for local development
try:
    from dotenv import load_dotenv
    load_dotenv()
except ImportError:
    pass

# ---------------------------------------------------------------------------
# Configuration (hackathon mandatory variables)
# ---------------------------------------------------------------------------
API_BASE_URL: str = os.environ.get("API_BASE_URL", "https://router.huggingface.co/v1")
MODEL_NAME: str = os.environ.get("MODEL_NAME", "meta-llama/Meta-Llama-3-8B-Instruct")
# Accept HF_TOKEN or API_KEY
API_KEY: str = os.environ.get("HF_TOKEN") or os.environ.get("API_KEY", "")

BENCHMARK: str = "fitscript_env"

USE_DOCKER: bool = os.environ.get("USE_DOCKER", "false").lower() == "true"

IMAGE_NAME: str = (
    os.environ.get("LOCAL_IMAGE_NAME")
    or os.environ.get("FITSCRIPT_IMAGE", "fitscript-env:latest")
)

LOCAL_SERVER_URL: str = os.environ.get("LOCAL_SERVER_URL", "http://localhost:8000")

FITSCRIPT_TASK: str = os.environ.get("FITSCRIPT_TASK", "")

MAX_STEPS: int = int(os.environ.get("MAX_STEPS", "8"))

ALL_TASKS = ["basic_plan", "injury_safe_modification", "periodized_program"]

# ---------------------------------------------------------------------------
# Structured log helpers (exact hackathon spec format — do not change)
# ---------------------------------------------------------------------------

def log_start(task: str, env_name: str, model: str) -> None:
    print(f"[START] task={task} env={env_name} model={model}", flush=True)


def log_step(step: int, action: str, reward: float, done: bool, error) -> None:
    err_str = str(error) if error else "null"
    action_str = str(action).replace("\n", " ").replace("\r", "")[:120]
    print(
        f"[STEP] step={step} action={action_str} reward={reward:.2f}"
        f" done={str(done).lower()} error={err_str}",
        flush=True,
    )


def log_end(success: bool, steps: int, score: float, rewards: list) -> None:
    rewards_str = ",".join(f"{r:.2f}" for r in rewards)
    print(
        f"[END] success={str(success).lower()} steps={steps}"
        f" score={score:.2f} rewards={rewards_str}",
        flush=True,
    )


# ---------------------------------------------------------------------------
# System prompt
# ---------------------------------------------------------------------------

SYSTEM_PROMPT = """You are an expert personal trainer and exercise scientist.
You will receive a client profile and must generate a structured workout plan as JSON.

IMPORTANT: Respond with ONLY a valid JSON object. No prose, no markdown fences, no explanation.

For a basic plan or injury-modification plan, use:
{
  "days": [
    {
      "name": "Day 1 - Lower Body",
      "focus": "legs",
      "exercises": [
        {"name": "Squat", "sets": 3, "reps": 10, "rest_seconds": 60}
      ]
    }
  ]
}

For a periodized 4-week powerlifting program, use:
{
  "weeks": [
    {
      "week": 1,
      "intensity": 72.5,
      "total_sets": 80,
      "days": [
        {
          "name": "Day 1 - Squat",
          "exercises": [
            {"name": "Back Squat", "sets": 5, "reps": 5, "intensity_pct": 72.5}
          ]
        }
      ]
    }
  ]
}
"""

# ---------------------------------------------------------------------------
# LLM helpers — using OpenAI-compatible HuggingFace router
# ---------------------------------------------------------------------------

def _call_llm_sync(messages: list) -> str:
    """
    Call HuggingFace Inference API via its OpenAI-compatible /v1/chat/completions
    endpoint. Works with any model available on HF's serverless inference router.
    """
    try:
        from openai import OpenAI
    except ImportError:
        raise ImportError(
            "openai package is required. Install with: pip install openai"
        )

    if not API_KEY:
        raise ValueError(
            "HF_TOKEN (or API_KEY) environment variable is not set. "
            "Get your token from https://huggingface.co/settings/tokens"
        )

    client = OpenAI(
        base_url=API_BASE_URL,
        api_key=API_KEY,
    )

    response = client.chat.completions.create(
        model=MODEL_NAME,
        messages=messages,
        max_tokens=2048,
        temperature=0.7,
    )

    return response.choices[0].message.content


async def call_llm_async(messages: list) -> str:
    loop = asyncio.get_event_loop()
    return await loop.run_in_executor(None, _call_llm_sync, messages)


def build_user_message(observation) -> str:
    profile   = getattr(observation, "client_profile",  {})
    feedback  = getattr(observation, "feedback",         "")
    breakdown = getattr(observation, "score_breakdown",  {})
    task_id   = getattr(observation, "task_id",          "")

    parts = [
        f"Task: {task_id}",
        f"Client profile:\n{json.dumps(profile, indent=2)}",
    ]
    if feedback:
        parts.append(f"Environment feedback: {feedback}")
    if breakdown:
        parts.append(f"Score breakdown: {json.dumps(breakdown, indent=2)}")
    parts.append("Generate or revise the workout plan as a JSON object only.")
    return "\n\n".join(parts)


def strip_fences(text: str) -> str:
    """Remove ```json ... ``` markdown fences if the LLM added them."""
    text = text.strip()
    if text.startswith("```"):
        lines = [l for l in text.split("\n") if not l.startswith("```")]
        text = "\n".join(lines).strip()
    return text


# ---------------------------------------------------------------------------
# Single episode runner
# ---------------------------------------------------------------------------

async def run_episode(task_name: str, env) -> None:
    """
    Run one episode for task_name against env (an async EnvClient).
    Emits [START] / [STEP] / [END] to stdout.
    """
    from FitScript import FitscriptAction

    log_start(task_name, BENCHMARK, MODEL_NAME)

    rewards: list = []
    final_score   = 0.0
    success       = False
    step          = 0
    error_msg     = None

    try:
        reset_result = await env.reset()
        obs = reset_result.observation

        messages = [{"role": "system", "content": SYSTEM_PROMPT}]

        for step in range(1, MAX_STEPS + 1):
            user_content = build_user_message(obs)
            messages.append({"role": "user", "content": user_content})

            # LLM call (async-wrapped sync)
            try:
                assistant_reply = await call_llm_async(messages)
            except Exception as exc:
                error_msg = str(exc)
                log_step(step, "LLM_ERROR", 0.0, True, error_msg)
                break

            messages.append({"role": "assistant", "content": assistant_reply})

            plan_str    = strip_fences(assistant_reply)
            action_type = "modify_plan" if task_name == "injury_safe_modification" else "generate_plan"
            action      = FitscriptAction(action_type=action_type, plan=plan_str)

            try:
                result = await env.step(action)
            except Exception as exc:
                error_msg = str(exc)
                log_step(step, action_type, 0.0, True, error_msg)
                break

            obs         = result.observation
            reward      = float(result.reward or 0.0)
            done        = bool(result.done)
            rewards.append(reward)
            final_score = max(final_score, reward)

            log_step(step, action_type, reward, done, None)

            if done:
                break

        success = final_score >= 0.75

    except Exception as exc:
        error_msg = str(exc)
        print(f"[ERROR] episode failed: {error_msg}", file=sys.stderr, flush=True)

    log_end(success, step, final_score, rewards)


# ---------------------------------------------------------------------------
# Main entry point
# ---------------------------------------------------------------------------

async def main() -> None:
    from FitScript import FitscriptEnv

    tasks_to_run = [FITSCRIPT_TASK] if FITSCRIPT_TASK else ALL_TASKS

    if USE_DOCKER:
        for task_name in tasks_to_run:
            print(
                f"[INFO] Starting Docker container ({IMAGE_NAME}) for task={task_name}",
                file=sys.stderr, flush=True,
            )
            try:
                env = await FitscriptEnv.from_docker_image(
                    IMAGE_NAME,
                    env={"FITSCRIPT_TASK": task_name},
                )
            except TypeError:
                env = await FitscriptEnv.from_docker_image(IMAGE_NAME)
            try:
                await run_episode(task_name, env)
            finally:
                await env.close()

    else:
        for task_name in tasks_to_run:
            print(
                f"[INFO] Connecting to local server at {LOCAL_SERVER_URL} for task={task_name}",
                file=sys.stderr, flush=True,
            )
            env = FitscriptEnv(base_url=LOCAL_SERVER_URL)
            try:
                await run_episode(task_name, env)
            finally:
                # close() is async — await it properly
                try:
                    await env.close()
                except TypeError:
                    env.close()


if __name__ == "__main__":
    asyncio.run(main())