File size: 1,247 Bytes
4196711
 
 
 
 
f8f74cc
87414fd
f8f74cc
d13d693
4196711
f8f74cc
 
 
 
 
0024ab7
4196711
 
f8f74cc
 
eb75c2d
 
f8f74cc
 
eb75c2d
 
 
5cb5b27
4196711
 
 
 
454eaa3
4196711
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import gradio as gr
import spacy
from spacy import displacy
from spacy.tokens import Span
from random import randint
import spacy
import random
from fastcoref import spacy_component
nlp = spacy.blank("th")

nlp.add_pipe(
   "fastcoref", 
   config={'model_architecture': 'FCoref','model_path': 'pythainlp/han-coref-v1.0'}
)

default = "แกว่าเราเข้มแข็งมากพอที่จะเห็นแกไปน่ารักกับใครหรอ"

def corefer(text):
    doc = nlp(text)
    tags = doc._.coref_clusters
    title=None
    dic_ents = {"text":text,"ents":[],"title":title}
    _tag=[str(i) for i in list(range(len(tags)))]
    for i,tag in enumerate(tags):
        for s,e in tag:
            dic_ents["ents"].append({"start": s, "end": e, "label": _tag[i]})
    colors={i:"#"+''.join([random.choice('0123456789ABCDEF') for j in range(6)]) for i in _tag} # thank https://stackoverflow.com/a/50218895
    return displacy.render(dic_ents, manual=True, style="ent",options=colors, page=True)


iface = gr.Interface(fn=corefer, 
                     inputs=gr.Textbox(label="Enter Text To Corefer with FastCoref", lines=2, value=default), 
                     outputs="html").queue()
iface.launch()