Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add command to run ML models on proofs without predictions #599

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ GUNICORN_WORKERS=1
# It works because we added the special `host.docker.internal:host-gateway`
# host in dev.yml for all services
# Triton is the ML inference server used at Open Food Facts
TRITON_URI=host.docker.internal:5004
TRITON_URI=host.docker.internal:5504

# By default, don't enable ML predictions, as we don't necessarily have a Triton
# server running.
# During local development, to enable ML predictions, set this to True and make sure
# you have Triton running on port 5004.
# you have Triton running on port 5504.
ENABLE_ML_PREDICTIONS=False
2 changes: 1 addition & 1 deletion config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,5 +293,5 @@
# Triton Inference Server (ML)
# ------------------------------------------------------------------------------

TRITON_URI = os.getenv("TRITON_URI", "localhost:5004")
TRITON_URI = os.getenv("TRITON_URI", "localhost:5504")
ENABLE_ML_PREDICTIONS = os.getenv("ENABLE_ML_PREDICTIONS") == "True"
53 changes: 53 additions & 0 deletions open_prices/proofs/management/commands/run_ml_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import argparse

import tqdm
from django.core.management.base import BaseCommand

from open_prices.proofs.ml.image_classifier import (
PROOF_CLASSIFICATION_MODEL_NAME,
run_and_save_proof_prediction,
)
from open_prices.proofs.models import Proof


class Command(BaseCommand):
help = """Run ML models on images with proof predictions, and save the predictions
in DB."""
_allowed_types = ["proof_classification"]

def add_arguments(self, parser: argparse.ArgumentParser) -> None:
parser.add_argument(
"--limit", type=int, help="Limit the number of proofs to process."
)
parser.add_argument("type", type=str, help="Type of model to run.", nargs="+")

def handle(self, *args, **options) -> None: # type: ignore
self.stdout.write(
"Running ML models on images without proof predictions for this model..."
)
limit = options["limit"]
types = options["type"]

if not all(t in self._allowed_types for t in types):
raise ValueError(
f"Invalid type(s) provided: {types}, allowed: {self._allowed_types}"
)

if "proof_classification" in types:
# Get proofs that don't have a proof prediction with
# model_name = PROOF_CLASSIFICATION_MODEL_NAME by performing an
# outer join on the Proof and Prediction tables.
proofs = (
Proof.objects.filter(predictions__model_name__isnull=True)
| Proof.objects.exclude(
predictions__model_name=PROOF_CLASSIFICATION_MODEL_NAME
)
).distinct()

if limit:
proofs = proofs[:limit]

for proof in tqdm.tqdm(proofs):
self.stdout.write(f"Processing proof {proof.id}...")
run_and_save_proof_prediction(proof.id)
self.stdout.write("Done.")
Loading