File size: 1,042 Bytes
8ae27d9
1acc131
8ae27d9
1acc131
 
 
 
8ae27d9
1acc131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from transformers import GPT2LMHeadModel, GPT2Tokenizer

# Load pre-trained model and tokenizer
model_name = "gpt2"
model = GPT2LMHeadModel.from_pretrained(model_name)
tokenizer = GPT2Tokenizer.from_pretrained(model_name)

def generate_blog_post(topic, max_length=500):
    # Encode the input text
    input_ids = tokenizer.encode(topic, return_tensors='pt')
    
    # Generate text
    outputs = model.generate(input_ids, max_length=max_length, num_return_sequences=1)
    
    # Decode the generated text
    text = tokenizer.decode(outputs[0], skip_special_tokens=True)
    return text

# Streamlit app
st.title("Blog Post Generator")
st.write("Enter a topic, and the model will generate a blog post for you.")

topic = st.text_input("Topic", value="Artificial Intelligence")
max_length = st.slider("Max Length", min_value=50, max_value=1000, value=500)

if st.button("Generate Blog Post"):
    with st.spinner("Generating..."):
        blog_post = generate_blog_post(topic, max_length)
        st.write(blog_post)