|
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}] |
|
|