# Foundry Job Service — Serverless Fine-Tune Tracker (Flask + SQLAlchemy + httpx)

## PURPOSE
In the sponsored segment of the video, the presenter uploads a dataset of her own study
notes to Crusoe Intelligence Foundry, picks an open model, and starts a serverless
fine-tune — producing a custom model trained on how she personally studies, which she can
then call from any device, "even as small as an Arduino or Raspberry Pi." This project is
the small backend that makes that workflow trackable: register a fine-tune job, persist
it, poll the remote artifact service, and expose the catalog of allowed base models.

## WHY THIS MATTERS
It encodes the transcript's point that open-model work is bottlenecked by infrastructure,
not by model quality. The service separates three concerns the video names explicitly:
the dataset (your notes), the base model choice, and the deployment target (any device
calling the finished model). It is the Alchemist-class concern of taking an experiment
and making it a reliable, observable system rather than a one-off notebook.

## HOW TO BUILD
Stack: **Flask + SQLAlchemy 2.0 (ORM) + httpx**. Declare a `FineTuneJob` table with
`DeclarativeBase` and `Mapped` columns, validate the requested `base_model` against an
allowlist, then wrap the upstream call in a thin async `FoundryClient`. The key trick is
a deterministic stub so the starter runs with no credentials:

```python
async def submit(self, dataset_name, base_model):
    if self.api_key:
        async with httpx.AsyncClient(base_url=BASE_URL,
                headers={"Authorization": f"Bearer {self.api_key}"}) as c:
            r = await c.post("/fine_tunes", json={"dataset": dataset_name,
                                                  "model": base_model})
            r.raise_for_status()
            return r.json()
    return {"job_id": f"stub-{digest(dataset_name)}", "status": "queued"}
```

POST `/jobs` inserts a row; GET `/jobs` returns the catalog of jobs.

## WHEN TO USE IN PRODUCTION
Use it whenever a team triggers long-running external ML jobs and needs durable state,
status polling, and an audit trail — fine-tunes, batch inference, embedding rebuilds.
It is also the right shape for a gateway in front of several model providers.
Do NOT use it for synchronous sub-second inference (skip the DB), for jobs with no
external side effects, or where a managed queue like Celery/SQS already owns retries.

## HOW IT HELPS STUDENTS
Students practice ORM modeling, input validation, an async HTTP client, and the boundary
between a stubbed dependency and a live one — exactly the seam that keeps prototypes
shippable.

## STARTER CODE
`starter/main.py` contains the `FineTuneJob` model, the `FoundryClient` with a stub,
and the `/jobs` routes, with `# TODO` markers for real endpoints, status mapping, and
auth.
