File size: 1,347 Bytes
c54c38e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr

"""
Meal Recommender System
User inputs a list of ingredients and we call the model with these ingredients, prompting it to return a list of meals that can be made with these ingredients.
"""


# Function to dynamically add new text fields
def add_text_field(inputs):
    inputs.append("")
    return gr.update(visible=True), inputs


# Function to aggregate all inputs into a list
def process_ingredients(*ingredients):
    return [ingredient for ingredient in ingredients if ingredient]


# Initial setup
def app():
    with gr.Blocks() as demo:
        inputs = []
        textboxes = []

        with gr.Row():
            add_button = gr.Button("+ Add Ingredient")
            submit_button = gr.Button("Submit")

        output = gr.Textbox(label="Ingredients List", interactive=False)

        # Function to update interface with new fields
        def update_inputs():
            textboxes.clear()
            for i, value in enumerate(inputs):
                box = gr.Textbox(
                    label=f"Ingredient {i+1}", value=value, interactive=True, lines=1
                )
                textboxes.append(box)

        add_button.click(add_text_field, inputs, inputs, post_update=update_inputs)
        submit_button.click(process_ingredients, inputs, output)

    return demo


demo = app()
demo.launch()