File size: 1,567 Bytes
40f633f
4123d5a
ef48bcf
62be7f8
ef48bcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4123d5a
 
62be7f8
ef48bcf
 
 
 
 
 
 
62be7f8
ef48bcf
 
 
 
062bda3
4123d5a
 
 
 
 
 
 
62be7f8
062bda3
4123d5a
062bda3
ef48bcf
 
 
062bda3
 
 
62be7f8
4ebffee
3dc0018
ef48bcf
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import copy
from typing import Optional

import pandas as pd
import requests
import streamlit as st

http_session = requests.Session()

LOCAL_DB = False

if LOCAL_DB:
    ROBOTOFF_BASE_URL = "http://localhost:5500/api/v1"
else:
    ROBOTOFF_BASE_URL = "https://robotoff.openfoodfacts.org/api/v1"

PREDICTION_URL = ROBOTOFF_BASE_URL + "/predict/category"


@st.cache_data
def get_predictions(barcode: str, threshold: Optional[float] = None):
    data = {"barcode": barcode}
    if threshold is not None:
        data["threshold"] = threshold

    r = requests.post(PREDICTION_URL, json=data)
    r.raise_for_status()
    return r.json()["neural"]


def display_predictions(
    barcode: str,
    threshold: Optional[float] = None,
):
    debug = None
    response = get_predictions(barcode, threshold)
    response = copy.deepcopy(response)
    if "debug" in response:
        if debug is None:
            debug = response["debug"]
        response.pop("debug")
    st.write(pd.DataFrame(response["predictions"]))

    if debug is not None:
        st.markdown("**Debug information**")
        st.write(debug)


st.sidebar.title("Category Prediction Demo")
query_params = st.experimental_get_query_params()

default_barcode = query_params["barcode"][0] if "barcode" in query_params else ""
barcode = st.sidebar.text_input("Product barcode", default_barcode)
threshold = st.sidebar.number_input("Threshold", format="%f", value=0.5) or None

if barcode:
    barcode = barcode.strip()
    display_predictions(
        barcode=barcode,
        threshold=threshold,
    )