lterriel commited on
Commit
96f9554
Β·
1 Parent(s): 45e0546

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -37
app.py CHANGED
@@ -24,6 +24,12 @@ ENTITY_COLORS = {
24
  "ORG": "#f39c12",
25
  "PER": "#3498db"
26
  }
 
 
 
 
 
 
27
 
28
  # Read configuration
29
  with open(CONFIG_FILE, mode="r") as json_file:
@@ -123,59 +129,68 @@ def main():
123
  plain = "\n".join(sentences)
124
  col2.text_area("Plain Text View (read-only)", value=plain, height=500, disabled=True)
125
  flag_view = True
 
126
 
127
  if flag_view:
128
  streamlit.write("## βš™οΈ Configure NER pipeline and options:")
129
  streamlit.write("⚠️ Using Bert based model and/or linking may increase considerably the processing time.")
130
  # Normaly: Load from PIP or directory (install issues with HF spaces)
131
- models = ["fr_ner4archives_v3_with_vectors", "fr_ner4archives_v3", "fr_ner4archives_V3_camembert_base"]
132
- #for pipe in spacy.info()["pipelines"]:
133
- # models.append(pipe)
134
  option = streamlit.selectbox(
135
  'Choose a NER model you want to apply in the list: ',
136
- models)
 
137
  model = option
138
- map_model = {
139
- "fr_ner4archives_V3_camembert_base": "https://huggingface.co/ner4archives/fr_ner4archives_V3_camembert_base/resolve/main/fr_ner4archives_V3_camembert_base-any-py3-none-any.whl#sha256=919aca257e4b2342a3773e7dcc3442b30f2ac50c4a3466d5ecda5e0e337ea396",
140
- "fr_ner4archives_v3": "https://huggingface.co/ner4archives/fr_ner4archives_v3_default/resolve/main/fr_ner4archives_v3_default-any-py3-none-any.whl#sha256=040a5c67a7e5a14e0125b2600e6aff1323f8ba9d800d5ab49446faeaf2dad6e6",
141
- "fr_ner4archives_v3_with_vectors":"https://huggingface.co/ner4archives/fr_ner4archives_v3_with_vectors/resolve/main/fr_ner4archives_v3_with_vectors-any-py3-none-any.whl#sha256=4c938618ce84a919008d17822980cfd914f9ad8f73231a64cd1ffcaa80ab09f2"
142
- }
143
- try:
144
- spacy.load(model)
145
- except:
146
- streamlit.write(f"Download model: {model} in progress...")
147
- subprocess.run(["pip", "install", map_model[model]])
148
  if model != "":
149
- flag_model = True
150
-
151
- gpu = streamlit.checkbox('Check to use GPU (if available)', value=False)
152
- gpu_icon = "❌"
153
- if gpu:
154
- spacy.prefer_gpu()
155
- gpu_icon = "βœ…οΈ"
156
- else:
157
- spacy.require_cpu()
158
-
159
- if is_entity_fishing_online():
160
- streamlit.write("Entity-fishing server status: 🟒 (you can use linking feature)")
161
- linking = streamlit.checkbox('Check to apply named entity linking (entity-fishing component)',
162
- value=False)
163
- linkingicon = "βœ…οΈ"
164
- if linking is False:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
  linkingicon = "❌"
166
- else:
167
- streamlit.write("Entity-fishing server status: πŸ”΄ (you can't use linking feature)")
168
- linking = False
169
- linkingicon = "❌"
170
- streamlit.write("#### Actual Parameters:")
171
- streamlit.write(f'- NER model selected: {option}\n - Linking activated: {linkingicon} - GPU activated: {gpu_icon}')
172
 
173
  # Launch NER process:
174
  if flag_model:
175
  if streamlit.button('Launch'):
176
  plain = "\n".join(sentences)
177
  with streamlit.spinner('Initialize NER...'):
178
- nlp = spacy.load(model)
179
  nlp.max_length = 5000000
180
  if linking:
181
  nlp.add_pipe('entityfishing',
 
24
  "ORG": "#f39c12",
25
  "PER": "#3498db"
26
  }
