File size: 1,380 Bytes
5e433de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import os
import subprocess
import webbrowser
from groq import Groq


# Function to validate and save the API key
def validate_api_key(user_api_key):
    global api_key
    if not user_api_key:
        return "❌ Please enter your Groq Cloud API key."

    try:
        # Initialize Groq client
        client = Groq(api_key=user_api_key)

        # Make a test request
        response = client.chat.completions.create(
            messages=[{"role": "user", "content": "Hello"}],
            model="llama-3.1-8b-instant"
        )

        # Save API key to env variable if successful
        api_key = user_api_key
        os.environ["GROQ_API_KEY"] = api_key

        return "βœ… API key is valid and saved!"
    except Exception as e:
        return f"❌ Invalid API key: {str(e)}"

# Gradio Interface
with gr.Blocks() as demo:
    gr.Markdown("## πŸ” Enter Your Groq Cloud API Key")
    gr.Markdown("You can create an API key at [Groq Cloud Console](https://console.groq.com/keys)")

    api_key_input = gr.Textbox(label="Groq Cloud API Key", type="password", placeholder="sk-...")
    submit_button = gr.Button("Validate API Key")
    output_text = gr.Textbox(label="Status", interactive=False)

    # Use the validation function on button click
    submit_button.click(fn=validate_api_key, inputs=api_key_input, outputs=output_text)

demo.launch()