chicham commited on
Commit
54ab090
1 Parent(s): 03bec6d

Pre-commit configuration (#1)

Browse files
Files changed (3) hide show
  1. .pre-commit-config.yaml +0 -6
  2. README.md +10 -10
  3. app.py +26 -17
.pre-commit-config.yaml CHANGED
@@ -52,12 +52,6 @@ repos:
52
  args: [--include="(src|tests|scripts)/"]
53
  require_serial: true
54
  types_or: [python, pyi]
55
- - repo: https://github.com/econchick/interrogate
56
- rev: 1.5.0
57
- hooks:
58
- - id: interrogate
59
- exclude: ^(docs/conf.py|setup.py|scripts/)
60
- args: [--config=setup.cfg, --quiet]
61
  - repo: https://github.com/pre-commit/mirrors-prettier
62
  rev: v2.5.1
63
  hooks:
 
52
  args: [--include="(src|tests|scripts)/"]
53
  require_serial: true
54
  types_or: [python, pyi]
 
 
 
 
 
 
55
  - repo: https://github.com/pre-commit/mirrors-prettier
56
  rev: v2.5.1
57
  hooks:
README.md CHANGED
@@ -10,28 +10,28 @@ pinned: false
10
 
11
  # Configuration
12
 
13
- `title`: _string_
14
  Display title for the Space
15
 
16
- `emoji`: _string_
17
  Space emoji (emoji-only character allowed)
18
 
19
- `colorFrom`: _string_
20
  Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
21
 
22
- `colorTo`: _string_
23
  Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
24
 
25
- `sdk`: _string_
26
  Can be either `gradio` or `streamlit`
27
 
28
- `sdk_version` : _string_
29
- Only applicable for `streamlit` SDK.
30
  See [doc](https://hf.co/docs/hub/spaces) for more info on supported versions.
31
 
32
- `app_file`: _string_
33
- Path to your main application file (which contains either `gradio` or `streamlit` Python code).
34
  Path is relative to the root of the repository.
35
 
36
- `pinned`: _boolean_
37
  Whether the Space stays on top of your list.
 
10
 
11
  # Configuration
12
 
13
+ `title`: _string_
14
  Display title for the Space
15
 
16
+ `emoji`: _string_
17
  Space emoji (emoji-only character allowed)
18
 
19
+ `colorFrom`: _string_
20
  Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
21
 
22
+ `colorTo`: _string_
23
  Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
24
 
25
+ `sdk`: _string_
26
  Can be either `gradio` or `streamlit`
27
 
28
+ `sdk_version` : _string_
29
+ Only applicable for `streamlit` SDK.
30
  See [doc](https://hf.co/docs/hub/spaces) for more info on supported versions.
31
 
32
+ `app_file`: _string_
33
+ Path to your main application file (which contains either `gradio` or `streamlit` Python code).
34
  Path is relative to the root of the repository.
35
 
36
+ `pinned`: _boolean_
37
  Whether the Space stays on top of your list.
app.py CHANGED
@@ -1,19 +1,22 @@
1
  """Demo gradio app for some text/query augmentation."""
2
-
3
  from __future__ import annotations
4
- from collections import defaultdict
5
  import functools
 
6
  from itertools import chain
7
- from typing import Any, Callable, Mapping, Optional, Sequence, Tuple
 
 
 
8
 
9
  import attr
10
  import environ
11
  import fasttext # not working with python3.9
12
  import gradio as gr
 
13
  from transformers.pipelines import pipeline
14
  from transformers.pipelines.base import Pipeline
15
  from transformers.pipelines.token_classification import AggregationStrategy
16
- from tokenizers.pre_tokenizers import Whitespace
17
 
18
 
19
  def compose(*functions) -> Callable:
@@ -105,9 +108,8 @@ class AppConfig:
105
 
106
  @environ.config
107
  class NER:
108
- # general = environ.var(default="Davlan/xlm-roberta-base-ner-hrl")
109
  general = environ.var(
110
- default="asahi417/tner-xlm-roberta-large-uncased-wnut2017"
111
  )
112
 
113
  recipe = environ.var(default="adamlin/recipe-tag-model")
@@ -122,13 +124,13 @@ def predict(
122
  models: Models,
123
  query: str,
124
  categories: Sequence[str],
125
- supported_languages: Tuple[str, ...] = ("fr", "de"),
126
- ) -> Tuple[
127
  Mapping[str, float],
128
  str,
129
  Mapping[str, float],
130
- Sequence[Tuple[str, Optional[str]]],
131
- Sequence[Tuple[str, Optional[str]]],
132
  ]:
133
  """Predict from a textual query:
134
  - the language
@@ -146,7 +148,8 @@ def predict(
146
  @mapped(map)
147
  def format_label(prediction: Prediction) -> Prediction:
148
  return attr.evolve(
149
- prediction, label=prediction.label.replace("__label__", "")
 
150
  )
151
 
152
  def filter_labels(prediction: Prediction) -> bool:
@@ -183,8 +186,9 @@ def predict(
183
  return dict(zip(predictions["labels"], predictions["scores"]))
184
 
185
  def extract_entities(
186
- predict_fn: Callable, query: str
187
- ) -> Sequence[Tuple[str, Optional[str]]]:
 
188
  def get_entity(pred: Mapping[str, str]):
189
  return pred.get("entity", pred.get("entity_group", None))
190
 
@@ -195,7 +199,7 @@ def predict(
195
  res = tuple(
196
  chain.from_iterable(
197
  ((word, mapping[word]), (" ", None)) for word, _ in query_processed
198
- )
199
  )
200
  return res
201
 
@@ -211,7 +215,9 @@ def main():
211
  cfg: AppConfig = AppConfig.from_environ()
212
 
213
  def load_translation_models(
214
- sources: Sequence[str], target: str, models: Sequence[str]
 
 
215
  ) -> Pipeline:
216
  result = {
217
  src: pipeline(f"translation_{src}_to_{target}", models)
@@ -238,7 +244,8 @@ def main():
238
  ),
239
  classification=Predictor(
240
  load_fn=lambda: pipeline(
241
- "zero-shot-classification", model=cfg.classification.model
 
242
  ),
243
  predict_fn=lambda model, query, categories: model(query, categories),
244
  ),
@@ -256,7 +263,9 @@ def main():
256
 
257
  iface = gr.Interface(
258
  fn=lambda query, categories: predict(
259
- models, query.strip(), extract_commas_separated_values(categories)
 
 
260
  ),
261
  examples=[["gateau au chocolat paris"], ["Newyork LA flight"]],
262
  inputs=[
 
1
  """Demo gradio app for some text/query augmentation."""
 
2
  from __future__ import annotations
3
+
4
  import functools
5
+ from collections import defaultdict
6
  from itertools import chain
7
+ from typing import Any
8
+ from typing import Callable
9
+ from typing import Mapping
10
+ from typing import Sequence
11
 
12
  import attr
13
  import environ
14
  import fasttext # not working with python3.9
15
  import gradio as gr
16
+ from tokenizers.pre_tokenizers import Whitespace
17
  from transformers.pipelines import pipeline
18
  from transformers.pipelines.base import Pipeline
19
  from transformers.pipelines.token_classification import AggregationStrategy
 
20
 
21
 
22
  def compose(*functions) -> Callable:
 
108
 
109
  @environ.config
110
  class NER:
 
111
  general = environ.var(
112
+ default="asahi417/tner-xlm-roberta-large-uncased-wnut2017",
113
  )
114
 
115
  recipe = environ.var(default="adamlin/recipe-tag-model")
 
124
  models: Models,
125
  query: str,
126
  categories: Sequence[str],
127
+ supported_languages: tuple[str, ...] = ("fr", "de"),
128
+ ) -> tuple[
129
  Mapping[str, float],
130
  str,
131
  Mapping[str, float],
132
+ Sequence[tuple[str, str | None]],
133
+ Sequence[tuple[str, str | None]],
134
  ]:
135
  """Predict from a textual query:
136
  - the language
 
148
  @mapped(map)
149
  def format_label(prediction: Prediction) -> Prediction:
150
  return attr.evolve(
151
+ prediction,
152
+ label=prediction.label.replace("__label__", ""),
153
  )
154
 
155
  def filter_labels(prediction: Prediction) -> bool:
 
186
  return dict(zip(predictions["labels"], predictions["scores"]))
187
 
188
  def extract_entities(
189
+ predict_fn: Callable,
190
+ query: str,
191
+ ) -> Sequence[tuple[str, str | None]]:
192
  def get_entity(pred: Mapping[str, str]):
193
  return pred.get("entity", pred.get("entity_group", None))
194
 
 
199
  res = tuple(
200
  chain.from_iterable(
201
  ((word, mapping[word]), (" ", None)) for word, _ in query_processed
202
+ ),
203
  )
204
  return res
205
 
 
215
  cfg: AppConfig = AppConfig.from_environ()
216
 
217
  def load_translation_models(
218
+ sources: Sequence[str],
219
+ target: str,
220
+ models: Sequence[str],
221
  ) -> Pipeline:
222
  result = {
223
  src: pipeline(f"translation_{src}_to_{target}", models)
 
244
  ),
245
  classification=Predictor(
246
  load_fn=lambda: pipeline(
247
+ "zero-shot-classification",
248
+ model=cfg.classification.model,
249
  ),
250
  predict_fn=lambda model, query, categories: model(query, categories),
251
  ),
 
263
 
264
  iface = gr.Interface(
265
  fn=lambda query, categories: predict(
266
+ models,
267
+ query.strip(),
268
+ extract_commas_separated_values(categories),
269
  ),
270
  examples=[["gateau au chocolat paris"], ["Newyork LA flight"]],
271
  inputs=[