Text Generation
Transformers
PyTorch
TensorFlow
JAX
LiteRT
Rust
ONNX
Safetensors
English
gpt2
exbert
text-generation-inference
Instructions to use openai-community/gpt2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use openai-community/gpt2 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="openai-community/gpt2")# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2") model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2") - Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use openai-community/gpt2 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "openai-community/gpt2" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "openai-community/gpt2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/openai-community/gpt2
- SGLang
How to use openai-community/gpt2 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 "openai-community/gpt2" \ --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": "openai-community/gpt2", "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 "openai-community/gpt2" \ --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": "openai-community/gpt2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use openai-community/gpt2 with Docker Model Runner:
docker model run hf.co/openai-community/gpt2
Code generator
#19
by Samuelblue - opened
- codegenerator.py +57 -0
codegenerator.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed, pipeline
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# change model to the finetuned one
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained("codeparrot/codeparrot-small-text-to-code")
|
| 11 |
+
model = AutoModelForCausalLM.from_pretrained("codeparrot/codeparrot-small-text-to-code")
|
| 12 |
+
|
| 13 |
+
def make_doctring(gen_prompt):
|
| 14 |
+
return "\"\"\"\n" + gen_prompt + "\n\"\"\"\n\n"
|
| 15 |
+
|
| 16 |
+
def code_generation(gen_prompt, max_tokens, temperature=0.6, seed=42):
|
| 17 |
+
set_seed(seed)
|
| 18 |
+
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 19 |
+
prompt = make_doctring(gen_prompt)
|
| 20 |
+
generated_text = pipe(prompt, do_sample=True, top_p=0.95, temperature=temperature, max_new_tokens=max_tokens)[0]['generated_text']
|
| 21 |
+
return generated_text
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
iface = gr.Interface(
|
| 25 |
+
fn=code_generation,
|
| 26 |
+
inputs=[
|
| 27 |
+
gr.Textbox(lines=10, label="Text"),
|
| 28 |
+
gr.inputs.Slider(
|
| 29 |
+
minimum=8,
|
| 30 |
+
maximum=1000,
|
| 31 |
+
step=1,
|
| 32 |
+
default=8,
|
| 33 |
+
label="Number of tokens to generate",
|
| 34 |
+
),
|
| 35 |
+
gr.inputs.Slider(
|
| 36 |
+
minimum=0,
|
| 37 |
+
maximum=2.5,
|
| 38 |
+
step=0.1,
|
| 39 |
+
default=0.6,
|
| 40 |
+
label="Temperature",
|
| 41 |
+
),
|
| 42 |
+
gr.inputs.Slider(
|
| 43 |
+
minimum=0,
|
| 44 |
+
maximum=1000,
|
| 45 |
+
step=1,
|
| 46 |
+
default=42,
|
| 47 |
+
label="Random seed to use for the generation"
|
| 48 |
+
)
|
| 49 |
+
],
|
| 50 |
+
outputs=gr.Textbox(label="Python code", lines=10),
|
| 51 |
+
examples=example,
|
| 52 |
+
layout="horizontal",
|
| 53 |
+
theme="peach",
|
| 54 |
+
description=description,
|
| 55 |
+
title=title
|
| 56 |
+
)
|
| 57 |
+
iface.launch()
|