test app
Browse files
app.py
CHANGED
@@ -1,48 +1,53 @@
|
|
1 |
import gradio as gr
|
2 |
|
|
|
|
|
3 |
|
4 |
-
# Function to dynamically add new text fields
|
5 |
-
def add_text_field(inputs):
|
6 |
-
inputs.append("")
|
7 |
-
return gr.update(visible=True), inputs
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
# Function to aggregate all inputs into a list
|
11 |
-
def process_ingredients(*ingredients):
|
12 |
-
return [ingredient for ingredient in ingredients if ingredient]
|
13 |
|
|
|
|
|
|
|
|
|
|
|
14 |
|
|
|
|
|
15 |
def app():
|
16 |
with gr.Blocks() as demo:
|
17 |
-
inputs = [] # Shared list to store ingredients
|
18 |
-
|
19 |
-
# Container to hold text fields
|
20 |
-
ingredient_fields = gr.Row(visible=True)
|
21 |
-
|
22 |
-
# Buttons for adding and submitting
|
23 |
with gr.Row():
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
|
47 |
return demo
|
48 |
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
+
# Data storage
|
4 |
+
ingredients_list = []
|
5 |
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
# Function to add ingredient
|
8 |
+
def add_ingredient(ingredient, quantity):
|
9 |
+
if ingredient and quantity > 0:
|
10 |
+
ingredients_list.append(f"{ingredient}: {quantity} grams")
|
11 |
+
return (
|
12 |
+
"\n".join(ingredients_list),
|
13 |
+
gr.update(value="", interactive=True),
|
14 |
+
gr.update(value=None, interactive=True),
|
15 |
+
)
|
16 |
|
|
|
|
|
|
|
17 |
|
18 |
+
# Function to enable/disable add button
|
19 |
+
def validate_inputs(ingredient, quantity):
|
20 |
+
if ingredient and quantity > 0:
|
21 |
+
return gr.update(interactive=True)
|
22 |
+
return gr.update(interactive=False)
|
23 |
|
24 |
+
|
25 |
+
# App
|
26 |
def app():
|
27 |
with gr.Blocks() as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
with gr.Row():
|
29 |
+
ingredient_input = gr.Textbox(
|
30 |
+
label="Ingredient", placeholder="Enter ingredient name"
|
31 |
+
)
|
32 |
+
quantity_input = gr.Number(label="Quantity (grams)", value=None)
|
33 |
+
|
34 |
+
add_button = gr.Button("Add Ingredient", interactive=False)
|
35 |
+
output = gr.Textbox(label="Ingredients List", lines=10, interactive=False)
|
36 |
+
|
37 |
+
# Validate inputs
|
38 |
+
ingredient_input.change(
|
39 |
+
validate_inputs, [ingredient_input, quantity_input], add_button
|
40 |
+
)
|
41 |
+
quantity_input.change(
|
42 |
+
validate_inputs, [ingredient_input, quantity_input], add_button
|
43 |
+
)
|
44 |
+
|
45 |
+
# Add ingredient logic
|
46 |
+
add_button.click(
|
47 |
+
add_ingredient,
|
48 |
+
[ingredient_input, quantity_input],
|
49 |
+
[output, ingredient_input, quantity_input],
|
50 |
+
)
|
51 |
|
52 |
return demo
|
53 |
|