Spaces:
Running
Running
Raphaël Bournhonesque
commited on
Commit
·
df8a41e
1
Parent(s):
d63d911
first commit
Browse files- app.py +94 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import io
|
2 |
+
import time
|
3 |
+
|
4 |
+
import requests
|
5 |
+
import streamlit as st
|
6 |
+
from openfoodfacts.images import generate_image_url
|
7 |
+
from PIL import Image
|
8 |
+
|
9 |
+
|
10 |
+
@st.cache_data
|
11 |
+
def send_prediction_request(image_url: str, model_name: str, server_base_url: str):
|
12 |
+
return requests.get(
|
13 |
+
f"{server_base_url}/api/v1/images/predict",
|
14 |
+
params={"image_url": image_url, "models": model_name, "output_image": 1},
|
15 |
+
)
|
16 |
+
|
17 |
+
|
18 |
+
def get_product(barcode: str):
|
19 |
+
r = requests.get(f"https://world.openfoodfacts.org/api/v2/product/{barcode}")
|
20 |
+
|
21 |
+
if r.status_code == 404:
|
22 |
+
return None
|
23 |
+
|
24 |
+
return r.json()["product"]
|
25 |
+
|
26 |
+
|
27 |
+
def run(barcode: str, model_names: list[str], server_base_url: str):
|
28 |
+
product = get_product(barcode)
|
29 |
+
st.markdown(f"[Product page](https://world.openfoodfacts.org/product/{barcode})")
|
30 |
+
|
31 |
+
if not product:
|
32 |
+
st.error(f"Product {barcode} not found")
|
33 |
+
return
|
34 |
+
|
35 |
+
images = product.get("images", [])
|
36 |
+
|
37 |
+
if not images:
|
38 |
+
st.error(f"No images found for product {barcode}")
|
39 |
+
return
|
40 |
+
|
41 |
+
for image_id, _ in images.items():
|
42 |
+
if not image_id.isdigit():
|
43 |
+
continue
|
44 |
+
|
45 |
+
image_url = generate_image_url(barcode, f"{image_id}")
|
46 |
+
|
47 |
+
for model_name in model_names:
|
48 |
+
start = time.monotonic()
|
49 |
+
response = send_prediction_request(image_url, model_name, server_base_url)
|
50 |
+
elapsed = time.monotonic() - start
|
51 |
+
|
52 |
+
if response.headers["Content-Type"] != "image/jpeg":
|
53 |
+
st.error(f"Error: {response.text}")
|
54 |
+
continue
|
55 |
+
image = Image.open(io.BytesIO(response.content))
|
56 |
+
st.write(f"Image {image_id}")
|
57 |
+
st.image(image, caption=f"Model: {model_name} ({elapsed:.2f}s)")
|
58 |
+
st.divider()
|
59 |
+
|
60 |
+
|
61 |
+
st.title("Object detection demo")
|
62 |
+
st.markdown(
|
63 |
+
"This Streamlit is useful to test object detection models running in production at Open Food Facts."
|
64 |
+
)
|
65 |
+
default_barcode = st.query_params["barcode"] if "barcode" in st.query_params else ""
|
66 |
+
model_names = st.multiselect(
|
67 |
+
"Models",
|
68 |
+
options=[
|
69 |
+
"nutrition-table-yolo",
|
70 |
+
"nutrition-table",
|
71 |
+
"nutriscore",
|
72 |
+
"nutriscore-yolo",
|
73 |
+
"universal-logo-detector",
|
74 |
+
],
|
75 |
+
help="Select the model(s) to use",
|
76 |
+
default=["nutrition-table-yolo", "nutrition-table"],
|
77 |
+
)
|
78 |
+
barcode = st.text_input(
|
79 |
+
"barcode", help="Barcode of the product", value=default_barcode
|
80 |
+
).strip()
|
81 |
+
st.query_params["barcode"] = barcode
|
82 |
+
|
83 |
+
# Default server is staging
|
84 |
+
server_base_url = "https://robotoff.openfoodfacts.net"
|
85 |
+
|
86 |
+
if "env" in st.query_params:
|
87 |
+
if st.query_params["env"] == "prod":
|
88 |
+
server_base_url = "https://robotoff.openfoodfacts.net"
|
89 |
+
elif st.query_params["env"] == "dev":
|
90 |
+
server_base_url = "http://localhost:5000"
|
91 |
+
|
92 |
+
|
93 |
+
if barcode:
|
94 |
+
run(barcode, model_names, server_base_url)
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
requests==2.32.3
|
2 |
+
streamlit==1.36.0
|
3 |
+
openfoodfacts==0.4.0
|
4 |
+
Pillow==10.4.0
|