File size: 10,373 Bytes
d83f998
085ad5d
a47280a
 
bd0503e
 
 
 
d83f998
ea89c24
d83f998
ea89c24
bd0503e
ea89c24
d83f998
bd0503e
 
 
 
a0dffb0
bb11018
bd0503e
 
 
bb11018
76ca705
bd0503e
a0dffb0
bd0503e
 
 
 
 
 
 
1915921
 
 
 
 
 
 
 
 
 
 
 
 
a0dffb0
1915921
 
bd0503e
a0dffb0
bd0503e
 
 
76ca705
085ad5d
a0dffb0
 
085ad5d
 
 
 
ea89c24
 
 
085ad5d
a0dffb0
085ad5d
1915921
bb11018
bd0503e
 
085ad5d
a0dffb0
 
085ad5d
 
 
 
ea89c24
 
 
085ad5d
a0dffb0
085ad5d
1915921
 
 
 
 
 
 
 
 
a0dffb0
 
1915921
 
 
 
 
 
 
 
 
a0dffb0
1915921
 
bb11018
bd0503e
 
085ad5d
a0dffb0
 
085ad5d
 
 
ea89c24
 
 
085ad5d
a0dffb0
1915921
 
 
 
 
 
a0dffb0
 
1915921
 
 
 
 
 
 
a0dffb0
1915921
 
 
 
 
085ad5d
1915921
 
 
 
bb11018
1915921
a0dffb0
 
1915921
 
 
 
 
 
 
 
a0dffb0
1915921
 
 
 
 
 
 
 
ea89c24
 
a0dffb0
bd0503e
 
 
ea89c24
085ad5d
1915921
a0dffb0
085ad5d
 
 
 
 
ea89c24
a0dffb0
1915921
d83f998
ea89c24
085ad5d
a0dffb0
bd0503e
 
 
76ca705
bd0503e
ea89c24
bd0503e
76ca705
bb11018
ea89c24
bb11018
 
085ad5d
bd0503e
ea89c24
a0dffb0
ea89c24
 
bd0503e
1915921
bd0503e
76ca705
085ad5d
bb11018
085ad5d
bb11018
bd0503e
ea89c24
bd0503e
 
085ad5d
bb11018
a0dffb0
bb11018
 
a0dffb0
bb11018
 
 
 
 
 
 
bd0503e
 
 
bb11018
 
a0dffb0
 
 
bb11018
 
085ad5d
 
bd0503e
 
 
 
085ad5d
 
76ca705
bd0503e
ea89c24
bd0503e
 
085ad5d
bb11018
a0dffb0
bb11018
 
a0dffb0
bb11018
 
 
 
 
 
 
bd0503e
 
 
bb11018
 
a0dffb0
 
 
bb11018
 
085ad5d
 
bd0503e
 
 
 
085ad5d
 
76ca705
bd0503e
1915921
bd0503e
ea89c24
1915921
bb11018
a0dffb0
bb11018
1915921
a0dffb0
1915921
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a0dffb0
1915921
 
 
 
a0dffb0
1915921
a0dffb0
bb11018
ea89c24
bb11018
 
1915921
 
a0dffb0
 
1915921
 
a0dffb0
1915921
 
bb11018
 
1915921
ea89c24
 
bb11018
 
1915921
 
 
 
a0dffb0
085ad5d
a0dffb0
085ad5d
 
76ca705
a0dffb0
1915921
a0dffb0
1915921
 
 
a0dffb0
1915921
 
a0dffb0
1915921
 
a0dffb0
1915921
 
 
 
 
 
 
 
a0dffb0
1915921
 
a0dffb0
1915921
 
 
 
 
bd0503e
1915921
bd0503e
ea89c24
085ad5d
bb11018
a0dffb0
bb11018
1915921
a0dffb0
1915921
 
 
 
 
 
 
085ad5d
bb11018
085ad5d
bb11018
 
a0dffb0
bb11018
 
 
 
a0dffb0
bb11018
 
085ad5d
 
bb11018
bd0503e
085ad5d
bb11018
bd0503e
 
 
bb11018
085ad5d
bb11018
 
