aronvandepol commited on
Commit
90c4f8c
·
1 Parent(s): 2ec1a6a

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -9
app.py CHANGED
@@ -1,13 +1,15 @@
1
  import gradio as gr
 
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
 
4
- model = AutoModelForCausalLM.from_pretrained("aronvandepol/K-GPT125M", use_auth_token="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")
 
5
 
6
  tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-neo-125M")
7
 
8
  model = AutoModelForCausalLM.from_pretrained("K-GPT_NEO125M")
9
 
10
- def get_gen(sample: str, returns: int=1, length: int=50):
11
  input_ids = tokenizer(sample, return_tensors='pt').input_ids
12
  output = model.generate(input_ids,
13
  max_length = length,
@@ -15,15 +17,45 @@ def get_gen(sample: str, returns: int=1, length: int=50):
15
  no_repeat_ngram_size = 3,
16
  early_stopping = True,
17
  do_sample=True,
18
- num_return_sequences = returns,
19
  pad_token_id=tokenizer.eos_token_id
20
  )
21
  generation = tokenizer.batch_decode(output, skip_special_tokens=True)
22
- return generation[0]
 
 
23
 
24
- with gr.Blocks() as demo:
25
- textbox = gr.Textbox(placeholder="Type here and press enter...", lines=4)
26
- btn = gr.Button("Generate")
27
- btn.click(get_gen, textbox, textbox)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
- demo.launch(share=False)
 
1
  import gradio as gr
2
+ import random
3
  from transformers import AutoTokenizer, AutoModelForCausalLM
4
 
5
+
6
+ model = AutoModelForCausalLM.from_pretrained("aronvandepol/K-GPT125M", use_auth_token=os.environ['TOKEN'])
7
 
8
  tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-neo-125M")
9
 
10
  model = AutoModelForCausalLM.from_pretrained("K-GPT_NEO125M")
11
 
12
+ def get_gen(sample: str, length: int=30, beam: int =1):
13
  input_ids = tokenizer(sample, return_tensors='pt').input_ids
14
  output = model.generate(input_ids,
15
  max_length = length,
 
17
  no_repeat_ngram_size = 3,
18
  early_stopping = True,
19
  do_sample=True,
20
+ num_return_sequences = 10,
21
  pad_token_id=tokenizer.eos_token_id
22
  )
23
  generation = tokenizer.batch_decode(output, skip_special_tokens=True)
24
+ gen_text = generation[beam-1]
25
+ return gen_text
26
+
27
 
28
+ with gr.Blocks() as app:
29
+ gr.Markdown("<h1><center>K-GPT_NEO</center></h1>")
30
+ gr.Markdown(
31
+ """
32
+ <justify>
33
+ Interact with the K-GPT_NEO model and generate kpop song texts! By entering a few words into the <i>input</i> prompt and press generate to get the most probable sentence. If you want to see some less probable results press the <i>I'm feeling lucky</i> button.
34
+ </justify>
35
+ """
36
+ )
37
+ beam=gr.Number(value=1, visible=False, precision=0)
38
+ with gr.Row():
39
+ length = gr.Slider(0, 100, step=5, label="Max generated words", value=30)
40
+ with gr.Group():
41
+ txt1 = gr.Textbox(label = "Input", placeholder="Type here and press enter...", lines=4)
42
+ txt2 = gr.Textbox(label = "Output", placeholder="Generated sentence will appear here", lines=4, interactive=False)
43
+ with gr.Row():
44
+ btn = gr.Button("Generate most probable")
45
+ rnd = gr.Button("Feeling Lucky!")
46
+ btn.click(fn=get_gen, inputs=[txt1, length, beam], outputs=txt2)
47
+ rnd.click(fn=get_gen, inputs=[txt1, length, gr.Number(value=random.randint(2, 10), visible=False, precision=0)], outputs=txt2)
48
+ gr.Examples(examples=[['I miss you'], ['My Love has not faded yet'], ['Dancing the stars away']], inputs=txt1, outputs=txt2, fn=get_gen, cache_examples=True)
49
+ gr.Markdown(
50
+ """
51
+ <br>
52
+ <br>
53
+ <br>
54
+ <justify>
55
+ K-GPT_NEO is based on the GPT-Neo text generation model developed by Eleuther AI.
56
+ This architecture was fine-tuned on 2000 English translations of K-pop songs ranging from BTS and BLACKPINK, to TWICE and ZONE. For more information on the training and data, please visit:
57
+ </justify>
58
+ """
59
+ )
60
 
61
+ app.launch()