Spaces:
Build error
Build error
artem-oneai
commited on
Commit
·
27e9cd8
1
Parent(s):
5d16720
Add app files
Browse files- app.py +38 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from fastcoref import FCoref
|
3 |
+
import spacy
|
4 |
+
from spacy import displacy
|
5 |
+
import torch
|
6 |
+
from random import randint
|
7 |
+
|
8 |
+
nlp = spacy.load('uk_core_news_md')
|
9 |
+
|
10 |
+
device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
|
11 |
+
|
12 |
+
model_path = "artemkramov/coref-ua"
|
13 |
+
model = FCoref(model_name_or_path=model_path, device=device, nlp=nlp)
|
14 |
+
|
15 |
+
default = "Мій друг дав мені свою машину та ключі до неї; крім того, він дав мені його книгу. Я з радістю її читаю."
|
16 |
+
|
17 |
+
|
18 |
+
def random_color():
|
19 |
+
rand = lambda: randint(100, 255)
|
20 |
+
return '#%02X%02X%02X' % (rand(), rand(), rand())
|
21 |
+
|
22 |
+
|
23 |
+
def corefer(text):
|
24 |
+
preds = model.predict(texts=[text])
|
25 |
+
clusters = preds[0].get_clusters(as_strings=False)
|
26 |
+
doc = nlp(text)
|
27 |
+
doc.spans["sc"] = []
|
28 |
+
colors = {"Cluster {}".format(i): random_color() for i in range(len(clusters))}
|
29 |
+
for i, cluster in enumerate(clusters):
|
30 |
+
for sp in cluster:
|
31 |
+
doc.spans["sc"] += [doc.char_span(sp[0], sp[1], "Cluster {}".format(i))]
|
32 |
+
return displacy.render(doc, style="span", options={"colors": colors}, page=True)
|
33 |
+
|
34 |
+
|
35 |
+
iface = gr.Interface(fn=corefer,
|
36 |
+
inputs=gr.Textbox(label="Enter Text To Corefer", lines=2, value=default),
|
37 |
+
outputs="html", allow_flagging="never")
|
38 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
spacy==3.4.1
|
2 |
+
fastcoref==2.1.1
|