Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,131 +7,63 @@ from spacy.tokens import Span
|
|
| 7 |
|
| 8 |
nlp = spacy.load("en_core_web_md")
|
| 9 |
|
| 10 |
-
#Text 1
|
| 11 |
-
def process_text(text1):
|
| 12 |
-
d = load(text1)
|
| 13 |
-
return [
|
| 14 |
-
(for ent in doc1.ents:
|
| 15 |
-
print(ent.text, ent.label_))
|
| 16 |
-
(for ent in doc1.ents:
|
| 17 |
-
print(ent.label_, spacy.explain(ent.label_)))
|
| 18 |
-
]
|
| 19 |
-
|
| 20 |
-
def load(text):
|
| 21 |
-
user_input = str(text.strip())
|
| 22 |
-
doc1 = nlp(user_input)
|
| 23 |
-
|
| 24 |
-
#Text 2
|
| 25 |
-
def entities(text2):
|
| 26 |
-
a = named_ents(text2)
|
| 27 |
-
return [print("patterns:", patterns)]
|
| 28 |
-
|
| 29 |
-
def named_ents(text):
|
| 30 |
-
pattern_list = []
|
| 31 |
-
for i in text.strip().split():
|
| 32 |
-
pattern_list.append(i)
|
| 33 |
-
|
| 34 |
-
patterns = list(nlp.pipe(pattern_list))
|
| 35 |
-
|
| 36 |
-
#Text 3
|
| 37 |
-
def run(text3):
|
| 38 |
-
b = pipe(text3)
|
| 39 |
-
return [
|
| 40 |
-
doc
|
| 41 |
-
print(nlp.pipe_names)]
|
| 42 |
-
|
| 43 |
-
def pipe(text):
|
| 44 |
-
matcher = PhraseMatcher(nlp.vocab)
|
| 45 |
-
#Create label for pattern
|
| 46 |
-
user_named = str(text.strip()) #gradio text box here to enter pattern label
|
| 47 |
-
matcher.add(user_named, patterns)
|
| 48 |
-
# Define the custom component
|
| 49 |
-
@Language.component("covid_component")
|
| 50 |
-
def covid_component_function(doc):
|
| 51 |
-
# Apply the matcher to the doc
|
| 52 |
-
matches = matcher(doc)
|
| 53 |
-
# Create a Span for each match and assign the label "ANIMAL"
|
| 54 |
-
spans = [Span(doc, start, end, label=user_named) for match_id, start, end in matches]
|
| 55 |
-
# Overwrite the doc.ents with the matched spans
|
| 56 |
-
doc.ents = spans
|
| 57 |
-
return doc
|
| 58 |
-
# Add the component to the pipeline after the "ner" component
|
| 59 |
-
nlp.add_pipe((user_named + "component"), after="ner")
|
| 60 |
-
print(nlp.pipe_names)
|
| 61 |
-
|
| 62 |
-
#Text 4
|
| 63 |
-
|
| 64 |
-
def test(text4):
|
| 65 |
-
c = new_sample(text4)
|
| 66 |
-
return [
|
| 67 |
-
print([(ent.text, ent.label_) for ent in apply_doc.ents])
|
| 68 |
-
Counter(labels)]
|
| 69 |
-
|
| 70 |
|
| 71 |
-
def new_sample(text):
|
| 72 |
-
user_doc = str(text).strip())
|
| 73 |
-
apply_doc = nlp(user_doc)
|
| 74 |
-
print([(ent.text, ent.label_) for ent in apply_doc.ents])
|
| 75 |
-
#Count total mentions of label COVID in the 3rd document
|
| 76 |
-
from collections import Counter
|
| 77 |
-
labels = [ent.label_ for ent in apply_doc.ents]
|
| 78 |
-
Counter(labels)
|
| 79 |
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
|
| 103 |
#Instantiate PhraseMatcher
|
| 104 |
-
|
| 105 |
|
| 106 |
#Create label for pattern
|
| 107 |
-
|
| 108 |
-
|
| 109 |
|
| 110 |
# Define the custom component
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
|
| 121 |
# Add the component to the pipeline after the "ner" component
|
| 122 |
-
|
| 123 |
-
|
| 124 |
|
| 125 |
|
| 126 |
#Verify that your model now detects all specified mentions of Covid on another text
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
|
| 131 |
#Count total mentions of label COVID in the 3rd document
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
|
| 136 |
iface = gr.Interface(
|
| 137 |
process_text,
|
|
|
|
| 7 |
|
| 8 |
nlp = spacy.load("en_core_web_md")
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
|
| 13 |
+
user_input = input(str(""))
|
| 14 |
+
doc1 = nlp(user_input)
|
| 15 |
|
| 16 |
+
print list of entities captured by pertained model
|
| 17 |
+
for ent in doc1.ents:
|
| 18 |
+
print(ent.text, ent.label_)
|
| 19 |
|
| 20 |
+
inspect labels and their meaning
|
| 21 |
+
for ent in doc1.ents:
|
| 22 |
+
print(ent.label_, spacy.explain(ent.label_))
|
| 23 |
|
| 24 |
+
Use PhraseMatcher to find all references of interest
|
| 25 |
+
Define the different references to Covid
|
| 26 |
+
user_entries = input(str("")) #gradio text box here to enter sample terms
|
| 27 |
+
pattern_list = []
|
| 28 |
|
| 29 |
+
for i in user_entries.strip().split():
|
| 30 |
+
pattern_list.append(i)
|
| 31 |
|
| 32 |
+
patterns = list(nlp.pipe(pattern_list))
|
| 33 |
+
print("patterns:", patterns)
|
| 34 |
|
| 35 |
#Instantiate PhraseMatcher
|
| 36 |
+
matcher = PhraseMatcher(nlp.vocab)
|
| 37 |
|
| 38 |
#Create label for pattern
|
| 39 |
+
user_named = input(str("").strip()) #gradio text box here to enter pattern label
|
| 40 |
+
matcher.add(user_named, patterns)
|
| 41 |
|
| 42 |
# Define the custom component
|
| 43 |
+
@Language.component("covid_component")
|
| 44 |
+
def covid_component_function(doc):
|
| 45 |
+
#Apply the matcher to the doc
|
| 46 |
+
matches = matcher(doc)
|
| 47 |
+
#Create a Span for each match and assign the label
|
| 48 |
+
spans = [Span(doc, start, end, label=user_named) for match_id, start, end in matches]
|
| 49 |
+
# Overwrite the doc.ents with the matched spans
|
| 50 |
+
doc.ents = spans
|
| 51 |
+
return doc
|
| 52 |
|
| 53 |
# Add the component to the pipeline after the "ner" component
|
| 54 |
+
nlp.add_pipe((user_named + "component"), after="ner")
|
| 55 |
+
print(nlp.pipe_names)
|
| 56 |
|
| 57 |
|
| 58 |
#Verify that your model now detects all specified mentions of Covid on another text
|
| 59 |
+
user_doc = input(str("").strip())
|
| 60 |
+
apply_doc = nlp(user_doc)
|
| 61 |
+
print([(ent.text, ent.label_) for ent in apply_doc.ents])
|
| 62 |
|
| 63 |
#Count total mentions of label COVID in the 3rd document
|
| 64 |
+
from collections import Counter
|
| 65 |
+
labels = [ent.label_ for ent in apply_doc.ents]
|
| 66 |
+
Counter(labels)
|
| 67 |
|
| 68 |
iface = gr.Interface(
|
| 69 |
process_text,
|