ajeetkumar01 commited on
Commit
7de082d
·
verified ·
1 Parent(s): 2ffa1bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -17
app.py CHANGED
@@ -2,9 +2,6 @@ import torch
2
  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)
@@ -13,14 +10,12 @@ model = AutoModelForCausalLM.from_pretrained(model_name)
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
  """
@@ -36,13 +31,11 @@ def generate_text(input_text, max_length=16, num_beams=5, do_sample=False, no_re
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
-
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
  """
@@ -54,17 +47,25 @@ def generate_text_with_nucleus_search(input_text, max_length=16, do_sample=True,
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)
 
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import gradio as gr
4
 
 
 
 
5
  # Load pre-trained GPT-2 model and tokenizer
6
  model_name = "gpt2-large"
7
  tokenizer = AutoTokenizer.from_pretrained(model_name)
 
10
  def generate_text(input_text, max_length=16, num_beams=5, do_sample=False, no_repeat_ngram_size=2):
11
  """
12
  Generate text based on the given input text.
 
13
  Parameters:
14
  - input_text (str): The input text to start generation from.
15
  - max_length (int): Maximum length of the generated text.
16
  - num_beams (int): Number of beams for beam search.
17
  - do_sample (bool): Whether to use sampling or not.
18
  - no_repeat_ngram_size (int): Size of the n-gram to avoid repetition.
 
19
  Returns:
20
  - generated_text (str): The generated text.
21
  """
 
31
  def generate_text_with_nucleus_search(input_text, max_length=16, do_sample=True, top_p=0.9):
32
  """
33
  Generate text with nucleus sampling based on the given input text.
 
34
  Parameters:
35
  - input_text (str): The input text to start generation from.
36
  - max_length (int): Maximum length of the generated text.
37
  - do_sample (bool): Whether to use sampling or not.
38
  - top_p (float): Nucleus sampling parameter.
 
39
  Returns:
40
  - generated_text (str): The generated text.
41
  """
 
47
  generated_text = tokenizer.decode(output[0])
48
  return generated_text
49
 
50
+ # Create Gradio input interface
51
  input_text_interface = gr.Textbox(lines=5, label="Input Text", placeholder="Enter text for generation...")
52
 
53
+ # Create Gradio output interfaces
54
+ output_text_interface1 = gr.Textbox(label="Generated Text (Regular)", placeholder="Generated text will appear here...")
55
+ output_text_interface2 = gr.Textbox(label="Generated Text (Nucleus Sampling)", placeholder="Generated text will appear here...")
56
+
57
+ # Interface for regular text generation
58
+ interface1 = gr.Interface(generate_text, input_text_interface, output_text_interface1,
59
+ title="Text Generation with GPT-2",
60
+ description="Generate text using the GPT-2 model with regular generation method.",
61
+ allow_flagging="never")
62
 
63
+ # Interface for text generation with nucleus sampling
64
+ interface2 = gr.Interface(generate_text_with_nucleus_search, input_text_interface, output_text_interface2,
65
+ title="Text Generation with Nucleus Sampling",
66
+ description="Generate text using nucleus sampling with the GPT-2 model.",
67
+ allow_flagging="never")
68
 
69
+ # Launch both interfaces
70
+ interface1.launch(share=True)
71
+ interface2.launch(share=True)