File size: 5,262 Bytes
3b1fcce
 
 
 
9e897fe
3b1fcce
9e897fe
 
9ba9174
3b1fcce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e897fe
 
 
 
 
 
 
 
9ba9174
9e897fe
 
 
 
 
 
 
9ba9174
 
9e897fe
 
 
 
 
 
 
 
 
 
 
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
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

DEBUG = True

prefer_frontal_cam_html = """
<script>
const originalGetUserMedia = navigator.mediaDevices.getUserMedia.bind(navigator.mediaDevices);

navigator.mediaDevices.getUserMedia = (constraints) => {
  if (!constraints.video.facingMode) {
    constraints.video.facingMode = {ideal: "environment"};
  }
  return originalGetUserMedia(constraints);
};
</script>
"""

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


def login(username, password) -> bool:
    # TODO from username and password get restaurant_id
    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.update(config_aux)

            return True
    return False


def start_app(shift_id, mode):
    try:
        config_aux = {'shift_id': shift_id,
                      'mode': mode}
        config.update(config_aux)
        gr.Info('Loading models', )
        status_code, r = upload_models(**config)
        if status_code in (201, 200, 204):
            gr.Info('Models Correctly Loaded. Ready to predict')
        else:
            raise gr.Error(f'Error loading the models: {r}')
        config.update(r)
    except Exception as e:
        raise gr.Error(f'Error Uploading the models. \n {e}')


def predict_app(image, patient_id):
    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.Tab(label='Welcome'):
        gr.Markdown(f'# User: {config.get("restaurant_name", "Proppos")}')


        @gr.render()
        def render_dropdowns():
            shift_dropdown = gr.Dropdown(label='Meal/Comida/Apat',
                                         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"])
            start_button = gr.Button(value='START')
            start_button.click(fn=start_app, inputs=[shift_dropdown, mode_dropdown])

    with gr.Tab(label='📷 Capture'):
        # MAIN TAB TO PREDICT
        gr.Markdown(f""" 1. Click to Access Webcam
                         2.  
                    """)
        im = gr.Image(sources=['webcam'], streaming=True, mirror_webcam=False, type='pil')
        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'):
        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'):
        gr.Markdown()

#block.launch(auth=("proppos", "Proppos2019"))
block.launch(show_api=False, auth=login)