File size: 1,597 Bytes
c8766ff
3b64339
a9d6e8c
c8766ff
deb90aa
e32e0a4
ab9ad6a
e32e0a4
 
 
 
f306387
72c2b08
e32e0a4
 
 
 
 
 
 
 
 
01bb18b
e32e0a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab9ad6a
 
c8766ff
 
deb90aa
 
9db86ab
12df622
374409a
ab9ad6a
9db86ab
ea0b887
f8bd591
 
ab9ad6a
d7e74f5
889dd23
5781692
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

import gradio as gr


from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
device = "cpu"

tokenizer = AutoTokenizer.from_pretrained("humarin/chatgpt_paraphraser_on_T5_base")

model = AutoModelForSeq2SeqLM.from_pretrained("humarin/chatgpt_paraphraser_on_T5_base").to(device)

translator = pipeline("translation", model="facebook/nllb-200-distilled-600M")

def paraphrase(
    question,
    num_beams=5,
    num_beam_groups=5,
    num_return_sequences=1,
    repetition_penalty=10.0,
    diversity_penalty=3.0,
    no_repeat_ngram_size=2,
    temperature=0.7,
    max_length=1024
):
    input_ids = tokenizer(
        f'paraphrase: {question}',
        return_tensors="pt", padding="longest",
        max_length=max_length,
        truncation=True,
    ).input_ids.to(device)
    
    outputs = model.generate(
        input_ids, temperature=temperature, repetition_penalty=repetition_penalty,
        num_return_sequences=num_return_sequences, no_repeat_ngram_size=no_repeat_ngram_size,
        num_beams=num_beams, num_beam_groups=num_beam_groups,
        max_length=max_length, diversity_penalty=diversity_penalty
    )

    res = tokenizer.batch_decode(outputs, skip_special_tokens=True)

    return res


def translate(myinput):
    myout = translator(myinput,src_lang="eng_Latn",tgt_lang="fra_Latn")
    return myout

def predict(mytextInput):
    out = translate(paraphrase(mytextInput))
    return out

iface = gr.Interface(predict,
                     inputs="textbox",
                     outputs="text",
                     live=True,)

iface.launch(share=True)