Text Generation
Transformers
Safetensors
Russian
rugpt3xl
gpt3
russian
causal-lm
conversational
custom_code
Instructions to use evilfreelancer/ruGPT3XL with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use evilfreelancer/ruGPT3XL with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="evilfreelancer/ruGPT3XL", trust_remote_code=True) messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("evilfreelancer/ruGPT3XL", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use evilfreelancer/ruGPT3XL with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "evilfreelancer/ruGPT3XL" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "evilfreelancer/ruGPT3XL", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/evilfreelancer/ruGPT3XL
- SGLang
How to use evilfreelancer/ruGPT3XL 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 "evilfreelancer/ruGPT3XL" \ --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": "evilfreelancer/ruGPT3XL", "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 "evilfreelancer/ruGPT3XL" \ --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": "evilfreelancer/ruGPT3XL", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use evilfreelancer/ruGPT3XL with Docker Model Runner:
docker model run hf.co/evilfreelancer/ruGPT3XL
Upload generate.py
Browse files- generate.py +146 -0
generate.py
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Simple text generation demo for the converted ruGPT-3 XL model."""
|
| 3 |
+
|
| 4 |
+
import argparse
|
| 5 |
+
import sys
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
import torch
|
| 9 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def main():
|
| 13 |
+
parser = argparse.ArgumentParser(description="ruGPT-3 XL text generation demo")
|
| 14 |
+
parser.add_argument(
|
| 15 |
+
"--model_path",
|
| 16 |
+
type=str,
|
| 17 |
+
default="evilfreelancer/ruGPT3XL",
|
| 18 |
+
help="Path to the converted model directory",
|
| 19 |
+
)
|
| 20 |
+
parser.add_argument("--prompt", type=str, default=None, help="Text prompt")
|
| 21 |
+
parser.add_argument(
|
| 22 |
+
"--max_new_tokens", type=int, default=128, help="Max tokens to generate"
|
| 23 |
+
)
|
| 24 |
+
parser.add_argument("--temperature", type=float, default=0.7)
|
| 25 |
+
parser.add_argument("--top_k", type=int, default=50)
|
| 26 |
+
parser.add_argument("--top_p", type=float, default=0.9)
|
| 27 |
+
parser.add_argument("--repetition_penalty", type=float, default=1.2)
|
| 28 |
+
parser.add_argument(
|
| 29 |
+
"--device",
|
| 30 |
+
type=str,
|
| 31 |
+
default="cuda" if torch.cuda.is_available() else "cpu",
|
| 32 |
+
)
|
| 33 |
+
parser.add_argument(
|
| 34 |
+
"--dtype",
|
| 35 |
+
type=str,
|
| 36 |
+
default="float32",
|
| 37 |
+
choices=["float16", "float32", "bfloat16"],
|
| 38 |
+
)
|
| 39 |
+
parser.add_argument(
|
| 40 |
+
"--interactive", action="store_true", help="Interactive multi-turn mode"
|
| 41 |
+
)
|
| 42 |
+
args = parser.parse_args()
|
| 43 |
+
|
| 44 |
+
dtype_map = {
|
| 45 |
+
"float16": torch.float16,
|
| 46 |
+
"float32": torch.float32,
|
| 47 |
+
"bfloat16": torch.bfloat16,
|
| 48 |
+
}
|
| 49 |
+
torch_dtype = dtype_map[args.dtype]
|
| 50 |
+
|
| 51 |
+
print(f"Loading model from {args.model_path} ...")
|
| 52 |
+
print(f"Device: {args.device}, dtype: {args.dtype}")
|
| 53 |
+
|
| 54 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 55 |
+
args.model_path, trust_remote_code=True
|
| 56 |
+
)
|
| 57 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 58 |
+
args.model_path,
|
| 59 |
+
trust_remote_code=True,
|
| 60 |
+
dtype=torch_dtype,
|
| 61 |
+
).to(args.device)
|
| 62 |
+
model.eval()
|
| 63 |
+
|
| 64 |
+
print(f"Model loaded. Parameters: {sum(p.numel() for p in model.parameters()):,}")
|
| 65 |
+
print()
|
| 66 |
+
|
| 67 |
+
if args.interactive:
|
| 68 |
+
run_interactive(model, tokenizer, args)
|
| 69 |
+
elif args.prompt:
|
| 70 |
+
run_single(model, tokenizer, args.prompt, args)
|
| 71 |
+
else:
|
| 72 |
+
prompts = [
|
| 73 |
+
"Москва - столица",
|
| 74 |
+
"Искусственный интеллект - это",
|
| 75 |
+
"В далеком космосе",
|
| 76 |
+
]
|
| 77 |
+
for prompt in prompts:
|
| 78 |
+
run_single(model, tokenizer, prompt, args)
|
| 79 |
+
print("-" * 60)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def run_single(model, tokenizer, prompt, args):
|
| 83 |
+
print(f"Prompt: {prompt}")
|
| 84 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 85 |
+
|
| 86 |
+
with torch.no_grad():
|
| 87 |
+
outputs = model.generate(
|
| 88 |
+
**inputs,
|
| 89 |
+
max_new_tokens=args.max_new_tokens,
|
| 90 |
+
temperature=args.temperature,
|
| 91 |
+
top_k=args.top_k,
|
| 92 |
+
top_p=args.top_p,
|
| 93 |
+
repetition_penalty=args.repetition_penalty,
|
| 94 |
+
do_sample=True,
|
| 95 |
+
pad_token_id=tokenizer.pad_token_id,
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
generated = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 99 |
+
print(f"Generated: {generated}\n")
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def run_interactive(model, tokenizer, args):
|
| 103 |
+
print("Interactive mode. Type 'quit' to exit.\n")
|
| 104 |
+
while True:
|
| 105 |
+
try:
|
| 106 |
+
prompt = input("You: ").strip()
|
| 107 |
+
except (EOFError, KeyboardInterrupt):
|
| 108 |
+
print("\nGoodbye!")
|
| 109 |
+
break
|
| 110 |
+
|
| 111 |
+
if prompt.lower() in ("quit", "exit", "q"):
|
| 112 |
+
print("Goodbye!")
|
| 113 |
+
break
|
| 114 |
+
if not prompt:
|
| 115 |
+
continue
|
| 116 |
+
|
| 117 |
+
messages = [{"role": "user", "content": prompt}]
|
| 118 |
+
text = tokenizer.apply_chat_template(
|
| 119 |
+
messages, tokenize=False, add_generation_prompt=True
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
inputs = tokenizer(text, return_tensors="pt").to(model.device)
|
| 123 |
+
with torch.no_grad():
|
| 124 |
+
outputs = model.generate(
|
| 125 |
+
**inputs,
|
| 126 |
+
max_new_tokens=args.max_new_tokens,
|
| 127 |
+
temperature=args.temperature,
|
| 128 |
+
top_k=args.top_k,
|
| 129 |
+
top_p=args.top_p,
|
| 130 |
+
repetition_penalty=args.repetition_penalty,
|
| 131 |
+
do_sample=True,
|
| 132 |
+
pad_token_id=tokenizer.pad_token_id,
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
full_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 136 |
+
answer_marker = "Ответ: "
|
| 137 |
+
if answer_marker in full_text:
|
| 138 |
+
answer = full_text.split(answer_marker)[-1].strip()
|
| 139 |
+
else:
|
| 140 |
+
answer = full_text[len(text) :].strip()
|
| 141 |
+
|
| 142 |
+
print(f"Model: {answer}\n")
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
if __name__ == "__main__":
|
| 146 |
+
main()
|