File size: 1,528 Bytes
f589ad8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
import gradio as gr
from openai import OpenAI

def run_agent(openai_key, user_prompt):
    if not openai_key:
        return "Please enter your OpenAI API Key first!"
    
    try:
        # Initialize the OpenAI client right inside the function
        client = OpenAI(api_key=openai_key)
        
        # Simple agent/LLM call (adjust the prompt/system message to match your specific agent logic)
        response = client.chat.completions.create(
            model="gpt-4o-mini",  # Highly capable and fast default
            messages=[
                {"role": "system", "content": "You are a helpful AI Descriptor agent."},
                {"role": "user", "content": user_prompt}
            ]
        )
        return response.choices[0].message.content
    except Exception as e:
        return f"Error: {str(e)}"

# Define a clean Gradio Interface
with gr.Blocks() as demo:
    gr.Markdown("# AI Descriptor Agent")
    
    with gr.Row():
        key_input = gr.Textbox(
            label="1. Enter your OpenAI API Key", 
            placeholder="sk-proj-...", 
            type="password"
        )
        
    with gr.Row():
        prompt_input = gr.Textbox(
            label="2. Ask the Agent anything", 
            placeholder="Describe what you need..."
        )
        
    submit_btn = gr.Button("Run Agent")
    output_text = gr.Textbox(label="Agent Response")
    
    submit_btn.click(
        fn=run_agent, 
        inputs=[key_input, prompt_input], 
        outputs=output_text
    )

demo.launch()