unilm commited on
Commit
02a1232
1 Parent(s): b3aa6f1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -0
app.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as grad
2
+ import torch
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+
5
+ def load_prompter():
6
+ prompter_model = AutoModelForCausalLM.from_pretrained("microsoft/Promptist")
7
+ tokenizer = AutoTokenizer.from_pretrained("gpt2")
8
+ tokenizer.pad_token = tokenizer.eos_token
9
+ tokenizer.padding_side = "left"
10
+ return prompter_model, tokenizer
11
+
12
+ prompter_model, prompter_tokenizer = load_prompter()
13
+
14
+ def generate(plain_text):
15
+ input_ids = prompter_tokenizer(plain_text.strip()+" Rephrase:", return_tensors="pt").input_ids
16
+ eos_id = prompter_tokenizer.eos_token_id
17
+ outputs = prompter_model.generate(input_ids, do_sample=False, max_new_tokens=75, num_beams=8, num_return_sequences=8, eos_token_id=eos_id, pad_token_id=eos_id, length_penalty=-1.0)
18
+ output_texts = prompter_tokenizer.batch_decode(outputs, skip_special_tokens=True)
19
+ res = output_texts[0].replace(plain_text+" Rephrase:", "").strip()
20
+ print("[I] Prompter input: %s" % plain_text)
21
+ print("[I] Prompter output: %s \n------------\n" % res)
22
+ return res
23
+
24
+ txt = grad.Textbox(lines=1, label="Initial Text", placeholder="Input Prompt")
25
+ out = grad.Textbox(lines=1, label="Optimized Prompt")
26
+
27
+ grad.Interface(fn=generate,
28
+ inputs=txt,
29
+ outputs=out,
30
+ title="Promptist",
31
+ allow_flagging='never',
32
+ cache_examples=False,
33
+ theme="default").launch(enable_queue=True, share=True, debug=True)