Spaces:
Runtime error
Runtime error
Commit
•
008231d
1
Parent(s):
5286d26
Create app.py file for FastCoref Demo
Browse files
app.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import spacy
|
3 |
+
from spacy import displacy
|
4 |
+
from spacy.tokens import Span
|
5 |
+
from random import randint
|
6 |
+
from fastcoref import LingMessCoref
|
7 |
+
|
8 |
+
model = LingMessCoref()
|
9 |
+
nlp = spacy.blank("en")
|
10 |
+
|
11 |
+
def corefer(text):
|
12 |
+
preds = model.predict(texts=[text])
|
13 |
+
clusters = preds[0].get_clusters(as_strings=False)
|
14 |
+
doc = nlp(text)
|
15 |
+
doc.spans["sc"] = []
|
16 |
+
colors = {"Cluster {}".format(i):'#%06X' % randint(0, 0xFFFFFF) for i in range(len(clusters))}
|
17 |
+
for i, cluster in enumerate(clusters):
|
18 |
+
for sp in cluster:
|
19 |
+
doc.spans["sc"] += [doc.char_span(sp[0], sp[1], "Cluster {}".format(i))]
|
20 |
+
return displacy.render(doc, style="span", options= {"colors":colors }, page=True )
|
21 |
+
|
22 |
+
|
23 |
+
iface = gr.Interface(fn=corefer,
|
24 |
+
inputs=gr.Textbox(label="Enter Text To Corefer with FastCoref", lines=2, value="We are so happy to see you using our coref package. This package is very fast!"),
|
25 |
+
outputs="html")
|
26 |
+
iface.launch()
|