File size: 969 Bytes
aac08f8
 
eb25be6
a0841b5
eb25be6
 
2c632c2
 
 
 
 
aac08f8
 
2c632c2
 
 
 
 
 
 
eb25be6
 
 
 
f4c4c6b
eb25be6
a0841b5
 
 
eb25be6
a0841b5
eb25be6
a0841b5
eb25be6
a0841b5
eb25be6
a0841b5
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
# 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)