xtreme86 commited on
Commit
10b722d
1 Parent(s): 207f47a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()