News_Summarizer / app.py
chinhon's picture
Upload app.py
b3118cd
raw
history blame
2.48 kB
import gradio as gr
import re
from transformers import (
AutoTokenizer,
AutoModelForSeq2SeqLM
)
def clean_text(text):
text = text.encode("ascii", errors="ignore").decode(
"ascii"
) # remove non-ascii, Chinese characters
text = re.sub(r"\n", " ", text)
text = re.sub(r"\n\n", " ", text)
text = re.sub(r"\t", " ", text)
text = text.strip(" ")
text = re.sub(
" +", " ", text
).strip() # get rid of multiple spaces and replace with a single
return text
modchoice_1 = "chinhon/bart-large-cnn-summarizer_03"
def summarizer1(text):
input_text = clean_text(text)
tokenizer_1 = AutoTokenizer.from_pretrained(modchoice_1)
model_1 = AutoModelForSeq2SeqLM.from_pretrained(modchoice_1)
with tokenizer_1.as_target_tokenizer():
batch = tokenizer_1(
input_text, truncation=True, padding="longest", return_tensors="pt"
)
raw_1 = model_1.generate(**batch)
summary_1 = tokenizer_1.batch_decode(raw_1, skip_special_tokens=True)
summed_1 = summary_1[0]
lines1 = summed_1.split(". ")
for i in range(len(lines1)):
lines1[i] = "* " + lines1[i]
summ_bullet1 = "\n".join(lines1)
return summ_bullet1
summary1 = gr.Interface(
fn=summarizer1, inputs=gr.inputs.Textbox(), outputs=gr.outputs.Textbox(label="")
)
modchoice_2 = (
"chinhon/pegasus-newsroom-summarizer_02"
)
def summarizer2(text):
input_text = clean_text(text)
tokenizer_2 = AutoTokenizer.from_pretrained(modchoice_2)
model_2 = AutoModelForSeq2SeqLM.from_pretrained(modchoice_2)
with tokenizer_2.as_target_tokenizer():
batch = tokenizer_2(
input_text, truncation=True, padding="longest", return_tensors="pt"
)
raw_2 = model_2.generate(**batch)
summary_2 = tokenizer_2.batch_decode(raw_2, skip_special_tokens=True)
summed_2 = summary_2[0]
lines2 = summed_2.split(". ")
for i in range(len(lines2)):
lines2[i] = "* " + lines2[i]
summ_bullet2 = "\n".join(lines2)
return summ_bullet2
summary2 = gr.Interface(
fn=summarizer2, inputs=gr.inputs.Textbox(), outputs=gr.outputs.Textbox(label="")
)
gradio_ui = gr.Parallel(
summary1,
summary2,
title="Compare 2 AI Summarizers",
inputs=gr.inputs.Textbox(
lines=20,
label="Paste your news story here, and choose from 2 suggested summaries",
),
theme="huggingface",
)
gradio_ui.launch(enable_queue=True)