brew install git-lfs
.git clone https://huggingface.co/gpt2
.cd gpt2
.python -m venv .env
then source .env/bin/activate
.python -m venv .env
then .env\Scripts\activate
.pip install transformers
.Create a file named main.py
inside your project folder. Copy and paste the following script into this file:
import torch
import logging
from transformers import pipeline, set_seed
# Set logging level to ERROR to suppress informational messages
logging.getLogger('transformers').setLevel(logging.ERROR)
# Function to generate text using the GPT-2 model
def generate_text(seed_text, max_length=200, num_return_sequences=1):
generated = generator(seed_text, max_length=max_length, num_return_sequences=num_return_sequences)[0]["generated_text"]
# Remove the seed text from the beginning of the generated text
if generated.startswith(seed_text):
return generated[len(seed_text):].strip()
return generated
# Set the device to MPS if available, else use CPU
device = "mps" if torch.backends.mps.is_available() else "cpu"
# Initialize the GPT-2 text-generation pipeline
generator = pipeline('text-generation', model='gpt2', device=device)
# Set a seed for reproducibility
set_seed(420)
# Get initial input from the user
seed_text = input("What should the first sentence be?\n")
max_length = int(input("Enter the maximum length for text generation (e.g., 200): "))
num_sequences = int(input("Enter the number of sequences to generate (usually 1): "))
# Generate initial text
txt = generate_text(seed_text, max_length, num_sequences)
print("Keep clicking enter to generate more...")
print(txt)
# Continue generating text on pressing Enter
while input("") == "":
# Dynamically determine the number of words for the new seed
# based on a fraction of max_length, e.g., half of max_length
num_words = max_length // 2
seed_words = txt.split()
# Ensure we don't exceed the actual number of words in txt
num_words = min(num_words, len(seed_words))
new_seed = " ".join(txt.split()[:num_words])
# Generate more text
txt = generate_text(new_seed, max_length, num_sequences)
print(txt)
Run the script using python main.py
in your terminal. If that does not work, try python3 main.py