1from transformers import AutoModelForCausalLM, AutoTokenizer, AutoProcessor
2import torch
3import numpy as np
45hf_model ="bytedance-research/ChatTS-14B"6# Load the model, tokenizer and processor7# For pre-Ampere GPUs (like V100) use `_attn_implementation='eager'`8model = AutoModelForCausalLM.from_pretrained(hf_model, trust_remote_code=True, device_map="auto", torch_dtype='float16')9tokenizer = AutoTokenizer.from_pretrained(hf_model, trust_remote_code=True)10processor = AutoProcessor.from_pretrained(hf_model, trust_remote_code=True, tokenizer=tokenizer)11# Create time series and prompts12timeseries = np.sin(np.arange(256)/10)*5.013timeseries[100:]-=10.014prompt =f"I have a time series length of 256: <ts><ts/>. Please analyze the local changes in this time series."15# Apply Chat Template16prompt =f"""<|im_start|>system
17You are a helpful assistant.<|im_end|><|im_start|>user
18{prompt}<|im_end|><|im_start|>assistant
19"""20# Convert to tensor21inputs = processor(text=[prompt], timeseries=[timeseries], padding=True, return_tensors="pt")22# Model Generate23outputs = model.generate(**inputs, max_new_tokens=300)24print(tokenizer.decode(outputs[0][len(inputs['input_ids'][0]):], skip_special_tokens=True))
@article{xie2024chatts,
title={ChatTS: Aligning Time Series with LLMs via Synthetic Data for Enhanced Understanding and Reasoning},
author={Xie, Zhe and Li, Zeyan and He, Xiao and Xu, Longlong and Wen, Xidao and Zhang, Tieying and Chen, Jianjun and Shi, Rui and Pei, Dan},
journal={arXiv preprint arXiv:2412.03104},
year={2024}
}