Text Generation
Transformers
Safetensors
gpt_oss
russian
orthography-normalization
historical-text
unsloth
lora
sft
trl
4-bit precision
bitsandbytes
conversational
4-bit precision
Instructions to use ZennyKenny/novoyaz-20b with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use ZennyKenny/novoyaz-20b with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="ZennyKenny/novoyaz-20b") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("ZennyKenny/novoyaz-20b") model = AutoModelForCausalLM.from_pretrained("ZennyKenny/novoyaz-20b", device_map="auto") messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=256) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use ZennyKenny/novoyaz-20b with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "ZennyKenny/novoyaz-20b" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "ZennyKenny/novoyaz-20b", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/ZennyKenny/novoyaz-20b
- SGLang
How to use ZennyKenny/novoyaz-20b with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "ZennyKenny/novoyaz-20b" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "ZennyKenny/novoyaz-20b", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "ZennyKenny/novoyaz-20b" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "ZennyKenny/novoyaz-20b", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Unsloth Desktop
- Docker Model Runner
How to use ZennyKenny/novoyaz-20b with Docker Model Runner:
docker model run hf.co/ZennyKenny/novoyaz-20b
Download handler.py from ZennyKenny/novoyaz-20b: direct link, hf CLI and curl.
- Browser
- Download file 6.12 kB
-
https://huggingface.co/ZennyKenny/novoyaz-20b/resolve/main/handler.py
- Command line
-
hf download hf://ZennyKenny/novoyaz-20b/handler.py
-
curl -L -o handler.py https://huggingface.co/ZennyKenny/novoyaz-20b/resolve/main/handler.py
6.12 kB
| # handler.py - PRODUCTION VERSION FOR INFERENCE ENDPOINTS | |
| from __future__ import annotations | |
| import os | |
| from typing import Any, Dict, List, Union | |
| import torch | |
| from transformers import AutoTokenizer, AutoModelForCausalLM | |
| PROMPT_PREFIX = ( | |
| "Ты – модель, которая строго переписывает дореформенный русский текст " | |
| "в современную орфографию, не меняя смысл и пунктуацию. " | |
| "Не добавляй комментарии и не переводь текст.\n\nТекст:\n" | |
| ) | |
| PROMPT_SUFFIX = "\n\nСовременный орфографический вариант:" | |
| def _as_list(x: Union[str, List[str]]) -> List[str]: | |
| return [x] if isinstance(x, str) else [str(t) for t in x] | |
| # Load from unsloth's 4-bit quantized version which doesn't use custom files | |
| # OR use the base openai model with trust_remote_code | |
| USE_BASE_MODEL = os.getenv("USE_BASE_MODEL", "true").lower() == "true" | |
| if USE_BASE_MODEL: | |
| MODEL_ID = "openai/gpt-oss-20b" | |
| TRUST_REMOTE_CODE = True | |
| else: | |
| # Alternative: use unsloth's version which may have custom files included | |
| MODEL_ID = "unsloth/gpt-oss-20b-bnb-4bit" | |
| TRUST_REMOTE_CODE = False | |
| GEN_KW = { | |
| "do_sample": False, | |
| "temperature": 0.0, | |
| "num_beams": 1, | |
| "max_new_tokens": int(os.getenv("GEN_MAX_NEW_TOKENS", "512")), | |
| "repetition_penalty": 1.0, | |
| } | |
| class EndpointHandler: | |
| def __init__(self, model_dir: str): | |
| """ | |
| Initialize the endpoint handler. | |
| NOTE: For Inference Endpoints, model_dir points to /repository | |
| but we're loading from HuggingFace Hub instead since your | |
| quantized model is missing the custom architecture files. | |
| """ | |
| print(f"[handler] Model directory provided: {model_dir}") | |
| print(f"[handler] Loading model from: {MODEL_ID}") | |
| print(f"[handler] Trust remote code: {TRUST_REMOTE_CODE}") | |
| self.device = "cuda" if torch.cuda.is_available() else "cpu" | |
| # Load tokenizer | |
| self.tokenizer = AutoTokenizer.from_pretrained( | |
| MODEL_ID, | |
| use_fast=True, | |
| trust_remote_code=TRUST_REMOTE_CODE | |
| ) | |
| # Load model | |
| # The openai/gpt-oss-20b model uses MXFP4 quantization by default | |
| # which requires specific hardware (H100/A100) | |
| # For general deployment, we use bfloat16 or float16 | |
| if torch.cuda.is_available(): | |
| # Check if we can use MXFP4 (ideal) | |
| dtype = "auto" # Will use MXFP4 if available, otherwise bf16/f16 | |
| else: | |
| dtype = torch.float32 | |
| try: | |
| self.model = AutoModelForCausalLM.from_pretrained( | |
| MODEL_ID, | |
| torch_dtype=dtype, | |
| device_map="auto" if torch.cuda.is_available() else None, | |
| trust_remote_code=TRUST_REMOTE_CODE, | |
| low_cpu_mem_usage=True, | |
| ) | |
| except Exception as e: | |
| print(f"[handler] Error loading with MXFP4/auto dtype: {e}") | |
| print(f"[handler] Falling back to bfloat16...") | |
| # Fallback to bfloat16 if MXFP4 not supported | |
| self.model = AutoModelForCausalLM.from_pretrained( | |
| MODEL_ID, | |
| torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32, | |
| device_map="auto" if torch.cuda.is_available() else None, | |
| trust_remote_code=TRUST_REMOTE_CODE, | |
| low_cpu_mem_usage=True, | |
| ) | |
| # Set pad token | |
| if self.tokenizer.pad_token is None: | |
| self.tokenizer.pad_token = self.tokenizer.eos_token | |
| # Disable caching on CPU | |
| if not torch.cuda.is_available(): | |
| self.model.config.use_cache = False | |
| self.model.eval() | |
| print(f"[handler] ✓ Model loaded successfully") | |
| print(f"[handler] Device: {self.model.device}") | |
| print(f"[handler] Dtype: {self.model.dtype}") | |
| print(f"[handler] Model architecture: {self.model.config.architectures}") | |
| def _encode(self, texts: List[str]) -> Dict[str, Any]: | |
| """Encode texts with task-specific prompt.""" | |
| prompts = [f"{PROMPT_PREFIX}{t}{PROMPT_SUFFIX}" for t in texts] | |
| toks = self.tokenizer( | |
| prompts, | |
| return_tensors="pt", | |
| padding=True, | |
| truncation=True, | |
| max_length=2048 # Prevent overly long inputs | |
| ) | |
| return {k: v.to(self.model.device) for k, v in toks.items()} | |
| def __call__(self, data: Dict[str, Any]) -> List[Dict[str, str]]: | |
| """ | |
| Process inference request. | |
| Expected input format: | |
| { | |
| "inputs": "дореформенный текст" or ["текст1", "текст2"] | |
| } | |
| Returns: | |
| [ | |
| {"generated_text": "современный текст"}, | |
| ... | |
| ] | |
| """ | |
| if "inputs" not in data: | |
| return [{"error": "missing 'inputs' field"}] | |
| texts = _as_list(data["inputs"]) | |
| if not texts or all(not t.strip() for t in texts): | |
| return [{"error": "empty input text"}] | |
| try: | |
| inputs = self._encode(texts) | |
| outputs = self.model.generate(**inputs, **GEN_KW) | |
| results: List[Dict[str, str]] = [] | |
| for i, seq in enumerate(outputs): | |
| # Remove input tokens from output | |
| in_len = inputs["input_ids"][i].shape[-1] | |
| gen_only = seq[in_len:] | |
| text = self.tokenizer.decode(gen_only, skip_special_tokens=True).strip() | |
| results.append({"generated_text": text}) | |
| return results | |
| except Exception as e: | |
| print(f"[handler] Error during generation: {e}") | |
| import traceback | |
| traceback.print_exc() | |
| return [{"error": f"generation failed: {str(e)}"}] |