Shuja007 commited on
Commit
8f4ee84
1 Parent(s): 19d2f32

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -5
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import streamlit as st
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
 
3
 
4
  # Load the GPT-2 large model and tokenizer
5
  model_name = "gpt2-large"
@@ -7,19 +8,35 @@ model = AutoModelForCausalLM.from_pretrained(model_name)
7
  tokenizer = AutoTokenizer.from_pretrained(model_name)
8
 
9
  def generate_blogpost(topic):
10
- input_text = f"Write a blog post about {topic}:"
11
- inputs = tokenizer.encode(input_text, return_tensors="pt")
12
- outputs = model.generate(inputs, max_length=500, num_return_sequences=1)
13
- generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
14
- return generated_text
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  # Streamlit UI
17
  st.title("Blog Post Generator")
18
  st.write("Generate a blog post for a given topic using GPT-2 large.")
19
 
 
20
  topic = st.text_input("Enter the topic:")
 
 
21
  if st.button("Generate"):
22
  if topic:
 
23
  blog_post = generate_blogpost(topic)
24
  st.write(blog_post)
25
  else:
 
1
  import streamlit as st
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
 
5
  # Load the GPT-2 large model and tokenizer
6
  model_name = "gpt2-large"
 
8
  tokenizer = AutoTokenizer.from_pretrained(model_name)
9
 
10
  def generate_blogpost(topic):
11
+ try:
12
+ # Prepare input text
13
+ input_text = f"Write a blog post about {topic}:"
14
+ inputs = tokenizer.encode(input_text, return_tensors="pt")
15
+ st.write(f"Input IDs: {inputs}")
16
+
17
+ # Generate output
18
+ with torch.no_grad():
19
+ outputs = model.generate(inputs, max_length=500, num_return_sequences=1)
20
+ st.write(f"Output IDs: {outputs}")
21
+
22
+ # Decode the generated text
23
+ generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
24
+ return generated_text
25
+
26
+ except Exception as e:
27
+ return f"An error occurred: {str(e)}"
28
 
29
  # Streamlit UI
30
  st.title("Blog Post Generator")
31
  st.write("Generate a blog post for a given topic using GPT-2 large.")
32
 
33
+ # Input for the topic
34
  topic = st.text_input("Enter the topic:")
35
+
36
+ # Generate button
37
  if st.button("Generate"):
38
  if topic:
39
+ # Generate and display the blog post
40
  blog_post = generate_blogpost(topic)
41
  st.write(blog_post)
42
  else: