ajeetkumar01 commited on
Commit
239b2f3
·
verified ·
1 Parent(s): d077743

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -4
app.py CHANGED
@@ -3,12 +3,12 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import gradio as gr
4
 
5
  # Check if GPU is available, otherwise use CPU
6
- device = "cuda" if torch.cuda.is_available() else "cpu"
7
 
8
  # Load pre-trained GPT-2 model and tokenizer
9
  model_name = "gpt2-large"
10
  tokenizer = AutoTokenizer.from_pretrained(model_name)
11
- model = AutoModelForCausalLM.from_pretrained(model_name).to(device)
12
 
13
  def generate_text(input_text, max_length=16, num_beams=5, do_sample=False, no_repeat_ngram_size=2):
14
  """
@@ -25,7 +25,7 @@ def generate_text(input_text, max_length=16, num_beams=5, do_sample=False, no_re
25
  - generated_text (str): The generated text.
26
  """
27
  # Encode the input text and move it to the appropriate device
28
- input_ids = tokenizer(input_text, return_tensors='pt')['input_ids'].to(device)
29
  # Generate text using the model
30
  output = model.generate(input_ids, max_length=max_length, num_beams=num_beams,
31
  do_sample=do_sample, no_repeat_ngram_size=no_repeat_ngram_size)
@@ -47,7 +47,7 @@ def generate_text_with_nucleus_search(input_text, max_length=128, do_sample=True
47
  - generated_text (str): The generated text.
48
  """
49
  # Encode the input text and move it to the appropriate device
50
- input_ids = tokenizer(input_text, return_tensors='pt')['input_ids'].to(device)
51
  # Generate text using nucleus sampling
52
  output = model.generate(input_ids, max_length=max_length, do_sample=do_sample, top_p=top_p)
53
  # Decode the generated output
 
3
  import gradio as gr
4
 
5
  # Check if GPU is available, otherwise use CPU
6
+ # device = "cuda" if torch.cuda.is_available() else "cpu"
7
 
8
  # Load pre-trained GPT-2 model and tokenizer
9
  model_name = "gpt2-large"
10
  tokenizer = AutoTokenizer.from_pretrained(model_name)
11
+ model = AutoModelForCausalLM.from_pretrained(model_name)
12
 
13
  def generate_text(input_text, max_length=16, num_beams=5, do_sample=False, no_repeat_ngram_size=2):
14
  """
 
25
  - generated_text (str): The generated text.
26
  """
27
  # Encode the input text and move it to the appropriate device
28
+ input_ids = tokenizer(input_text, return_tensors='pt')['input_ids']
29
  # Generate text using the model
30
  output = model.generate(input_ids, max_length=max_length, num_beams=num_beams,
31
  do_sample=do_sample, no_repeat_ngram_size=no_repeat_ngram_size)
 
47
  - generated_text (str): The generated text.
48
  """
49
  # Encode the input text and move it to the appropriate device
50
+ input_ids = tokenizer(input_text, return_tensors='pt')['input_ids']
51
  # Generate text using nucleus sampling
52
  output = model.generate(input_ids, max_length=max_length, do_sample=do_sample, top_p=top_p)
53
  # Decode the generated output