Upload folder using huggingface_hub
Browse files- README.md +68 -0
- config.json +29 -0
- generation_config.json +6 -0
- handler.py +99 -0
- model.safetensors +3 -0
- requirements.txt +2 -0
- special_tokens_map.json +30 -0
- tokenizer.json +0 -0
- tokenizer.model +3 -0
- tokenizer_config.json +45 -0
README.md
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language:
|
| 3 |
+
- en
|
| 4 |
+
tags:
|
| 5 |
+
- causal-lm
|
| 6 |
+
- llama
|
| 7 |
+
- instruction-tuned
|
| 8 |
+
- point-in-time
|
| 9 |
+
- dated
|
| 10 |
+
- lookahead-bias-free
|
| 11 |
+
pipeline_tag: text-generation
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
# DatedGPT-2013-Instruct
|
| 15 |
+
|
| 16 |
+
**DatedGPT** is a family of point-in-time language models: each vintage is
|
| 17 |
+
trained only on data available up to its cutoff date, making it suitable for
|
| 18 |
+
lookahead-bias-free prediction and point-in-time analysis.
|
| 19 |
+
|
| 20 |
+
This is the **instruction-tuned chat model** with data up to **2013**.
|
| 21 |
+
For the base (pretrained) model, see
|
| 22 |
+
[datedgpt/datedgpt-2013-base](https://huggingface.co/datedgpt/datedgpt-2013-base).
|
| 23 |
+
|
| 24 |
+
| Property | Value |
|
| 25 |
+
|----------|-------|
|
| 26 |
+
| Architecture | LlamaForCausalLM |
|
| 27 |
+
| Parameters | ~1.3 B |
|
| 28 |
+
| Context length | 2048 |
|
| 29 |
+
| Vocab | 32,000 (SentencePiece) |
|
| 30 |
+
| Precision | bfloat16 |
|
| 31 |
+
| Data vintage | 2013 |
|
| 32 |
+
|
| 33 |
+
## Chat template
|
| 34 |
+
|
| 35 |
+
The Llama-2-style chat template ships in `tokenizer_config.json` — apply it
|
| 36 |
+
with the tokenizer. The BOS token must come from the tokenizer, **not** as a
|
| 37 |
+
literal `"<s>"` string in your prompt text.
|
| 38 |
+
|
| 39 |
+
```python
|
| 40 |
+
import torch
|
| 41 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 42 |
+
|
| 43 |
+
repo_id = "datedgpt/datedgpt-2013-instruct"
|
| 44 |
+
tokenizer = AutoTokenizer.from_pretrained(repo_id)
|
| 45 |
+
model = AutoModelForCausalLM.from_pretrained(repo_id, torch_dtype=torch.bfloat16, device_map="auto")
|
| 46 |
+
|
| 47 |
+
prompt = tokenizer.apply_chat_template(
|
| 48 |
+
[{"role": "user", "content": "What is the capital of France?"}],
|
| 49 |
+
tokenize=False,
|
| 50 |
+
)
|
| 51 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 52 |
+
output = model.generate(**inputs, max_new_tokens=128, do_sample=True,
|
| 53 |
+
temperature=0.7, top_p=0.95, use_cache=True,
|
| 54 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 55 |
+
pad_token_id=tokenizer.eos_token_id)
|
| 56 |
+
print(tokenizer.decode(output[0, inputs["input_ids"].shape[-1]:], skip_special_tokens=True))
|
| 57 |
+
```
|
| 58 |
+
|
| 59 |
+
## Serving on HF Inference Endpoints
|
| 60 |
+
|
| 61 |
+
This repo ships a `handler.py` that applies the chat template server-side —
|
| 62 |
+
clients send plain text (or a messages list for multi-turn). Deploy with the
|
| 63 |
+
Default container on a bf16-capable GPU (A10G or better).
|
| 64 |
+
|
| 65 |
+
## Limitations
|
| 66 |
+
|
| 67 |
+
- Knowledge limited to the 2013 data vintage.
|
| 68 |
+
- No RLHF or safety tuning; outputs can be confidently wrong.
|
config.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"LlamaForCausalLM"
|
| 4 |
+
],
|
| 5 |
+
"attention_bias": false,
|
| 6 |
+
"attention_dropout": 0.0,
|
| 7 |
+
"bos_token_id": 1,
|
| 8 |
+
"eos_token_id": 2,
|
| 9 |
+
"head_dim": 128,
|
| 10 |
+
"hidden_act": "silu",
|
| 11 |
+
"hidden_size": 2048,
|
| 12 |
+
"initializer_range": 0.02,
|
| 13 |
+
"intermediate_size": 5504,
|
| 14 |
+
"max_position_embeddings": 2048,
|
| 15 |
+
"mlp_bias": false,
|
| 16 |
+
"model_type": "llama",
|
| 17 |
+
"num_attention_heads": 16,
|
| 18 |
+
"num_hidden_layers": 24,
|
| 19 |
+
"num_key_value_heads": 16,
|
| 20 |
+
"pretraining_tp": 1,
|
| 21 |
+
"rms_norm_eps": 1e-05,
|
| 22 |
+
"rope_scaling": null,
|
| 23 |
+
"rope_theta": 10000.0,
|
| 24 |
+
"tie_word_embeddings": false,
|
| 25 |
+
"torch_dtype": "bfloat16",
|
| 26 |
+
"transformers_version": "4.51.0",
|
| 27 |
+
"use_cache": false,
|
| 28 |
+
"vocab_size": 32000
|
| 29 |
+
}
|
generation_config.json
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_from_model_config": true,
|
| 3 |
+
"bos_token_id": 1,
|
| 4 |
+
"eos_token_id": 2,
|
| 5 |
+
"transformers_version": "4.51.0"
|
| 6 |
+
}
|
handler.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
+
|
| 5 |
+
# Keep only the last round (1 user+assistant pair) + the current user message
|
| 6 |
+
MAX_HISTORY_MESSAGES = 3
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class EndpointHandler:
|
| 10 |
+
"""
|
| 11 |
+
Hugging Face Inference Endpoints custom handler.
|
| 12 |
+
|
| 13 |
+
Expects input like:
|
| 14 |
+
- {"inputs": "hello"} -> single-turn, auto-wrapped with chat template
|
| 15 |
+
- {"inputs": [{"role":"user","content":"hello"}, ...]} -> multi-turn chat (messages list)
|
| 16 |
+
- {"inputs": "hello", "parameters": {"raw": true}} -> sent as-is (no template)
|
| 17 |
+
|
| 18 |
+
Optional:
|
| 19 |
+
- {"parameters": {"max_new_tokens": 512, "temperature": 0.7, ...}}
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
def __init__(self, path: str = ""):
|
| 23 |
+
model_dir = path or os.getenv("HF_MODEL_DIR", ".")
|
| 24 |
+
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 25 |
+
|
| 26 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_dir, trust_remote_code=True)
|
| 27 |
+
|
| 28 |
+
# Ensure pad token exists (common for causal LMs)
|
| 29 |
+
if self.tokenizer.pad_token is None:
|
| 30 |
+
self.tokenizer.pad_token = self.tokenizer.eos_token
|
| 31 |
+
self.tokenizer.pad_token_id = self.tokenizer.eos_token_id
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
| 35 |
+
model_dir,
|
| 36 |
+
torch_dtype="auto",
|
| 37 |
+
device_map="auto" if torch.cuda.is_available() else None,
|
| 38 |
+
trust_remote_code=True,
|
| 39 |
+
)
|
| 40 |
+
self.model.eval()
|
| 41 |
+
|
| 42 |
+
def __call__(self, data: dict) -> dict:
|
| 43 |
+
inputs = data.get("inputs", data)
|
| 44 |
+
params = data.get("parameters", {}) or {}
|
| 45 |
+
|
| 46 |
+
raw = bool(params.pop("raw", False))
|
| 47 |
+
|
| 48 |
+
if raw:
|
| 49 |
+
if not isinstance(inputs, str):
|
| 50 |
+
raise ValueError("raw mode requires inputs to be a string.")
|
| 51 |
+
prompt = inputs
|
| 52 |
+
elif isinstance(inputs, list):
|
| 53 |
+
# Multi-turn: inputs is a list of {"role": ..., "content": ...}
|
| 54 |
+
inputs = inputs[-MAX_HISTORY_MESSAGES:]
|
| 55 |
+
prompt = self.tokenizer.apply_chat_template(
|
| 56 |
+
inputs, tokenize=False,
|
| 57 |
+
)
|
| 58 |
+
elif isinstance(inputs, str):
|
| 59 |
+
# Single-turn: wrap in a one-message list
|
| 60 |
+
prompt = self.tokenizer.apply_chat_template(
|
| 61 |
+
[{"role": "user", "content": inputs}],
|
| 62 |
+
tokenize=False,
|
| 63 |
+
)
|
| 64 |
+
else:
|
| 65 |
+
raise ValueError("inputs must be a string or a list of messages.")
|
| 66 |
+
|
| 67 |
+
enc = self.tokenizer(
|
| 68 |
+
prompt,
|
| 69 |
+
return_tensors="pt",
|
| 70 |
+
padding=False,
|
| 71 |
+
truncation=True,
|
| 72 |
+
)
|
| 73 |
+
input_ids = enc["input_ids"].to(self.model.device)
|
| 74 |
+
attention_mask = enc.get("attention_mask", torch.ones_like(input_ids)).to(self.model.device)
|
| 75 |
+
|
| 76 |
+
gen_kwargs = {
|
| 77 |
+
"max_new_tokens": min(int(params.pop("max_new_tokens", 512)), 512),
|
| 78 |
+
"do_sample": bool(params.pop("do_sample", True)),
|
| 79 |
+
"temperature": float(params.pop("temperature", 0.7)),
|
| 80 |
+
"top_p": float(params.pop("top_p", 0.95)),
|
| 81 |
+
"repetition_penalty": float(params.pop("repetition_penalty", 1.2)),
|
| 82 |
+
"eos_token_id": self.tokenizer.eos_token_id,
|
| 83 |
+
"pad_token_id": self.tokenizer.pad_token_id,
|
| 84 |
+
}
|
| 85 |
+
gen_kwargs.update(params)
|
| 86 |
+
|
| 87 |
+
with torch.no_grad():
|
| 88 |
+
out = self.model.generate(
|
| 89 |
+
input_ids=input_ids,
|
| 90 |
+
attention_mask=attention_mask,
|
| 91 |
+
**gen_kwargs,
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
# Return only newly generated tokens (your current behavior)
|
| 95 |
+
new_tokens = out[0, input_ids.shape[-1]:]
|
| 96 |
+
text = self.tokenizer.decode(new_tokens, skip_special_tokens=True)
|
| 97 |
+
|
| 98 |
+
return {"generated_text": text}
|
| 99 |
+
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c6d8ffbcdd568826258f51e4a00bcf518f9fafe81bb717698d04dc390e35ac59
|
| 3 |
+
size 2690871976
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
accelerate
|
| 2 |
+
safetensors
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"bos_token": {
|
| 3 |
+
"content": "<s>",
|
| 4 |
+
"lstrip": false,
|
| 5 |
+
"normalized": false,
|
| 6 |
+
"rstrip": false,
|
| 7 |
+
"single_word": false
|
| 8 |
+
},
|
| 9 |
+
"eos_token": {
|
| 10 |
+
"content": "</s>",
|
| 11 |
+
"lstrip": false,
|
| 12 |
+
"normalized": false,
|
| 13 |
+
"rstrip": false,
|
| 14 |
+
"single_word": false
|
| 15 |
+
},
|
| 16 |
+
"pad_token": {
|
| 17 |
+
"content": "</s>",
|
| 18 |
+
"lstrip": false,
|
| 19 |
+
"normalized": false,
|
| 20 |
+
"rstrip": false,
|
| 21 |
+
"single_word": false
|
| 22 |
+
},
|
| 23 |
+
"unk_token": {
|
| 24 |
+
"content": "<unk>",
|
| 25 |
+
"lstrip": false,
|
| 26 |
+
"normalized": false,
|
| 27 |
+
"rstrip": false,
|
| 28 |
+
"single_word": false
|
| 29 |
+
}
|
| 30 |
+
}
|
tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tokenizer.model
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9e556afd44213b6bd1be2b850ebbbd98f5481437a8021afaf58ee7fb1818d347
|
| 3 |
+
size 499723
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"add_bos_token": true,
|
| 3 |
+
"add_eos_token": false,
|
| 4 |
+
"add_prefix_space": null,
|
| 5 |
+
"added_tokens_decoder": {
|
| 6 |
+
"0": {
|
| 7 |
+
"content": "<unk>",
|
| 8 |
+
"lstrip": false,
|
| 9 |
+
"normalized": false,
|
| 10 |
+
"rstrip": false,
|
| 11 |
+
"single_word": false,
|
| 12 |
+
"special": true
|
| 13 |
+
},
|
| 14 |
+
"1": {
|
| 15 |
+
"content": "<s>",
|
| 16 |
+
"lstrip": false,
|
| 17 |
+
"normalized": false,
|
| 18 |
+
"rstrip": false,
|
| 19 |
+
"single_word": false,
|
| 20 |
+
"special": true
|
| 21 |
+
},
|
| 22 |
+
"2": {
|
| 23 |
+
"content": "</s>",
|
| 24 |
+
"lstrip": false,
|
| 25 |
+
"normalized": false,
|
| 26 |
+
"rstrip": false,
|
| 27 |
+
"single_word": false,
|
| 28 |
+
"special": true
|
| 29 |
+
}
|
| 30 |
+
},
|
| 31 |
+
"bos_token": "<s>",
|
| 32 |
+
"chat_template": "{% if messages[0]['role'] == 'system' %}{% set loop_messages = messages[1:] %}{% set system_message = messages[0]['content'] %}{% else %}{% set loop_messages = messages %}{% endif %}{% for message in loop_messages %}{% if loop.index0 == 0 and system_message is defined %}{% set content = '<<SYS>>\n' + system_message + '\n<</SYS>>\n\n' + message['content'] %}{% else %}{% set content = message['content'] %}{% endif %}{% if message['role'] == 'user' %}{{ '<s>' + '[INST] ' + content + ' [/INST]' }}{% elif message['role'] == 'assistant' %}{{ content + '</s>' }}{% endif %}{% endfor %}",
|
| 33 |
+
"clean_up_tokenization_spaces": false,
|
| 34 |
+
"eos_token": "</s>",
|
| 35 |
+
"extra_special_tokens": {},
|
| 36 |
+
"legacy": false,
|
| 37 |
+
"model_max_length": 2048,
|
| 38 |
+
"pad_token": "</s>",
|
| 39 |
+
"padding_side": "right",
|
| 40 |
+
"sp_model_kwargs": {},
|
| 41 |
+
"split_special_tokens": false,
|
| 42 |
+
"tokenizer_class": "LlamaTokenizer",
|
| 43 |
+
"unk_token": "<unk>",
|
| 44 |
+
"use_default_system_prompt": false
|
| 45 |
+
}
|