import os os.system('pip install streamlit transformers torch') import streamlit as st from transformers import GPT2LMHeadModel, GPT2Tokenizer import torch # Load the GPT-2 model and tokenizer model_name = 'gpt2-large' tokenizer = GPT2Tokenizer.from_pretrained(model_name) model = GPT2LMHeadModel.from_pretrained(model_name) def generate_blog_post(topic): try: # Encode the input topic inputs = tokenizer.encode(topic, return_tensors='pt') # Generate the blog post outputs = model.generate(inputs, max_length=500, num_return_sequences=1, no_repeat_ngram_size=2, do_sample=True, top_k=50, top_p=0.95, temperature=0.9) # Decode the generated text blog_post = tokenizer.decode(outputs[0], skip_special_tokens=True) return blog_post except Exception as e: st.error(f"Error: {e}") return "" # Streamlit app st.title("Blog Post Generator") st.write("Enter a topic to generate a blog post.") topic = st.text_input("Topic:") if st.button("Generate"): with st.spinner('Generating...'): blog_post = generate_blog_post(topic) st.write(blog_post)