Han-Coref / app.py
wannaphong's picture
Update app.py
0024ab7
raw
history blame
1.07 kB
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)
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()