Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,13 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
| 3 |
|
| 4 |
-
|
| 5 |
model_name = "gpt2"
|
| 6 |
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
|
| 7 |
model = GPT2LMHeadModel.from_pretrained(model_name)
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
prompt = f"""
|
| 10 |
Title: The Benefits of Daily Meditation
|
| 11 |
|
|
@@ -20,14 +22,22 @@ benefits such as reducing stress, improving concentration, and promoting a healt
|
|
| 20 |
Blog Content:
|
| 21 |
"""
|
| 22 |
return prompt
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
output = model.generate(input_ids, max_length=800, num_return_sequences=1, no_repeat_ngram_size=2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
if st.button("Generate Blog"):
|
| 28 |
if title:
|
| 29 |
-
|
| 30 |
-
blog_content = generate_blog(
|
| 31 |
st.subheader("Generated Blog")
|
| 32 |
st.write(blog_content)
|
| 33 |
-
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
| 3 |
|
| 4 |
+
# Load the model and tokenizer
|
| 5 |
model_name = "gpt2"
|
| 6 |
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
|
| 7 |
model = GPT2LMHeadModel.from_pretrained(model_name)
|
| 8 |
+
|
| 9 |
+
# Function to create the prompt
|
| 10 |
+
def make_prompt(new_title):
|
| 11 |
prompt = f"""
|
| 12 |
Title: The Benefits of Daily Meditation
|
| 13 |
|
|
|
|
| 22 |
Blog Content:
|
| 23 |
"""
|
| 24 |
return prompt
|
| 25 |
+
|
| 26 |
+
# Function to generate the blog content
|
| 27 |
+
def generate_blog(prompt):
|
| 28 |
+
input_ids = tokenizer.encode(prompt, return_tensors='pt')
|
| 29 |
output = model.generate(input_ids, max_length=800, num_return_sequences=1, no_repeat_ngram_size=2)
|
| 30 |
+
return tokenizer.decode(output[0], skip_special_tokens=True)
|
| 31 |
+
|
| 32 |
+
# Streamlit interface
|
| 33 |
+
st.title("AI Blog Generator")
|
| 34 |
+
|
| 35 |
+
# Input box for the blog title
|
| 36 |
+
title = st.text_input("Enter the Blog Title:")
|
| 37 |
|
| 38 |
if st.button("Generate Blog"):
|
| 39 |
if title:
|
| 40 |
+
prompt = make_prompt(title)
|
| 41 |
+
blog_content = generate_blog(prompt)
|
| 42 |
st.subheader("Generated Blog")
|
| 43 |
st.write(blog_content)
|
|
|