Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -119,194 +119,6 @@ st.subheader("")
|
|
119 |
|
120 |
|
121 |
|
122 |
-
#caching the models in the dictionary
|
123 |
-
@st.cache(allow_output_mutation=True, show_spinner=False)
|
124 |
-
def load_sparknlp_models():
|
125 |
-
ner_models_list= ["nerdl_fewnerd_100d", "ner_conll_elmo", "ner_mit_movie_complex_distilbert_base_cased", "ner_conll_albert_large_uncased", "onto_100"]
|
126 |
-
embeddings_list= ["glove_100d", "elmo", "distilbert_base_cased", "albert_large_uncased", "glove_100d_for_onto"]
|
127 |
|
128 |
|
129 |
-
documentAssembler = DocumentAssembler()\
|
130 |
-
.setInputCol("text")\
|
131 |
-
.setOutputCol("document")
|
132 |
-
|
133 |
-
sentenceDetector= SentenceDetector()\
|
134 |
-
.setInputCols(["document"])\
|
135 |
-
.setOutputCol("sentence")
|
136 |
-
|
137 |
-
tokenizer = Tokenizer()\
|
138 |
-
.setInputCols(["sentence"])\
|
139 |
-
.setOutputCol("token")
|
140 |
-
|
141 |
-
ner_converter= NerConverter()\
|
142 |
-
.setInputCols(["document", "token", "ner"])\
|
143 |
-
.setOutputCol("ner_chunk")
|
144 |
-
|
145 |
-
model_dict= {
|
146 |
-
'documentAssembler': documentAssembler,
|
147 |
-
'sentenceDetector': sentenceDetector,
|
148 |
-
'tokenizer': tokenizer,
|
149 |
-
'ner_converter': ner_converter
|
150 |
-
}
|
151 |
-
|
152 |
-
for embeddings_name, ner_model_name in zip(embeddings_list, ner_models_list):
|
153 |
-
|
154 |
-
try:
|
155 |
-
if embeddings_name=="glove_100d":
|
156 |
-
model_dict[embeddings_name]= WordEmbeddingsModel.pretrained(embeddings_name, "en")\
|
157 |
-
.setInputCols(["sentence", "token"])\
|
158 |
-
.setOutputCol("embeddings")
|
159 |
-
|
160 |
-
elif embeddings_name=="elmo":
|
161 |
-
model_dict[embeddings_name]= ElmoEmbeddings.pretrained(embeddings_name, "en")\
|
162 |
-
.setInputCols(["token", "document"])\
|
163 |
-
.setOutputCol("embeddings")\
|
164 |
-
.setPoolingLayer("elmo")
|
165 |
-
|
166 |
-
elif embeddings_name=="distilbert_base_cased":
|
167 |
-
model_dict[embeddings_name]= DistilBertEmbeddings\
|
168 |
-
.pretrained(embeddings_name, 'en')\
|
169 |
-
.setInputCols(["token", "document"])\
|
170 |
-
.setOutputCol("embeddings")
|
171 |
-
|
172 |
-
elif embeddings_name=="albert_large_uncased":
|
173 |
-
model_dict[embeddings_name]= AlbertEmbeddings\
|
174 |
-
.pretrained(embeddings_name, 'en')\
|
175 |
-
.setInputCols(["document", "token"])\
|
176 |
-
.setOutputCol("embeddings")
|
177 |
-
|
178 |
-
elif embeddings_name=="glove_100d_for_onto":
|
179 |
-
model_dict[embeddings_name]= WordEmbeddingsModel.pretrained("glove_100d", "en")\
|
180 |
-
.setInputCols(["sentence", "token"])\
|
181 |
-
.setOutputCol("embeddings")
|
182 |
-
|
183 |
-
|
184 |
-
model_dict[ner_model_name]= NerDLModel.pretrained(ner_model_name, "en")\
|
185 |
-
.setInputCols(["document", "token", "embeddings"])\
|
186 |
-
.setOutputCol("ner")
|
187 |
-
|
188 |
-
|
189 |
-
except:
|
190 |
-
pass
|
191 |
-
return model_dict
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
placeholder_= st.empty()
|
196 |
-
placeholder_.info("If you are launching the app for the first time, it may take some time (approximately 1 minute) for SparkNLP models to load...")
|
197 |
-
nlp_dict= load_sparknlp_models()
|
198 |
-
placeholder_.empty()
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
if selected_model=="ner_conll_albert_large_uncased":
|
204 |
-
text= st.text_input("Type here your text and press enter to run:", value="Mark Knopfler was born in Glasgow, Scotland. He is a British singer-songwriter, guitarist, and record producer. He became known as the lead guitarist, singer and songwriter of the rock band Dire Straits.")
|
205 |
-
|
206 |
-
elif selected_model=="ner_mit_movie_complex_distilbert_base_cased":
|
207 |
-
text= st.text_input("Type here your text and press enter to run:", value="It's only appropriate that Solaris, Russian filmmaker Andrei Tarkovsky's psychological sci-fi classic from 1972, contains an equally original and mind-bending score. Solaris explores the inadequacies of time and memory on an enigmatic planet below a derelict space station. To reinforce the film's chilling setting, Tarkovsky commissioned composer Eduard Artemiev to construct an electronic soundscape reflecting planet Solaris' amorphous and mysterious surface")
|
208 |
-
|
209 |
-
elif selected_model=="ner_conll_elmo":
|
210 |
-
text= st.text_input("Type here your text and press enter to run: ", value="Tottenham Hotspur Football Club, commonly referred to as Tottenham or Spurs, is an English professional football club based in Tottenham, London, that competes in the Premier League, the top flight of English football.")
|
211 |
-
|
212 |
-
elif selected_model=="onto_100":
|
213 |
-
text= st.text_input("Type here your text and press enter to run: ", value="William Henry Gates III (born October 28, 1955) is an American business magnate, software developer, investor, and philanthropist. He is best known as the co-founder of Microsoft Corporation. During his career at Microsoft, Gates held the positions of chairman, chief executive officer (CEO), president and chief software architect, while also being the largest individual shareholder until May 2014. He is one of the best-known entrepreneurs and pioneers of the microcomputer revolution of the 1970s and 1980s. Born and raised in Seattle, Washington, Gates co-founded Microsoft with childhood friend Paul Allen in 1975, in Albuquerque, New Mexico; it went on to become the world's largest personal computer software company. Gates led the company as chairman and CEO until stepping down as CEO in January 2000, but he remained chairman and became chief software architect.")
|
214 |
-
|
215 |
-
else:
|
216 |
-
text= st.text_input("Type here your text and press enter to run:", value="12 Corazones ('12 Hearts') is Spanish-language dating game show produced in the United States for the television network Telemundo since January 2005, based on its namesake Argentine TV show format. The show is filmed in Los Angeles and revolves around the twelve Zodiac signs that identify each contestant. In 2008, Ho filmed a cameo in the Steven Spielberg feature film The Cloverfield Paradox, as a news pundit.")
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
def build_pipeline(text, model_name=selected_model):
|
221 |
-
|
222 |
-
base_pipeline= Pipeline(stages=[
|
223 |
-
nlp_dict["documentAssembler"],
|
224 |
-
nlp_dict["sentenceDetector"],
|
225 |
-
nlp_dict["tokenizer"]
|
226 |
-
])
|
227 |
-
|
228 |
-
fewnerd_pipeline= Pipeline(stages=[
|
229 |
-
base_pipeline,
|
230 |
-
nlp_dict["glove_100d"],
|
231 |
-
nlp_dict[model_name],
|
232 |
-
nlp_dict["ner_converter"]
|
233 |
-
])
|
234 |
-
|
235 |
-
elmo_pipeline= Pipeline(stages=[
|
236 |
-
base_pipeline,
|
237 |
-
nlp_dict["elmo"],
|
238 |
-
nlp_dict[model_name],
|
239 |
-
nlp_dict["ner_converter"]
|
240 |
-
])
|
241 |
-
|
242 |
-
movie_pipeline= Pipeline(stages=[
|
243 |
-
base_pipeline,
|
244 |
-
nlp_dict["distilbert_base_cased"],
|
245 |
-
nlp_dict[model_name],
|
246 |
-
nlp_dict["ner_converter"]
|
247 |
-
])
|
248 |
-
|
249 |
-
albert_pipeline= Pipeline(stages=[
|
250 |
-
base_pipeline,
|
251 |
-
nlp_dict["albert_large_uncased"],
|
252 |
-
nlp_dict[model_name],
|
253 |
-
nlp_dict["ner_converter"]
|
254 |
-
])
|
255 |
-
|
256 |
-
onto_pipeline= Pipeline(stages=[
|
257 |
-
base_pipeline,
|
258 |
-
nlp_dict["glove_100d_for_onto"],
|
259 |
-
nlp_dict[model_name],
|
260 |
-
nlp_dict["ner_converter"]
|
261 |
-
])
|
262 |
-
|
263 |
-
|
264 |
-
empty_df = spark.createDataFrame([[""]]).toDF("text")
|
265 |
-
|
266 |
-
if model_name=="nerdl_fewnerd_100d":
|
267 |
-
pipeline_model= fewnerd_pipeline.fit(empty_df)
|
268 |
-
|
269 |
-
elif model_name=="ner_conll_elmo":
|
270 |
-
pipeline_model= elmo_pipeline.fit(empty_df)
|
271 |
-
|
272 |
-
elif model_name=="ner_mit_movie_complex_distilbert_base_cased":
|
273 |
-
pipeline_model= movie_pipeline.fit(empty_df)
|
274 |
-
|
275 |
-
elif model_name=="ner_conll_albert_large_uncased":
|
276 |
-
pipeline_model= albert_pipeline.fit(empty_df)
|
277 |
-
|
278 |
-
elif model_name=="onto_100":
|
279 |
-
pipeline_model= onto_pipeline.fit(empty_df)
|
280 |
-
|
281 |
-
|
282 |
-
text_df= spark.createDataFrame(pd.DataFrame({"text": [text]}))
|
283 |
-
result= pipeline_model.transform(text_df).toPandas()
|
284 |
-
|
285 |
-
return result
|
286 |
-
|
287 |
-
#placeholder for warning
|
288 |
-
placeholder= st.empty()
|
289 |
-
placeholder.info("Processing...")
|
290 |
-
|
291 |
-
result= build_pipeline(text)
|
292 |
-
placeholder.empty()
|
293 |
-
|
294 |
-
df= pd.DataFrame({"ner_chunk": result["ner_chunk"].iloc[0]})
|
295 |
-
|
296 |
-
labels_set = set()
|
297 |
-
for i in df['ner_chunk'].values:
|
298 |
-
labels_set.add(i[4]['entity'])
|
299 |
-
labels_set = list(labels_set)
|
300 |
-
|
301 |
-
labels = st.sidebar.multiselect(
|
302 |
-
"NER Labels", options=labels_set, default=list(labels_set)
|
303 |
-
)
|
304 |
-
|
305 |
-
show_html2(text, df, labels, "Text annotated with identified Named Entities")
|
306 |
-
|
307 |
-
try_link="""<a href="https://colab.research.google.com/github/JohnSnowLabs/spark-nlp-workshop/blob/master/tutorials/Certification_Trainings/Public/3.SparkNLP_Pretrained_Models.ipynb"><img src="https://colab.research.google.com/assets/colab-badge.svg" style="zoom: 1.3" alt="Open In Colab"/></a>"""
|
308 |
-
st.sidebar.title('')
|
309 |
-
st.sidebar.markdown('Try it yourself:')
|
310 |
-
st.sidebar.markdown(try_link, unsafe_allow_html=True)
|
311 |
-
|
312 |
|
|
|
119 |
|
120 |
|
121 |
|
|
|
|
|
|
|
|
|
|
|
122 |
|
123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
|