File size: 1,345 Bytes
cf24198
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import pickle
import pickle
from transformers import AutoTokenizer, TFAutoModelForSeq2SeqLM, pipeline

# File Paths
model_path = 'fine_tuned_sum' 
tokenizer_path = "tokenizer"
examples_path = "examples.pkl"

# Load the tokenizer
tokenizer = AutoTokenizer.from_pretrained(tokenizer_path)

# Load the fine-tuned BERT model
seq2seq_model = TFAutoModelForSeq2SeqLM.from_pretrained(model_path)

# loading the examples
with open('examples.pkl', 'rb') as f: examples = pickle.load(f)

# Creating the pipeline
sum_params = {
    "model":seq2seq_model,
    "tokenizer":tokenizer,
    "framework":"tf",
}

summarizer = pipeline("summarization", **sum_params)
# Load the model
# Define a function to make predictions with the model
def summarize(text):

    # defining the params
    prms = {
        "min_length":5,
        "max_length":128
    }
    return summarizer(text,**prms)[0]["summary_text"]

# GUI Component
# defining the params
if_p = {
    "fn":summarize,
    "inputs":gr.inputs.Textbox(label="Text"),
    "outputs":gr.outputs.Textbox(label="Output"),
    "title":"Fine-tuned BERT moedl for text generation",
    "description":"Write something to generate text",
    "examples":examples
}

# Create a Gradio interface instance
demo = gr.Interface(**if_p)

# Launching the demo
if __name__ == "__main__":
    demo.launch()