Week3Day1 / app.py
ASaboor's picture
Update app.py
12c43aa verified
raw
history blame contribute delete
919 Bytes
import streamlit as st
from transformers import GPT2LMHeadModel, GPT2Tokenizer
# Load the GPT-2 model and tokenizer
model_name = "gpt2"
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
model = GPT2LMHeadModel.from_pretrained(model_name)
# Streamlit app layout
st.title("Blog Post Generator")
topic = st.text_input("Enter a topic for your blog post:")
if st.button("Generate Blog Post"):
if topic:
# Encode the input topic
input_ids = tokenizer.encode(topic, return_tensors='pt')
# Generate text
output = model.generate(input_ids, max_length=500, num_return_sequences=1)
# Decode the generated text
blog_post = tokenizer.decode(output[0], skip_special_tokens=True)
# Display the generated blog post
st.subheader("Generated Blog Post:")
st.write(blog_post)
else:
st.warning("Please enter a topic.")