File size: 5,060 Bytes
dea5ddd 907dbc4 087c387 ee279c9 6d1f41e 087c387 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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
css = """
/* Define the background colors for dark and light squares */
.dark-square {
background-color: #555;
}
.light-square {
background-color: #ccc;
}
/* Style the column containers to have the appropriate background color */
.col {
flex: 1 0 8.33333%;
margin: 0 5px;
text-align: center;
padding: 10px;
}
/* Alternate the background colors between dark and light squares */
.layout .col:nth-child(odd) .col:nth-child(odd) {
background-color: #555;
}
.layout .col:nth-child(even) .col:nth-child(odd) {
background-color: #ccc;
}
.layout .col:nth-child(odd) .col:nth-child(even) {
background-color: #ccc;
}
.layout .col:nth-child(even) .col:nth-child(even) {
background-color: #555;
}
"""
# 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
def generate_grid():
with gr.Blocks(css=css, label="The Big Gridio", elem_class="layout", elem_id="grid-lg") as grid:
for row in range(12):
with gr.Row() as row_container:
for col in range(12):
with gr.Column() as col_container:
col_container.style.elem_class = f"col-{col}"
col_container.style.elem_id = f"col-{col}"
# Add any additional content or components to the column container here
gr.Label(f"Row: {row} | Col: {col}")
generate_grid()
# 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
# 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()
|