Downloads · 30 days
0
Anveenkumar/Q_and_A_gpt
Q_and_A_gpt is a machine learning model from Anveenkumar. Use it for the machine learning task on the model card, and read the license before you ship it in a product.
Downloads · 30 days
0
Access
Public
Updated Aug 13, 2024
Repo size
—
Likes
0
Public
Click a slice to open those files.
Other1.5 KB · 63%
From the Hugging Face model README
"""Untitled4.ipynb
Automatically generated by Colab.
Original file is located at https://colab.research.google.com/drive/1pyZu93Mw54OZFZeWXadoTjeZaGki22rt """
!pip install transformers !pip install torch
from transformers import GPT2LMHeadModel, GPT2Tokenizer import torch
model_name = "gpt2" tokenizer = GPT2Tokenizer.from_pretrained(model_name) model = GPT2LMHeadModel.from_pretrained(model_name)
def generate_response(question): inputs = tokenizer.encode(question + tokenizer.eos_token, return_tensors="pt") outputs = model.generate(inputs, max_length=50, pad_token_id=tokenizer.eos_token_id) response = tokenizer.decode(outputs[0], skip_special_tokens=True) return response
question = "How are you?" response = generate_response(question) print(response)
question = "What's your name?" response = generate_response(question) print(response)