File size: 1,482 Bytes
d4e9fa6 |
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 |
# from transformers import DistilBertTokenizer, DistilBertForSequenceClassification
# import torch
# tokenizer = DistilBertTokenizer.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
# model = DistilBertForSequenceClassification.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
# inputs = tokenizer("Hello, my dog is sad", return_tensors="pt")
# inputs = tokenizer("Hello, my dog is sad", return_tensors="pt")
# with torch.no_grad():
# logits = model(**inputs).logits
# predicted_class_id = logits.argmax().item()
# model.config.id2label[predicted_class_id]
# outputs = model(**inputs)
# print(predicted_class_id)
from transformers import AutoTokenizer, AutoModelForCausalLM
# Load the tokenizer
tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2")
tokenizer.pad_token = tokenizer.eos_token
# Load the model
model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2")
# Example input prompt
input_text = "Ann wants to buy a new car"
# Tokenize input
inputs = tokenizer(input_text, return_tensors="pt",padding=True, truncation=True)
# Generate text
outputs = model.generate(inputs.input_ids, max_length=100, num_return_sequences=1, top_k=50, top_p=0.9, temperature=0.7,do_sample=True,eos_token_id=None, attention_mask=inputs.attention_mask)
print(model.config)
# Decode the generated text
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
print("Generated Text:\n", generated_text)
|