xtreme86 commited on
Commit
5b675bb
1 Parent(s): 10b722d
Files changed (1) hide show
  1. app.py +77 -32
app.py CHANGED
@@ -1,61 +1,106 @@
1
  import gradio as gr
 
2
 
3
- # Function to generate Gradio code based on user inputs
4
- def generate_gradio_code(interface_name, num_fields, field_types, field_labels):
5
- # Begin constructing the Gradio code
6
  code = f"import gradio as gr\n\n"
7
  code += f"def {interface_name.lower()}_interface(*args):\n"
8
- code += f" # Define what the function should return based on inputs\n"
9
  code += f" return args\n\n"
10
 
11
  code += f"with gr.Blocks() as {interface_name.lower()}:\n"
12
  code += f" inputs = []\n"
13
 
14
- for i in range(num_fields):
15
- field_type = field_types[i]
16
- field_label = field_labels[i]
17
- if field_type == "Text":
18
- code += f" input_{i} = gr.Textbox(label='{field_label}')\n"
 
 
19
  elif field_type == "Number":
20
- code += f" input_{i} = gr.Number(label='{field_label}')\n"
 
 
21
  elif field_type == "Dropdown":
22
- code += f" input_{i} = gr.Dropdown(label='{field_label}', choices=['Option 1', 'Option 2'])\n"
 
23
  elif field_type == "Slider":
24
- code += f" input_{i} = gr.Slider(label='{field_label}', minimum=0, maximum=100)\n"
 
 
25
  elif field_type == "Checkbox":
26
- code += f" input_{i} = gr.Checkbox(label='{field_label}')\n"
 
27
  code += f" inputs.append(input_{i})\n"
28
-
29
  code += f" outputs = gr.Textbox(label='Output')\n"
30
  code += f" gr.Interface(fn={interface_name.lower()}_interface, inputs=inputs, outputs=outputs).launch()\n"
31
-
32
  return code
33
 
34
-
35
- # Gradio UI for user inputs
36
  def ui():
37
  with gr.Blocks() as demo:
38
  with gr.Column():
 
39
  interface_name = gr.Textbox(label="Interface Name", placeholder="Enter the interface name")
40
- num_fields = gr.Number(label="Number of Fields", value=1, precision=0)
41
 
42
- field_types = gr.Dropdown(choices=["Text", "Number", "Dropdown", "Slider", "Checkbox"],
43
- label="Field Types", interactive=True, multiselect=True)
44
- field_labels = gr.Textbox(label="Field Labels", placeholder="Enter field labels separated by commas")
45
-
46
- generate_button = gr.Button("Generate Code")
47
- generated_code = gr.Code(label="Generated Gradio Code")
48
-
49
- # Logic to generate code based on input
50
- def on_generate(interface_name, num_fields, field_types, field_labels):
51
- field_labels_list = field_labels.split(",")
52
- return generate_gradio_code(interface_name, int(num_fields), field_types, field_labels_list)
53
 
54
- generate_button.click(on_generate, inputs=[interface_name, num_fields, field_types, field_labels],
55
- outputs=generated_code)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  return demo
58
 
59
  # Launch the Gradio app
60
  if __name__ == "__main__":
61
- ui().launch()
 
1
  import gradio as gr
2
+ import json
3
 
4
+ # Function to generate Gradio code dynamically
5
+ def generate_gradio_code(interface_name, components):
 
6
  code = f"import gradio as gr\n\n"
7
  code += f"def {interface_name.lower()}_interface(*args):\n"
8
+ code += f" # Your processing logic here\n"
9
  code += f" return args\n\n"
10
 
11
  code += f"with gr.Blocks() as {interface_name.lower()}:\n"
12
  code += f" inputs = []\n"
13
 
14
+ for i, component in enumerate(components):
15
+ label = component.get('label', f'Field {i+1}')
16
+ field_type = component['type']
17
+
18
+ if field_type == "Textbox":
19
+ placeholder = component.get('placeholder', '')
20
+ code += f" input_{i} = gr.Textbox(label='{label}', placeholder='{placeholder}')\n"
21
  elif field_type == "Number":
