Update app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,28 @@
|
|
1 |
-
import os
|
2 |
-
os.system("pip install transformers~=4.12.3")
|
3 |
import streamlit as st
|
4 |
-
from transformers import
|
5 |
|
6 |
-
# Load
|
7 |
-
|
8 |
-
|
|
|
9 |
|
10 |
-
#
|
11 |
-
|
|
|
12 |
|
13 |
-
# Create sliders for the user to specify the max length, temperature, top-k, and top-p
|
14 |
-
max_length = st.sidebar.slider("Max Length", min_value=10, max_value=30)
|
15 |
-
temperature = st.sidebar.slider("Temperature", value=1.0, min_value=0.0, max_value=1.0, step=0.05)
|
16 |
-
top_k = st.sidebar.slider("Top-k", min_value=0, max_value=5, value=0)
|
17 |
-
top_p = st.sidebar.slider("Top-p", min_value=0.0, max_value=1.0, step=0.05, value=0.9)
|
18 |
-
num_return_sequences = st.sidebar.number_input('Number of Return Sequences', min_value=1, max_value=5, value=1, step=1)
|
19 |
-
|
20 |
-
# Define a function to generate the blog post
|
21 |
-
def generate_blogpost(topic):
|
22 |
-
# Encode the topic using the tokenizer
|
23 |
-
encoded_input = tokenizer(topic, return_tensors='pt')
|
24 |
-
|
25 |
-
# Generate text using the model
|
26 |
-
output = model(**encoded_input)
|
27 |
-
generated_text = tokenizer.decode(output.last_hidden_state[:, 0, :], skip_special_tokens=True)
|
28 |
-
|
29 |
-
return generated_text
|
30 |
-
|
31 |
-
# Create a button to generate the blog post
|
32 |
if st.button("Generate Blog Post"):
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
3 |
|
4 |
+
# Load the GPT-2 model and tokenizer
|
5 |
+
model_name = "gpt2"
|
6 |
+
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
|
7 |
+
model = GPT2LMHeadModel.from_pretrained(model_name)
|
8 |
|
9 |
+
# Streamlit app layout
|
10 |
+
st.title("Blog Post Generator")
|
11 |
+
topic = st.text_input("Enter a topic for your blog post:")
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
if st.button("Generate Blog Post"):
|
14 |
+
if topic:
|
15 |
+
# Encode the input topic
|
16 |
+
input_ids = tokenizer.encode(topic, return_tensors='pt')
|
17 |
+
|
18 |
+
# Generate text
|
19 |
+
output = model.generate(input_ids, max_length=500, num_return_sequences=1)
|
20 |
+
|
21 |
+
# Decode the generated text
|
22 |
+
blog_post = tokenizer.decode(output[0], skip_special_tokens=True)
|
23 |
+
|
24 |
+
# Display the generated blog post
|
25 |
+
st.subheader("Generated Blog Post:")
|
26 |
+
st.write(blog_post)
|
27 |
+
else:
|
28 |
+
st.warning("Please enter a topic.")
|