Catmeow commited on
Commit
f321a43
1 Parent(s): c8029b1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ title = "tory Generator"
4
+
5
+ # gpt-neo-2.7B gpt-j-6B
6
+
7
+ def generate(text,the_model,max_length,temperature,repetition_penalty):
8
+ generator = pipeline('text-generation', model=the_model)
9
+ result = generator(text, num_return_sequences=3,
10
+ max_length=max_length,
11
+ temperature=temperature,
12
+ repetition_penalty = repetition_penalty,
13
+ no_repeat_ngram_size=2,early_stopping=False)
14
+ return result[0]["generated_text"],result[1]["generated_text"],result[2]["generated_text"]
15
+
16
+
17
+ def complete_with_gpt(text,context,the_model,max_length,temperature,repetition_penalty):
18
+ # Use the last 50 characters of the text as context
19
+ # text[:-context] +
20
+ return generate(text[-context:],the_model,max_length,temperature,repetition_penalty)
21
+
22
+ def send(text1,context,text2):
23
+ return text1 + text2[context:]
24
+
25
+ with gr.Blocks() as demo:
26
+ textbox = gr.Textbox(placeholder="Type here and press enter...", lines=4)
27
+ context = gr.Slider(value=50,label="Truncate input text length",minimum=1,maximum=300)
28
+ the_model = gr.Dropdown(choices=['gpt2','gpt2-medium','gpt2-large','gpt2-xl'],value = 'gpt2',label="Choose model")
29
+ max_length = gr.Slider(value=50,label="Max Generate Length",minimum=1,maximum=300)
30
+ temperature = gr.Slider(value=1.0,label="Temperature",minimum=0.0,maximum=1.0,step=0.05)
31
+ repetition_penalty = gr.Slider(value=1.1,label="Repetition penalty",minimum=0.2,maximum=2,step=0.1)
32
+ output1 = gr.Textbox(lines=4)
33
+ output2 = gr.Textbox(lines=4)
34
+ output3 = gr.Textbox(lines=4)
35
+ btn = gr.Button("Generate")
36
+ btn.click(complete_with_gpt,inputs=[textbox,context,the_model,max_length,temperature,repetition_penalty], outputs=[output1,output2,output3])
37
+
38
+ send1 = gr.Button("Send1 to Origin Textbox").click(send,inputs=[textbox,context,output1],outputs=textbox)
39
+ send2 = gr.Button("Send2 to Origin Textbox").click(send,inputs=[textbox,context,output2],outputs=textbox)
40
+ send3 = gr.Button("Send3 to Origin Textbox").click(send,inputs=[textbox,context,output3],outputs=textbox)
41
+
42
+
43
+ demo.launch()