import torch
from transformers import AutoModelForCausalLM, AutoTokenizer


def resolve_model(model, tokenizer=None):
    if isinstance(model, str):
        loaded_model = AutoModelForCausalLM.from_pretrained(
            model, device_map="auto", torch_dtype=torch.float16
        )
        loaded_tokenizer = AutoTokenizer.from_pretrained(model)
        if loaded_tokenizer.pad_token is None:
            loaded_tokenizer.pad_token = loaded_tokenizer.eos_token
        loaded_tokenizer.padding_side = "left"
        loaded_model.eval()
        return loaded_model, loaded_tokenizer, True
    if tokenizer is None:
        raise ValueError(
            "When passing a model object, you must also provide a tokenizer. "
            "Use: geood.calibrate(model, ref_texts, tokenizer=tokenizer)"
        )
    model.eval()
    return model, tokenizer, False
