FastCoref / app.py
pythiccoder's picture
Update app.py
2ffba81
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()