File size: 1,703 Bytes
95fb164
a5fc4bb
6957aa9
 
c88c1f5
6957aa9
a5fc4bb
6957aa9
a5fc4bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95fb164
6957aa9
 
39b9d3e
a5fc4bb
 
 
 
 
 
 
 
 
 
39b9d3e
a5fc4bb
 
 
 
 
 
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
import streamlit as st
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

# Load model and tokenizer
model_name = "google/flan-t5-large"  # You can use "google/flan-t5-xl" for better results if you have more computational resources
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)

def generate_blog_post(topic, max_length=1000):
    prompt = f"Write a detailed blog post about {topic}. The blog post should be informative, engaging, and well-structured."
    inputs = tokenizer(prompt, return_tensors="pt", max_length=512, truncation=True)
    
    outputs = model.generate(
        inputs.input_ids,
        max_length=max_length,
        num_return_sequences=1,
        do_sample=True,
        top_k=50,
        top_p=0.95,
        temperature=0.7,
    )
    
    generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
    return generated_text

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

topic = st.text_input("Enter a topic for your blog post:")
max_length = st.slider("Maximum length of the blog post", min_value=100, max_value=1000, value=500, step=50)
generate_button = st.button("Generate Blog Post")

if generate_button and topic:
    with st.spinner("Generating blog post... This may take a moment."):
        blog_post = generate_blog_post(topic, max_length)
        
        # Display the generated blog post
        st.subheader("Generated Blog Post")
        st.write(blog_post)

st.sidebar.title("About")
st.sidebar.info(
    "This app generates a blog post on a given topic using a large language model. "
    "Enter a topic and click 'Generate Blog Post' to create your content."
)