Spaces:
Sleeping
Sleeping
Unityraptor
commited on
Commit
•
e32e0a4
1
Parent(s):
ea0b887
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,48 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import
|
|
|
3 |
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
|
7 |
def launch(input):
|
8 |
-
out =
|
9 |
return out[0]['generated_text']
|
10 |
|
11 |
iface = gr.Interface(launch,
|
12 |
inputs="textbox",
|
13 |
-
outputs="
|
14 |
|
15 |
iface.launch()
|
16 |
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
3 |
+
device = "cpu"
|
4 |
|
5 |
+
tokenizer = AutoTokenizer.from_pretrained("humarin/chatgpt_paraphraser_on_T5_base")
|
6 |
+
|
7 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("humarin/chatgpt_paraphraser_on_T5_base").to(device)
|
8 |
+
|
9 |
+
def paraphrase(
|
10 |
+
question,
|
11 |
+
num_beams=5,
|
12 |
+
num_beam_groups=5,
|
13 |
+
num_return_sequences=1,
|
14 |
+
repetition_penalty=10.0,
|
15 |
+
diversity_penalty=3.0,
|
16 |
+
no_repeat_ngram_size=2,
|
17 |
+
temperature=0.7,
|
18 |
+
max_length=128
|
19 |
+
):
|
20 |
+
input_ids = tokenizer(
|
21 |
+
f'paraphrase: {question}',
|
22 |
+
return_tensors="pt", padding="longest",
|
23 |
+
max_length=max_length,
|
24 |
+
truncation=True,
|
25 |
+
).input_ids.to(device)
|
26 |
+
|
27 |
+
outputs = model.generate(
|
28 |
+
input_ids, temperature=temperature, repetition_penalty=repetition_penalty,
|
29 |
+
num_return_sequences=num_return_sequences, no_repeat_ngram_size=no_repeat_ngram_size,
|
30 |
+
num_beams=num_beams, num_beam_groups=num_beam_groups,
|
31 |
+
max_length=max_length, diversity_penalty=diversity_penalty
|
32 |
+
)
|
33 |
+
|
34 |
+
res = tokenizer.batch_decode(outputs, skip_special_tokens=True)
|
35 |
+
|
36 |
+
return res
|
37 |
|
38 |
|
39 |
def launch(input):
|
40 |
+
out = paraphrase(input)
|
41 |
return out[0]['generated_text']
|
42 |
|
43 |
iface = gr.Interface(launch,
|
44 |
inputs="textbox",
|
45 |
+
outputs="textbox")
|
46 |
|
47 |
iface.launch()
|
48 |
|