copied demo app to app
Browse files
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
token_skill_classifier = pipeline(model="jjzha/jobbert_skill_extraction", aggregation_strategy="first")
|
| 5 |
+
token_knowledge_classifier = pipeline(model="jjzha/jobbert_knowledge_extraction", aggregation_strategy="first")
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
examples = [
|
| 9 |
+
"Knowing Python is a plus",
|
| 10 |
+
"Recommend changes, develop and implement processes to ensure compliance with IFRS standards"
|
| 11 |
+
]
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def aggregate_span(results):
|
| 15 |
+
new_results = []
|
| 16 |
+
current_result = results[0]
|
| 17 |
+
|
| 18 |
+
for result in results[1:]:
|
| 19 |
+
if result["start"] == current_result["end"] + 1:
|
| 20 |
+
current_result["word"] += " " + result["word"]
|
| 21 |
+
current_result["end"] = result["end"]
|
| 22 |
+
else:
|
| 23 |
+
new_results.append(current_result)
|
| 24 |
+
current_result = result
|
| 25 |
+
|
| 26 |
+
new_results.append(current_result)
|
| 27 |
+
|
| 28 |
+
return new_results
|
| 29 |
+
|
| 30 |
+
def ner(text):
|
| 31 |
+
output_skills = token_skill_classifier(text)
|
| 32 |
+
for result in output_skills:
|
| 33 |
+
if result.get("entity_group"):
|
| 34 |
+
result["entity"] = "Skill"
|
| 35 |
+
del result["entity_group"]
|
| 36 |
+
|
| 37 |
+
output_knowledge = token_knowledge_classifier(text)
|
| 38 |
+
for result in output_knowledge:
|
| 39 |
+
if result.get("entity_group"):
|
| 40 |
+
result["entity"] = "Knowledge"
|
| 41 |
+
del result["entity_group"]
|
| 42 |
+
|
| 43 |
+
if len(output_skills) > 0:
|
| 44 |
+
output_skills = aggregate_span(output_skills)
|
| 45 |
+
if len(output_knowledge) > 0:
|
| 46 |
+
output_knowledge = aggregate_span(output_knowledge)
|
| 47 |
+
|
| 48 |
+
return {"text": text, "entities": output_skills}, {"text": text, "entities": output_knowledge}
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
demo = gr.Interface(fn=ner,
|
| 52 |
+
inputs=gr.Textbox(placeholder="Enter sentence here..."),
|
| 53 |
+
outputs=["highlight", "highlight"],
|
| 54 |
+
examples=examples)
|
| 55 |
+
|
| 56 |
+
demo.launch()
|