Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
|
|
2 |
from transformers import pipeline
|
3 |
from typing import List, Dict, Any
|
4 |
|
5 |
-
def merge_tokens(tokens: List[Dict[str,
|
6 |
merged_tokens = []
|
7 |
for token in tokens:
|
8 |
if merged_tokens and token['entity'].startswith('I-') and merged_tokens[-1]['entity'].endswith(token['entity'][2:]):
|
@@ -12,27 +12,15 @@ def merge_tokens(tokens: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
|
12 |
last_token['score'] = (last_token['score'] + token['score']) / 2
|
13 |
else:
|
14 |
merged_tokens.append(token)
|
|
|
15 |
return merged_tokens
|
16 |
|
17 |
get_completion = pipeline("ner", model="b3x0m/bert-xomlac-ner")
|
18 |
|
19 |
-
def ner(input: str) -> str:
|
20 |
output = get_completion(input)
|
21 |
merged_tokens = merge_tokens(output)
|
22 |
-
|
23 |
-
"PER": "tên người",
|
24 |
-
"LOC": "địa điểm",
|
25 |
-
"ORG": "tổ chức",
|
26 |
-
"MISC": "vị trí",
|
27 |
-
}
|
28 |
-
result = []
|
29 |
-
for token in merged_tokens:
|
30 |
-
entity = token['entity']
|
31 |
-
if entity in entity_map: # Filter only relevant entities
|
32 |
-
entity_label = entity_map.get(entity, "khác")
|
33 |
-
result.append(f"{token['word']} ({entity_label})")
|
34 |
-
|
35 |
-
return ", ".join(result)
|
36 |
|
37 |
css = '''
|
38 |
h1#title {
|
|
|
2 |
from transformers import pipeline
|
3 |
from typing import List, Dict, Any
|
4 |
|
5 |
+
def merge_tokens(tokens: List[Dict[str, any]]) -> List[Dict[str, any]]:
|
6 |
merged_tokens = []
|
7 |
for token in tokens:
|
8 |
if merged_tokens and token['entity'].startswith('I-') and merged_tokens[-1]['entity'].endswith(token['entity'][2:]):
|
|
|
12 |
last_token['score'] = (last_token['score'] + token['score']) / 2
|
13 |
else:
|
14 |
merged_tokens.append(token)
|
15 |
+
|
16 |
return merged_tokens
|
17 |
|
18 |
get_completion = pipeline("ner", model="b3x0m/bert-xomlac-ner")
|
19 |
|
20 |
+
def ner(input: str) -> Dict[str, Any]:
|
21 |
output = get_completion(input)
|
22 |
merged_tokens = merge_tokens(output)
|
23 |
+
return {"text": input, "entities": merged_tokens}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
css = '''
|
26 |
h1#title {
|