Spaces:
Runtime error
Runtime error
Commit
·
b781b11
1
Parent(s):
c16b594
first commit
Browse files- app.py +59 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import spacy
|
4 |
+
import concepcy
|
5 |
+
|
6 |
+
LIST_RELATIONS = ["RelatedTo", "FormOf", "IsA", "PartOf", "HasA", "UserFor", "CapableOf", "AtLocation", "Causes",
|
7 |
+
"HasSubevent", "HasFirstSubevent", "HasLastSubevent", "HasPrerequisite", "HasProperty",
|
8 |
+
"MotivatedByGoal", "ObstructedBy", "Desires", "CreatedBy", "Synonym", "Antonym", "DistinctFrom",
|
9 |
+
"DerivedFrom", "SymbolOf", "DefinedAs", "MannerOf", "LocatedNear", "HasContext", "SimilarTo",
|
10 |
+
"EtymologicallyRelatedTo", "EtymologicallyDerivedFrom", "CausesDesire", "MadeOf", "ReceivesAction",
|
11 |
+
"ExternalURL"]
|
12 |
+
|
13 |
+
nlp = spacy.load("en_core_web_sm")
|
14 |
+
nlp.add_pipe(
|
15 |
+
"concepcy",
|
16 |
+
config={
|
17 |
+
"relations_of_interest": LIST_RELATIONS,
|
18 |
+
"filter_missing_text": True,
|
19 |
+
"filter_edge_weight": 2,
|
20 |
+
}
|
21 |
+
)
|
22 |
+
|
23 |
+
|
24 |
+
def greet(text, relations, to_enrich):
|
25 |
+
doc = nlp(text)
|
26 |
+
|
27 |
+
outs0, outs1 = [], []
|
28 |
+
if "document" in to_enrich:
|
29 |
+
outs0 = []
|
30 |
+
for relation in relations:
|
31 |
+
for r in doc._.get(relation.lower()).values():
|
32 |
+
outs0.extend([[relation, elt["text"]] for elt in r])
|
33 |
+
|
34 |
+
if "token" in to_enrich:
|
35 |
+
|
36 |
+
for token in doc:
|
37 |
+
for relation in relations:
|
38 |
+
rels = token._.get(relation.lower())
|
39 |
+
if len(rels) > 0:
|
40 |
+
print(rels)
|
41 |
+
print([[token.text, relation, r["text"]] for r in rels])
|
42 |
+
outs1.extend([[token.text, relation, r["text"]] for r in rels])
|
43 |
+
|
44 |
+
return pd.DataFrame(outs0, columns=["relation", "text"]), pd.DataFrame(outs1, columns=["word", "relation", "text"])
|
45 |
+
|
46 |
+
|
47 |
+
iface = gr.Interface(
|
48 |
+
fn=greet,
|
49 |
+
inputs=[
|
50 |
+
gr.Textbox(placeholder="Enter sentence here...", lines=5, value="I love eating pizzas"),
|
51 |
+
gr.CheckboxGroup(choices=LIST_RELATIONS, value=["IsA"]),
|
52 |
+
gr.CheckboxGroup(choices=["document", "token"], value=["document"])
|
53 |
+
],
|
54 |
+
outputs=[
|
55 |
+
gr.Dataframe(headers=["relation", "text"], label="Document-level relations"),
|
56 |
+
gr.Dataframe(headers=["word", "relation", "text"], label="Token-level relations")
|
57 |
+
]
|
58 |
+
)
|
59 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
concepcy==0.1.0
|
2 |
+
spacy==3.3.0
|
3 |
+
https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.3.0/en_core_web_sm-3.3.0.tar.gz#egg=en_core_web_sm
|
4 |
+
pandas
|
5 |
+
|