Spaces:
Sleeping
Sleeping
File size: 1,741 Bytes
27f27e1 80b6635 58c6588 27f27e1 80b6635 58c6588 80b6635 58c6588 80b6635 58c6588 80b6635 27f27e1 58c6588 |
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 |
import streamlit as st
from transformers import GPT2LMHeadModel, GPT2Tokenizer
import torch
# Load the GPT-2 model and tokenizer
model_name = 'gpt2-large'
model = GPT2LMHeadModel.from_pretrained(model_name)
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
st.title("AI Article Generator")
# Input for the article title
title = st.text_input("Enter the title of the article")
# Parameters for text generation
max_length = st.slider("Maximum length of the article", min_value=50, max_value=1000, value=500, step=50)
temperature = st.slider("Temperature (creativity level)", min_value=0.7, max_value=1.5, value=1.0, step=0.1)
# Generate the article
if st.button("Generate Article"):
if title:
with st.spinner("Generating article..."):
try:
input_ids = tokenizer.encode(title, return_tensors='pt')
output = model.generate(
input_ids,
max_length=max_length,
temperature=temperature,
num_return_sequences=1,
no_repeat_ngram_size=2,
early_stopping=True
)
article = tokenizer.decode(output[0], skip_special_tokens=True)
st.success("Article generated successfully!")
st.write(article)
except Exception as e:
st.error(f"An error occurred: {e}")
else:
st.warning("Please enter a title to generate an article")
st.markdown("""
<style>
.reportview-container {
flex-direction: column;
align-items: center;
}
.css-1kyxreq {
width: 100%;
max-width: 700px;
margin: auto;
}
</style>
""", unsafe_allow_html=True)
|