Spaces:
Running
Running
# Run app again with same input if model gives repeating output. | |
import transformers | |
from transformers import GPT2LMHeadModel, GPT2Tokenizer, GenerationConfig | |
import streamlit as st | |
st.markdown( | |
""" | |
<style> | |
.title-input textarea { | |
height: 100px; | |
padding-top: 20px; | |
} | |
</style> | |
""", | |
unsafe_allow_html=True | |
) | |
model_name = "gpt2-large" | |
model = GPT2LMHeadModel.from_pretrained(model_name) | |
tokenizer = GPT2Tokenizer.from_pretrained(model_name) | |
title = st.text_input("Enter a title to generate a blog post:") | |
if title: | |
input_prompt = f"Blog Title: {title}\n\nBlog Post:\n" | |
input_ids = tokenizer.encode(input_prompt, return_tensors='pt') | |
generation_config = GenerationConfig(max_new_tokens=100, do_sample=True, temperature=0.7) | |
output_ids = model.generate(input_ids, generation_config=generation_config)[0] | |
output_text = tokenizer.decode(output_ids, skip_special_tokens=True) | |
st.write(output_text) | |