Upload 4 files
Browse files- app.py +19 -0
- ner_utils.py +54 -0
- requirements.txt +2 -0
- summerizer_utils.py +43 -0
app.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import warnings
|
| 2 |
+
warnings.filterwarnings('ignore')
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
from ner_utils import ner_with_markdown
|
| 6 |
+
from summerizer_utils import summarizer_with_markdown
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# Combine both the app
|
| 10 |
+
demo = gr.Blocks()
|
| 11 |
+
with demo:
|
| 12 |
+
gr.TabbedInterface(
|
| 13 |
+
[ner_with_markdown, summarizer_with_markdown],
|
| 14 |
+
['Named Entity Recognition', 'Text Summarization']
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
if __name__ == "__main__":
|
| 19 |
+
demo.launch()
|
ner_utils.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
#Load the NER model
|
| 5 |
+
named_entity_recognizer= pipeline(task = 'ner', model = 'dslim/bert-base-NER')
|
| 6 |
+
# NER helper functions
|
| 7 |
+
def merge_tokens(tokens):
|
| 8 |
+
merged_tokens = []
|
| 9 |
+
for token in tokens:
|
| 10 |
+
if merged_tokens and token['entity'].startswith('I-') and merged_tokens[-1]['entity'].endswith(token['entity'][2:]):
|
| 11 |
+
# If current token continues the entity of the last one, merge them
|
| 12 |
+
last_token = merged_tokens[-1]
|
| 13 |
+
last_token['word'] += token['word'].replace('##', '')
|
| 14 |
+
last_token['end'] = token['end']
|
| 15 |
+
last_token['score'] = (last_token['score'] + token['score']) / 2
|
| 16 |
+
else:
|
| 17 |
+
# Otherwise, add the token to the list
|
| 18 |
+
merged_tokens.append(token)
|
| 19 |
+
|
| 20 |
+
return merged_tokens
|
| 21 |
+
def ner(input):
|
| 22 |
+
output = named_entity_recognizer(input)
|
| 23 |
+
merged_tokens = merge_tokens(output)
|
| 24 |
+
return {'text': input, 'entities': merged_tokens}
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
# NER App
|
| 28 |
+
NER = gr.Interface(
|
| 29 |
+
fn = ner,
|
| 30 |
+
inputs = [gr.Textbox(label = "Text to find entities", lines = 3)],
|
| 31 |
+
outputs = [gr.HighlightedText(label = 'Text with entities')],
|
| 32 |
+
allow_flagging = 'never',
|
| 33 |
+
examples=[
|
| 34 |
+
"My name is Nabi, I'm building NER Application",
|
| 35 |
+
"My name is Emon, I live in Rajshahi and study at RUET"
|
| 36 |
+
]
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# Add Markdown content
|
| 40 |
+
markdown_content_ner = gr.Markdown(
|
| 41 |
+
"""
|
| 42 |
+
<div style='text-align: center; font-family: "Times New Roman";'>
|
| 43 |
+
<h1 style='color: #FF6347;'>Named Entity Recognition APP</h1>
|
| 44 |
+
<h3 style='color: #4682B4;'>Model: dslim/bert-base-NER</h3>
|
| 45 |
+
<h3 style='color: #32CD32;'>Made By: Md. Mahmudun Nabi</h3>
|
| 46 |
+
</div>
|
| 47 |
+
"""
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
# Combine the Markdown content and the demo interface
|
| 51 |
+
ner_with_markdown = gr.Blocks()
|
| 52 |
+
with ner_with_markdown:
|
| 53 |
+
markdown_content_ner.render()
|
| 54 |
+
NER.render()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|
summerizer_utils.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load the summarization model
|
| 5 |
+
summarizer = pipeline(task="summarization",
|
| 6 |
+
model="sshleifer/distilbart-cnn-12-6")
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# Function to summarize input text
|
| 10 |
+
def summarize(input,
|
| 11 |
+
min_length,
|
| 12 |
+
max_length):
|
| 13 |
+
output = summarizer(input,
|
| 14 |
+
min_length = min_length,
|
| 15 |
+
max_length = max_length)
|
| 16 |
+
return output[0]['summary_text']
|
| 17 |
+
|
| 18 |
+
# Create the Gradio interface
|
| 19 |
+
SUMMARIZER = gr.Interface(
|
| 20 |
+
fn=summarize,
|
| 21 |
+
inputs=[gr.Textbox(label='Text to summarize', lines=6),
|
| 22 |
+
gr.Slider(label='Min Length', minimum=10, maximum=50, value=10),
|
| 23 |
+
gr.Slider(label='Max Length', minimum=50, maximum=200, value=100)],
|
| 24 |
+
outputs=[gr.Textbox(label='Result', lines=3)],
|
| 25 |
+
allow_flagging='never'
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# Add Markdown content
|
| 29 |
+
markdown_content_summarizer = gr.Markdown(
|
| 30 |
+
"""
|
| 31 |
+
<div style='text-align: center; font-family: "Times New Roman";'>
|
| 32 |
+
<h1 style='color: #FF6347;'>Text Summarization with DistilBART-CNN</h1>
|
| 33 |
+
<h3 style='color: #4682B4;'>Model: sshleifer/distilbart-cnn-12-6</h3>
|
| 34 |
+
<h3 style='color: #32CD32;'>Made By: Md. Mahmudun Nabi</h3>
|
| 35 |
+
</div>
|
| 36 |
+
"""
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# Combine the Markdown content and the demo interface
|
| 40 |
+
summarizer_with_markdown = gr.Blocks()
|
| 41 |
+
with summarizer_with_markdown:
|
| 42 |
+
markdown_content_summarizer.render()
|
| 43 |
+
SUMMARIZER.render()
|