jeremyarancio
commited on
Commit
β’
af7959d
1
Parent(s):
5d1a4ad
feat: :art: init
Browse files- .gitignore +1 -0
- README.md +2 -2
- app.py +75 -0
- back_end.py +42 -0
- requirements.txt +1 -0
- utils.py +9 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
__pycache__
|
README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
---
|
2 |
title: Ingredients Spellcheck Annotate
|
3 |
-
emoji:
|
4 |
colorFrom: red
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.44.1
|
8 |
app_file: app.py
|
|
|
1 |
---
|
2 |
title: Ingredients Spellcheck Annotate
|
3 |
+
emoji: πβοΈ
|
4 |
colorFrom: red
|
5 |
+
colorTo: blue
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.44.1
|
8 |
app_file: app.py
|
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
from back_end import next_annotation, submit_correction
|
4 |
+
from utils import diff_texts
|
5 |
+
|
6 |
+
|
7 |
+
# Gradio Interface
|
8 |
+
with gr.Blocks() as demo:
|
9 |
+
gr.Markdown(
|
10 |
+
"""# Ingredients Spellcheck π
|
11 |
+
### You can review corrections generated by our model and validate or correct the predictions.\n
|
12 |
+
Your feedback will be integrated to the OFF database!
|
13 |
+
|
14 |
+
*Note: We are working on connecting this tool to OFF Product Opener. π·*
|
15 |
+
""")
|
16 |
+
|
17 |
+
insight_id = gr.Textbox(
|
18 |
+
label="Insight Id",
|
19 |
+
interactive=False,
|
20 |
+
visible=False,
|
21 |
+
)
|
22 |
+
|
23 |
+
original_text = gr.Textbox(
|
24 |
+
label="Original Text (Uneditable)",
|
25 |
+
info="This is the original text.",
|
26 |
+
interactive=False, # Make this text box uneditable
|
27 |
+
lines=3
|
28 |
+
)
|
29 |
+
|
30 |
+
corrected_text = gr.Textbox(
|
31 |
+
label="Corrected Text (Editable)",
|
32 |
+
info="This is the AI-corrected text. You can modify it.",
|
33 |
+
interactive=True, # Make this text box editable
|
34 |
+
lines=3
|
35 |
+
)
|
36 |
+
|
37 |
+
# Diff Display using HighlightedText
|
38 |
+
diff_display = gr.HighlightedText(
|
39 |
+
label="Difference Between Original and Corrected Text",
|
40 |
+
combine_adjacent=True,
|
41 |
+
show_legend=True,
|
42 |
+
color_map={"+": "green", "-": "red"} # "+" for inserted text, "-" for deleted text
|
43 |
+
)
|
44 |
+
|
45 |
+
# Validate button to move to next annotation
|
46 |
+
with gr.Row():
|
47 |
+
validate_button = gr.Button("Validate")
|
48 |
+
skip_button = gr.Button("Skip")
|
49 |
+
|
50 |
+
# Define action when validate button is clicked
|
51 |
+
validate_button.click(
|
52 |
+
submit_correction, # Function to handle submission
|
53 |
+
inputs=[insight_id, original_text, corrected_text], # Original and edited texts as inputs
|
54 |
+
outputs=[insight_id, original_text, corrected_text] # Load next pair of texts
|
55 |
+
)
|
56 |
+
|
57 |
+
skip_button.click(
|
58 |
+
next_annotation, # Function to handle submission
|
59 |
+
inputs=[], # Original and edited texts as inputs
|
60 |
+
outputs=[insight_id, original_text, corrected_text] # Load next pair of texts
|
61 |
+
)
|
62 |
+
|
63 |
+
# Update diff display dynamically when corrected text is modified
|
64 |
+
corrected_text.change(
|
65 |
+
diff_texts, # Call diff function
|
66 |
+
inputs=[original_text, corrected_text], # Compare original and corrected texts
|
67 |
+
outputs=diff_display # Update diff display
|
68 |
+
)
|
69 |
+
|
70 |
+
# Load the first set of texts when the demo starts
|
71 |
+
demo.load(next_annotation, inputs=[], outputs=[insight_id, original_text, corrected_text])
|
72 |
+
|
73 |
+
|
74 |
+
if __name__ == "__main__":
|
75 |
+
demo.launch()
|
back_end.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Dict, Tuple
|
2 |
+
import requests
|
3 |
+
|
4 |
+
|
5 |
+
BASE_URL = "https://robotoff.openfoodfacts.org/api/v1/"
|
6 |
+
|
7 |
+
|
8 |
+
def next_annotation() -> Tuple[str, str, str]:
|
9 |
+
insight = import_random_insight()
|
10 |
+
return insight["id"], insight["data"]["original"], insight["data"]["correction"]
|
11 |
+
|
12 |
+
|
13 |
+
def submit_correction(insight_id: str, original: str, correction: str) -> Tuple[str, str, str]:
|
14 |
+
# Not implemented yet
|
15 |
+
return next_annotation()
|
16 |
+
|
17 |
+
|
18 |
+
def import_random_insight(
|
19 |
+
insight_type: str = "ingredient_spellcheck",
|
20 |
+
count: int = 1,
|
21 |
+
predictor: str = "fine-tuned-mistral-7b",
|
22 |
+
) -> Dict:
|
23 |
+
url = f"{BASE_URL}/insights/random?count={count}&type={insight_type}&predictor={predictor}"
|
24 |
+
response = requests.get(url)
|
25 |
+
data = response.json()
|
26 |
+
insight = data["insights"][0]
|
27 |
+
return insight
|
28 |
+
|
29 |
+
|
30 |
+
def submit_to_product_opener(
|
31 |
+
insight_id: str,
|
32 |
+
skipped: bool,
|
33 |
+
update: int = 0,
|
34 |
+
) -> None:
|
35 |
+
url = f"{BASE_URL}/insights/annotate"
|
36 |
+
annotation = -1 if skipped else 1
|
37 |
+
data = {
|
38 |
+
"insight_id": insight_id,
|
39 |
+
"annotation": annotation,
|
40 |
+
"update": update,
|
41 |
+
}
|
42 |
+
requests.post(url, data=data)
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
gradio==4.44.1
|
utils.py
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from difflib import Differ
|
2 |
+
|
3 |
+
|
4 |
+
def diff_texts(text1, text2):
|
5 |
+
d = Differ()
|
6 |
+
return [
|
7 |
+
(token[2:], token[0] if token[0] != " " else None)
|
8 |
+
for token in d.compare(text1, text2)
|
9 |
+
]
|