Run this model with HF Jobs

Run UEC-InabaLab/CounselTrajLM on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.

Paste one of these prompts into your coding agent:

Fine-tune on my dataset

Use HF Jobs and TRL to fine-tune https://huggingface.co/UEC-InabaLab/CounselTrajLM. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.

Run batch inference

Use HF Jobs to run batch inference with https://huggingface.co/UEC-InabaLab/CounselTrajLM on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.

Evaluate on a benchmark

Use HF Jobs to evaluate https://huggingface.co/UEC-InabaLab/CounselTrajLM. Ask me which benchmark to run, then save the scores and the per-example results to a new dataset repo. Run `hf jobs --help` first to learn the CLI.

Quantize for lower memory use

Use HF Jobs to create a 4-bit version of https://huggingface.co/UEC-InabaLab/CounselTrajLM. Push it to a new model repo, then reload it from there and report its memory footprint. Run `hf jobs --help` first to learn the CLI.
CounselTrajLM / generate_reply.py
ZhiyangQi97's picture
Release CounselTrajLM Qwen3.5-9B counselor response adapter
9203391 verified
Raw History Blame
4.7 kB
"""Generate the next Chinese counselor reply with the CounselTrajLM adapter."""
import argparse
import json
from pathlib import Path
import torch
from peft import PeftModel
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
REPO_ID = "UEC-InabaLab/CounselTrajLM"
BASE_MODEL = "Qwen/Qwen3.5-9B"
BASE_REVISION = "c202236235762e1c871ad0ccb60c8ee5ba337b9a"
SYSTEM_PROMPT = (
"你是一名专业、温和、自然的线上心理咨询师。"
"请根据对话历史生成下一句咨询师回复。"
"回复只写咨询师要说的话,不要写说话人名称,不要写对话标签。"
)
def build_prompt(tokenizer, history):
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
{
"role": "user",
"content": f"对话历史:\n{history[-1800:]}\n\n请生成下一句咨询师回复。",
},
]
return tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
enable_thinking=False,
)
def load_model(adapter, base_model=BASE_MODEL, local_files_only=False, use_4bit=True):
tokenizer = AutoTokenizer.from_pretrained(adapter, local_files_only=local_files_only)
if tokenizer.pad_token_id is None:
tokenizer.pad_token = tokenizer.eos_token
kwargs = {
"dtype": torch.bfloat16,
"device_map": "auto",
"attn_implementation": "eager",
"local_files_only": local_files_only,
}
if base_model == BASE_MODEL:
kwargs["revision"] = BASE_REVISION
if use_4bit:
kwargs["quantization_config"] = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16,
)
base = AutoModelForCausalLM.from_pretrained(base_model, **kwargs)
model = PeftModel.from_pretrained(base, adapter, local_files_only=local_files_only)
model.eval()
return tokenizer, model
def generate_reply(tokenizer, model, history, max_new_tokens=256):
prompt = build_prompt(tokenizer, history)
inputs = tokenizer(
prompt,
return_tensors="pt",
add_special_tokens=False,
truncation=True,
max_length=2048,
).to(model.device)
with torch.inference_mode():
output = model.generate(
**inputs,
max_new_tokens=max_new_tokens,
do_sample=False,
repetition_penalty=1.05,
pad_token_id=tokenizer.pad_token_id,
eos_token_id=tokenizer.eos_token_id,
)
return tokenizer.decode(
output[0, inputs["input_ids"].shape[1]:], skip_special_tokens=True
).strip()
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--adapter", default=REPO_ID)
parser.add_argument("--base-model", default=BASE_MODEL)
parser.add_argument("--history")
parser.add_argument("--input-file", type=Path)
parser.add_argument("--output-file", type=Path)
parser.add_argument("--max-new-tokens", type=int, default=256)
parser.add_argument("--local-files-only", action="store_true")
parser.add_argument("--no-4bit", action="store_true")
args = parser.parse_args()
if bool(args.history) == bool(args.input_file):
parser.error("Provide exactly one of --history or --input-file")
rows = [{"id": "example", "history": args.history}] if args.history else [
json.loads(line)
for line in args.input_file.read_text(encoding="utf-8").splitlines()
if line.strip()
]
if not rows or any(not isinstance(row.get("history"), str) or not row["history"].strip() for row in rows):
parser.error("Every input row must contain a nonempty history string")
tokenizer, model = load_model(
args.adapter,
base_model=args.base_model,
local_files_only=args.local_files_only,
use_4bit=not args.no_4bit,
)
results = []
for row in rows:
reply = generate_reply(tokenizer, model, row["history"], args.max_new_tokens)
if not reply:
raise RuntimeError("Empty reply for input " + str(row.get("id", "")))
result = {"id": row.get("id", ""), "history": row["history"], "response": reply}
results.append(result)
print(json.dumps(result, ensure_ascii=False), flush=True)
if args.output_file:
args.output_file.parent.mkdir(parents=True, exist_ok=True)
args.output_file.write_text(
"".join(json.dumps(row, ensure_ascii=False) + "\n" for row in results),
encoding="utf-8",
)
if __name__ == "__main__":
main()