File size: 1,194 Bytes
008231d
 
 
 
 
 
 
 
 
 
2ffba81
 
008231d
 
 
 
 
 
 
 
 
 
 
 
 
2ffba81
008231d
 
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
import gradio as gr
import spacy
from spacy import displacy
from spacy.tokens import Span
from random import randint
from fastcoref import LingMessCoref

model = LingMessCoref()
nlp = spacy.blank("en")

default = "Lionel Messi has won a record seven Ballon d'Or awards. He signed for Paris Saint-Germain in August 2021. “I would like to thank my family” said the Argentinian footballer. Messi holds the records for most goals in La Liga. Paris Saint-Germain hopes he will do the same in Ligue 1."

def corefer(text):
    preds = model.predict(texts=[text])
    clusters = preds[0].get_clusters(as_strings=False)
    doc = nlp(text)
    doc.spans["sc"] = []
    colors = {"Cluster {}".format(i):'#%06X' % randint(0, 0xFFFFFF) for i in range(len(clusters))}
    for i, cluster in enumerate(clusters):
      for sp in cluster:
        doc.spans["sc"] += [doc.char_span(sp[0], sp[1], "Cluster {}".format(i))]
    return displacy.render(doc, style="span", options= {"colors":colors }, page=True )


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