Commit
·
98db947
1
Parent(s):
af7959d
Add picture
Browse files- app.py +34 -29
- back_end.py +24 -2
app.py
CHANGED
@@ -14,33 +14,38 @@ with gr.Blocks() as demo:
|
|
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 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
# Validate button to move to next annotation
|
46 |
with gr.Row():
|
@@ -51,13 +56,13 @@ with gr.Blocks() as demo:
|
|
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
|
@@ -68,7 +73,7 @@ with gr.Blocks() as demo:
|
|
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__":
|
|
|
14 |
*Note: We are working on connecting this tool to OFF Product Opener. 👷*
|
15 |
""")
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
+
with gr.Row():
|
19 |
+
with gr.Column():
|
20 |
+
insight_id = gr.Textbox(
|
21 |
+
label="Insight Id",
|
22 |
+
interactive=False,
|
23 |
+
visible=False,
|
24 |
+
)
|
25 |
+
|
26 |
+
original_text = gr.Textbox(
|
27 |
+
label="Original Text (Uneditable)",
|
28 |
+
info="This is the original text.",
|
29 |
+
interactive=False, # Make this text box uneditable
|
30 |
+
lines=3
|
31 |
+
)
|
32 |
+
|
33 |
+
corrected_text = gr.Textbox(
|
34 |
+
label="Corrected Text (Editable)",
|
35 |
+
info="This is the AI-corrected text. You can modify it.",
|
36 |
+
interactive=True, # Make this text box editable
|
37 |
+
lines=3
|
38 |
+
)
|
39 |
+
|
40 |
+
# Diff Display using HighlightedText
|
41 |
+
diff_display = gr.HighlightedText(
|
42 |
+
label="Difference Between Original and Corrected Text",
|
43 |
+
combine_adjacent=True,
|
44 |
+
show_legend=True,
|
45 |
+
color_map={"+": "green", "-": "red"} # "+" for inserted text, "-" for deleted text
|
46 |
+
)
|
47 |
+
|
48 |
+
image = gr.Image()
|
49 |
|
50 |
# Validate button to move to next annotation
|
51 |
with gr.Row():
|
|
|
56 |
validate_button.click(
|
57 |
submit_correction, # Function to handle submission
|
58 |
inputs=[insight_id, original_text, corrected_text], # Original and edited texts as inputs
|
59 |
+
outputs=[insight_id, original_text, corrected_text, image] # Load next pair of texts
|
60 |
)
|
61 |
|
62 |
skip_button.click(
|
63 |
next_annotation, # Function to handle submission
|
64 |
inputs=[], # Original and edited texts as inputs
|
65 |
+
outputs=[insight_id, original_text, corrected_text, image] # Load next pair of texts
|
66 |
)
|
67 |
|
68 |
# Update diff display dynamically when corrected text is modified
|
|
|
73 |
)
|
74 |
|
75 |
# Load the first set of texts when the demo starts
|
76 |
+
demo.load(next_annotation, inputs=[], outputs=[insight_id, original_text, corrected_text, image])
|
77 |
|
78 |
|
79 |
if __name__ == "__main__":
|
back_end.py
CHANGED
@@ -1,13 +1,20 @@
|
|
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
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
|
13 |
def submit_correction(insight_id: str, original: str, correction: str) -> Tuple[str, str, str]:
|
@@ -40,3 +47,18 @@ def submit_to_product_opener(
|
|
40 |
"update": update,
|
41 |
}
|
42 |
requests.post(url, data=data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from typing import Dict, Tuple
|
2 |
import requests
|
3 |
|
4 |
+
from openfoodfacts.api import ProductResource, APIConfig
|
5 |
+
|
6 |
|
7 |
BASE_URL = "https://robotoff.openfoodfacts.org/api/v1/"
|
8 |
|
9 |
|
10 |
+
def next_annotation() -> Tuple[str, str, str, str]:
|
11 |
insight = import_random_insight()
|
12 |
+
return (
|
13 |
+
insight["id"],
|
14 |
+
insight["data"]["original"],
|
15 |
+
insight["data"]["correction"],
|
16 |
+
get_image_url(insight["barcode"])
|
17 |
+
)
|
18 |
|
19 |
|
20 |
def submit_correction(insight_id: str, original: str, correction: str) -> Tuple[str, str, str]:
|
|
|
47 |
"update": update,
|
48 |
}
|
49 |
requests.post(url, data=data)
|
50 |
+
|
51 |
+
|
52 |
+
def get_image_url(
|
53 |
+
code: str,
|
54 |
+
user_agent: str = "Spellcheck-Annotate",
|
55 |
+
) -> str:
|
56 |
+
fields = ["image_ingredients_url"]
|
57 |
+
data = ProductResource(
|
58 |
+
api_config=APIConfig(user_agent=user_agent)
|
59 |
+
).get(
|
60 |
+
code=code,
|
61 |
+
fields=fields,
|
62 |
+
)
|
63 |
+
image_url = data.get(fields[0])
|
64 |
+
return image_url
|