Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Load the model and tokenizer
|
6 |
+
model_name = "gpt2-large"
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
9 |
+
|
10 |
+
# Streamlit app
|
11 |
+
st.title("Blog Post Generator (GPT-2 Large)")
|
12 |
+
|
13 |
+
# Input area for the topic
|
14 |
+
topic = st.text_area("Enter the topic for your blog post:")
|
15 |
+
|
16 |
+
# Generate button
|
17 |
+
if st.button("Generate Blog Post"):
|
18 |
+
if topic:
|
19 |
+
# Prepare the prompt
|
20 |
+
prompt = f"Write a blog post about {topic}:\n\n"
|
21 |
+
|
22 |
+
# Tokenize the input
|
23 |
+
input_ids = tokenizer.encode(prompt, return_tensors="pt")
|
24 |
+
|
25 |
+
# Generate text
|
26 |
+
with torch.no_grad():
|
27 |
+
output = model.generate(
|
28 |
+
input_ids,
|
29 |
+
max_length=500,
|
30 |
+
num_return_sequences=1,
|
31 |
+
no_repeat_ngram_size=2,
|
32 |
+
top_k=50,
|
33 |
+
top_p=0.95,
|
34 |
+
temperature=0.7
|
35 |
+
)
|
36 |
+
|
37 |
+
# Decode the generated text
|
38 |
+
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
39 |
+
|
40 |
+
# Display the generated blog post
|
41 |
+
st.subheader("Generated Blog Post:")
|
42 |
+
st.write(generated_text)
|
43 |
+
else:
|
44 |
+
st.warning("Please enter a topic.")
|
45 |
+
|
46 |
+
# Add some information about the app
|
47 |
+
st.sidebar.header("About")
|
48 |
+
st.sidebar.info("This app uses the GPT-2 Large model to generate blog posts based on your input topic.")
|