# 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) | |