# Import the necessary libraries import gradio as gr # Import Gradio for creating the user interface # Define a dictionary with color names and their corresponding hex codes color_codes = { "red": "#FF0000", "blue": "#0000FF", "green": "#00FF00", "black": "#000000", "white": "#FFFFFF", "yellow": "#FFFF00", "purple": "#800080", "orange": "#FFA500", "pink": "#FFC0CB", "gray": "#808080", # Add additional colors as needed } # Define the function to retrieve the color code def get_color_code(color_name): """ Retrieves the hex color code based on the color name input by the user. If the color is not in the list, it returns a 'Color not found' message. """ color_name = color_name.lower() # Convert the color name to lowercase if color_name in color_codes: return color_codes[color_name] else: return "Color not found in the list. Try another color." # Set up the Gradio interface with gr.Blocks() as app: # Creates the Gradio app interface # Title and description for the app gr.Markdown("

Color Code Finder

") gr.Markdown("Enter a color name to get its hex color code.") # Input box for the color name color_name_input = gr.Textbox(label="Enter color name:", placeholder="e.g., red, blue, green") # Output box for displaying the hex code color_code_output = gr.Textbox(label="Color Code", interactive=False) # Button to find the color code find_color_button = gr.Button("Find Color Code") # Define the button's action to call get_color_code and output the result find_color_button.click(fn=get_color_code, inputs=color_name_input, outputs=color_code_output) # Launch the app on Hugging Face app.launch() # Launches the Gradio app