27
+ MAP_MODELS = {
28
+ "":"",
29
+ "fr_ner4archives_V3_camembert_base": "https://huggingface.co/ner4archives/fr_ner4archives_V3_camembert_base/resolve/main/fr_ner4archives_V3_camembert_base-any-py3-none-any.whl#sha256=919aca257e4b2342a3773e7dcc3442b30f2ac50c4a3466d5ecda5e0e337ea396",
30
+ "fr_ner4archives_v3_default": "https://huggingface.co/ner4archives/fr_ner4archives_v3_default/resolve/main/fr_ner4archives_v3_default-any-py3-none-any.whl#sha256=040a5c67a7e5a14e0125b2600e6aff1323f8ba9d800d5ab49446faeaf2dad6e6",
31
+ "fr_ner4archives_v3_with_vectors":"https://huggingface.co/ner4archives/fr_ner4archives_v3_with_vectors/resolve/main/fr_ner4archives_v3_with_vectors-any-py3-none-any.whl#sha256=4c938618ce84a919008d17822980cfd914f9ad8f73231a64cd1ffcaa80ab09f2"
32
+ }
33
 
34
  # Read configuration
35
  with open(CONFIG_FILE, mode="r") as json_file:
 
129
  plain = "\n".join(sentences)
130
  col2.text_area("Plain Text View (read-only)", value=plain, height=500, disabled=True)
131
  flag_view = True
132
+ flag_model = False
133
 
134
  if flag_view:
135
  streamlit.write("## βš™οΈ Configure NER pipeline and options:")
136
  streamlit.write("⚠️ Using Bert based model and/or linking may increase considerably the processing time.")
137
  # Normaly: Load from PIP or directory (install issues with HF spaces)
138
+ models = [str(key) for key in MAP_MODELS.keys()]
 
 
139
  option = streamlit.selectbox(
140
  'Choose a NER model you want to apply in the list: ',
141
+ models,
142
+ index=0)
143
  model = option
144
+ model_loaded = None
 
 
 
 
 
 
 
 
 
145
  if model != "":
146
+ try:
147
+ model_loaded = spacy.load(model)
148
+ flag_model = True
149
+ streamlit.write(f"{model} is available locally.")
150
+ except:
151
+ placeholder = streamlit.empty()
152
+ button = streamlit.button(f"Download model: {model}")
153
+ with placeholder.container():
154
+ if button:
155
+ streamlit.write(f"Download model: {model} in progress...")
156
+ p1 = subprocess.Popen(["pip", "install", MAP_MODELS[model]])
157
+ o = p1.wait()
158
+ if o == 0:
159
+ streamlit.write(f"Download model: {model} done.")
160
+ model_loaded = spacy.load(model)
161
+ flag_model = True
162
+ streamlit.write(f"{model} is available locally.")
163
+ placeholder.empty()
164
+
165
+ if flag_model:
166
+ gpu = streamlit.checkbox('Check to use GPU (if available)', value=False)
167
+ gpu_icon = "❌"
168
+ if gpu:
169
+ spacy.prefer_gpu()
170
+ gpu_icon = "βœ…οΈ"
171
+ else:
172
+ spacy.require_cpu()
173
+
174
+ if is_entity_fishing_online():
175
+ streamlit.write("Entity-fishing server status: 🟒 (you can use linking feature)")
176
+ linking = streamlit.checkbox('Check to apply named entity linking (entity-fishing component)',
177
+ value=False)
178
+ linkingicon = "βœ…οΈ"
179
+ if linking is False:
180
+ linkingicon = "❌"
181
+ else:
182
+ streamlit.write("Entity-fishing server status: πŸ”΄ (you can't use linking feature)")
183
+ linking = False
184
  linkingicon = "❌"
185
+ streamlit.write("#### Actual Parameters:")
186
+ streamlit.write(f'- NER model selected: {option}\n - Linking activated: {linkingicon} - GPU activated: {gpu_icon}')
 
 
 
 
187
 
188
  # Launch NER process:
189
  if flag_model:
190
  if streamlit.button('Launch'):
191
  plain = "\n".join(sentences)
192
  with streamlit.spinner('Initialize NER...'):
193
+ nlp = model_loaded
194
  nlp.max_length = 5000000
195
  if linking:
196
  nlp.add_pipe('entityfishing',