Spaces:
Sleeping
Sleeping
| 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) | |