a0dffb0
bb11018
 
085ad5d
 
ea89c24
 
 
 
 
085ad5d
 
76ca705
bd0503e
a0dffb0
bd0503e
bb11018
bd0503e
ea89c24
a47280a
ea89c24
 
 
bd0503e
 
ea89c24
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
import os
import gradio as gr
from gradio_client import Client, handle_file

# ---------------------------------------------------
# Environment
# ---------------------------------------------------

HF_TOKEN = os.getenv("HF_TOKEN")

if not HF_TOKEN:
    raise ValueError(
        "HF_TOKEN environment variable not set."
    )

# ---------------------------------------------------
# Coldet Client
# ---------------------------------------------------

client = Client(
    "Coldet/backend",
    headers={
        "Authorization": f"Bearer {HF_TOKEN}"
    }
)

# ---------------------------------------------------
# Model List
# ---------------------------------------------------

AVAILABLE_MODELS = [
    "coldet-v1-mini.coldet",
    "coldet-v2-mini.coldet",
]

# ---------------------------------------------------
# Model Credit Costs
# ---------------------------------------------------

MODEL_COSTS = {
    "coldet-v1-mini.coldet": 1,
    "coldet-v2-mini.coldet": 2,
}

# ---------------------------------------------------
# Helpers
# ---------------------------------------------------

def format_error(e):
    return f"Error:\n\n{str(e)}"

# ---------------------------------------------------
# API FUNCTIONS
# ---------------------------------------------------

def create_account(username: str, password: str):

    try:

        result = client.predict(
            username=username,
            password=password,
            api_name="/create_account",
        )

        return f"Account created!\n\nSession ID:\n{result}"

    except Exception as e:
        return format_error(e)

# ---------------------------------------------------

def login(username: str, password: str):

    try:

        result = client.predict(
            username=username,
            password=password,
            api_name="/login",
        )

        return f"Logged in!\n\nSession ID:\n{result}"

    except Exception as e:
        return format_error(e)

# ---------------------------------------------------

def change_password(
    session_id: str,
    old_password: str,
    new_password: str
):

    try:

        result = client.predict(
            session_id=session_id,
            old_password=old_password,
            new_password=new_password,
            api_name="/change_password",
        )

        return result

    except Exception as e:
        return format_error(e)

# ---------------------------------------------------

def create_token(session_id: str):

    try:

        result = client.predict(
            session_id=session_id,
            api_name="/create_token",
        )

        return f"API Key:\n{result}"

    except Exception as e:
        return format_error(e)

# ---------------------------------------------------

def list_keys(session_id: str):

    try:

        result = client.predict(
            session_id=session_id,
            api_name="/list_keys",
        )

        return result

    except Exception as e:

        return {
            "error": str(e)
        }

# ---------------------------------------------------

def delete_key(
    session_id: str,
    api_key: str
):

    try:

        result = client.predict(
            session_id=session_id,
            api_key=api_key,
            api_name="/delete_key",
        )

        return result

    except Exception as e:
        return format_error(e)

# ---------------------------------------------------

def predict(
    image,
    api_key: str,
    model: str
):

    if image is None:

        return {
            "error": "Please upload an image."
        }

    try:

        result = client.predict(
            image=handle_file(image),
            api_key=api_key,
            model=model,
            api_name="/predict",
        )

        result["estimated_cost"] = MODEL_COSTS.get(model, 1)

        return result

    except Exception as e:

        return {
            "error": str(e)
        }

# ---------------------------------------------------
# UI
# ---------------------------------------------------

