Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
+
# Load models
|
5 |
+
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
|
6 |
+
answerer = pipeline("question-answering", model="valhalla/bart-large-finetuned-squadv1")
|
7 |
+
translator = pipeline("translation", model="facebook/nllb-200-distilled-600M")
|
8 |
+
filler = pipeline("fill-mask", model="FacebookAI/roberta-large")
|
9 |
+
paraphraser = pipeline("text2text-generation", model="humarin/chatgpt_paraphraser_on_T5_base")
|
10 |
+
|
11 |
+
# NLP functions
|
12 |
+
def summarize(text, min_length, max_length):
|
13 |
+
summary = summarizer(text, min_length=min_length, max_length=max_length)
|
14 |
+
return summary[0]['summary_text']
|
15 |
+
|
16 |
+
def answer(context, question):
|
17 |
+
answers = answerer(context=context, question=question, top_k=3)
|
18 |
+
return [" ".join(answer['answer'].split("\n")) for answer in answers]
|
19 |
+
|
20 |
+
languages = ["zho_Hans (Chinese)", "spa_Latn (Spanish)", "eng_Latn (English)", "hin_Deva (Hindi)", "por_Latn (Portuguese)", "rus_Cyrl (Russian)",
|
21 |
+
"jpn_Jpan (Japanese)", "deu_Latn (German)", "yue_Hant (Yue Chinese)", "kor_Hang (Korean)", "fra_Latn (French)", "ita_Latn (Italian)"]
|
22 |
+
def translate(text, src_lang, tgt_lang):
|
23 |
+
src_lang = src_lang.split()[0]
|
24 |
+
tgt_lang = tgt_lang.split()[0]
|
25 |
+
translation = translator(text, src_lang=src_lang, tgt_lang=tgt_lang, max_length=translator.tokenizer.model_max_length)
|
26 |
+
return translation[0]['translation_text']
|
27 |
+
|
28 |
+
def fill(text, to_fill):
|
29 |
+
if not to_fill:
|
30 |
+
text = text.replace("_", filler.tokenizer.mask_token)
|
31 |
+
else:
|
32 |
+
text = text.replace(to_fill, filler.tokenizer.mask_token)
|
33 |
+
words = filler(text, top_k=3)
|
34 |
+
return [word['token_str'].strip() for word in words]
|
35 |
+
|
36 |
+
def paraphrase(text):
|
37 |
+
paraphrases = paraphraser(text, num_beams=3, num_beam_groups=3, num_return_sequences=3, diversity_penalty=3.0, max_length=paraphraser.tokenizer.model_max_length)
|
38 |
+
return [paraphrase['generated_text'] for paraphrase in paraphrases]
|
39 |
+
|
40 |
+
# Build demo
|
41 |
+
with gr.Blocks() as demo:
|
42 |
+
gr.HTML("<center><h1>NLP Toolbox</h1></center>")
|
43 |
+
with gr.Tabs():
|
44 |
+
with gr.TabItem("Summarization"):
|
45 |
+
text = gr.Textbox(label="text", placeholder="Enter text here...", lines=8)
|
46 |
+
with gr.Accordion("set summary length", open=False):
|
47 |
+
with gr.Row():
|
48 |
+
with gr.Column():
|
49 |
+
min_length = gr.Slider(label="minimum length", minimum=50, maximum=1000, step=10, value=100)
|
50 |
+
with gr.Column():
|
51 |
+
max_length = gr.Slider(label="maximum length", minimum=50, maximum=1000, step=10, value=800)
|
52 |
+
output = gr.Textbox(label="summary", lines=3)
|
53 |
+
submit = gr.Button("Summarize")
|
54 |
+
submit.click(summarize, inputs=[text, min_length, max_length], outputs=output)
|
55 |
+
with gr.TabItem("Question Answering"):
|
56 |
+
context = gr.Textbox(label="context", placeholder="Enter text here...", lines=8)
|
57 |
+
question = gr.Textbox(label="question", placeholder="Enter question here...", lines=1)
|
58 |
+
output1 = gr.Textbox(label="answer no.1", lines=1)
|
59 |
+
output2 = gr.Textbox(label="answer no.2", lines=1)
|
60 |
+
output3 = gr.Textbox(label="answer no.3", lines=1)
|
61 |
+
submit = gr.Button("Answer")
|
62 |
+
submit.click(answer, inputs=[context, question], outputs=[output1, output2, output3])
|
63 |
+
with gr.TabItem("Translation"):
|
64 |
+
text = gr.Textbox(label="text", placeholder="Enter text here...", lines=8)
|
65 |
+
with gr.Row():
|
66 |
+
with gr.Column():
|
67 |
+
src_lang = gr.Dropdown(languages, label="source language")
|
68 |
+
with gr.Column():
|
69 |
+
tgt_lang = gr.Dropdown(languages, label="target language")
|
70 |
+
output = gr.Textbox(label="translation", lines=8)
|
71 |
+
submit = gr.Button("Translate")
|
72 |
+
submit.click(translate, inputs=[text, src_lang, tgt_lang], outputs=output)
|
73 |
+
with gr.TabItem("Fill-Mask"):
|
74 |
+
text = gr.Textbox(label="text", placeholder="Enter text here...", lines=6)
|
75 |
+
gr.Markdown("Please use the \"_\" symbol to represent the blank.")
|
76 |
+
to_fill = gr.Textbox(label="word to replace", placeholder="Enter word here...", lines=1)
|
77 |
+
gr.Markdown("If you are filling a blank, please leave the cell above blank.")
|
78 |
+
output1 = gr.Textbox(label="1st option", lines=1)
|
79 |
+
output2 = gr.Textbox(label="2nd option", lines=1)
|
80 |
+
output3 = gr.Textbox(label="3rd option", lines=1)
|
81 |
+
submit = gr.Button("Fill/Replace")
|
82 |
+
submit.click(fill, inputs=[text, to_fill], outputs=[output1, output2, output3])
|
83 |
+
with gr.TabItem("Paraphrase"):
|
84 |
+
text = gr.Textbox(label="text", placeholder="Enter text here...", lines=8)
|
85 |
+
output1 = gr.Textbox(label="1st option", lines=8)
|
86 |
+
output2 = gr.Textbox(label="2nd option", lines=8)
|
87 |
+
output3 = gr.Textbox(label="3rd option", lines=8)
|
88 |
+
submit = gr.Button("Paraphrase")
|
89 |
+
submit.click(paraphrase, inputs=text, outputs=[output1, output2, output3])
|
90 |
+
|
91 |
+
demo.launch(share=True)
|