rayl-aoit commited on
Commit
b2ae370
·
verified ·
1 Parent(s): f96feeb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -2
app.py CHANGED
@@ -3,7 +3,8 @@ from transformers import pipeline
3
 
4
  playground = gr.Blocks()
5
  image_pipe = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
6
- get_completion = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
 
7
 
8
  examples_summarization = [
9
  ["The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side. During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest man-made structure in the world, a title it held for 41 years until the Chrysler Building in New York City was finished in 1930. It was the first structure to reach a height of 300 metres. Due to the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the Chrysler Building by 5.2 metres (17 ft). Excluding transmitters, the Eiffel Tower is the second tallest free-standing structure in France after the Millau Viaduct."],
@@ -24,11 +25,31 @@ def translate(input_text, source, target):
24
  return "", f"Error: Translation direction {source_readable} to {target} is not supported by Helsinki Translation Models"
25
 
26
  def summarize(input):
27
- output = get_completion(input)
28
  summary_origin = output[0]['summary_text']
29
  summary_translated = translate(summary_origin,'en','fr')
30
  return summary_origin, summary_translated[0]
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  def create_playground_header():
33
  gr.Markdown("""
34
  # 🤗 Hugging Face Labs
@@ -111,6 +132,15 @@ with playground:
111
  gr.Markdown("""
112
  > Name Entity Recognition
113
  """)
 
 
 
 
 
 
 
 
 
114
 
115
  create_playground_footer()
116
 
 
3
 
4
  playground = gr.Blocks()
5
  image_pipe = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
6
+ summary_pipe = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
7
+ ner_pipe = pipeline("ner", model="dslim/bert-base-NER")
8
 
9
  examples_summarization = [
10
  ["The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side. During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest man-made structure in the world, a title it held for 41 years until the Chrysler Building in New York City was finished in 1930. It was the first structure to reach a height of 300 metres. Due to the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the Chrysler Building by 5.2 metres (17 ft). Excluding transmitters, the Eiffel Tower is the second tallest free-standing structure in France after the Millau Viaduct."],
 
25
  return "", f"Error: Translation direction {source_readable} to {target} is not supported by Helsinki Translation Models"
26
 
27
  def summarize(input):
28
+ output = summary_pipe(input)
29
  summary_origin = output[0]['summary_text']
30
  summary_translated = translate(summary_origin,'en','fr')
31
  return summary_origin, summary_translated[0]
32
 
33
+ def merge_tokens(tokens):
34
+ merged_tokens = []
35
+ for token in tokens:
36
+ if merged_tokens and token['entity'].startswith('I-') and merged_tokens[-1]['entity'].endswith(token['entity'][2:]):
37
+ # If current token continues the entity of the last one, merge them
38
+ last_token = merged_tokens[-1]
39
+ last_token['word'] += token['word'].replace('##', '')
40
+ last_token['end'] = token['end']
41
+ last_token['score'] = (last_token['score'] + token['score']) / 2
42
+ else:
43
+ # Otherwise, add the token to the list
44
+ merged_tokens.append(token)
45
+
46
+ return merged_tokens
47
+
48
+ def ner(input):
49
+ output = ner_pipe(input)
50
+ merged_tokens = merge_tokens(output)
51
+ return {"text": input, "entities": merged_tokens}
52
+
53
  def create_playground_header():
54
  gr.Markdown("""
55
  # 🤗 Hugging Face Labs
 
132
  gr.Markdown("""
133
  > Name Entity Recognition
134
  """)
135
+
136
+ iface = gr.Interface(fn=ner,
137
+ inputs=[gr.Textbox(label="Text to find entities", lines=2)],
138
+ outputs=[gr.HighlightedText(label="Text with entities")],
139
+ title="NER with dslim/bert-base-NER",
140
+ description="Find entities using the `dslim/bert-base-NER` model!",
141
+ allow_flagging="never",
142
+ examples=["My name is Ray, I'm learning through Hugging Face and DeepLearning.AI and I live in Caversham, Reading", "My name is Raymond, I work at A&O IT Group"])
143
+
144
 
145
  create_playground_footer()
146