Spaces:
Sleeping
Sleeping
Raphaël Bournhonesque
commited on
Commit
·
ef48bcf
1
Parent(s):
60077aa
first commit
Browse files- main.py +77 -0
- requirements.txt +2 -0
main.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import enum
|
| 2 |
+
import os
|
| 3 |
+
from typing import Optional
|
| 4 |
+
|
| 5 |
+
import requests
|
| 6 |
+
import streamlit as st
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
http_session = requests.Session()
|
| 10 |
+
|
| 11 |
+
@enum.unique
|
| 12 |
+
class NeuralCategoryClassifierModel(enum.Enum):
|
| 13 |
+
keras_2_0 = "keras-2.0"
|
| 14 |
+
keras_sota_3_0 = "keras-sota-3-0"
|
| 15 |
+
keras_ingredient_ocr_3_0 = "keras-ingredient-ocr-3.0"
|
| 16 |
+
keras_baseline_3_0 = "keras-baseline-3.0"
|
| 17 |
+
keras_original_3_0 = "keras-original-3.0"
|
| 18 |
+
keras_product_name_only_3_0 = "keras-product-name-only-3.0"
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
LOCAL_DB = False
|
| 22 |
+
|
| 23 |
+
if LOCAL_DB:
|
| 24 |
+
ROBOTOFF_BASE_URL = "http://localhost:5500/api/v1"
|
| 25 |
+
else:
|
| 26 |
+
ROBOTOFF_BASE_URL = "https://robotoff.openfoodfacts.org/api/v1"
|
| 27 |
+
|
| 28 |
+
PREDICTION_URL = ROBOTOFF_BASE_URL + "/predict/category"
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
@st.cache_data()
|
| 32 |
+
def get_predictions(barcode: str, model_name: str, threshold: Optional[float] = None):
|
| 33 |
+
data = {"barcode": barcode, "predictors": ["neural"], "neural_model_name": model_name}
|
| 34 |
+
if threshold is not None:
|
| 35 |
+
data["threshold"] = threshold
|
| 36 |
+
|
| 37 |
+
r = requests.post(PREDICTION_URL, json=data)
|
| 38 |
+
r.raise_for_status()
|
| 39 |
+
return r.json()["neural"]
|
| 40 |
+
|
| 41 |
+
def display_predictions(
|
| 42 |
+
barcode: str,
|
| 43 |
+
model_names: list[str],
|
| 44 |
+
threshold: Optional[float] = None,
|
| 45 |
+
):
|
| 46 |
+
debug_showed = False
|
| 47 |
+
for model_name in model_names:
|
| 48 |
+
response = get_predictions(barcode, model_name, threshold)
|
| 49 |
+
|
| 50 |
+
if "debug" in response:
|
| 51 |
+
if not debug_showed:
|
| 52 |
+
debug_showed = True
|
| 53 |
+
st.write(response["debug"])
|
| 54 |
+
response.pop("debug")
|
| 55 |
+
st.write(f"** {model_name} **")
|
| 56 |
+
st.write(response)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
st.sidebar.title("Category Prediction Demo")
|
| 61 |
+
barcode = st.sidebar.text_input(
|
| 62 |
+
"Product barcode"
|
| 63 |
+
)
|
| 64 |
+
threshold = st.sidebar.number_input("Threshold", format="%f") or None
|
| 65 |
+
model_names = st.multiselect(
|
| 66 |
+
"Name of the model",
|
| 67 |
+
[x.name for x in NeuralCategoryClassifierModel],
|
| 68 |
+
default=NeuralCategoryClassifierModel.keras_sota_3_0.name,
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
if barcode:
|
| 72 |
+
barcode = barcode.strip()
|
| 73 |
+
display_predictions(
|
| 74 |
+
barcode=barcode,
|
| 75 |
+
threshold=threshold,
|
| 76 |
+
model_names=model_names,
|
| 77 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
requests
|
| 2 |
+
streamlit
|