File size: 1,375 Bytes
fdbdc93
 
3c97d9a
4ab991c
fdbdc93
 
 
 
4ab991c
fdbdc93
4d3bd41
4ab991c
fdbdc93
888b62c
 
 
 
 
4d3bd41
9ddd8ad
fdbdc93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
888b62c
be8e82b
888b62c
 
 
 
be8e82b
888b62c
be8e82b
888b62c
 
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
49
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
import streamlit as st

# Load GPT-2 model and tokenizer
model_name = "gpt2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

# Streamlit app
st.title("Blog Post Generator")

# User input
topic = st.text_input("Enter a blog post topic:")
max_length = st.slider("Maximum length of generated text:", min_value=100, max_value=2000, value=500, step=50)

if topic:
    # Construct a detailed prompt
    prompt = f"""Write a well-formatted blog post about {topic}. 
"""

    # Tokenize input
    input_ids = tokenizer.encode(prompt, return_tensors="pt")

    # Generate text
    with torch.no_grad():
        output = model.generate(
            input_ids,
            max_length=max_length,
            num_return_sequences=1,
            no_repeat_ngram_size=2,
            top_k=50,
            top_p=0.95,
            temperature=0.7
        )

    # Decode and display generated text
    generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
    
    st.subheader("Generated Blog Post:")
    st.markdown(generated_text)

    # Option to download the blog post
    st.download_button(
        label="Download Blog Post",
        data=generated_text,
        file_name="generated_blog.md",
        mime="text/markdown"
    )