ASaboor commited on
Commit
12c43aa
·
verified ·
1 Parent(s): 61b97da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -29
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 GPT2Tokenizer, GPT2Model
5
 
6
- # Load pre-trained GPT-2 model and tokenizer
7
- tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
8
- model = GPT2Model.from_pretrained('gpt2')
 
9
 
10
- # Create a text area for the user to input the topic
11
- topic = st.text_area("Enter a topic to generate a blog post", height=275)
 
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
- generated_text = generate_blogpost(topic)
34
- st.write(generated_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.")