with gr.Blocks(
    title="Coldet Backend Interface"
) as demo:

    gr.Markdown("# πŸš€ Coldet Backend Interface")

    gr.Markdown(
        "Frontend for the Coldet backend API."
    )

    # ---------------------------------------------------
    # ACCOUNT TAB
    # ---------------------------------------------------

    with gr.Tab("πŸ”‘ Account"):

        with gr.Row():

            # -----------------------------
            # Create Account
            # -----------------------------

            with gr.Column():

                gr.Markdown("## Create Account")

                create_username = gr.Textbox(
                    label="Username"
                )

                create_password = gr.Textbox(
                    label="Password",
                    type="password"
                )

                create_btn = gr.Button(
                    "Create Account"
                )

                create_output = gr.Textbox(
                    label="Session",
                    lines=4,
                    interactive=False
                )

                create_btn.click(
                    fn=create_account,
                    inputs=[
                        create_username,
                        create_password
                    ],
                    outputs=create_output,
                )

            # -----------------------------
            # Login
            # -----------------------------

            with gr.Column():

                gr.Markdown("## Login")

                login_username = gr.Textbox(
                    label="Username"
                )

                login_password = gr.Textbox(
                    label="Password",
                    type="password"
                )

                login_btn = gr.Button(
                    "Login"
                )

                login_output = gr.Textbox(
                    label="Session",
                    lines=4,
                    interactive=False
                )

                login_btn.click(
                    fn=login,
                    inputs=[
                        login_username,
                        login_password
                    ],
                    outputs=login_output,
                )

    # ---------------------------------------------------
    # PASSWORD TAB
    # ---------------------------------------------------

    with gr.Tab("πŸ”’ Password"):

        gr.Markdown("## Change Password")

        password_session = gr.Textbox(
            label="Session ID"
        )

        old_password = gr.Textbox(
            label="Old Password",
            type="password"
        )

        new_password = gr.Textbox(
            label="New Password",
            type="password"
        )

        password_btn = gr.Button(
            "Change Password"
        )

        password_output = gr.Textbox(
            label="Result",
            interactive=False
        )

        password_btn.click(
            fn=change_password,
            inputs=[
                password_session,
                old_password,
                new_password
            ],
            outputs=password_output,
        )

    # ---------------------------------------------------
    # API KEYS TAB
    # ---------------------------------------------------

    with gr.Tab("πŸ” API Keys"):

        gr.Markdown("## API Key Management")

        key_session = gr.Textbox(
            label="Session ID",
            lines=2
        )

        with gr.Row():

            create_key_btn = gr.Button(
                "Create API Key"
            )

            refresh_keys_btn = gr.Button(
                "Refresh Keys"
            )

        token_output = gr.Textbox(
            label="New API Key",
            interactive=False,
            lines=3
        )

        keys_output = gr.JSON(
            label="Your Keys"
        )

        create_key_btn.click(
            fn=create_token,
            inputs=key_session,
            outputs=token_output,
        )

        refresh_keys_btn.click(
            fn=list_keys,
            inputs=key_session,
            outputs=keys_output,
        )

        gr.Markdown("## Delete API Key")

        delete_key_input = gr.Textbox(
            label="API Key"
        )

        delete_key_btn = gr.Button(
            "Delete Key"
        )

        delete_output = gr.Textbox(
            label="Delete Result",
            interactive=False
        )

        delete_key_btn.click(
            fn=delete_key,
            inputs=[
                key_session,
                delete_key_input
            ],
            outputs=delete_output,
        )

    # ---------------------------------------------------
    # INFERENCE TAB
    # ---------------------------------------------------

    with gr.Tab("πŸ€– Inference"):

        gr.Markdown("## Run Inference")

        gr.Markdown(
            "\n".join(
                [
                    f"- `{model}` β†’ {cost} credit(s)"
                    for model, cost in MODEL_COSTS.items()
                ]
            )
        )

        with gr.Row():

            with gr.Column():

                image_input = gr.Image(
                    label="Image",
                    type="filepath"
                )

                api_key_input = gr.Textbox(
                    label="API Key"
                )

                model_input = gr.Dropdown(
                    label="Model",
                    choices=AVAILABLE_MODELS,
                    value=AVAILABLE_MODELS[0],
                )

                predict_btn = gr.Button(
                    "Run Inference"
                )

            with gr.Column():

                predict_output = gr.JSON(
                    label="Result"
                )

        predict_btn.click(
            fn=predict,
            inputs=[
                image_input,
                api_key_input,
                model_input
            ],
            outputs=predict_output,
        )

# ---------------------------------------------------
# Launch
# ---------------------------------------------------

demo.queue()

if __name__ == "__main__":

    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        ssr_mode=False
    )