Spaces:
Sleeping
Sleeping
import streamlit as st | |
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig | |
import torch | |
# Load the model and tokenizer | |
model_name = "gpt2-large" | |
tokenizer = AutoTokenizer.from_pretrained(model_name) | |
model = AutoModelForCausalLM.from_pretrained(model_name) | |
# Streamlit app | |
st.title("Blog Post Generator (GPT-2 Large)") | |
# Input area for the topic | |
topic = st.text_area("Enter the topic for your blog post:") | |
# Generate button | |
if st.button("Generate Blog Post"): | |
if topic: | |
# Prepare the prompt | |
prompt = f"Write a blog post about {topic}:\n\n" | |
# Generate text | |
generation_config = GenerationConfig(max_new_tokens=50, do_sample=True, temperature=0.7) | |
# Tokenize the input | |
inputs_encoded = tokenizer.encode(prompt, return_tensors="pt") | |
# Model output | |
model_output = model.generate(inputs_encoded["input_ids"], generation_config=generation_config)[0] | |
# Decode the output | |
output = tokenizer.decode(model_output, skip_special_tokens=True) | |
# Display the generated blog post | |
st.subheader("Generated Blog Post:") | |
st.write(output) | |
else: | |
st.warning("Please enter a topic.") | |
# Add some information about the app | |
st.sidebar.header("About") | |
st.sidebar.info("This app uses the GPT-2 Large model to generate blog posts based on your input topic.") |