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