Spaces:
Runtime error
Runtime error
init
Browse files- app.py +84 -0
- requirements.txt +4 -0
app.py
CHANGED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
import os
|
3 |
+
|
4 |
+
os.system("pip3 install torch==1.10.1+cpu torchvision==0.11.2+cpu torchaudio==0.10.1+cpu -f "
|
5 |
+
"https://download.pytorch.org/whl/cpu/torch_stable.html")
|
6 |
+
|
7 |
+
import gradio as gr
|
8 |
+
from transformers import pipeline
|
9 |
+
|
10 |
+
import spacy
|
11 |
+
from spacy import displacy
|
12 |
+
|
13 |
+
POS_TAG_MAP = {"NOUN": "PODMET", "VERB": "PRÍSUDOK", "ADJ": "PRÍDAVNÉ MENO", "ADP": "ADPOZÍCIA",
|
14 |
+
"ADV": "PRÍSLOVKA", "AUX": "POMOCNÉ", "CCONJ": "KORDINAČNÁ KONJUKCIA", "DET": "DETERMINANT",
|
15 |
+
"INTJ": "CYTOSLOVCIA", "NUM": "NUMERICKÉ", "PART": "ČASTICA", "PRON": "ZÁMMENO",
|
16 |
+
"PROPN": "VLASTNÉ MENO", "PUNCT": "INTERPUNKCIA", "SCONJ": "", "SYM": "SYMBÓL", "X": "INÉ"}
|
17 |
+
|
18 |
+
options = {"ents": list(POS_TAG_MAP.values()),
|
19 |
+
"colors": {"PODMET": "lightblue", "PRÍSUDOK": "lightcoral",
|
20 |
+
"PRÍDAVNÉ MENO": "lightgreen", "VLASTNÉ MENO": "papayawhip"}}
|
21 |
+
|
22 |
+
pipe = pipeline(task='token-classification', model="crabz/slovakbert-ner")
|
23 |
+
nlp = spacy.blank("sk")
|
24 |
+
|
25 |
+
|
26 |
+
def exists_double_class(classified_ents):
|
27 |
+
for i in range(len(classified_ents) - 1):
|
28 |
+
if classified_ents[i][2] == classified_ents[i + 1][1]:
|
29 |
+
return i
|
30 |
+
return -1
|
31 |
+
|
32 |
+
|
33 |
+
def postprocess(classifications):
|
34 |
+
entities = []
|
35 |
+
for i in range(len(classifications)):
|
36 |
+
entities.append((POS_TAG_MAP[classifications[i]['entity']], classifications[i]['start'],
|
37 |
+
classifications[i]['end']))
|
38 |
+
|
39 |
+
while True:
|
40 |
+
i = exists_double_class(entities)
|
41 |
+
if i == -1:
|
42 |
+
break
|
43 |
+
|
44 |
+
entities.append((entities[i][0], entities[i][1], entities[i + 1][2]))
|
45 |
+
del entities[i]
|
46 |
+
del entities[i]
|
47 |
+
return entities
|
48 |
+
|
49 |
+
|
50 |
+
def set_entities(sentence, entities):
|
51 |
+
doc = nlp(sentence)
|
52 |
+
ents = []
|
53 |
+
for label, start, end in entities:
|
54 |
+
ents.append(doc.char_span(start, end, label))
|
55 |
+
try:
|
56 |
+
doc.ents = ents
|
57 |
+
except TypeError:
|
58 |
+
pass
|
59 |
+
return doc
|
60 |
+
|
61 |
+
|
62 |
+
def apply_pos(Sentence: str):
|
63 |
+
classifications = pipe(Sentence.replace(",", " ,")
|
64 |
+
.replace(".", " .")
|
65 |
+
.replace("!", " !")
|
66 |
+
.replace("?", " ?")
|
67 |
+
.replace(":", " :"))
|
68 |
+
entities = postprocess(classifications)
|
69 |
+
doc = set_entities(Sentence, entities)
|
70 |
+
displacy_html = displacy.render(doc, style="ent", options=options)
|
71 |
+
return displacy_html
|
72 |
+
|
73 |
+
|
74 |
+
intf = gr.Interface(fn=apply_pos, inputs="text", outputs="html", title='Slovak Part-Of-Speech Tagging',
|
75 |
+
allow_flagging=False,
|
76 |
+
examples=[["Laboratóriá Úradu verejného zdravotníctva sekvenovaním potvrdili výskyt ďalších štyroch prípadov variantu omikron na Slovensku."],
|
77 |
+
["Čaputová opakovane tvrdí, že \"spravodlivosť na Slovensku neplatí vždy pre všetkých rovnako\"."],
|
78 |
+
["Informácie o týchto veľkolepých plánoch prišli týždeň po tom, ako sa japonský miliardár Jusaku Maezawa vrátil z 12-dňového pobytu na Medzinárodnej vesmírnej stanici (ISS), čím sa stal prvým vesmírnym turistom, ktorý cestoval na ISS za viac ako desať rokov."],
|
79 |
+
["Minister financií a líder mandátovo najsilnejšieho hnutia OĽaNO Igor Matovič upozorňuje, že následky tretej vlny budú na Slovensku veľmi veľké."],
|
80 |
+
["Začiatkom roka 2021 sa objavili nezhody medzi Richardom Sulíkom a šéfom hnutia OĽANO Igorom Matovičom, ktoré v istej miere pretrvávajú aj dodnes."]],
|
81 |
+
description="",
|
82 |
+
article="",
|
83 |
+
analytics_enabled=False)
|
84 |
+
intf.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio==2.8.1
|
2 |
+
spacy==3.2.3
|
3 |
+
transformers==4.17.0
|
4 |
+
|