File size: 4,078 Bytes
087c387
 
ee279c9
0efd2dd
6d1f41e
087c387
4744e0e
79e9184
6d1f41e
087c387
f9a4722
087c387
f9a4722
087c387
6d1f41e
 
087c387
6d1f41e
08d6f20
 
a4c0588
 
 
 
087c387
f9a4722
 
a4c0588
087c387
7d66a24
087c387
6d1f41e
087c387
027ba63
2cc1407
8fc34b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
087c387
 
 
 
 
f9a4722
087c387
6d1f41e
087c387
f9a4722
087c387
ca0f3b5
087c387
3377f72
087c387
6d1f41e
 
087c387
f9a4722
087c387
f9a4722
6d1f41e
087c387
3fb27d6
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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.import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM, AutoModel

# A list of model names to start with. These are names of popular models from the Hugging Face library.
model_list = ["EleutherAI/gpt-neo-125m", "bert-base-uncased", "TigerResearch/tigerbot-70b-chat-4bit-v2", "openai/whisper-large-v3", "openai/shap-e","google/switch-c-2048", "gpt2","mosaicml/mpt-30b", 
              "google/flan-t5-small","mosaicml/mpt-7b","distilbert-base-uncased,","microsoft/speecht5_tts","suno/bark-small","suno/bark",]

# 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

    # Ensure num_labels is an integer
    num_labels = int(num_labels)

    # 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='true')
    return str(config)  # Return the configuration as a string.

# Start building the Gradio interface
with gr.Group(elem_id="UI-conf"):
    custom_css = """
.gradio-container {
    background-color: #f0f0f0; /* Light grey background */
    font-family: Arial, sans-serif;
}
.gradio-textbox {
    border: 2px solid #4CAF50; /* Green border for textboxes */
    border-radius: 5px;
}
.gradio-button {
    background-color: #4CAF50; /* Green background for buttons */
    color: white;
    border: none;
    border-radius: 5px;
    padding: 10px 20px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
    margin: 4px 2px;
    cursor: pointer;
}
.gradio-button:hover {
    background-color: #45a049; /* Darker green on hover */
}
"""

with gr.Blocks(css=custom_css) 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()