2020"""
2121
2222import argparse
23+ from collections .abc import Callable
2324import os
24- import shutil
2525from pathlib import Path
26- from typing import Dict , Optional
26+ import shutil
27+ from typing import Any
2728
28- import torch
29- import yaml
30- from datasets import Dataset
29+ from datasets import Dataset # type: ignore
3130from huggingface_hub import hf_hub_download
31+ import torch
3232from transformers import AutoModelForCausalLM
3333from trl import SFTConfig , SFTTrainer
34+ import yaml
3435
3536from llmsql .loggers .logging_config import log
3637from llmsql .utils .utils import choose_prompt_builder , load_jsonl
3738
3839
39- def parse_args_and_config ():
40+ def parse_args_and_config () -> argparse . Namespace :
4041 """Parse CLI args and optionally merge with YAML config."""
4142 p = argparse .ArgumentParser (
4243 description = "Fine-tune a causal LM on Text-to-SQL benchmark."
@@ -66,10 +67,10 @@ def parse_args_and_config():
6667 # Load YAML config
6768 config = {}
6869 if args .get ("config_file" ):
69- with open (args ["config_file" ], "r" ) as f :
70+ with open (args ["config_file" ]) as f :
7071 config = yaml .safe_load (f )
7172
72- def flatten (d , parent_key = "" , sep = "_" ):
73+ def flatten (d : Any , parent_key : str = "" , sep : str = "_" ) -> dict [ str , Any ] :
7374 items = {}
7475 for k , v in d .items ():
7576 new_key = f"{ parent_key } { sep } { k } " if parent_key else k
@@ -88,7 +89,7 @@ def flatten(d, parent_key="", sep="_"):
8889 return argparse .Namespace (** args )
8990
9091
91- def build_dataset (file_path : str , tables : Dict , prompt_builder ) -> Dataset :
92+ def build_dataset (file_path : str , tables : dict , prompt_builder : Callable ) -> Dataset :
9293 """Convert JSONL file to HF dataset samples."""
9394 questions = load_jsonl (file_path )
9495 samples = []
@@ -115,15 +116,13 @@ def _download_file(filename: str, repo_id: str, workdir_path: str) -> str:
115116 shutil .copy (cached_path , local_path )
116117 return local_path
117118
118- return cached_path
119-
120119
121120def main (
122121 model_name_or_path : str ,
123122 output_dir : str ,
124- train_file : Optional [ str ] = None ,
125- val_file : Optional [ str ] = None ,
126- tables_file : Optional [ str ] = None ,
123+ train_file : str | None = None ,
124+ val_file : str | None = None ,
125+ tables_file : str | None = None ,
127126 shots : int = 5 ,
128127 num_train_epochs : int = 3 ,
129128 per_device_train_batch_size : int = 4 ,
@@ -136,11 +135,11 @@ def main(
136135 max_length : int = 32768 ,
137136 no_eval : bool = False ,
138137 eval_steps : int = 100 ,
139- wandb_project : Optional [ str ] = None ,
140- wandb_run_name : Optional [ str ] = None ,
141- wandb_key : Optional [ str ] = None ,
138+ wandb_project : str | None = None ,
139+ wandb_run_name : str | None = None ,
140+ wandb_key : str | None = None ,
142141 wandb_offline : bool = False ,
143- ):
142+ ) -> None :
144143 os .makedirs (output_dir , exist_ok = True )
145144
146145 # Seed
@@ -268,7 +267,7 @@ def main(
268267 log .info (f"Model saved at { output_dir } /final_model" )
269268
270269
271- def run_cli ():
270+ def run_cli () -> None :
272271 args = parse_args_and_config ()
273272 main (
274273 train_file = args .train_file ,
0 commit comments