File size: 2,944 Bytes
087c387
 
 
6d1f41e
087c387
6d1f41e
 
087c387
f9a4722
087c387
f9a4722
087c387
6d1f41e
 
a16c241
087c387
6d1f41e
08d6f20
 
 
087c387
f9a4722
 
087c387
6d1f41e
087c387
6d1f41e
087c387
6d1f41e
087c387
 
 
 
 
f9a4722
087c387
6d1f41e
087c387
f9a4722
087c387
f9a4722
087c387
 
 
6d1f41e
 
087c387
f9a4722
087c387
f9a4722
6d1f41e
087c387
a16c241
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Import necessary libraries
import gradio as gr  # Gradio is used to create web interfaces for Python scripts.
from transformers import AutoConfig  # AutoConfig is from the Hugging Face Transformers library, used to create configuration for various models.

# A list of model names to start with. These are names of popular models from the Hugging Face library.
model_list = ["bert-base-uncased", "gpt2", "distilbert-base-uncased"]

# Function to add a new model to the list.
def add_model_to_list(new_model):
    # Check if the new model is not already in the list and is not an empty string.
    if new_model and new_model not in model_list:
        model_list.append(new_model)  # Add the new model to the list.
    return model_list


# Function to create a configuration for the selected model.
def create_config(model_name, num_labels, use_cache):
    if isinstance(model_name, list):
        model_name = model_name[0]  # Take the first model name from the list if it's a list
    
    # If the selected model is not in the list, add it (this is a safety check).
    if model_name not in model_list:
        model_list.append(model_name)
    # Create a configuration for the selected model using AutoConfig.
    config = AutoConfig.from_pretrained(model_name, num_labels=num_labels, use_cache=use_cache)
    return str(config)  # Return the configuration as a string.

# Start building the Gradio interface
with gr.Blocks() as demo:
    gr.Markdown("## Config Class - Transformers")  # Display a title for the web interface.
    with gr.Row():  # Create a row in the interface to organize elements horizontally.
        # Dropdown menu to select a model.
        model_dropdown = gr.Dropdown(label="Select a Model", choices=model_list, value=model_list[0], allow_custom_value=True)
        # Textbox for users to input a new model name.
        new_model_input = gr.Textbox(label="Add a New Model", placeholder="Enter model name")
        # Button to add the new model to the dropdown list.
        add_model_button = gr.Button("Add Model")
    # Numeric input for the number of labels (used in the model configuration).
    num_labels_input = gr.Number(label="Number of Labels", value=2)
    # Checkbox for users to decide whether to use caching.
    use_cache_input = gr.Checkbox(label="Use Cache", value=True)
    # Textbox to display the generated configuration.
    output_area = gr.Textbox(label="Config Output")
    # Button to create the configuration.
    submit_button = gr.Button("Create Config")

    # When the "Add Model" button is clicked, call `add_model_to_list` function.
    add_model_button.click(fn=add_model_to_list, inputs=new_model_input, outputs=model_dropdown)
    # When the "Create Config" button is clicked, call `create_config` function.
    submit_button.click(fn=create_config, inputs=[model_dropdown, num_labels_input, use_cache_input], outputs=output_area)

# Launch the Gradio interface.
demo.launch()