title-to-blog / app.py
ibrahimgiki's picture
Create app.py
6df2d37 verified
raw
history blame
860 Bytes
# Load the GPT-2 large model and tokenizer
model_name = "gpt2-large"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
# Function to generate a blog post based on a topic title
def generate_blog(topic_title, max_length=300):
# Step 1: Encode the input
input_ids = tokenizer.encode(topic_title, return_tensors='pt')
# Step 2: Generate model output
output_ids = model.generate(input_ids, max_length=max_length, num_return_sequences=1, pad_token_id=tokenizer.eos_token_id)
# Step 3: Decode the output
blog_post = tokenizer.decode(output_ids[0], skip_special_tokens=True)
return blog_post
# Example usage
topic_title = input("Enter a topic title for the blog post: ")
blog_post = generate_blog(topic_title)
print("\nGenerated Blog Post:\n")
print(blog_post)