File size: 1,177 Bytes
4196711
 
 
 
 
d13d693
4196711
d13d693
 
4196711
0024ab7
4196711
 
 
 
eb75c2d
 
 
454eaa3
eb75c2d
 
 
 
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
import gradio as gr
import spacy
from spacy import displacy
from spacy.tokens import Span
from random import randint
from fastcoref import FCoref

model = FCoref("pythainlp/han-coref-v1.0")
nlp = spacy.blank("th")

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

def corefer(text):
    preds = model.predict(texts=[text])
    clusters = preds[0].get_clusters(as_strings=False)
    title=None
    dic_ents = {"text":text,"ents":[],"title":title}
    _tag=[str(i) for i in list(range(len(clusters)))]
    for i,tag in enumerate(clusters):
        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)


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