File size: 2,137 Bytes
f2cbb60
319c415
 
 
 
 
 
f2cbb60
 
319c415
 
 
f2cbb60
 
 
 
 
 
 
 
319c415
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f2cbb60
 
 
 
 
 
319c415
 
 
 
 
 
 
f2cbb60
319c415
 
f2cbb60
319c415
f2cbb60
 
 
 
319c415
f2cbb60
 
 
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
61
62
63
64
65
from transformers import T5ForConditionalGeneration, T5TokenizerFast, pipeline
from transformers.models.f_t5.modeling_t5 import (
    T5ForConditionalGeneration as FT5ForConditionalGeneration,
)
from transformers.models.f_t5.tokenization_t5_fast import (
    T5TokenizerFast as FT5TokenizerFast,
)

import json

with open("examples.json") as f:
    examples = json.load(f)["article"]

model_name = "flax-community/ft5-cnn-dm"
ft5_model = FT5ForConditionalGeneration.from_pretrained(model_name)
ft5_tokenizer = FT5TokenizerFast.from_pretrained(model_name)
ft5_summarizer = pipeline(
    "summarization", model=ft5_model, tokenizer=ft5_tokenizer, framework="pt"
)

# model_name = 'flax-community/t5-base-cnn-dm'
# t5_model = T5ForConditionalGeneration.from_pretrained(model_name)
# t5_tokenizer = T5TokenizerFast.from_pretrained(model_name)
# predict_t5 = get_predict(t5_model, t5_tokenizer)


def fn(text, do_sample, min_length, max_length, temperature, top_p):
    out = ft5_summarizer(
        text,
        do_sample=do_sample,
        min_length=min_length,
        max_length=max_length,
        temperature=temperature,
        top_p=top_p,
        truncation=True,
    )
    return out[0]["summary_text"]


import gradio as gr

interface = gr.Interface(
    fn,
    inputs=[
        gr.inputs.Textbox(lines=10, label="text"),
        gr.inputs.Checkbox(label="do_sample"),
        gr.inputs.Slider(1, 128, step=1, default=64, label="min_length"),
        gr.inputs.Slider(1, 128, step=1, default=64, label="max_length"),
        gr.inputs.Slider(0.0, 1.0, step=0.1, default=1, label="temperature"),
        gr.inputs.Slider(0.0, 1.0, step=0.1, default=1, label="top_p"),
    ],
    outputs=gr.outputs.Textbox(),
    # server_port=8080,
    # server_name='0.0.0.0',
    examples=[[ex] for ex in examples],
    title="F-T5 News Summarization",
    description="""
F-T5 is a hybrid encoder-decoder model based on T5 and FNet.
The model architecture is based on T5, except the encoder self attention is replaced by fourier transform as in FNet.
The model is pre-trained on openwebtext, fine-tuned on CNN/DM.
""",
)

interface.launch()