-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(batch - spellcheck): ⚡ From predictions to insights
- Loading branch information
1 parent
c14338d
commit 6c83b8c
Showing
4 changed files
with
110 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ | |
from .buckets import ( | ||
GoogleStorageBucketForBatchJob, | ||
) | ||
from .importer import generate_predictions_from_batch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import io | ||
from typing import Iterator | ||
|
||
import pandas as pd | ||
|
||
from robotoff.batch import BatchJobType | ||
from robotoff.types import Prediction, PredictionType | ||
|
||
|
||
BATCH_JOB_TYPE_TO_FEATURES = { | ||
BatchJobType.ingredients_spellcheck: { | ||
"barcode": "code", | ||
"value": "correction", | ||
"value_tag": "lang", | ||
}, | ||
} | ||
|
||
BATCH_JOB_TYPE_TO_PREDICTION_TYPE = { | ||
BatchJobType.ingredients_spellcheck: PredictionType.ingredient_spellcheck, | ||
} | ||
|
||
PREDICTOR_VERSION = "1" | ||
|
||
|
||
def generate_predictions_from_batch( | ||
f: io.BufferedReader, | ||
job_type: BatchJobType | ||
) -> Iterator[Prediction]: | ||
"""From a file imported from google storage, generate predictions depending on the job type. | ||
:param f: Readable object. Should be a parquet file. | ||
:type f: io.BufferedReader | ||
:param job_type: Batch job type. | ||
:type job_type: BatchJobType | ||
:rtype: Iterable[Prediction] | ||
:yield: Predictions. | ||
:rtype: Iterator[Prediction] | ||
""" | ||
features_dict = BATCH_JOB_TYPE_TO_FEATURES[job_type] | ||
prediction_type = BATCH_JOB_TYPE_TO_PREDICTION_TYPE[job_type] | ||
|
||
try: | ||
df = pd.read_parquet(f) | ||
except Exception as e: | ||
raise ValueError(f"Failed to read parquet file: {e}") | ||
|
||
for _, row in df.iterrows(): | ||
yield Prediction( | ||
type=prediction_type, | ||
value=row[features_dict["value"]], | ||
value_tag=[features_dict["value_tag"]], | ||
barcode=row[features_dict["barcode"]], | ||
predictor_version=PREDICTOR_VERSION, | ||
predictor="llm", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters