File size: 6,723 Bytes
3b1fcce
a292a25
3b1fcce
 
9e897fe
3b1fcce
9e897fe
 
9ba9174
1040b57
 
3b1fcce
 
 
 
 
 
 
9e897fe
 
 
 
 
 
 
 
9ba9174
9e897fe
 
 
 
 
 
 
9ba9174
 
9e897fe
 
 
 
 
 
 
1040b57
9e897fe
 
 
 
3b1fcce
 
1040b57
3b1fcce
1040b57
 
 
 
 
3b1fcce
1040b57
3b1fcce
 
1040b57
3b1fcce
1040b57
3b1fcce
 
 
 
1040b57
3b1fcce
 
 
5e2a1f4
3b1fcce
 
 
 
 
 
 
 
 
 
 
 
 
1040b57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bed5116
 
 
 
 
 
 
 
 
 
1040b57
 
 
 
 
 
 
 
 
 
 
 
 
a292a25
1040b57
 
 
 
 
 
3b1fcce
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import gradio as gr
#from pyzbar.pyzbar import decode
from lambdas import upload_models, predict
import base64
from io import BytesIO, StringIO
from PIL import Image
import pandas as pd
import os.path
import numpy as np
from datetime import datetime, timedelta
from descriptions import prefer_frontal_cam_html, docu_eng, docu_cat

DEBUG = True

config = {'possible_shifts': {'No shifts': 0}, 'possible_modes': ["waste"]}


def login(username, password) -> bool:
    if os.path.isfile('credentials.csv'):
        df = pd.read_csv('credentials.csv')
    else:
        s = os.environ.get('CREDENTIALS')
        df = pd.read_csv(StringIO(s))

    if not len(df):
        return False
    df = df.replace({np.nan: None})

    for idx, row in df.iterrows():
        if row['username'] == username and row['password'] == password:
            restaurant_id = int(row['restaurant_id'])
            restaurant_name = str(row['restaurant_name'])
            mode = 'waste'
            possible_modes = str(row.get('modes')).split(':')
            possible_shifts = {i.split(':')[0]: i.split(':')[1] for i in str(row.get('shifts')).split('-')} \
                if row.get('shifts') else {'no shift': None}

            config_aux = {'restaurant_id': restaurant_id,
                          'restaurant_name': restaurant_name,
                          'mode': mode,
                          'possible_modes': possible_modes,
                          'possible_shifts': possible_shifts,
                          }
            config.clear()
            config.update(config_aux)

            return True
    return False


def start_app(shift_id, mode, date):
    try:
        config_aux = {'date': date,
                      'shift_id': shift_id,
                      'mode': mode,
                      'possible_shifts': {'No shifts': 0},
                      'possible_modes': ["waste"]}
        config.update(config_aux)
        gr.Info('Loading models. This may take a while')
        status_code, r = upload_models(**config)
        if status_code in (201, 200, 204):
            config.update(r)
            gr.Info('Models Correctly Loaded. Ready to predict')
            tab_id = 1
        else:
            raise gr.Error(f'Error loading the models: {r}')
    except Exception as e:
        raise gr.Error(f'Error Uploading the models. \n {e}')
    return gr.Tabs(selected=tab_id, visible=True)


def predict_app(image, patient_id):
    gr.Info('Predicting ...')
    buffered = BytesIO()
    image.save(buffered, format='JPEG')
    b64image = base64.b64encode(buffered.getvalue()).decode('utf-8')
    status_code, r = predict(b64image=b64image,
                             patient_identifier=patient_id,
                             **config)
    if status_code in (200, 201, 204):
        gr.Info('Prediction Successful')
    else:
        raise gr.Error(f'Error predicting {r}')


# APP
with (gr.Blocks(head=prefer_frontal_cam_html) as block):
    with gr.Tabs() as tabs:
        with gr.Tab(label='Welcome', id=0) as welcome_tab:
            gr.Info('Loading ...')
            @gr.render()
            def header():
                gr.Markdown(f'# User: {config.get("restaurant_name", "Proppos")}', render=True)


            logout_button = gr.Button(icon=None, link='/logout', value='Logout / Exit ↩')

            @gr.render()
            def render_dropdowns():
                shift_dropdown = gr.Dropdown(label='Meal/Apat/Comida',
                                             value=list(config["possible_shifts"].items())[0],
                                             choices=tuple(config["possible_shifts"].items()))
                mode_dropdown = gr.Dropdown(label='Mode',
                                            value=config['possible_modes'][0],
                                            choices=config["possible_modes"])

                days = [datetime.today().strftime('%Y-%m-%d'),
                        (datetime.now() - timedelta(days=1)).strftime('%Y-%m-%d')]
                date_chooser = gr.Dropdown(choices=days, label='Date/Data/Fecha', value=days[0])

                start_button = gr.Button(value='START ▶️')
                start_button.click(fn=start_app, inputs=[shift_dropdown, mode_dropdown, date_chooser], outputs=tabs)

            gr.Markdown(""" * Do you have any doubt? Please, see the documentation tab. \n
                            * Tens un dubte? Consulta la pestanya de documentació. """)

            # logout_button.click(fn=lambda: config.clear())

        with gr.Tab(label='📷 Capture', id=1):
            # MAIN TAB TO PREDICT
            gr.Markdown(f""" 1. Click to Access Webcam
                             2. Press the red button to start recording
                             3. Place the tray so it is centered in the displayed image and
                             4. Press the grey button 'PRESS TO PREDICT'
                        """)
            im = gr.Image(sources=['webcam'], streaming=True, mirror_webcam=False, type='pil',
                          height=720, width=1280)
            with gr.Accordion():
                eater_id = gr.Textbox(label='Patient Identification', placeholder='Searching Patient ID')

            current_eater_id = {'value': None}


            # @gr.on(inputs=im, outputs=eater_id)
            # def search_eater_id(image):
            #     d = []#decode(image)
            #     default_value = None
            #     current_value = current_eater_id['value'] or default_value
            #     new_value = d[0].data if d else default_value
            #     # If it is really a new value different from the default one, change it.
            #     final_value = new_value if new_value != default_value else current_value
            #     current_eater_id['value'] = final_value
            #     return final_value


            b = gr.Button('PRESS TO PREDICT')
            b.click(fn=predict_app, inputs=[im, eater_id], outputs=gr.Info())

        with gr.Tab(label='ℹ️ Status', id=2):
            gr.Markdown(' Press the button to see the status of the Application and technical information')
            load_status_button = gr.Button('Load Status')
            status_json = gr.Json(label='Status')
            load_status_button.click(fn=lambda: config, outputs=status_json)

        with gr.Tab(label='📄 Documentation', id=3):
            choices = [('ENG 🇬🇧', docu_eng), ('CAT', docu_cat)]
            c = gr.Radio(label='Select the language', choices=choices)

            @gr.render()
            def display_docu():
                d = gr.Markdown(docu_eng)
                c.change(fn=lambda x: x, inputs=c, outputs=d)



block.launch(show_api=False, auth=login)