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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -23
app.py CHANGED
@@ -3,28 +3,29 @@ 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)
12
-
13
 
14
  def generate_text(input_text, max_length=16, num_beams=5, do_sample=False, no_repeat_ngram_size=2):
15
  """
16
  Generate text based on the given input text.
 
17
  Parameters:
18
  - input_text (str): The input text to start generation from.
19
  - max_length (int): Maximum length of the generated text.
20
  - num_beams (int): Number of beams for beam search.
21
  - do_sample (bool): Whether to use sampling or not.
22
  - no_repeat_ngram_size (int): Size of the n-gram to avoid repetition.
 
23
  Returns:
24
  - generated_text (str): The generated text.
25
  """
26
  # Encode the input text and move it to the appropriate device
27
- input_ids = tokenizer(input_text, return_tensors='pt')['input_ids']
28
  # Generate text using the model
29
  output = model.generate(input_ids, max_length=max_length, num_beams=num_beams,
30
  do_sample=do_sample, no_repeat_ngram_size=no_repeat_ngram_size)
@@ -32,43 +33,38 @@ def generate_text(input_text, max_length=16, num_beams=5, do_sample=False, no_re
32
  generated_text = tokenizer.decode(output[0])
33
  return generated_text
34
 
35
-
36
- def generate_text_with_nucleus_search(input_text, max_length=16, do_sample=True, top_p=0.9):
37
  """
38
  Generate text with nucleus sampling based on the given input text.
 
39
  Parameters:
40
  - input_text (str): The input text to start generation from.
41
  - max_length (int): Maximum length of the generated text.
42
  - do_sample (bool): Whether to use sampling or not.
43
  - top_p (float): Nucleus sampling parameter.
 
44
  Returns:
45
  - generated_text (str): The generated text.
46
  """
47
  # Encode the input text and move it to the appropriate device
48
- input_ids = tokenizer(input_text, return_tensors='pt')['input_ids']
49
  # Generate text using nucleus sampling
50
  output = model.generate(input_ids, max_length=max_length, do_sample=do_sample, top_p=top_p)
51
  # Decode the generated output
52
  generated_text = tokenizer.decode(output[0])
53
  return generated_text
54
 
 
 
55
 
56
- # Create Gradio interface
57
- input_text = gr.Textbox(lines=10, label="Input Text", placeholder="Enter text for text generation...")
58
- output_text1 = gr.Textbox(label="Generated Text")
59
- output_text2 = gr.Textbox(label="Generated Text with Nucleus Search")
60
 
61
- # Set examples to None or empty list if not available
62
- examples = [
63
- ["I am happy."],
64
- ["This is a good day."],
65
- ["It is raining outside."],
66
- None # Example for output_text2
67
- ]
68
-
69
- gr.Interface(generate_text, input_text, output_text1, output_text2,
70
  title="Text Generation with GPT-2",
71
  description="Generate text using the GPT-2 model.",
72
- theme="default", # Change theme to default
73
- allow_flagging="never",
74
- examples=examples).launch(share=True)
 
 
 
 
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
  """
15
  Generate text based on the given input text.
16
+
17
  Parameters:
18
  - input_text (str): The input text to start generation from.
19
  - max_length (int): Maximum length of the generated text.
20
  - num_beams (int): Number of beams for beam search.
21
  - do_sample (bool): Whether to use sampling or not.
22
  - no_repeat_ngram_size (int): Size of the n-gram to avoid repetition.
23
+
24
  Returns:
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)
 
33
  generated_text = tokenizer.decode(output[0])
34
  return generated_text
35
 
36
+ def generate_text_with_nucleus_search(input_text, max_length=128, do_sample=True, top_p=0.9):
 
37
  """
38
  Generate text with nucleus sampling based on the given input text.
39
+
40
  Parameters:
41
  - input_text (str): The input text to start generation from.
42
  - max_length (int): Maximum length of the generated text.
43
  - do_sample (bool): Whether to use sampling or not.
44
  - top_p (float): Nucleus sampling parameter.
45
+
46
  Returns:
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
54
  generated_text = tokenizer.decode(output[0])
55
  return generated_text
56
 
57
+ # Create Gradio interfaces
58
+ input_text_interface = gr.Textbox(lines=5, label="Input Text", placeholder="Enter text for generation...")
59
 
60
+ output_text_interface = gr.Textbox(label="Generated Text", placeholder="Generated text will appear here...")
 
 
 
61
 
62
+ gr.Interface(generate_text, input_text_interface, output_text_interface,
 
 
 
 
 
 
 
 
63
  title="Text Generation with GPT-2",
64
  description="Generate text using the GPT-2 model.",
65
+ allow_flagging="never").launch(share=True)
66
+
67
+ gr.Interface(generate_text_with_nucleus_search, input_text_interface, output_text_interface,
68
+ title="Text Generation with Nucleus Sampling",
69
+ description="Generate text using nucleus sampling with the GPT-2 model.",
70
+ allow_flagging="never").launch(share=True)