Spaces:
Sleeping
Sleeping
File size: 670 Bytes
2cfa511 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
def load_model():
model_name = "Salesforce/codet5-base"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
model.eval()
device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)
return tokenizer, model, device
def generate_explanation(prompt, tokenizer, model, device):
inputs = tokenizer(prompt, return_tensors="pt", truncation=True).to(device)
output = model.generate(**inputs, max_new_tokens=256, temperature=0.7)
return tokenizer.decode(output[0], skip_special_tokens=True)
|