lik07 commited on
Commit
61e2291
verified
1 Parent(s): 94fa824

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -18
app.py CHANGED
@@ -1,37 +1,67 @@
1
  from transformers import pipeline
2
  import gradio as gr
3
 
4
- get_completion = pipeline("ner", model="gyr66/Ernie-3.0-base-chinese-finetuned-ner")
 
 
 
5
 
 
 
 
 
 
6
  def merge_tokens(tokens):
7
  merged_tokens = []
8
  for token in tokens:
9
  if merged_tokens and token['entity'].startswith('I-') and merged_tokens[-1]['entity'].endswith(token['entity'][2:]):
10
- # If current token continues the entity of the last one, merge them
11
  last_token = merged_tokens[-1]
12
  last_token['word'] += token['word'].replace('##', '')
13
  last_token['end'] = token['end']
14
  last_token['score'] = (last_token['score'] + token['score']) / 2
15
  else:
16
- # Otherwise, add the token to the list
17
  merged_tokens.append(token)
18
-
19
  return merged_tokens
20
 
 
21
  def ner(input):
22
- output = get_completion(input)
23
- merged_tokens = merge_tokens(output)
24
- print(output)
25
- print(merged_tokens)
26
- return {"text": input, "entities": merged_tokens}
27
-
28
- gr.close_all()
29
- demo = gr.Interface(fn=ner,
30
- inputs=[gr.Textbox(label="Text to find entities", lines=2)],
31
- outputs=[gr.HighlightedText(label="Text with entities")],
32
- title="NER with dslim/bert-base-NER",
33
- description="Find entities using the `dslim/bert-base-NER` model under the hood!",
34
- allow_flagging="never",
35
- examples=["My name is Andrew, I'm building DeeplearningAI and I live in California", "My name is Poli, I live in Vienna and work at HuggingFace"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  demo.launch(inline=False)
 
1
  from transformers import pipeline
2
  import gradio as gr
3
 
4
+ # Cargar modelos
5
+ model1 = "gyr66/RoBERTa-ext-large-crf-chinese-finetuned-ner-v2"
6
+ model2 = "gyr66/Ernie-3.0-large-chinese-finetuned-ner"
7
+ model3 = "gyr66/Ernie-3.0-base-chinese-finetuned-ner"
8
 
9
+ get_completion1 = pipeline("ner", model1)
10
+ get_completion2 = pipeline("ner", model2)
11
+ get_completion3 = pipeline("ner", model3)
12
+
13
+ # Funci贸n para fusionar tokens
14
  def merge_tokens(tokens):
15
  merged_tokens = []
16
  for token in tokens:
17
  if merged_tokens and token['entity'].startswith('I-') and merged_tokens[-1]['entity'].endswith(token['entity'][2:]):
18
+ # Si el token contin煤a la entidad del anterior, fusi贸nalos
19
  last_token = merged_tokens[-1]
20
  last_token['word'] += token['word'].replace('##', '')
21
  last_token['end'] = token['end']
22
  last_token['score'] = (last_token['score'] + token['score']) / 2
23
  else:
24
+ # De lo contrario, agrega el token a la lista
25
  merged_tokens.append(token)
 
26
  return merged_tokens
27
 
28
+ # Funci贸n de NER
29
  def ner(input):
30
+ output1 = get_completion1(input)
31
+ output2 = get_completion2(input)
32
+ output3 = get_completion3(input)
33
+
34
+ merged_tokens1 = merge_tokens(output1)
35
+ merged_tokens2 = merge_tokens(output2)
36
+ merged_tokens3 = merge_tokens(output3)
37
+
38
+ # Formatear la salida para Gradio
39
+ entities1 = [{"entity": t['entity'], "start": t['start'], "end": t['end']} for t in merged_tokens1]
40
+ entities2 = [{"entity": t['entity'], "start": t['start'], "end": t['end']} for t in merged_tokens2]
41
+ entities3 = [{"entity": t['entity'], "start": t['start'], "end": t['end']} for t in merged_tokens3]
42
+
43
+ return (
44
+ {"text": input, "entities": entities1},
45
+ {"text": input, "entities": entities2},
46
+ {"text": input, "entities": entities3}
47
+ )
48
+
49
+ # Crear interfaz Gradio
50
+ demo = gr.Interface(
51
+ fn=ner,
52
+ inputs=gr.Textbox(label="Text to find entities", lines=2),
53
+ outputs=[
54
+ gr.HighlightedText(label=f"NER Output - Model 1"),
55
+ gr.HighlightedText(label=f"NER Output - Model 2"),
56
+ gr.HighlightedText(label=f"NER Output - Model 3")
57
+ ],
58
+ title="NER with Multiple Models",
59
+ description="Extract entities using three different models.",
60
+ allow_flagging="never",
61
+ examples=[
62
+ "My name is Andrew, I'm building DeeplearningAI and I live in California",
63
+ "My name is Poli, I live in Vienna and work at HuggingFace"
64
+ ]
65
+ )
66
 
67
  demo.launch(inline=False)