File size: 2,260 Bytes
2c91872
 
 
 
b2a8d79
2c91872
b2a8d79
2c91872
 
 
 
e076c74
2c91872
 
 
 
 
 
 
 
0fac84d
 
2c91872
 
d5e6e07
 
9f1b92c
2c91872
 
 
 
 
0fac84d
 
 
2c91872
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
import torch
from transformers import (T5ForConditionalGeneration,T5Tokenizer)
import gradio as gr

best_model_path = "swcrazyfan/Dekingify-T5-Large"
model = T5ForConditionalGeneration.from_pretrained(best_model_path)
tokenizer = T5Tokenizer.from_pretrained("swcrazyfan/Dekingify-T5-Large")

def tokenize_data(text):
    # Tokenize the review body
 #   input_ = "paraphrase: "+ str(text) + ' >'
    input_ = "dekingify: " + str(text) + ' </s>'
    max_len = 512
    # tokenize inputs
    tokenized_inputs = tokenizer(input_, padding='max_length', truncation=True, max_length=max_len, return_attention_mask=True, return_tensors='pt')

    inputs={"input_ids": tokenized_inputs['input_ids'],
        "attention_mask": tokenized_inputs['attention_mask']}
    return inputs

#def generate_answers(text, max_length, min_length, num_beams):
def generate_answers(text, max_length, num_beams):
    inputs = tokenize_data(text)
    results= model.generate(input_ids= inputs['input_ids'], attention_mask=inputs['attention_mask'], do_sample=True,
                           num_beams=num_beams,
                            max_length=max_length,
#                            min_length=min_length,
                            early_stopping=True,
                            num_return_sequences=1)
    answer = tokenizer.decode(results[0], skip_special_tokens=True)
    return answer

#iface = gr.Interface(title="DeKingify", description="Write anything below. Then, click submit to 'DeKingify' it.", fn=generate_answers, inputs=[gr.inputs.Textbox(label="Original Text",lines=10), gr.inputs.Slider(label="Maximum Length", minimum=1, maximum=512, default=512, step=1), gr.inputs.Slider(label="Minimum Length", minimum=1, maximum=512, default=1, step=1), gr.inputs.Slider(label="Number of Beams", minimum=1, maximum=50, default=5, step=1)], outputs=["text"])

iface = gr.Interface(title="DeKingify", description="Write anything below. Then, click submit to 'DeKingify' it.", fn=generate_answers, inputs=[gr.inputs.Textbox(label="Original Text",lines=10), gr.inputs.Slider(label="Maximum Length", minimum=1, maximum=512, default=512, step=1), gr.inputs.Slider(label="Number of Beams", minimum=1, maximum=50, default=5, step=1)], outputs=["text"])
iface.launch(inline=False)