File size: 6,167 Bytes
3443b2c
 
095af58
3443b2c
 
 
 
 
095af58
3443b2c
 
ea78d12
3443b2c
 
 
3fb7896
 
3443b2c
 
de9fa06
37dfef1
de9fa06
 
 
91542bf
 
3443b2c
 
 
 
 
5e2c5eb
 
 
3443b2c
5e2c5eb
3443b2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a22415a
 
 
d85b07b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3443b2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2a2ad8
3443b2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f94d97f
3443b2c
 
 
 
de9fa06
3fb7896
3443b2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import pandas as pd
import thirdai
from thirdai import bolt, licensing
import os
import time


thirdai.licensing.activate("1FF7B0-458ABC-5F382D-0A1513-904CF0-V3")
max_posts = 5
df = pd.read_csv("processed_recipes_3.csv")
model = bolt.UniversalDeepTransformer.load("1bn_name_ctg_keywords_4gram.bolt")

recipe_id_to_row_num = {}

for i in range(df.shape[0]):
    recipe_id_to_row_num[df.iloc[i,0]] = i


INTRO_MARKDOWN = (
    """# A billion parameter model, trained on a single CPU, in just 90 mins, on 522K recipes from food.com !!
    """
)

LIKE_TEXT = "👍 update LLM"
FEEDBACK_RECEIVED_TEXT = "👌 Click search for updated results"
SHOW_MORE = "Show more"
SHOW_LESS = "Show less"


def retrain(query, doc_id):
    query = query.lower()
    query.replace('\n', ' ')
    query = ' '.join([query[i:i+4] for i in range(len(query)-3)])
    df = pd.DataFrame({
        "Name": [query],
        "RecipeId": [str(doc_id)]
    })

    filename = f"temptrain{hash(query)}{hash(doc_id)}{time.time()}.csv"

    df.to_csv(filename)

    prediction = None

    while prediction != doc_id:
        model.train(filename, epochs=1)
        prediction = model.predict(
            {"Name": query.replace('\n', ' ')},
            return_predicted_class=True)

    os.remove(filename)

    # sample = {"query": query.replace('\n', ' '), "id": str(doc_id)}
    # batch = [sample]

    # prediction = None

    # while prediction != doc_id:
    #     model.train_batch(batch, metrics=["categorical_accuracy"])
    #     prediction = model.predict(sample, return_predicted_class=True)


def search(query):
    query = query.lower()
    query = ' '.join([query[i:i+4] for i in range(len(query)-3)])
    scores = model.predict({"Name": query})
    ####
    sorted_ids = scores.argsort()[-max_posts:][::-1]
    relevant_posts = [
        df.iloc[pid] for pid in sorted_ids
    ]
    ####
    # K = min(2*max_posts, len(scores) - 1)
    # sorted_post_ids = scores.argsort()[-K:][::-1]
    # print(sorted_post_ids)
    # sorted_ids = []
    # relevant_posts = []
    # count = 0
    # for pid in sorted_post_ids:
    #     if pid in recipe_id_to_row_num:
    #         relevant_posts.append(df.iloc[recipe_id_to_row_num[pid]])
    #         sorted_ids.append(pid)
    #     count += 1
    #     if count==max_posts:
    #         break
    ####
    header = [gr.Markdown.update(visible=True)]
    boxes = [
        gr.Box.update(visible=True)
        for _ in relevant_posts
    ]
    titles = [
        gr.Markdown.update(f"## {post['Name']}")
        for post in relevant_posts
    ]
    toggles = [
        gr.Button.update(
            visible=True,
            value=SHOW_MORE,
            interactive=True,
        )
        for _ in relevant_posts
    ]
    matches = [
        gr.Button.update(
            value=LIKE_TEXT,
            interactive=True,
        )
        for _ in relevant_posts
    ]
    bodies = [
        gr.HTML.update(
            visible=False,
            value=f"<br/>"
            f"<h2>Description:</h2>\n{post['Description']}\n\n"
            "<hr class='solid'>"
            f"<h2>Ingredients:</h2>\n{post['RecipeIngredientParts']}\n\n"
            "<br/>"
            f"<h2>Instructions:</h2>\n{post['RecipeInstructions']}\n\n"
            "<br/>")
        for post in relevant_posts
    ]

    return (
        header +
        boxes +
        titles +
        toggles +
        matches +
        bodies +
        [sorted_ids]
    )


def handle_toggle(toggle):
    if toggle == SHOW_MORE:
        new_toggle_text = SHOW_LESS
        visible = True
    if toggle == SHOW_LESS:
        new_toggle_text = SHOW_MORE
        visible = False
    return [
        gr.Button.update(new_toggle_text),
        gr.HTML.update(visible=visible),
    ]


def handle_feedback(button_id: int):
    def register_feedback(doc_ids, query):
        retrain(
            query=query,
            doc_id=doc_ids[button_id]
        )
        return gr.Button.update(
            value=FEEDBACK_RECEIVED_TEXT,
            interactive=False,
        )

    return register_feedback


default_query = (
    "biryani lamb spicy contains cloves and red chili powder, made with ghee and hard boiled eggs, made by grinding coconut and cashew"
)


with gr.Blocks() as demo:
    gr.Markdown(INTRO_MARKDOWN)
    query = gr.Textbox(value=default_query, label="Query", lines=10)
    submit = gr.Button(value="Search")

    header = [gr.Markdown("# Relevant Recipes", visible=False)]
    post_boxes = []
    post_titles = []
    toggle_buttons = []
    match_buttons = []
    post_bodies = []
    post_ids = gr.State([])

    for i in range(max_posts):
        with gr.Box(visible=False) as box:
            post_boxes.append(box)

            with gr.Row():
                with gr.Column(scale=5):
                    title = gr.Markdown("")
                    post_titles.append(title)
                with gr.Column(scale=1, min_width=370):
                    with gr.Row():
                        with gr.Column(scale=3, min_width=170):
                            toggle = gr.Button(SHOW_MORE)
                            toggle_buttons.append(toggle)
                        with gr.Column(scale=1, min_width=170):
                            match = gr.Button(LIKE_TEXT)
                            match.click(
                                fn=handle_feedback(button_id=i),
                                inputs=[post_ids, query],
                                outputs=[match],
                            )
                            match_buttons.append(match)

            body = gr.HTML("")
            post_bodies.append(body)

            toggle.click(
                fn=handle_toggle,
                inputs=[toggle],
                outputs=[toggle, body],
            )

    allblocks = (
        header +
        post_boxes +
        post_titles +
        toggle_buttons +
        match_buttons +
        post_bodies +
        [post_ids]
    )

    query.submit(
        fn=search,
        inputs=[query],
        outputs=allblocks)
    submit.click(
        fn=search,
        inputs=[query],
        outputs=allblocks)


demo.launch()