Spaces:
Build error

File size: 3,620 Bytes
4310245
 
 
 
 
 
 
 
 
 
 
 
 
79667df
4310245
2b4a25f
4310245
 
 
4789579
4310245
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import base64
from io import BytesIO

import firebase_admin
import gradio as gr
import numpy as np
from fastai.vision.all import *
import json
from firebase_admin import credentials, firestore
import pathlib

temp = pathlib.PosixPath
pathlib.PosixPath = pathlib.WindowsPath
pathlib.PosixPath = temp

cred = credentials.Certificate("firebase_key.json")
app = firebase_admin.initialize_app(cred)
db = firestore.client()

learn = load_learner("model.pkl")
names = json.load(open("./translations.json"))

with gr.Blocks(
    css=".gradio-container {background-image: url('file=Background/bgbg.jpeg');background-size: cover; background-size: 100% 100%;}"  # .block.svelte-kz0ejz{background-color: rgba(0,0,0,0);} kalo butuh transparan
) as demo:
    gr.Markdown(" ")
    gr.Markdown(" ")
    gr.Markdown(" ")
    gr.Markdown(" ")
    gr.Markdown(" ")
    gr.Markdown(" ")
    gr.Markdown(" ")
    gr.Markdown(" ")
    gr.Markdown(" ")
    gr.Markdown(" ")
    gr.Markdown(" ")
    with gr.Row():
        with gr.Column():
            image_input = gr.Webcam(label="Gambar", shape=(200, 200), type="pil")
            with gr.Row():
                predict_id_btn = gr.Button("Bahasa Indonesia", variant="primary")
                predict_en_btn = gr.Button("Bahasa Inggris", variant="secondary")

        with gr.Column():
            with gr.Column(visible=False) as id_fruit_name_col:
                id_fruit_name = gr.Label(label="Bahasa indonesia")
                id_audio = gr.Audio(autoplay= True,label="Audio indonesia")
            with gr.Column(visible=False) as en_fruit_name_col:
                en_fruit_name = gr.Label(label="Bahasa inggris")
                en_audio = gr.Audio(autoplay= True,label="Audio inggris")

    def classify_id(image):
        buffered = BytesIO()
        image.save(buffered, format="JPEG")
        img_str = base64.b64encode(buffered.getvalue())

        pred, idx, probs = learn.predict(np.asarray(image))

        db.collection("preds").add(  # inilo db
            {
                "image": img_str,
                "prediction": pred.title(),
                "time_added": firestore.SERVER_TIMESTAMP,
            }
        )
        id_fruit_name_col = gr.update(visible=True)
        en_fruit_name_col = gr.update(visible=False)
        return [
            names[pred]["id"],
            f"./audios/id/" + names[pred]["id"] + ".mp3",
            id_fruit_name_col,
            en_fruit_name_col,
        ]

    def classify_en(image):
        buffered = BytesIO()
        image.save(buffered, format="JPEG")
        img_str = base64.b64encode(buffered.getvalue())

        pred, idx, probs = learn.predict(np.asarray(image))

        db.collection("preds").add(  # inilo db
            {
                "image": img_str,
                "prediction": pred.title(),
                "time_added": firestore.SERVER_TIMESTAMP,
            }
        )
        en_fruit_name_col = gr.update(visible=True)
        id_fruit_name_col = gr.update(visible=False)
        return [
            names[pred]["en"],
            f"./audios/en/" + names[pred]["id"] + ".mp3",
            id_fruit_name_col,
            en_fruit_name_col,
        ]

    predict_id_btn.click(
        fn=classify_id,
        inputs=image_input,
        outputs=[id_fruit_name, id_audio, id_fruit_name_col, en_fruit_name_col],
        api_name="classify_image",
    )
    predict_en_btn.click(
        fn=classify_en,
        inputs=image_input,
        outputs=[en_fruit_name, en_audio, id_fruit_name_col, en_fruit_name_col],
        api_name="classify_image",
    )


# demo.launch(share=True)
demo.launch()