Download src/modeling.py from sanidhya1910/northwind-support-lora: direct link, hf CLI and curl.
- Browser
- Download file 2.49 kB
-
https://huggingface.co/spaces/sanidhya1910/northwind-support-lora/resolve/main/src/modeling.py
- Command line
-
hf download hf://spaces/sanidhya1910/northwind-support-lora/src/modeling.py
-
curl -L -o modeling.py https://huggingface.co/spaces/sanidhya1910/northwind-support-lora/resolve/main/src/modeling.py
2.49 kB
| """Shared model loading / prompt rendering / generation. | |
| Used by the trainer, the evaluator and the Gradio app so that the exact same | |
| prompt format is applied everywhere. Qwen3 is run in non-thinking mode | |
| (`enable_thinking=False`) for both training and inference. | |
| """ | |
| import os | |
| import torch | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| DEFAULT_MODEL = os.environ.get("BASE_MODEL", "Qwen/Qwen3-1.7B") | |
| DEFAULT_ADAPTER = os.environ.get("ADAPTER_PATH", "outputs/lora-support") | |
| def best_dtype(): | |
| if torch.cuda.is_available(): | |
| return torch.bfloat16 if torch.cuda.is_bf16_supported() else torch.float16 | |
| return torch.float32 | |
| def load_tokenizer(model_id=DEFAULT_MODEL): | |
| tok = AutoTokenizer.from_pretrained(model_id) | |
| if tok.pad_token is None: | |
| tok.pad_token = tok.eos_token | |
| tok.padding_side = "left" # correct side for batched generation | |
| return tok | |
| def load_model(model_id=DEFAULT_MODEL, adapter=None, dtype=None): | |
| model = AutoModelForCausalLM.from_pretrained( | |
| model_id, | |
| dtype=dtype or best_dtype(), | |
| device_map="auto" if torch.cuda.is_available() else None, | |
| ) | |
| if adapter: | |
| from peft import PeftModel | |
| model = PeftModel.from_pretrained(model, adapter) | |
| model = model.merge_and_unload() # fold LoRA in for faster inference | |
| if not torch.cuda.is_available(): | |
| model = model.to("cpu") | |
| model.eval() | |
| return model | |
| def render_prompt(tok, messages): | |
| """Messages -> the exact string the model is asked to continue.""" | |
| return tok.apply_chat_template( | |
| messages, tokenize=False, add_generation_prompt=True, enable_thinking=False | |
| ) | |
| def prompt_token_count(tok, messages): | |
| return len(tok(render_prompt(tok, messages), add_special_tokens=False)["input_ids"]) | |
| def generate(model, tok, messages, max_new_tokens=300, temperature=0.0): | |
| text = render_prompt(tok, messages) | |
| enc = tok(text, return_tensors="pt", add_special_tokens=False).to(model.device) | |
| kwargs = dict( | |
| max_new_tokens=max_new_tokens, | |
| pad_token_id=tok.pad_token_id, | |
| eos_token_id=tok.eos_token_id, | |
| ) | |
| if temperature and temperature > 0: | |
| kwargs.update(do_sample=True, temperature=temperature, top_p=0.9) | |
| else: | |
| kwargs.update(do_sample=False) | |
| out = model.generate(**enc, **kwargs) | |
| new_tokens = out[0][enc["input_ids"].shape[1]:] | |
| return tok.decode(new_tokens, skip_special_tokens=True).strip() | |