Spaces:
Sleeping
Sleeping
File size: 1,643 Bytes
f2c8d49 c8654e4 f2c8d49 386e9b4 338b4ff 155abd5 c8654e4 338b4ff c8654e4 338b4ff 386e9b4 338b4ff 614359b c8654e4 386e9b4 614359b c8654e4 338b4ff f2c8d49 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
import streamlit as st
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
# Load GPT-2 large model and tokenizer
@st.cache(allow_output_mutation=True)
def load_model():
tokenizer = AutoTokenizer.from_pretrained("gpt2-large")
model = AutoModelForCausalLM.from_pretrained("gpt2-large")
return tokenizer, model
tokenizer, model = load_model()
st.title("Blog Post Generator")
st.write("Generate a blog post for a given topic using GPT-2 Large.")
# User input for the blog post topic
topic = st.text_input("Enter the topic for your blog post:")
# Generate blog post button
if st.button("Generate Blog Post"):
if topic:
# Refine the input prompt to guide the model towards generating a blog post
input_text = f"Write a detailed blog post about {topic}. The post should cover various aspects of the topic and provide valuable information to the readers. Start with an introduction and follow with detailed paragraphs."
# Encode the input text
inputs = tokenizer.encode(input_text, return_tensors="pt")
# Generate the blog post using GPT-2 large
outputs = model.generate(
inputs,
max_length=500,
num_return_sequences=1,
no_repeat_ngram_size=2,
early_stopping=True,
temperature=0.7,
top_p=0.9
)
# Decode the generated text
blog_post = tokenizer.decode(outputs[0], skip_special_tokens=True)
st.write("### Generated Blog Post:")
st.write(blog_post)
else:
st.write("Please enter a topic to generate a blog post.")
|