File size: 1,611 Bytes
cfc81cb
 
 
 
a93c77c
 
cfc81cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a93c77c
cfc81cb
 
a93c77c
cfc81cb
 
 
 
 
 
 
 
a93c77c
cfc81cb
 
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
#python3
#build a text summarizer using hugging face and gradio


import gradio as gr
import pandas as pd
import numpy as np
import tensorflow as tf
import transformers
from transformers import TFAutoModel, AutoTokenizer

model_class, tokenizer_class, pretrained_weights = (TFAutoModel, AutoTokenizer, 'bert-base-uncased')

# Load pretrained model/tokenizer
tokenizer = tokenizer_class.from_pretrained(pretrained_weights)
model = model_class.from_pretrained(pretrained_weights)

def get_summary(article):
    article_input_ids = tokenizer.encode(article, return_tensors='tf')
    summary_ids = model.generate(article_input_ids)
    summary_txt = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
    return summary_txt

def get_summary_gradio(article):
    return get_summary(article)

iface = gr.Interface(get_summary_gradio, "textbox", "textbox", live=True,
                     examples=[
                         ["The quick brown fox jumps over the lazy dog."],
                         ["The world is a strange place. Sometimes, things are what they seem. But then, if you look closer, they can become something entirely different."],
                         ["The sky is clear; the stars are twinkling. I'm going to bed now. Good night."],
                         ["The president of the United States, and the president of the United Kingdom, have both been in the White House."],
                         ["The president of the United States, and the president of the United Kingdom, have both been in the White House."]
                     ])

if __name__ == "__main__":
    iface.launch()