|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +import re |
| 16 | +from typing import Any, Optional |
| 17 | + |
| 18 | +from fastapi import FastAPI |
| 19 | + |
| 20 | +from nemo_gym.base_resources_server import ( |
| 21 | + BaseResourcesServerConfig, |
| 22 | + BaseRunRequest, |
| 23 | + BaseVerifyRequest, |
| 24 | + BaseVerifyResponse, |
| 25 | + SimpleResourcesServer, |
| 26 | +) |
| 27 | + |
| 28 | + |
| 29 | +POINT_PATTERN = re.compile(r"<point>\s*\[?\s*\(\s*(\d+)\s*,\s*(\d+)\s*\)\s*\]?\s*</point>") |
| 30 | + |
| 31 | + |
| 32 | +class GuiCoordinateResourcesServerConfig(BaseResourcesServerConfig): |
| 33 | + pass |
| 34 | + |
| 35 | + |
| 36 | +class GuiCoordinateRunRequest(BaseRunRequest): |
| 37 | + expected_answer: str # "x,y" normalized 0-1 coordinates |
| 38 | + max_dist: float = 0.15 |
| 39 | + metadata: Optional[dict[str, Any]] = None |
| 40 | + |
| 41 | + |
| 42 | +class GuiCoordinateVerifyRequest(GuiCoordinateRunRequest, BaseVerifyRequest): |
| 43 | + pass |
| 44 | + |
| 45 | + |
| 46 | +class GuiCoordinateVerifyResponse(BaseVerifyResponse): |
| 47 | + expected_answer: str |
| 48 | + extracted_x: Optional[float] |
| 49 | + extracted_y: Optional[float] |
| 50 | + distance: Optional[float] |
| 51 | + |
| 52 | + |
| 53 | +def _extract_last_assistant_text(body: BaseVerifyRequest) -> str: |
| 54 | + texts: list[str] = [] |
| 55 | + for o in body.response.output: |
| 56 | + if getattr(o, "type", None) == "message" and getattr(o, "role", None) == "assistant": |
| 57 | + content = getattr(o, "content", None) |
| 58 | + if isinstance(content, list): |
| 59 | + for c in content: |
| 60 | + t = getattr(c, "text", None) |
| 61 | + if isinstance(t, str): |
| 62 | + texts.append(t) |
| 63 | + elif isinstance(content, str): |
| 64 | + texts.append(content) |
| 65 | + return "\n".join(texts).strip() |
| 66 | + |
| 67 | + |
| 68 | +def _parse_gt(expected_answer: str) -> Optional[tuple[float, float]]: |
| 69 | + parts = expected_answer.split(",") |
| 70 | + if len(parts) != 2: |
| 71 | + return None |
| 72 | + try: |
| 73 | + return float(parts[0]), float(parts[1]) |
| 74 | + except ValueError: |
| 75 | + return None |
| 76 | + |
| 77 | + |
| 78 | +def _parse_prediction(text: str) -> Optional[tuple[float, float]]: |
| 79 | + match = POINT_PATTERN.search(text) |
| 80 | + if not match: |
| 81 | + return None |
| 82 | + return int(match.group(1)) / 1000.0, int(match.group(2)) / 1000.0 |
| 83 | + |
| 84 | + |
| 85 | +def _compute_reward( |
| 86 | + gt: tuple[float, float], |
| 87 | + pred: tuple[float, float], |
| 88 | + max_dist: float, |
| 89 | +) -> tuple[float, float]: |
| 90 | + dist = ((pred[0] - gt[0]) ** 2 + (pred[1] - gt[1]) ** 2) ** 0.5 |
| 91 | + if dist >= max_dist: |
| 92 | + return 0.0, dist |
| 93 | + reward = (1.0 - dist / max_dist) ** 2 |
| 94 | + return reward, dist |
| 95 | + |
| 96 | + |
| 97 | +class GuiCoordinateResourcesServer(SimpleResourcesServer): |
| 98 | + config: GuiCoordinateResourcesServerConfig |
| 99 | + |
| 100 | + def setup_webserver(self) -> FastAPI: |
| 101 | + app = super().setup_webserver() |
| 102 | + return app |
| 103 | + |
| 104 | + async def verify(self, body: GuiCoordinateVerifyRequest) -> GuiCoordinateVerifyResponse: |
| 105 | + text = _extract_last_assistant_text(body) |
| 106 | + gt = _parse_gt(body.expected_answer) |
| 107 | + pred = _parse_prediction(text) |
| 108 | + |
| 109 | + reward = 0.0 |
| 110 | + distance = None |
| 111 | + extracted_x = None |
| 112 | + extracted_y = None |
| 113 | + |
| 114 | + if gt is not None and pred is not None: |
| 115 | + extracted_x, extracted_y = pred |
| 116 | + reward, distance = _compute_reward(gt, pred, body.max_dist) |
| 117 | + |
| 118 | + return GuiCoordinateVerifyResponse( |
| 119 | + **body.model_dump( |
| 120 | + exclude={ |
| 121 | + "expected_answer", |
| 122 | + "extracted_x", |
| 123 | + "extracted_y", |
| 124 | + "distance", |
| 125 | + } |
| 126 | + ), |
| 127 | + reward=reward, |
| 128 | + expected_answer=body.expected_answer, |
| 129 | + extracted_x=extracted_x, |
| 130 | + extracted_y=extracted_y, |
| 131 | + distance=distance, |
| 132 | + ) |
| 133 | + |
| 134 | + |
| 135 | +if __name__ == "__main__": |
| 136 | + GuiCoordinateResourcesServer.run_webserver() |
0 commit comments