File size: 1,519 Bytes
715997e 5677d77 715997e 5677d77 bbec848 5677d77 bbec848 715997e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
Hugging Face's logo
Hugging Face
Search models, datasets, users...
Models
Datasets
Spaces
Docs
Solutions
Pricing
ammarnasr
/
CodeGen2_1B_merged
like
0
Text Generation
Transformers
PyTorch
codegen
custom_code
Inference Endpoints
Model card
Files and versions
Community
Settings
CodeGen2_1B_merged
/
handler.py
ammarnasr's picture
ammarnasr
Update handler.py
5f48ffc
7 minutes ago
raw
history
blame
edit
delete
1.06 kB
from typing import Any, Dict, List
import torch
import transformers
from transformers import AutoModelForCausalLM, AutoTokenizer
dtype = torch.bfloat16 if torch.cuda.get_device_capability()[0] ==8 else torch.float16
class EndpointHandler:
def __init__(self, path=""):
self.tokenizer = AutoTokenizer.from_pretrained(path)
self.model = AutoModelForCausalLM.from_pretrained(path, trust_remote_code=True, revision="main")
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.model = self.model.to(self.device)
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
prompt = data["inputs"]
if "config" in data:
config = data.pop("config", None)
else:
config = {'max_new_tokens':100}
input_ids = self.tokenizer(prompt, return_tensors="pt").input_ids.to(self.device)
generated_ids = self.model.generate(input_ids, **config)
generated_text = self.tokenizer.decode(generated_ids[0], skip_special_tokens=True)
return [{"generated_text": generated_text}]
|