Spaces:
Sleeping
Sleeping
File size: 1,531 Bytes
50160f2 4e630e1 50160f2 19d6e2b 4e630e1 8a7e2df 4e630e1 19d6e2b c380748 8a7e2df c380748 50160f2 c380748 19d6e2b 52101ba 19d6e2b 4e630e1 c380748 19d6e2b c380748 |
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
from transformers import GPT2LMHeadModel, GPT2Tokenizer
# Load the model and tokenizer
model_name = "gpt2"
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
model = GPT2LMHeadModel.from_pretrained(model_name)
# Function to create the prompt
def make_prompt(new_title):
prompt = f"""
Write a Blog Post On the given Title like the example:
Title: The Benefits of Daily Meditation
Blog Content:
Meditation is a practice where an individual uses a technique – such as mindfulness,
or focusing the mind on a particular object, thought, or activity – to train attention and awareness,
and achieve a mentally clear and emotionally calm and stable state. Daily meditation can bring numerous
benefits such as reducing stress, improving concentration, and promoting a healthy lifestyle.
Title: {new_title}
Blog Content:
"""
return prompt
# Function to generate the blog content
def generate_blog(prompt):
input_ids = tokenizer.encode(prompt, return_tensors='pt')
output = model.generate(input_ids, max_length=800, num_return_sequences=1, no_repeat_ngram_size=2)
return tokenizer.decode(output[0], skip_special_tokens=True)
# Streamlit interface
st.title("AI Blog Generator")
# Input box for the blog title
title = st.text_input("Enter the Blog Title:")
if st.button("Generate Blog"):
if title:
prompt = make_prompt(title)
blog_content = generate_blog(prompt)
st.subheader("Generated Blog")
st.write(blog_content)
|