22
+ min_value = component.get('min_value', 0)
23
+ max_value = component.get('max_value', 100)
24
+ code += f" input_{i} = gr.Number(label='{label}', minimum={min_value}, maximum={max_value})\n"
25
  elif field_type == "Dropdown":
26
+ choices = component.get('choices', ['Option 1', 'Option 2'])
27
+ code += f" input_{i} = gr.Dropdown(label='{label}', choices={choices})\n"
28
  elif field_type == "Slider":
29
+ min_value = component.get('min_value', 0)
30
+ max_value = component.get('max_value', 100)
31
+ code += f" input_{i} = gr.Slider(label='{label}', minimum={min_value}, maximum={max_value})\n"
32
  elif field_type == "Checkbox":
33
+ code += f" input_{i} = gr.Checkbox(label='{label}')\n"
34
+
35
  code += f" inputs.append(input_{i})\n"
36
+
37
  code += f" outputs = gr.Textbox(label='Output')\n"
38
  code += f" gr.Interface(fn={interface_name.lower()}_interface, inputs=inputs, outputs=outputs).launch()\n"
39
+
40
  return code
41
 
42
+ # Gradio UI for designing the interface
 
43
  def ui():
44
  with gr.Blocks() as demo:
45
  with gr.Column():
46
+ gr.Markdown("### Gradio UI Designer")
47
  interface_name = gr.Textbox(label="Interface Name", placeholder="Enter the interface name")
 
48
 
49
+ # Components UI
50
+ component_type = gr.Dropdown(choices=["Textbox", "Number", "Dropdown", "Slider", "Checkbox"], label="Field Type")
51
+ field_label = gr.Textbox(label="Field Label", placeholder="Enter field label")
52
+ placeholder_text = gr.Textbox(label="Placeholder (for Textbox)", visible=False)
53
+ min_value = gr.Number(label="Min Value (for Slider/Number)", value=0, visible=False)
54
+ max_value = gr.Number(label="Max Value (for Slider/Number)", value=100, visible=False)
55
+ choices = gr.Textbox(label="Choices (comma separated for Dropdown)", visible=False)
56
+
57
+ # Components list to track fields
58
+ components_list = gr.State([])
 
59
 
60
+ def add_field(component_type, field_label, placeholder_text, min_value, max_value, choices, components):
61
+ new_component = {"type": component_type, "label": field_label}
62
+
63
+ if component_type == "Textbox":
64
+ new_component["placeholder"] = placeholder_text
65
+ elif component_type in ["Number", "Slider"]:
66
+ new_component["min_value"] = min_value
67
+ new_component["max_value"] = max_value
68
+ elif component_type == "Dropdown":
69
+ new_component["choices"] = choices.split(',')
70
+
71
+ components.append(new_component)
72
+ return gr.JSON.update(value=components), components
73
+
74
+ def update_visibility(component_type):
75
+ return (
76
+ gr.update(visible=component_type == "Textbox"),
77
+ gr.update(visible=component_type in ["Slider", "Number"]),
78
+ gr.update(visible=component_type == "Dropdown"),
79
+ gr.update(visible=component_type in ["Slider", "Number"])
80
+ )
81
+
82
+ add_button = gr.Button("Add Field")
83
+ component_preview = gr.JSON(label="Component List")
84
 
85
+ # Linking the add field button
86
+ add_button.click(add_field,
87
+ inputs=[component_type, field_label, placeholder_text, min_value, max_value, choices, components_list],
88
+ outputs=[component_preview, components_list])
89
+
90
+ component_type.change(update_visibility, inputs=[component_type],
91
+ outputs=[placeholder_text, min_value, choices, max_value])
92
+
93
+ # Preview and generate code button
94
+ generate_button = gr.Button("Generate Code")
95
+ generated_code = gr.Code(label="Generated Code")
96
+
97
+ def generate_code(interface_name, components):
98
+ return generate_gradio_code(interface_name, components)
99
+
100
+ generate_button.click(generate_code, inputs=[interface_name, components_list], outputs=generated_code)
101
+
102
  return demo
103
 
104
  # Launch the Gradio app
105
  if __name__ == "__main__":
106
+ ui().launch()