Ui / app.py
xtreme86's picture
Create app.py
10b722d verified
raw
history blame
No virus
2.76 kB
import gradio as gr
# Function to generate Gradio code based on user inputs
def generate_gradio_code(interface_name, num_fields, field_types, field_labels):
# Begin constructing the Gradio code
code = f"import gradio as gr\n\n"
code += f"def {interface_name.lower()}_interface(*args):\n"
code += f" # Define what the function should return based on inputs\n"
code += f" return args\n\n"
code += f"with gr.Blocks() as {interface_name.lower()}:\n"
code += f" inputs = []\n"
for i in range(num_fields):
field_type = field_types[i]
field_label = field_labels[i]
if field_type == "Text":
code += f" input_{i} = gr.Textbox(label='{field_label}')\n"
elif field_type == "Number":
code += f" input_{i} = gr.Number(label='{field_label}')\n"
elif field_type == "Dropdown":
code += f" input_{i} = gr.Dropdown(label='{field_label}', choices=['Option 1', 'Option 2'])\n"
elif field_type == "Slider":
code += f" input_{i} = gr.Slider(label='{field_label}', minimum=0, maximum=100)\n"
elif field_type == "Checkbox":
code += f" input_{i} = gr.Checkbox(label='{field_label}')\n"
code += f" inputs.append(input_{i})\n"
code += f" outputs = gr.Textbox(label='Output')\n"
code += f" gr.Interface(fn={interface_name.lower()}_interface, inputs=inputs, outputs=outputs).launch()\n"
return code
# Gradio UI for user inputs
def ui():
with gr.Blocks() as demo:
with gr.Column():
interface_name = gr.Textbox(label="Interface Name", placeholder="Enter the interface name")
num_fields = gr.Number(label="Number of Fields", value=1, precision=0)
field_types = gr.Dropdown(choices=["Text", "Number", "Dropdown", "Slider", "Checkbox"],
label="Field Types", interactive=True, multiselect=True)
field_labels = gr.Textbox(label="Field Labels", placeholder="Enter field labels separated by commas")
generate_button = gr.Button("Generate Code")
generated_code = gr.Code(label="Generated Gradio Code")
# Logic to generate code based on input
def on_generate(interface_name, num_fields, field_types, field_labels):
field_labels_list = field_labels.split(",")
return generate_gradio_code(interface_name, int(num_fields), field_types, field_labels_list)
generate_button.click(on_generate, inputs=[interface_name, num_fields, field_types, field_labels],
outputs=generated_code)
return demo
# Launch the Gradio app
if __name__ == "__main__":
ui().launch()