jeremyarancio commited on
Commit
d008f82
·
1 Parent(s): 843ddde

feat: :sparkles: Add lang filter

Browse files
Files changed (3) hide show
  1. app.py +21 -8
  2. back_end.py +21 -8
  3. utils.py +60 -1
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
 
3
  from back_end import next_annotation, submit_correction, enable_buttons
4
- from utils import diff_texts
5
 
6
 
7
  # Gradio Interface
@@ -31,6 +31,13 @@ with gr.Blocks() as demo:
31
  label="OFF Password",
32
  type="password",
33
  )
 
 
 
 
 
 
 
34
 
35
  # Saved to detect change from annotator
36
  model_correction = gr.Text(visible=False)
@@ -54,7 +61,7 @@ with gr.Blocks() as demo:
54
  label="Difference Between Original and Corrected Text",
55
  combine_adjacent=True,
56
  show_legend=True,
57
- color_map={"+": "green", "-": "red"} # "+" for inserted text, "-" for deleted text
58
  )
59
 
60
  image = gr.Image()
@@ -66,26 +73,26 @@ with gr.Blocks() as demo:
66
 
67
  validate_button.click(
68
  submit_correction,
69
- inputs=[insight_id, annotator_correction, model_correction, off_username, off_password],
70
- outputs=[insight_id, original, model_correction, annotator_correction, image]
71
  )
72
 
73
  skip_button.click(
74
  next_annotation,
75
- inputs=[],
76
- outputs=[insight_id, original, model_correction, annotator_correction, image]
77
  )
78
 
79
  annotator_correction.change(
80
  diff_texts, # Call diff function
81
  inputs=[original, annotator_correction],
82
- outputs=diff_display
83
  )
84
 
85
  off_username.change(
86
  enable_buttons,
87
  inputs=[off_username, off_password],
88
- outputs=[validate_button, skip_button]
89
  )
90
 
91
  off_password.change(
@@ -94,6 +101,12 @@ with gr.Blocks() as demo:
94
  outputs=[validate_button, skip_button],
95
  )
96
 
 
 
 
 
 
 
97
  # Load the first set of texts when the demo starts
98
  demo.load(
99
  next_annotation,
 
1
  import gradio as gr
2
 
3
  from back_end import next_annotation, submit_correction, enable_buttons
4
+ from utils import diff_texts, LANGS
5
 
6
 
7
  # Gradio Interface
 
31
  label="OFF Password",
32
  type="password",
33
  )
34
+ lang = gr.Dropdown(
35
+ choices=LANGS.keys(),
36
+ value="All",
37
+ label="Language",
38
+ info="Optional: Filter a language.",
39
+
40
+ )
41
 
42
  # Saved to detect change from annotator
43
  model_correction = gr.Text(visible=False)
 
61
  label="Difference Between Original and Corrected Text",
62
  combine_adjacent=True,
63
  show_legend=True,
64
+ color_map={"+": "green", "-": "red"},
65
  )
66
 
67
  image = gr.Image()
 
73
 
74
  validate_button.click(
75
  submit_correction,
76
+ inputs=[insight_id, annotator_correction, model_correction, off_username, off_password, lang],
77
+ outputs=[insight_id, original, model_correction, annotator_correction, image],
78
  )
79
 
80
  skip_button.click(
81
  next_annotation,
82
+ inputs=[lang],
83
+ outputs=[insight_id, original, model_correction, annotator_correction, image],
84
  )
85
 
86
  annotator_correction.change(
87
  diff_texts, # Call diff function
88
  inputs=[original, annotator_correction],
89
+ outputs=diff_display,
90
  )
91
 
92
  off_username.change(
93
  enable_buttons,
94
  inputs=[off_username, off_password],
95
+ outputs=[validate_button, skip_button],
96
  )
97
 
98
  off_password.change(
 
101
  outputs=[validate_button, skip_button],
102
  )
103
 
104
+ lang.change(
105
+ next_annotation,
106
+ inputs=[lang],
107
+ outputs=[insight_id, original, model_correction, annotator_correction, image],
108
+ )
109
+
110
  # Load the first set of texts when the demo starts
