Spaces:
Sleeping
Sleeping
File size: 2,248 Bytes
a8d8832 0b3905f a8d8832 0b3905f a8d8832 0b3905f |
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
import torch
import streamlit as st
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
model_name = "gpt2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
generator = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
device=0 if torch.cuda.is_available() else -1
)
# Function for text generation with filtering of repeated sequences
def generate_text(prompt, section, max_length=200, temperature=0.7, top_k=50, repetition_penalty=1.2):
return generator(
f"{section} - {prompt}",
max_length=max_length,
do_sample=True,
top_k=top_k,
temperature=temperature,
repetition_penalty=repetition_penalty,
num_return_sequences=1,
eos_token_id=tokenizer.eos_token_id,
)[0]["generated_text"]
# Streamlit app
st.title("AI-Generated Blog Post")
# Keyword selection input
keywords_input = st.text_input("Step 1: Keyword Selection (Separate keywords with commas)","Artificial Intelligence")
keywords = [word.strip() for word in keywords_input.split(',')]
# Display generated content on button click
if st.button('Generate Article'):
if keywords_input:
generated_text = " ".join(keywords)
intro_text = generate_text(generated_text, "Introduction", max_length=200, temperature=0.7, top_k=50)
body_text = generate_text(generated_text, "Body", max_length=500, temperature=0.7, top_k=50)
conclusion_text = generate_text(generated_text, "Conclusion", max_length=150, temperature=0.7, top_k=50)
# Displaying the sections with adjusted parameters
st.header("Introduction")
st.write(intro_text)
st.header("Body")
st.write(body_text)
st.header("Conclusion")
st.write(conclusion_text)
else:
st.warning("Please input keywords to generate content.")
# Sidebar with instructions
st.sidebar.title("Instructions")
st.sidebar.write(
"1. Enter keywords related to the topic you want to generate content about."
"\n2. Click 'Generate Article' to create the AI-generated blog post."
"\n3. Explore the Introduction, Body, and Conclusion sections of the generated content."
)
|