Spaces:
Sleeping
Sleeping
Commit ·
47efb73
1
Parent(s): 4882762
Restore tasks folder
Browse files- client.py +7 -12
- evaluate.py +0 -6
- inference.py +14 -18
- tasks/1_day_move/__init__.py +8 -0
- tasks/1_day_move/grader.py +10 -0
- tasks/1_day_move/spec.py +18 -0
- tasks/30_day_move/__init__.py +8 -0
- tasks/30_day_move/grader.py +10 -0
- tasks/30_day_move/spec.py +18 -0
- tasks/__init__.py +23 -0
- tasks/exceptions.py +5 -0
- tasks/grading.py +44 -0
- tasks/loader.py +35 -0
- tasks/next_quarter_move/__init__.py +8 -0
- tasks/next_quarter_move/grader.py +10 -0
- tasks/next_quarter_move/spec.py +18 -0
- tasks/registry.py +56 -0
- tasks/sentiment_label/__init__.py +8 -0
- tasks/sentiment_label/grader.py +9 -0
- tasks/sentiment_label/spec.py +40 -0
- tasks/types.py +18 -0
client.py
CHANGED
|
@@ -19,19 +19,14 @@ class EarningsAnalystEnv(
|
|
| 19 |
enabling efficient multi-step interactions with lower latency.
|
| 20 |
Each client instance has its own dedicated environment session on the server.
|
| 21 |
|
| 22 |
-
Example
|
| 23 |
>>> # Connect to a running server
|
| 24 |
-
>>> async
|
| 25 |
-
...
|
| 26 |
-
...
|
| 27 |
-
...
|
| 28 |
-
...
|
| 29 |
-
...
|
| 30 |
-
|
| 31 |
-
Example (sync wrapper):
|
| 32 |
-
>>> with EarningsAnalystEnv(base_url="http://localhost:8000").sync() as client:
|
| 33 |
-
... result = client.reset()
|
| 34 |
-
... result = client.step(EarningsAnalystAction(prediction="neutral"))
|
| 35 |
|
| 36 |
Example with Docker:
|
| 37 |
>>> # Automatically start container and connect
|
|
|
|
| 19 |
enabling efficient multi-step interactions with lower latency.
|
| 20 |
Each client instance has its own dedicated environment session on the server.
|
| 21 |
|
| 22 |
+
Example:
|
| 23 |
>>> # Connect to a running server
|
| 24 |
+
>>> async with EarningsAnalystEnv(base_url="http://localhost:8000") as client:
|
| 25 |
+
... result = await client.reset()
|
| 26 |
+
... print(result.observation.task_instruction)
|
| 27 |
+
...
|
| 28 |
+
... result = await client.step(EarningsAnalystAction(prediction="neutral"))
|
| 29 |
+
... print(result.observation.metadata)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
Example with Docker:
|
| 32 |
>>> # Automatically start container and connect
|
evaluate.py
CHANGED
|
@@ -7,14 +7,12 @@ must match what you are measuring; ``--task`` here selects which task spec is us
|
|
| 7 |
Usage:
|
| 8 |
uv run python evaluate.py
|
| 9 |
uv run python evaluate.py --samples 50 --quiet
|
| 10 |
-
uv run python evaluate.py --samples 10 --csv runs.csv
|
| 11 |
"""
|
| 12 |
|
| 13 |
from __future__ import annotations
|
| 14 |
|
| 15 |
import argparse
|
| 16 |
import asyncio
|
| 17 |
-
import csv
|
| 18 |
import os
|
| 19 |
import sys
|
| 20 |
from collections import defaultdict
|
|
@@ -49,7 +47,6 @@ async def run_evaluation(
|
|
| 49 |
model: str | None,
|
| 50 |
task_id: str,
|
| 51 |
quiet: bool,
|
| 52 |
-
csv_path: str | None,
|
| 53 |
) -> None:
|
| 54 |
spec = TASKS.get(task_id) or TASKS[DEFAULT_TASK]
|
| 55 |
label_values = list(spec["label_values"])
|
|
@@ -61,9 +58,6 @@ async def run_evaluation(
|
|
| 61 |
lambda: {"n": 0, "correct": 0}
|
| 62 |
)
|
| 63 |
|
| 64 |
-
resolved_model = model or os.environ.get("OPENAI_MODEL", "gpt-4o")
|
| 65 |
-
session_rows: list[dict[str, object]] = []
|
| 66 |
-
|
| 67 |
for episode_index in range(samples):
|
| 68 |
if not quiet:
|
| 69 |
print(f"episode {episode_index + 1}/{samples} ...", flush=True)
|
|
|
|
| 7 |
Usage:
|
| 8 |
uv run python evaluate.py
|
| 9 |
uv run python evaluate.py --samples 50 --quiet
|
|
|
|
| 10 |
"""
|
| 11 |
|
| 12 |
from __future__ import annotations
|
| 13 |
|
| 14 |
import argparse
|
| 15 |
import asyncio
|
|
|
|
| 16 |
import os
|
| 17 |
import sys
|
| 18 |
from collections import defaultdict
|
|
|
|
| 47 |
model: str | None,
|
| 48 |
task_id: str,
|
| 49 |
quiet: bool,
|
|
|
|
| 50 |
) -> None:
|
| 51 |
spec = TASKS.get(task_id) or TASKS[DEFAULT_TASK]
|
| 52 |
label_values = list(spec["label_values"])
|
|
|
|
| 58 |
lambda: {"n": 0, "correct": 0}
|
| 59 |
)
|
| 60 |
|
|
|
|
|
|
|
|
|
|
| 61 |
for episode_index in range(samples):
|
| 62 |
if not quiet:
|
| 63 |
print(f"episode {episode_index + 1}/{samples} ...", flush=True)
|
inference.py
CHANGED
|
@@ -22,7 +22,7 @@ from dataclasses import dataclass
|
|
| 22 |
from typing import Any
|
| 23 |
|
| 24 |
from dotenv import load_dotenv
|
| 25 |
-
from openai import
|
| 26 |
|
| 27 |
try:
|
| 28 |
from earnings_analyst.client import EarningsAnalystEnv
|
|
@@ -83,10 +83,10 @@ def build_user_content(obs: EarningsAnalystObservation) -> str:
|
|
| 83 |
return "\n\n".join(parts)
|
| 84 |
|
| 85 |
|
| 86 |
-
def predict_with_openai(
|
| 87 |
obs: EarningsAnalystObservation,
|
| 88 |
*,
|
| 89 |
-
client:
|
| 90 |
model: str,
|
| 91 |
valid_labels: list[str] | None = None,
|
| 92 |
) -> tuple[str, str]:
|
|
@@ -104,7 +104,7 @@ def predict_with_openai(
|
|
| 104 |
+ ", ".join(f'"{lab}"' for lab in labels)
|
| 105 |
+ "."
|
| 106 |
)
|
| 107 |
-
completion = client.chat.completions.create(
|
| 108 |
model=model,
|
| 109 |
messages=[
|
| 110 |
{"role": "system", "content": system_prompt},
|
|
@@ -133,8 +133,6 @@ async def run_episode(
|
|
| 133 |
) -> EpisodeResult:
|
| 134 |
"""
|
| 135 |
One reset → OpenAI prediction → step. Returns reward and metadata from the server.
|
| 136 |
-
|
| 137 |
-
Uses async :class:`EarningsAnalystEnv` (``async with`` / ``await``).
|
| 138 |
"""
|
| 139 |
environment_base_url = base_url or os.environ.get(
|
| 140 |
"ENV_SERVER_URL", "http://localhost:8000"
|
|
@@ -149,12 +147,12 @@ async def run_episode(
|
|
| 149 |
openai_client_options: dict[str, Any] = {"api_key": api_key}
|
| 150 |
if resolved_openai_base_url:
|
| 151 |
openai_client_options["base_url"] = resolved_openai_base_url
|
| 152 |
-
client =
|
| 153 |
|
| 154 |
async with EarningsAnalystEnv(base_url=environment_base_url) as env:
|
| 155 |
reset_out = await env.reset()
|
| 156 |
observation = reset_out.observation
|
| 157 |
-
predicted, response_text = predict_with_openai(
|
| 158 |
observation, client=client, model=model_name
|
| 159 |
)
|
| 160 |
step_out = await env.step(EarningsAnalystAction(prediction=predicted))
|
|
@@ -181,7 +179,7 @@ async def run_episode(
|
|
| 181 |
)
|
| 182 |
|
| 183 |
|
| 184 |
-
|
| 185 |
parser = argparse.ArgumentParser(
|
| 186 |
description="Run one episode via OpenAI + env server (example inference script)"
|
| 187 |
)
|
|
@@ -193,16 +191,14 @@ async def _async_main() -> None:
|
|
| 193 |
parser.add_argument("--model", default=os.environ.get("OPENAI_MODEL", "gpt-4o"))
|
| 194 |
parser.add_argument("--quiet", action="store_true", help="Less output")
|
| 195 |
args = parser.parse_args()
|
| 196 |
-
await run_episode(
|
| 197 |
-
base_url=args.base_url,
|
| 198 |
-
model=args.model,
|
| 199 |
-
verbose=not args.quiet,
|
| 200 |
-
)
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
def main() -> None:
|
| 204 |
try:
|
| 205 |
-
asyncio.run(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
except Exception as e:
|
| 207 |
print(f"error: {e}", file=sys.stderr)
|
| 208 |
sys.exit(1)
|
|
|
|
| 22 |
from typing import Any
|
| 23 |
|
| 24 |
from dotenv import load_dotenv
|
| 25 |
+
from openai import AsyncOpenAI
|
| 26 |
|
| 27 |
try:
|
| 28 |
from earnings_analyst.client import EarningsAnalystEnv
|
|
|
|
| 83 |
return "\n\n".join(parts)
|
| 84 |
|
| 85 |
|
| 86 |
+
async def predict_with_openai(
|
| 87 |
obs: EarningsAnalystObservation,
|
| 88 |
*,
|
| 89 |
+
client: AsyncOpenAI,
|
| 90 |
model: str,
|
| 91 |
valid_labels: list[str] | None = None,
|
| 92 |
) -> tuple[str, str]:
|
|
|
|
| 104 |
+ ", ".join(f'"{lab}"' for lab in labels)
|
| 105 |
+ "."
|
| 106 |
)
|
| 107 |
+
completion = await client.chat.completions.create(
|
| 108 |
model=model,
|
| 109 |
messages=[
|
| 110 |
{"role": "system", "content": system_prompt},
|
|
|
|
| 133 |
) -> EpisodeResult:
|
| 134 |
"""
|
| 135 |
One reset → OpenAI prediction → step. Returns reward and metadata from the server.
|
|
|
|
|
|
|
| 136 |
"""
|
| 137 |
environment_base_url = base_url or os.environ.get(
|
| 138 |
"ENV_SERVER_URL", "http://localhost:8000"
|
|
|
|
| 147 |
openai_client_options: dict[str, Any] = {"api_key": api_key}
|
| 148 |
if resolved_openai_base_url:
|
| 149 |
openai_client_options["base_url"] = resolved_openai_base_url
|
| 150 |
+
client = AsyncOpenAI(**openai_client_options)
|
| 151 |
|
| 152 |
async with EarningsAnalystEnv(base_url=environment_base_url) as env:
|
| 153 |
reset_out = await env.reset()
|
| 154 |
observation = reset_out.observation
|
| 155 |
+
predicted, response_text = await predict_with_openai(
|
| 156 |
observation, client=client, model=model_name
|
| 157 |
)
|
| 158 |
step_out = await env.step(EarningsAnalystAction(prediction=predicted))
|
|
|
|
| 179 |
)
|
| 180 |
|
| 181 |
|
| 182 |
+
def main() -> None:
|
| 183 |
parser = argparse.ArgumentParser(
|
| 184 |
description="Run one episode via OpenAI + env server (example inference script)"
|
| 185 |
)
|
|
|
|
| 191 |
parser.add_argument("--model", default=os.environ.get("OPENAI_MODEL", "gpt-4o"))
|
| 192 |
parser.add_argument("--quiet", action="store_true", help="Less output")
|
| 193 |
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
try:
|
| 195 |
+
asyncio.run(
|
| 196 |
+
run_episode(
|
| 197 |
+
base_url=args.base_url,
|
| 198 |
+
model=args.model,
|
| 199 |
+
verbose=not args.quiet,
|
| 200 |
+
)
|
| 201 |
+
)
|
| 202 |
except Exception as e:
|
| 203 |
print(f"error: {e}", file=sys.stderr)
|
| 204 |
sys.exit(1)
|
tasks/1_day_move/__init__.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""1-day move task (scaffold). Task id: ``1_day_move``."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from .grader import grade
|
| 6 |
+
from .spec import CANONICAL_TASK_ID, SPEC
|
| 7 |
+
|
| 8 |
+
__all__ = ["CANONICAL_TASK_ID", "SPEC", "grade"]
|
tasks/1_day_move/grader.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Grading for ``1_day_move`` — implement when the task is ready."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def grade(predicted: str, ground_truth: str, label_values: list[str]) -> float:
|
| 7 |
+
raise NotImplementedError(
|
| 8 |
+
"Task '1_day_move' is not implemented yet. "
|
| 9 |
+
"Implement grader logic in tasks/1_day_move/grader.py."
|
| 10 |
+
)
|
tasks/1_day_move/spec.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Task specification for ``1_day_move`` — fill in when implementing."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from ..types import TaskSpec
|
| 6 |
+
|
| 7 |
+
CANONICAL_TASK_ID = "1_day_move"
|
| 8 |
+
|
| 9 |
+
SPEC: TaskSpec = {
|
| 10 |
+
"task_id": CANONICAL_TASK_ID,
|
| 11 |
+
"implemented": False,
|
| 12 |
+
"text_cols": [],
|
| 13 |
+
"numerical_cols": [],
|
| 14 |
+
"label_col": "",
|
| 15 |
+
"label_values": [],
|
| 16 |
+
"task_instruction": "",
|
| 17 |
+
"kind": "regression",
|
| 18 |
+
}
|
tasks/30_day_move/__init__.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""30-day move task (scaffold). Task id: ``30_day_move``."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from .grader import grade
|
| 6 |
+
from .spec import CANONICAL_TASK_ID, SPEC
|
| 7 |
+
|
| 8 |
+
__all__ = ["CANONICAL_TASK_ID", "SPEC", "grade"]
|
tasks/30_day_move/grader.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Grading for ``30_day_move`` — implement when the task is ready."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def grade(predicted: str, ground_truth: str, label_values: list[str]) -> float:
|
| 7 |
+
raise NotImplementedError(
|
| 8 |
+
"Task '30_day_move' is not implemented yet. "
|
| 9 |
+
"Implement grader logic in tasks/30_day_move/grader.py."
|
| 10 |
+
)
|
tasks/30_day_move/spec.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Task specification for ``30_day_move`` — fill in when implementing."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from ..types import TaskSpec
|
| 6 |
+
|
| 7 |
+
CANONICAL_TASK_ID = "30_day_move"
|
| 8 |
+
|
| 9 |
+
SPEC: TaskSpec = {
|
| 10 |
+
"task_id": CANONICAL_TASK_ID,
|
| 11 |
+
"implemented": False,
|
| 12 |
+
"text_cols": [],
|
| 13 |
+
"numerical_cols": [],
|
| 14 |
+
"label_col": "",
|
| 15 |
+
"label_values": [],
|
| 16 |
+
"task_instruction": "",
|
| 17 |
+
"kind": "regression",
|
| 18 |
+
}
|
tasks/__init__.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Per-task packages live under ``earnings_analyst.tasks.<name>``."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from .exceptions import TaskNotImplementedError
|
| 6 |
+
from .registry import (
|
| 7 |
+
DEFAULT_TASK,
|
| 8 |
+
GRADERS,
|
| 9 |
+
TASKS,
|
| 10 |
+
TASK_IDS,
|
| 11 |
+
get_grader,
|
| 12 |
+
get_task_spec,
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
__all__ = [
|
| 16 |
+
"DEFAULT_TASK",
|
| 17 |
+
"GRADERS",
|
| 18 |
+
"TASKS",
|
| 19 |
+
"TASK_IDS",
|
| 20 |
+
"TaskNotImplementedError",
|
| 21 |
+
"get_grader",
|
| 22 |
+
"get_task_spec",
|
| 23 |
+
]
|
tasks/exceptions.py
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Task-related errors."""
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class TaskNotImplementedError(RuntimeError):
|
| 5 |
+
"""Raised when reset() is called for a task that is registered but not implemented yet."""
|
tasks/grading.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Shared grading helpers for task modules."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def _normalize_text(text: str) -> str:
|
| 7 |
+
return str(text).strip().lower()
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def _index_of_label(label: str, label_values: list[str]) -> int | None:
|
| 11 |
+
normalized_target = _normalize_text(label)
|
| 12 |
+
for index, candidate in enumerate(label_values):
|
| 13 |
+
if _normalize_text(candidate) == normalized_target:
|
| 14 |
+
return index
|
| 15 |
+
return None
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def grade_ordinal(
|
| 19 |
+
predicted: str,
|
| 20 |
+
ground_truth: str,
|
| 21 |
+
label_values: list[str],
|
| 22 |
+
) -> float:
|
| 23 |
+
"""Ordinal distance reward: exact=1.0, one step=0.5, else 0.0."""
|
| 24 |
+
predicted_index = _index_of_label(predicted, label_values)
|
| 25 |
+
ground_truth_index = _index_of_label(ground_truth, label_values)
|
| 26 |
+
if predicted_index is None or ground_truth_index is None:
|
| 27 |
+
return 0.0
|
| 28 |
+
ordinal_distance = abs(predicted_index - ground_truth_index)
|
| 29 |
+
if ordinal_distance == 0:
|
| 30 |
+
return 1.0
|
| 31 |
+
if ordinal_distance == 1:
|
| 32 |
+
return 0.5
|
| 33 |
+
return 0.0
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def grade_exact(
|
| 37 |
+
predicted: str,
|
| 38 |
+
ground_truth: str,
|
| 39 |
+
label_values: list[str],
|
| 40 |
+
) -> float:
|
| 41 |
+
_ = label_values
|
| 42 |
+
if _normalize_text(predicted) == _normalize_text(ground_truth):
|
| 43 |
+
return 1.0
|
| 44 |
+
return 0.0
|
tasks/loader.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Load task packages under ``tasks/<dirname>/`` when the folder name is not a valid
|
| 3 |
+
Python module identifier (e.g. ``1_day_move``, ``30_day_move``).
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
from __future__ import annotations
|
| 7 |
+
|
| 8 |
+
import importlib.util
|
| 9 |
+
import sys
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
from types import ModuleType
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def load_task_subpackage(dirname: str, qualname: str) -> ModuleType:
|
| 15 |
+
"""
|
| 16 |
+
Load ``tasks/<dirname>/__init__.py`` as ``qualname``, with sibling modules
|
| 17 |
+
(``spec.py``, ``grader.py``) importable as submodules.
|
| 18 |
+
"""
|
| 19 |
+
root = Path(__file__).resolve().parent / dirname
|
| 20 |
+
init_path = root / "__init__.py"
|
| 21 |
+
if not init_path.is_file():
|
| 22 |
+
raise FileNotFoundError(f"Missing task package: {init_path}")
|
| 23 |
+
|
| 24 |
+
spec = importlib.util.spec_from_file_location(
|
| 25 |
+
qualname,
|
| 26 |
+
init_path,
|
| 27 |
+
submodule_search_locations=[str(root)],
|
| 28 |
+
)
|
| 29 |
+
if spec is None or spec.loader is None:
|
| 30 |
+
raise ImportError(f"Could not load task package from {init_path}")
|
| 31 |
+
|
| 32 |
+
module = importlib.util.module_from_spec(spec)
|
| 33 |
+
sys.modules[qualname] = module
|
| 34 |
+
spec.loader.exec_module(module)
|
| 35 |
+
return module
|
tasks/next_quarter_move/__init__.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Next-quarter move task (scaffold)."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from .grader import grade
|
| 6 |
+
from .spec import CANONICAL_TASK_ID, SPEC
|
| 7 |
+
|
| 8 |
+
__all__ = ["CANONICAL_TASK_ID", "SPEC", "grade"]
|
tasks/next_quarter_move/grader.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Grading for ``next_quarter_move`` — implement when the task is ready."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def grade(predicted: str, ground_truth: str, label_values: list[str]) -> float:
|
| 7 |
+
raise NotImplementedError(
|
| 8 |
+
"Task 'next_quarter_move' is not implemented yet. "
|
| 9 |
+
"Implement grader logic in tasks/next_quarter_move/grader.py."
|
| 10 |
+
)
|
tasks/next_quarter_move/spec.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Task specification for ``next_quarter_move`` — fill in when implementing."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from ..types import TaskSpec
|
| 6 |
+
|
| 7 |
+
CANONICAL_TASK_ID = "next_quarter_move"
|
| 8 |
+
|
| 9 |
+
SPEC: TaskSpec = {
|
| 10 |
+
"task_id": CANONICAL_TASK_ID,
|
| 11 |
+
"implemented": False,
|
| 12 |
+
"text_cols": [],
|
| 13 |
+
"numerical_cols": [],
|
| 14 |
+
"label_col": "",
|
| 15 |
+
"label_values": [],
|
| 16 |
+
"task_instruction": "",
|
| 17 |
+
"kind": "regression",
|
| 18 |
+
}
|
tasks/registry.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Central registry of all tasks.
|
| 3 |
+
|
| 4 |
+
Add a new task:
|
| 5 |
+
1. Create ``tasks/<folder>/`` with ``spec.py``, ``grader.py``, and ``__init__.py``
|
| 6 |
+
exporting ``SPEC`` and ``grade``. If the folder name is not a valid Python
|
| 7 |
+
identifier (e.g. ``1_day_move``), register it via ``load_task_subpackage`` in
|
| 8 |
+
this file.
|
| 9 |
+
2. Append ``(SPEC, grade)`` to ``_TASK_ENTRIES``.
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
from __future__ import annotations
|
| 13 |
+
|
| 14 |
+
from collections.abc import Callable
|
| 15 |
+
from typing import Final
|
| 16 |
+
|
| 17 |
+
from . import next_quarter_move, sentiment_label
|
| 18 |
+
from .loader import load_task_subpackage
|
| 19 |
+
from .types import TaskSpec
|
| 20 |
+
|
| 21 |
+
GradingFn = Callable[[str, str, list[str]], float]
|
| 22 |
+
|
| 23 |
+
_pkg_1_day_move = load_task_subpackage(
|
| 24 |
+
"1_day_move",
|
| 25 |
+
"earnings_analyst.tasks._pkg_1_day_move",
|
| 26 |
+
)
|
| 27 |
+
_pkg_30_day_move = load_task_subpackage(
|
| 28 |
+
"30_day_move",
|
| 29 |
+
"earnings_analyst.tasks._pkg_30_day_move",
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
_TASK_ENTRIES: list[tuple[TaskSpec, GradingFn]] = [
|
| 33 |
+
(sentiment_label.SPEC, sentiment_label.grade),
|
| 34 |
+
(_pkg_1_day_move.SPEC, _pkg_1_day_move.grade),
|
| 35 |
+
(_pkg_30_day_move.SPEC, _pkg_30_day_move.grade),
|
| 36 |
+
(next_quarter_move.SPEC, next_quarter_move.grade),
|
| 37 |
+
]
|
| 38 |
+
|
| 39 |
+
TASKS: dict[str, TaskSpec] = {spec["task_id"]: spec for spec, _ in _TASK_ENTRIES}
|
| 40 |
+
GRADERS: dict[str, GradingFn] = {spec["task_id"]: fn for spec, fn in _TASK_ENTRIES}
|
| 41 |
+
|
| 42 |
+
DEFAULT_TASK: Final[str] = "sentiment_label"
|
| 43 |
+
|
| 44 |
+
TASK_IDS: tuple[str, ...] = tuple(sorted(TASKS.keys()))
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def get_task_spec(task_id: str) -> TaskSpec:
|
| 48 |
+
if task_id not in TASKS:
|
| 49 |
+
raise KeyError(f"Unknown task_id={task_id!r}. Valid: {list(TASKS.keys())}")
|
| 50 |
+
return TASKS[task_id]
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def get_grader(task_id: str) -> GradingFn:
|
| 54 |
+
if task_id not in GRADERS:
|
| 55 |
+
raise KeyError(f"Unknown task_id={task_id!r}. Valid: {list(GRADERS.keys())}")
|
| 56 |
+
return GRADERS[task_id]
|
tasks/sentiment_label/__init__.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Sentiment label classification task (scaffold)."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from .grader import grade
|
| 6 |
+
from .spec import CANONICAL_TASK_ID, SPEC
|
| 7 |
+
|
| 8 |
+
__all__ = ["CANONICAL_TASK_ID", "SPEC", "grade"]
|
tasks/sentiment_label/grader.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Ordinal sentiment grading: exact=1.0, adjacent label=0.5, else 0.0."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from ..grading import grade_ordinal
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def grade(predicted: str, ground_truth: str, label_values: list[str]) -> float:
|
| 9 |
+
return grade_ordinal(predicted, ground_truth, label_values)
|
tasks/sentiment_label/spec.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Task specification for ``sentiment_label`` (ordinal sentiment classification)."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from ..types import TaskSpec
|
| 6 |
+
|
| 7 |
+
CANONICAL_TASK_ID = "sentiment_label"
|
| 8 |
+
|
| 9 |
+
SPEC: TaskSpec = {
|
| 10 |
+
"task_id": CANONICAL_TASK_ID,
|
| 11 |
+
"implemented": True,
|
| 12 |
+
"text_cols": [
|
| 13 |
+
"earnings_transcript",
|
| 14 |
+
"press_release_8k_body",
|
| 15 |
+
"press_release_ex991",
|
| 16 |
+
"press_release_ex992",
|
| 17 |
+
],
|
| 18 |
+
"numerical_cols": [
|
| 19 |
+
"price_momentum_30d",
|
| 20 |
+
"price_momentum_90d",
|
| 21 |
+
"pct_from_52w_high_pt",
|
| 22 |
+
"avg_volume_20d",
|
| 23 |
+
"d_minus_1_close",
|
| 24 |
+
],
|
| 25 |
+
"label_col": "sentiment_label",
|
| 26 |
+
"label_values": [
|
| 27 |
+
"very bearish",
|
| 28 |
+
"bearish",
|
| 29 |
+
"neutral",
|
| 30 |
+
"bullish",
|
| 31 |
+
"very bullish",
|
| 32 |
+
],
|
| 33 |
+
"task_instruction": (
|
| 34 |
+
"Analyse the provided earnings call materials and classify the overall market sentiment.\n\n"
|
| 35 |
+
"Return a JSON object matching this exact schema:\n"
|
| 36 |
+
'{"sentiment": "<one of: very bearish | bearish | neutral | bullish | very bullish>"}\n\n'
|
| 37 |
+
"Do not include any other keys or explanation."
|
| 38 |
+
),
|
| 39 |
+
"kind": "classification",
|
| 40 |
+
}
|
tasks/types.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Shared task specification types."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from typing import Literal, TypedDict
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class TaskSpec(TypedDict):
|
| 9 |
+
"""Configuration for one earnings-analyst task (per episode)."""
|
| 10 |
+
|
| 11 |
+
task_id: str
|
| 12 |
+
implemented: bool
|
| 13 |
+
text_cols: list[str]
|
| 14 |
+
numerical_cols: list[str]
|
| 15 |
+
label_col: str
|
| 16 |
+
label_values: list[str]
|
| 17 |
+
task_instruction: str
|
| 18 |
+
kind: Literal["classification", "regression", "other"]
|