File size: 1,667 Bytes
b4f4a2d
3e9d734
 
 
 
 
 
 
 
 
 
 
c5fed21
4ea832f
 
 
3e9d734
 
4ea832f
3e9d734
4ea832f
 
3e9d734
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ea832f
3e9d734
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
import streamlit as st
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM

# Load GPT-2 large model and tokenizer using autoTokenizers and auto model
@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:")

# Blog post button
if st.button("Generate Blog Post"):
    if topic:
        # Refine the input prompt to guide the model toward 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.")