111
  demo.load(
112
  next_annotation,
back_end.py CHANGED
@@ -4,7 +4,7 @@ import base64
4
  import json
5
  from dataclasses import dataclass
6
 
7
- from utils import get_logger
8
 
9
  from openfoodfacts.api import ProductResource, APIConfig
10
  import gradio as gr
@@ -29,8 +29,11 @@ class Authentification:
29
  return base64.b64encode(credentials.encode()).decode()
30
 
31
 
32
- def next_annotation() -> Annotation:
33
- insight = import_random_insight()
 
 
 
34
  logger.info("Imported insight: %s", insight)
35
  return (
36
  insight["id"],
@@ -74,13 +77,23 @@ def submit_correction(
74
  def import_random_insight(
75
  insight_type: str = "ingredient_spellcheck",
76
  predictor: str = "fine-tuned-mistral-7b",
 
77
  ) -> Dict:
78
  url = f"{BASE_URL}/insights/random?count=1&type={insight_type}&predictor={predictor}"
79
- response = requests.get(url)
80
- data = response.json()
81
- insight = data["insights"][0]
82
- return insight
83
-
 
 
 
 
 
 
 
 
 
84
 
85
  def submit_to_product_opener(
86
  insight_id: str,
 
4
  import json
5
  from dataclasses import dataclass
6
 
7
+ from utils import get_logger, LANGS
8
 
9
  from openfoodfacts.api import ProductResource, APIConfig
10
  import gradio as gr
 
29
  return base64.b64encode(credentials.encode()).decode()
30
 
31
 
32
+ def next_annotation(lang: Optional[str]) -> Annotation:
33
+ lang_key = LANGS.get(lang)
34
+ if not lang_key:
35
+ logger.warning("The provided Lang was not found. 'All' returned by default.")
36
+ insight = import_random_insight(lang_key=lang_key)
37
  logger.info("Imported insight: %s", insight)
38
  return (
39
  insight["id"],
 
77
  def import_random_insight(
78
  insight_type: str = "ingredient_spellcheck",
79
  predictor: str = "fine-tuned-mistral-7b",
80
+ lang_key: Optional[str] = None
81
  ) -> Dict:
82
  url = f"{BASE_URL}/insights/random?count=1&type={insight_type}&predictor={predictor}"
83
+ if lang_key:
84
+ url += f"&value_tag={lang_key}"
85
+ try:
86
+ response = requests.get(url)
87
+ response.raise_for_status() # Raises an HTTPError for bad responses (4xx or 5xx)
88
+ data = response.json()
89
+ if "insights" in data and data["insights"]:
90
+ return data["insights"][0]
91
+ else:
92
+ gr.Warning("No insights found; fetching default insight instead.")
93
+ return import_random_insight(insight_type, predictor)
94
+ except requests.RequestException as e:
95
+ gr.Error(f"Import product from Product Opener failed: {e}")
96
+
97
 
98
  def submit_to_product_opener(
99
  insight_id: str,
utils.py CHANGED
@@ -13,6 +13,65 @@ def diff_texts(text1, text2):
13
  def get_logger():
14
  logging.basicConfig(
15
  level=logging.INFO,
16
- format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
17
  )
18
  return logging.getLogger(__name__)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  def get_logger():
14
  logging.basicConfig(
15
  level=logging.INFO,
16
+ format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
17
  )
18
  return logging.getLogger(__name__)
19
+
20
+
21
+ LANGS = {
22
+ "All": None,
23
+ "English": "en",
24
+ "French": "fr",
25
+ "German": "de",
26
+ "Spanish": "es",
27
+ "Italian": "it",
28
+ "Dutch": "nl",
29
+ "Polish": "pl",
30
+ "Portuguese": "pt",
31
+ "Swedish": "sv",
32
+ "Bulgarian": "bg",
33
+ "Romanian": "ro",
34
+ "Finnish": "fi",
35
+ "Russian": "ru",
36
+ "Norwegian Bokmål": "nb",
37
+ "Czech": "cs",
38
+ "Thai": "th",
39
+ "Danish": "da",
40
+ "Croatian": "hr",
41
+ "Hungarian": "hu",
42
+ "Arabic": "ar",
43
+ "Greek": "el",
44
+ "Japanese": "ja",
45
+ "Catalan": "ca",
46
+ "Serbian": "sr",
47
+ "Slovenian": "sl",
48
+ "Slovak": "sk",
49
+ "Turkish": "tr",
50
+ "Lithuanian": "lt",
51
+ "Chinese": "zh",
52
+ "Estonian": "et",
53
+ "Latvian": "lv",
54
+ "Undefined": "xx",
55
+ "Ukrainian": "uk",
56
+ "Indonesian": "id",
57
+ "Hebrew": "he",
58
+ "Vietnamese": "vi",
59
+ "Icelandic": "is",
60
+ "Latin": "la",
61
+ "Korean": "ko",
62
+ "Albanian": "sq",
63
+ "Georgian": "ka",
64
+ "Malay": "ms",
65
+ "Bosnian": "bs",
66
+ "Persian": "fa",
67
+ "Bengali": "bn",
68
+ "Galician": "gl",
69
+ "Kazakh": "kk",
70
+ "Macedonian": "mk",
71
+ "Norwegian Nynorsk": "nn",
72
+ "Hindi": "hi",
73
+ "Afar": "aa",
74
+ "Uzbek": "uz",
75
+ "Somali": "so",
76
+ "Afrikaans": "af"
77
+ }