Han-Coref / app.py
wannaphong's picture
Update app.py
87414fd
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()