Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use a pipeline as a high-level helper
|
2 |
+
from transformers import pipeline
|
3 |
+
import gradio as gr
|
4 |
+
import os
|
5 |
+
|
6 |
+
pipe = pipeline("token-classification", model="akdeniz27/bert-base-turkish-cased-ner")
|
7 |
+
|
8 |
+
def merge_tokens(tokens):
|
9 |
+
merged_tokens = []
|
10 |
+
for token in tokens:
|
11 |
+
if merged_tokens and token['entity'].startswith('I-') and merged_tokens[-1]['entity'].endswith(token['entity'][2:]):
|
12 |
+
# If current token continues the entity of the last one, merge them
|
13 |
+
last_token = merged_tokens[-1]
|
14 |
+
last_token['word'] += token['word'].replace('##', '')
|
15 |
+
last_token['end'] = token['end']
|
16 |
+
last_token['score'] = (last_token['score'] + token['score']) / 2
|
17 |
+
else:
|
18 |
+
# Otherwise, add the token to the list
|
19 |
+
merged_tokens.append(token)
|
20 |
+
|
21 |
+
return merged_tokens
|
22 |
+
|
23 |
+
def ner(input):
|
24 |
+
output = pipeline(input)
|
25 |
+
merged_tokens = merge_tokens(output)
|
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=["Benim adım Mesut ve Türk Telekomdan şikayetçiyim"])
|
36 |
+
|
37 |
+
demo.launch()
|