Hex2Color / app.py
mroccuper's picture
Create app.py
c71c76a verified
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()