ASaboor commited on
Commit
575198c
·
verified ·
1 Parent(s): 8030654

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -0
app.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import GPT2Tokenizer, GPT2Model
3
+
4
+ # Load pre-trained GPT-2 model and tokenizer
5
+ tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
6
+ model = GPT2Model.from_pretrained('gpt2')
7
+
8
+ # Create a text area for the user to input the topic
9
+ topic = st.text_area("Enter a topic to generate a blog post", height=275)
10
+
11
+ # Create sliders for the user to specify the max length, temperature, top-k, and top-p
12
+ max_length = st.sidebar.slider("Max Length", min_value=10, max_value=30)
13
+ temperature = st.sidebar.slider("Temperature", value=1.0, min_value=0.0, max_value=1.0, step=0.05)
14
+ top_k = st.sidebar.slider("Top-k", min_value=0, max_value=5, value=0)
15
+ top_p = st.sidebar.slider("Top-p", min_value=0.0, max_value=1.0, step=0.05, value=0.9)
16
+ num_return_sequences = st.sidebar.number_input('Number of Return Sequences', min_value=1, max_value=5, value=1, step=1)
17
+
18
+ # Define a function to generate the blog post
19
+ def generate_blogpost(topic):
20
+ # Encode the topic using the tokenizer
21
+ encoded_input = tokenizer(topic, return_tensors='pt')
22
+
23
+ # Generate text using the model
24
+ output = model(**encoded_input)
25
+ generated_text = tokenizer.decode(output.last_hidden_state[:, 0, :], skip_special_tokens=True)
26
+
27
+ return generated_text
28
+
29
+ # Create a button to generate the blog post
30
+ if st.button("Generate Blog Post"):
31
+ generated_text = generate_blogpost(topic)
32
+ st.write(generated_text)