Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/rblu/data_argument.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"""A moudle"""
"""A moudle"""
59 changes: 31 additions & 28 deletions src/rblu/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
from torch.utils.data import DataLoader
from torch.utils.data import Dataset as TorchDataset
from tqdm import tqdm
from transformers import (BatchEncoding, PreTrainedTokenizer,
PreTrainedTokenizerFast)
from transformers import (
BatchEncoding,
PreTrainedTokenizer,
PreTrainedTokenizerFast,
)


class BackupGenerate:
Expand All @@ -26,10 +29,10 @@ class BackupGenerate:
"""

def __init__(
self,
backup_mongodb: MongoCollection,
query_model_name: str,
gen_kwargs: dict,
self,
backup_mongodb: MongoCollection,
query_model_name: str,
gen_kwargs: dict,
):
"""
Initialize the generator with a MongoDB collection and a model name.
Expand Down Expand Up @@ -162,15 +165,15 @@ class MyGenerator(BackupGenerate):
"""

def __init__(
self,
model,
tokenizer,
batch_size,
apply_template: Callable[[str], list[dict]] | None,
tokenizer_kwargs,
gen_kwargs,
backup_mongodb,
query_model_name,
self,
model,
tokenizer,
batch_size,
apply_template: Callable[[str], list[dict]] | None,
tokenizer_kwargs,
gen_kwargs,
backup_mongodb,
query_model_name,
) -> None:
"""
Initializes the generator class with the given parameters.
Expand Down Expand Up @@ -252,7 +255,7 @@ def generate(self, text_list: list[str]) -> list[str]:
}
outputs = self.model.generate(**inputs, **self.gen_kwargs)
decoded_outputs = self.tokenizer.batch_decode(
outputs[:, inputs["input_ids"].size(1):],
outputs[:, inputs["input_ids"].size(1) :],
skip_special_tokens=True,
)
responses.extend(decoded_outputs)
Expand Down Expand Up @@ -303,13 +306,13 @@ def _tokenize_texts(self, text_list: list[str]) -> BatchEncoding:

class APIGenerator(BackupGenerate):
def __init__(
self,
url: str,
model_name: str,
key: str,
mongodb: MongoCollection,
query_model_name: str,
gen_kwargs: dict,
self,
url: str,
model_name: str,
key: str,
mongodb: MongoCollection,
query_model_name: str,
gen_kwargs: dict,
):
allowed_gen_kwargs = [
"temperature",
Expand All @@ -333,11 +336,11 @@ def __init__(

@staticmethod
def openai_generate(
url: str,
key: str,
model_name: str,
messages: list[dict],
gen_kwargs: dict,
url: str,
key: str,
model_name: str,
messages: list[dict],
gen_kwargs: dict,
):
"""
Generates text using the OpenAI API with retry mechanism.
Expand Down
58 changes: 29 additions & 29 deletions src/rblu/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@
from rblu.evaluation import conservation_infer, reverse_infer, save_score
from rblu.generate import APIGenerator, MyGenerator
from rblu.metric import rouge_and_bert
from rblu.process.reservation_process import (get_reservation_process)
from rblu.process.reservation_process import get_reservation_process
from rblu.process.reverse_process import get_reverse_process
from rblu.template import apply_default_template, apply_default_zh_template
from rblu.utils.path import CONFIG_PATH, RESULT_DIR, SCORE_DIR


def create_generator(
language: str,
model_name: str,
model_checkpoint: str | dict,
backup_mongodb: MongoCollection,
batch_size: int,
gen_kwargs: dict,
tokenizer_kwargs: dict,
language: str,
model_name: str,
model_checkpoint: str | dict,
backup_mongodb: MongoCollection,
batch_size: int,
gen_kwargs: dict,
tokenizer_kwargs: dict,
) -> APIGenerator | MyGenerator:
"""
Creates a generator instance based on the provided model checkpoint.
Expand All @@ -63,8 +63,8 @@ def create_generator(
model_checkpoint = model_checkpoint

if (
not isinstance(model_checkpoint, dict)
or model_checkpoint["type"] != "api"
not isinstance(model_checkpoint, dict)
or model_checkpoint["type"] != "api"
):
return _get_local_generator(
model_checkpoint,
Expand All @@ -76,8 +76,8 @@ def create_generator(
tokenizer_kwargs=tokenizer_kwargs,
)
if (
"key" not in model_checkpoint.keys()
or model_checkpoint["key"] == "envs"
"key" not in model_checkpoint.keys()
or model_checkpoint["key"] == "envs"
):
model_checkpoint["key"] = os.getenv("GPTAPI_KEY")
return APIGenerator(
Expand All @@ -91,13 +91,13 @@ def create_generator(


def _get_local_generator(
model_checkpoint: str,
model_name: str,
language: str,
batch_size: int,
backup_mongodb: MongoCollection,
gen_kwargs: dict,
tokenizer_kwargs: dict,
model_checkpoint: str,
model_name: str,
language: str,
batch_size: int,
backup_mongodb: MongoCollection,
gen_kwargs: dict,
tokenizer_kwargs: dict,
) -> MyGenerator:
"""
Initializes and returns a MyGenerator instance with the specified
Expand Down Expand Up @@ -148,8 +148,8 @@ def _get_local_generator(


def start_evaluation(
config: dict,
evaluate_task: str,
config: dict,
evaluate_task: str,
) -> None:
"""
Evaluates a given task using the specified configuration and process.
Expand Down Expand Up @@ -196,8 +196,8 @@ def start_evaluation(
)

output_path = (
RESULT_DIR
/ f"{model_name}_{evaluate_task}_{config['stage']}_{language}"
RESULT_DIR
/ f"{model_name}_{evaluate_task}_{config['stage']}_{language}"
)

if os.path.exists(output_path) and not config["force_regenerate"]:
Expand Down Expand Up @@ -318,9 +318,9 @@ def eval():
# set the basic 'accelerate' environment on mutil-gpu
write_basic_config(mixed_precision="fp16")
with open(
config_path,
"r",
encoding="utf-8",
config_path,
"r",
encoding="utf-8",
) as config_file:
run_config = yaml.safe_load(config_file)
logging.info("Config loaded from %s", config_path)
Expand Down Expand Up @@ -358,9 +358,9 @@ def draw():
if args.suffix is None:
args.suffix = "png"
with open(
file=CONFIG_PATH,
mode="r",
encoding="utf-8",
file=CONFIG_PATH,
mode="r",
encoding="utf-8",
) as config_file:
config = yaml.safe_load(config_file) # noqa: F821
draw_metric(
Expand Down