Text Generation
Transformers
Safetensors
English
t5
text2text-generation
seq2seq
flan-t5
lightweight
grounded-qa
text-generation-inference
Instructions to use teapotai/tinyteapot with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use teapotai/tinyteapot with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="teapotai/tinyteapot")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForSeq2SeqLM tokenizer = AutoTokenizer.from_pretrained("teapotai/tinyteapot") model = AutoModelForSeq2SeqLM.from_pretrained("teapotai/tinyteapot", device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use teapotai/tinyteapot with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "teapotai/tinyteapot" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "teapotai/tinyteapot", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/teapotai/tinyteapot
- SGLang
How to use teapotai/tinyteapot 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 "teapotai/tinyteapot" \ --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": "teapotai/tinyteapot", "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 "teapotai/tinyteapot" \ --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": "teapotai/tinyteapot", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use teapotai/tinyteapot with Docker Model Runner:
docker model run hf.co/teapotai/tinyteapot
Update handler.py
Browse files- handler.py +9 -10
handler.py
CHANGED
|
@@ -1,14 +1,15 @@
|
|
| 1 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 2 |
import torch
|
| 3 |
|
|
|
|
| 4 |
MAX_INPUT_TOKENS = 512
|
| 5 |
|
|
|
|
| 6 |
class EndpointHandler:
|
| 7 |
def __init__(self, path: str = ""):
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
self.
|
| 11 |
-
self.model = AutoModelForSeq2SeqLM.from_pretrained(path)
|
| 12 |
|
| 13 |
self.model.eval()
|
| 14 |
self.device = torch.device("cpu")
|
|
@@ -29,7 +30,7 @@ class EndpointHandler:
|
|
| 29 |
raise ValueError("Missing required field 'inputs'.")
|
| 30 |
|
| 31 |
# Support:
|
| 32 |
-
# 1) inputs is a full prompt string (closest to your
|
| 33 |
# 2) inputs is {"context": ..., "question": ...}
|
| 34 |
if isinstance(inputs, str):
|
| 35 |
prompt = inputs
|
|
@@ -40,12 +41,12 @@ class EndpointHandler:
|
|
| 40 |
else:
|
| 41 |
raise ValueError("inputs must be a string OR an object with 'context' and 'question'.")
|
| 42 |
|
| 43 |
-
# Match your code
|
| 44 |
enc = self.tokenizer(prompt, return_tensors="pt")
|
| 45 |
input_ids = enc["input_ids"]
|
| 46 |
attention_mask = enc["attention_mask"]
|
| 47 |
|
| 48 |
-
# Keep only most recent tokens
|
| 49 |
if input_ids.shape[1] > MAX_INPUT_TOKENS:
|
| 50 |
input_ids = input_ids[:, -MAX_INPUT_TOKENS:]
|
| 51 |
attention_mask = attention_mask[:, -MAX_INPUT_TOKENS:]
|
|
@@ -53,13 +54,11 @@ class EndpointHandler:
|
|
| 53 |
input_ids = input_ids.to(self.device)
|
| 54 |
attention_mask = attention_mask.to(self.device)
|
| 55 |
|
| 56 |
-
# Match your code: generate(..., do_sample=False)
|
| 57 |
outputs = self.model.generate(
|
| 58 |
input_ids=input_ids,
|
| 59 |
attention_mask=attention_mask,
|
| 60 |
-
do_sample=False
|
| 61 |
)
|
| 62 |
|
| 63 |
-
# Match your code: decode(..., skip_special_tokens=True)
|
| 64 |
answer = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 65 |
return {"generated_text": answer}
|
|
|
|
| 1 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 2 |
import torch
|
| 3 |
|
| 4 |
+
MODEL_NAME = "teapotai/tinyteapot"
|
| 5 |
MAX_INPUT_TOKENS = 512
|
| 6 |
|
| 7 |
+
|
| 8 |
class EndpointHandler:
|
| 9 |
def __init__(self, path: str = ""):
|
| 10 |
+
# EXACTLY like your snippet: load by repo id from Hugging Face Hub
|
| 11 |
+
self.tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 12 |
+
self.model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
|
|
|
|
| 13 |
|
| 14 |
self.model.eval()
|
| 15 |
self.device = torch.device("cpu")
|
|
|
|
| 30 |
raise ValueError("Missing required field 'inputs'.")
|
| 31 |
|
| 32 |
# Support:
|
| 33 |
+
# 1) inputs is a full prompt string (closest to your snippet)
|
| 34 |
# 2) inputs is {"context": ..., "question": ...}
|
| 35 |
if isinstance(inputs, str):
|
| 36 |
prompt = inputs
|
|
|
|
| 41 |
else:
|
| 42 |
raise ValueError("inputs must be a string OR an object with 'context' and 'question'.")
|
| 43 |
|
| 44 |
+
# Match your code path
|
| 45 |
enc = self.tokenizer(prompt, return_tensors="pt")
|
| 46 |
input_ids = enc["input_ids"]
|
| 47 |
attention_mask = enc["attention_mask"]
|
| 48 |
|
| 49 |
+
# Keep only the most recent tokens if too long
|
| 50 |
if input_ids.shape[1] > MAX_INPUT_TOKENS:
|
| 51 |
input_ids = input_ids[:, -MAX_INPUT_TOKENS:]
|
| 52 |
attention_mask = attention_mask[:, -MAX_INPUT_TOKENS:]
|
|
|
|
| 54 |
input_ids = input_ids.to(self.device)
|
| 55 |
attention_mask = attention_mask.to(self.device)
|
| 56 |
|
|
|
|
| 57 |
outputs = self.model.generate(
|
| 58 |
input_ids=input_ids,
|
| 59 |
attention_mask=attention_mask,
|
| 60 |
+
do_sample=False,
|
| 61 |
)
|
| 62 |
|
|
|
|
| 63 |
answer = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 64 |
return {"generated_text": answer}
|