File size: 1,237 Bytes
c71c76a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import google.generativeai as genai

def hex_to_words(api_key, hex_input):
    # Initialize Gemini with API key
    try:
        genai.configure(api_key=api_key)
        model = genai.GenerativeModel('gemini-1.5-pro')
    except Exception as e:
        return f"Error with API key or model: {str(e)}"
    
    prompt = f"Convert the following hex color codes to their closest color names. Don't worry about exact matches. Example format: #ffc0cb #0000FF → pink blue\n\nNow convert:\n{hex_input}\nOnly return the color names."
    
    try:
        response = model.generate_content(prompt)
        return response.text.strip()
    except Exception as e:
        return f"Error generating response: {str(e)}"

# Gradio UI
with gr.Blocks() as demo:
    gr.Markdown("## 🎨 Hex Color to Word with Gemini 1.5 Pro")
    
    api_key_input = gr.Textbox(label="Gemini API Key", type="password", placeholder="Enter your Gemini 1.5 Pro API key")
    hex_input = gr.Textbox(label="Hex Color Input", placeholder="#ffc0cb #0000FF #008000")
    output = gr.Textbox(label="Output (Color Words)")
    
    btn = gr.Button("Submit")
    btn.click(fn=hex_to_words, inputs=[api_key_input, hex_input], outputs=output)

demo.launch()