|
"""app.py""" |
|
|
|
import streamlit as st |
|
from transformers import pipeline, GPT2LMHeadModel, GPT2Tokenizer |
|
|
|
|
|
model_name = "gpt2" |
|
model = GPT2LMHeadModel.from_pretrained(model_name) |
|
tokenizer = GPT2Tokenizer.from_pretrained(model_name) |
|
|
|
|
|
def generate_blogpost(topic): |
|
input_text = f"Blog post about {topic}:" |
|
input_ids = tokenizer.encode(input_text, return_tensors="pt") |
|
|
|
|
|
output = model.generate(input_ids, max_length=500, num_return_sequences=1, no_repeat_ngram_size=2) |
|
|
|
|
|
generated_text = tokenizer.decode(output[0], skip_special_tokens=True) |
|
return generated_text |
|
|
|
|
|
def main(): |
|
st.title("Blog Post Generator") |
|
|
|
|
|
topic = st.sidebar.text_input("Enter topic for the blog post", "a crazy person driving a car") |
|
|
|
|
|
if st.sidebar.button("Generate Blog Post"): |
|
blogpost = generate_blogpost(topic) |
|
st.subheader(f"Generated Blog Post on {topic}:") |
|
st.write(blogpost) |
|
|
|
if __name__ == "__main__": |
|
main() |
|
|