MattGPT commited on
Commit
087c387
1 Parent(s): 8bb20f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -10
app.py CHANGED
@@ -1,33 +1,49 @@
1
- import gradio as gr
2
- from transformers import AutoConfig
 
3
 
4
- # Initialize with some common models
5
  model_list = ["bert-base-uncased", "gpt2", "distilbert-base-uncased"]
6
 
 
7
  def add_model_to_list(new_model):
 
8
  if new_model and new_model not in model_list:
9
- model_list.append(new_model)
10
  return model_list
11
- allow_custom_value=True
12
 
 
13
  def create_config(model_name, num_labels, use_cache):
 
14
  if model_name not in model_list:
15
  model_list.append(model_name)
 
16
  config = AutoConfig.from_pretrained(model_name, num_labels=num_labels, use_cache=use_cache)
17
- return str(config)
18
 
 
19
  with gr.Blocks() as demo:
20
- gr.Markdown("## Config Class - Transformers")
21
- with gr.Row():
22
- model_dropdown = gr.Dropdown(label="Select a Model", choices=model_list, value=model_list[0], allow_custom_value=True)
 
 
23
  new_model_input = gr.Textbox(label="Add a New Model", placeholder="Enter model name")
 
24
  add_model_button = gr.Button("Add Model")
 
25
  num_labels_input = gr.Number(label="Number of Labels", value=2)
 
26
  use_cache_input = gr.Checkbox(label="Use Cache", value=True)
27
- output_area = gr.Textbox(label="Config Output") # Removed readonly=True
 
 
28
  submit_button = gr.Button("Create Config")
29
 
 
30
  add_model_button.click(fn=add_model_to_list, inputs=new_model_input, outputs=model_dropdown)
 
31
  submit_button.click(fn=create_config, inputs=[model_dropdown, num_labels_input, use_cache_input], outputs=output_area)
32
 
 
33
  demo.launch()
 
1
+ # Import necessary libraries
2
+ import gradio as gr # Gradio is used to create web interfaces for Python scripts.
3
+ from transformers import AutoConfig # AutoConfig is from the Hugging Face Transformers library, used to create configuration for various models.
4
 
5
+ # A list of model names to start with. These are names of popular models from the Hugging Face library.
6
  model_list = ["bert-base-uncased", "gpt2", "distilbert-base-uncased"]
7
 
8
+ # Function to add a new model to the list.
9
  def add_model_to_list(new_model):
10
+ # Check if the new model is not already in the list and is not an empty string.
11
  if new_model and new_model not in model_list:
12
+ model_list.append(new_model) # Add the new model to the list.
13
  return model_list
 
14
 
15
+ # Function to create a configuration for the selected model.
16
  def create_config(model_name, num_labels, use_cache):
17
+ # If the selected model is not in the list, add it (this is a safety check).
18
  if model_name not in model_list:
19
  model_list.append(model_name)
20
+ # Create a configuration for the selected model using AutoConfig.
21
  config = AutoConfig.from_pretrained(model_name, num_labels=num_labels, use_cache=use_cache)
22
+ return str(config) # Return the configuration as a string.
23
 
24
+ # Start building the Gradio interface
25
  with gr.Blocks() as demo:
26
+ gr.Markdown("## Config Class - Transformers") # Display a title for the web interface.
27
+ with gr.Row(): # Create a row in the interface to organize elements horizontally.
28
+ # Dropdown menu to select a model.
29
+ model_dropdown = gr.Dropdown(label="Select a Model", choices=model_list, value=model_list[0], allow_custom_value=True)
30
+ # Textbox for users to input a new model name.
31
  new_model_input = gr.Textbox(label="Add a New Model", placeholder="Enter model name")
32
+ # Button to add the new model to the dropdown list.
33
  add_model_button = gr.Button("Add Model")
34
+ # Numeric input for the number of labels (used in the model configuration).
35
  num_labels_input = gr.Number(label="Number of Labels", value=2)
36
+ # Checkbox for users to decide whether to use caching.
37
  use_cache_input = gr.Checkbox(label="Use Cache", value=True)
38
+ # Textbox to display the generated configuration.
39
+ output_area = gr.Textbox(label="Config Output")
40
+ # Button to create the configuration.
41
  submit_button = gr.Button("Create Config")
42
 
43
+ # When the "Add Model" button is clicked, call `add_model_to_list` function.
44
  add_model_button.click(fn=add_model_to_list, inputs=new_model_input, outputs=model_dropdown)
45
+ # When the "Create Config" button is clicked, call `create_config` function.
46
  submit_button.click(fn=create_config, inputs=[model_dropdown, num_labels_input, use_cache_input], outputs=output_area)
47
 
48
+ # Launch the Gradio interface.
49
  demo.launch()