Instructions to use Locutusque/gpt2-large-conversational with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Locutusque/gpt2-large-conversational with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="Locutusque/gpt2-large-conversational")# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("Locutusque/gpt2-large-conversational") model = AutoModelForCausalLM.from_pretrained("Locutusque/gpt2-large-conversational") - Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use Locutusque/gpt2-large-conversational with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "Locutusque/gpt2-large-conversational" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Locutusque/gpt2-large-conversational", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/Locutusque/gpt2-large-conversational
- SGLang
How to use Locutusque/gpt2-large-conversational 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 "Locutusque/gpt2-large-conversational" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Locutusque/gpt2-large-conversational", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'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 "Locutusque/gpt2-large-conversational" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Locutusque/gpt2-large-conversational", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use Locutusque/gpt2-large-conversational with Docker Model Runner:
docker model run hf.co/Locutusque/gpt2-large-conversational
| import torch | |
| from transformers import GPT2Tokenizer, AutoModelForCausalLM | |
| start_token = "<|ASSISTANT|>" | |
| end_token = "<|" | |
| tokenizer = GPT2Tokenizer.from_pretrained('gpt2-large') | |
| model = AutoModelForCausalLM.from_pretrained('gpt2-large', torch_dtype=torch.bfloat16) | |
| tokenizer.pad_token = "[PAD]" | |
| tokenizer.eos_token = "<|endoftext|>" | |
| tokenizer.add_special_tokens({"additional_special_tokens": ["<|ASSISTANT|>", "<|USER|>", "<|SYSTEM|>"]}) | |
| model.resize_token_embeddings(len(tokenizer)) | |
| model.load_state_dict(torch.load("/media/locutusque/T7/Projects/results/pytorch_model.bin")) | |
| model.cuda() | |
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
| def generate_text(model, tokenizer, prompt, max_length=1024): | |
| prompt = f'<|USER|> {prompt} <|ASSISTANT|> ' | |
| input_ids = tokenizer.encode(prompt, add_special_tokens=True, return_tensors="pt").to(device) | |
| attention_mask = torch.ones_like(input_ids).to(device) | |
| output = model.generate(input_ids, | |
| max_length=max_length, | |
| do_sample=True, | |
| top_k=0, | |
| top_p=0.1, | |
| temperature=0.75, | |
| repetition_penalty=1.176, | |
| pad_token_id=tokenizer.pad_token_id, | |
| eos_token_id=tokenizer.eos_token_id, | |
| attention_mask=attention_mask) | |
| output_ids = tokenizer.decode(output[0], skip_special_tokens=False) | |
| return output_ids | |
| # Loop to interact with the model | |
| while True: | |
| prompt = input("Enter a prompt (or 'q' to quit): ") | |
| if prompt == "q": | |
| break | |
| output_text = generate_text(model, tokenizer, prompt) | |
| text_between_tokens = output_text[output_text.find(start_token) + len(start_token):] | |
| out = text_between_tokens[:text_between_tokens.find(end_token)] | |
| print(out) | |