ibrahimgiki commited on
Commit
9c71be1
·
verified ·
1 Parent(s): 7a2febc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -35
app.py CHANGED
@@ -1,42 +1,31 @@
1
  from transformers import AutoModelForCausalLM, AutoTokenizer
2
  import torch
3
- import streamlit as st
4
 
5
- from transformers import GPT2LMHeadModel, GPT2Tokenizer
6
-
7
- # Load pre-trained model and tokenizer
8
  model_name = "gpt2-large"
9
- model = GPT2LMHeadModel.from_pretrained(model_name)
10
- tokenizer = GPT2Tokenizer.from_pretrained(model_name)
11
-
12
- # Function to generate article
13
- def generate_article(title, max_length=300):
14
- # Encode the title and add special tokens
15
- input_ids = tokenizer.encode(title, return_tensors='pt')
 
 
 
 
 
16
 
17
- # Generate the article
18
- output = model.generate(
19
- input_ids,
20
- max_length=max_length,
21
- num_return_sequences=1,
22
- no_repeat_ngram_size=2,
23
- early_stopping=True
24
- )
25
 
26
- # Decode the generated text
27
- article = tokenizer.decode(output[0], skip_special_tokens=True)
28
- return article
29
-
30
- # Title input
31
- #title = "The Impact of Artificial Intelligence on Modern Education"
32
- title = st.text_area('Enter title')
33
-
34
- # Generate and print the article
35
- if title:
36
- article = generate_article(title)
37
- st.json(article)
38
-
39
-
40
-
41
- #print(article)
42
 
 
 
 
 
 
 
1
  from transformers import AutoModelForCausalLM, AutoTokenizer
2
  import torch
 
3
 
4
+ # Load the GPT-2 large model and tokenizer
 
 
5
  model_name = "gpt2-large"
6
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
7
+ # Add padding token to the tokenizer
8
+ tokenizer.pad_token = tokenizer.eos_token # Set padding token to EOS token
9
+
10
+ model = AutoModelForCausalLM.from_pretrained(model_name)
11
+
12
+ # Function to generate a blog post based on a topic title
13
+ def generate_blog(topic_title, max_length=200):
14
+ # Step 1: Encode the input
15
+ inputs = tokenizer.encode_plus(topic_title, return_tensors='pt', padding=True)
16
+ input_ids = inputs['input_ids']
17
+ attention_mask = inputs['attention_mask']
18
 
19
+ # Step 2: Generate model output
20
+ output_ids = model.generate(input_ids, attention_mask=attention_mask, max_length=max_length, num_return_sequences=1, pad_token_id=tokenizer.eos_token_id)
 
 
 
 
 
 
21
 
22
+ # Step 3: Decode the output
23
+ blog_post = tokenizer.decode(output_ids[0], skip_special_tokens=True)
24
+
25
+ return blog_post
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
+ # Example usage
28
+ topic_title = input("Enter a topic title for the blog post: ")
29
+ blog_post = generate_blog(topic_title)
30
+ print("\nGenerated Blog Post:\n")
31
+ print(blog_post)