xtreme86 commited on
Commit
e6b1679
1 Parent(s): 3884c94
Files changed (1) hide show
  1. app.py +30 -24
app.py CHANGED
@@ -1,42 +1,37 @@
1
  import gradio as gr
2
 
3
- # Function to generate Gradio code dynamically
4
- def generate_gradio_code(interface_name, components):
5
- code = f"import gradio as gr\n\n"
6
- code += f"def {interface_name.lower()}_interface(*args):\n"
7
- code += f" # Your processing logic here\n"
8
- code += f" return args\n\n"
9
 
10
- code += f"with gr.Blocks() as {interface_name.lower()}:\n"
11
- code += f" inputs = []\n"
12
-
13
- for i, component in enumerate(components):
14
- label = component.get('label', f'Field {i+1}')
15
  field_type = component['type']
16
 
17
  if field_type == "Textbox":
18
  placeholder = component.get('placeholder', '')
19
- code += f" input_{i} = gr.Textbox(label='{label}', placeholder='{placeholder}')\n"
20
  elif field_type == "Number":
21
  min_value = component.get('min_value', 0)
22
  max_value = component.get('max_value', 100)
23
- code += f" input_{i} = gr.Number(label='{label}', minimum={min_value}, maximum={max_value})\n"
24
  elif field_type == "Dropdown":
25
  choices = component.get('choices', ['Option 1', 'Option 2'])
26
- code += f" input_{i} = gr.Dropdown(label='{label}', choices={choices})\n"
27
  elif field_type == "Slider":
28
  min_value = component.get('min_value', 0)
29
  max_value = component.get('max_value', 100)
30
- code += f" input_{i} = gr.Slider(label='{label}', minimum={min_value}, maximum={max_value})\n"
31
  elif field_type == "Checkbox":
32
- code += f" input_{i} = gr.Checkbox(label='{label}')\n"
33
-
34
- code += f" inputs.append(input_{i})\n"
35
-
36
- code += f" outputs = gr.Textbox(label='Output')\n"
37
- code += f" gr.Interface(fn={interface_name.lower()}_interface, inputs=inputs, outputs=outputs).launch()\n"
38
 
39
- return code
 
 
 
40
 
41
  # Function to handle dynamic field addition
42
  def add_field(component_type, field_label, placeholder_text, min_value, max_value, choices, components):
@@ -62,7 +57,7 @@ def update_visibility(component_type):
62
  gr.update(visible=component_type in ["Slider", "Number"])
63
  )
64
 
65
- # Gradio UI for designing the interface
66
  def ui():
67
  with gr.Blocks() as demo:
68
  with gr.Column():
@@ -83,6 +78,10 @@ def ui():
83
  # Add field button and component preview
84
  add_button = gr.Button("Add Field")
85
  component_preview = gr.JSON(label="Component List")
 
 
 
 
86
 
87
  add_button.click(
88
  add_field,
@@ -97,12 +96,19 @@ def ui():
97
  outputs=[placeholder_text, min_value, choices, max_value]
98
  )
99
 
 
 
 
 
 
 
 
100
  # Preview and generate code button
101
  generate_button = gr.Button("Generate Code")
102
  generated_code = gr.Code(label="Generated Code")
103
 
104
  generate_button.click(
105
- generate_gradio_code,
106
  inputs=[interface_name, components_list],
107
  outputs=generated_code
108
  )
 
1
  import gradio as gr
2
 
3
+ # Function to dynamically create Gradio UI elements for preview
4
+ def generate_ui_preview(components):
5
+ # A function to be linked to the dynamically created interface
6
+ def mock_fn(*args):
7
+ return args
 
8
 
9
+ inputs = []
10
+ for component in components:
11
+ label = component.get('label', 'No Label')
 
 
12
  field_type = component['type']
13
 
14
  if field_type == "Textbox":
15
  placeholder = component.get('placeholder', '')
16
+ inputs.append(gr.Textbox(label=label, placeholder=placeholder))
17
  elif field_type == "Number":
18
  min_value = component.get('min_value', 0)
19
  max_value = component.get('max_value', 100)
20
+ inputs.append(gr.Number(label=label, minimum=min_value, maximum=max_value))
21
  elif field_type == "Dropdown":
22
  choices = component.get('choices', ['Option 1', 'Option 2'])
23
+ inputs.append(gr.Dropdown(label=label, choices=choices))
24
  elif field_type == "Slider":
25
  min_value = component.get('min_value', 0)
26
  max_value = component.get('max_value', 100)
27
+ inputs.append(gr.Slider(label=label, minimum=min_value, maximum=max_value))
28
  elif field_type == "Checkbox":
29
+ inputs.append(gr.Checkbox(label=label))
 
 
 
 
 
30
 
31
+ if len(inputs) > 0:
32
+ return gr.Interface(fn=mock_fn, inputs=inputs, outputs=inputs).launch(prevent_thread_lock=True)
33
+ else:
34
+ return "No components added yet."
35
 
36
  # Function to handle dynamic field addition
37
  def add_field(component_type, field_label, placeholder_text, min_value, max_value, choices, components):
 
57
  gr.update(visible=component_type in ["Slider", "Number"])
58
  )
59
 
60
+ # Gradio UI for designing the interface with live preview
61
  def ui():
62
  with gr.Blocks() as demo:
63
  with gr.Column():
 
78
  # Add field button and component preview
79
  add_button = gr.Button("Add Field")
80
  component_preview = gr.JSON(label="Component List")
81
+
82
+ # Live UI preview
83
+ preview_button = gr.Button("Preview UI")
84
+ ui_preview = gr.HTML(label="UI Preview will appear here")
85
 
86
  add_button.click(
87
  add_field,
 
96
  outputs=[placeholder_text, min_value, choices, max_value]
97
  )
98
 
99
+ # Preview the dynamically generated UI
100
+ preview_button.click(
101
+ generate_ui_preview,
102
+ inputs=[components_list],
103
+ outputs=[ui_preview]
104
+ )
105
+
106
  # Preview and generate code button
107
  generate_button = gr.Button("Generate Code")
108
  generated_code = gr.Code(label="Generated Code")
109
 
110
  generate_button.click(
111
+ lambda interface_name, components: generate_gradio_code(interface_name, components),
112
  inputs=[interface_name, components_list],
113
  outputs=generated_code
114
  )