Spaces:
Running
Running
import gradio as gr | |
import os | |
from langchain_community.agent_toolkits.load_tools import load_tools | |
from langchain.agents import initialize_agent, AgentType | |
from langchain_community.llms import OpenAI | |
ICON_URL = "https://diegoromero.es/wp-content/uploads/2021/10/cropped-network-icon-3.png" | |
def get_response(input_text, google_cse_id, google_api_key, openai_api_key): | |
try: | |
os.environ["GOOGLE_CSE_ID"] = google_cse_id | |
os.environ["GOOGLE_API_KEY"] = google_api_key | |
os.environ["OPENAI_API_KEY"] = openai_api_key | |
llm = OpenAI(model_name="gpt-4.1-mini", temperature=0) | |
tools = load_tools(["google-search", "llm-math"], llm=llm) | |
agent = initialize_agent( | |
tools, llm, | |
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, | |
verbose=True, | |
return_intermediate_steps=True | |
) | |
response = agent({"input": input_text}) | |
return response["output"], response["intermediate_steps"] | |
except Exception as e: | |
error_msg = f"Error interno: {e}" | |
return "", [{"error": error_msg}] | |
with gr.Blocks() as demo: | |
# T铆tulo con icono | |
gr.Markdown(f"<img src='{ICON_URL}' alt='icon' style='height:24px;vertical-align:middle;margin-right:8px;'>" | |
f"**GPT Agents Demo**") | |
with gr.Row(): | |
# Columna de configuraci贸n (colapsable) | |
with gr.Column(scale=1): | |
with gr.Accordion("API Keys e IDs", open=False): | |
google_cse_id = gr.Textbox(label="Google CSE - ID:", type="text") | |
google_api_key = gr.Textbox(label="Google API KEY:", type="password") | |
openai_api_key = gr.Textbox(label="OpenAI API KEY:", placeholder="sk-...", type="password") | |
# Columna de Inputs/Outputs | |
with gr.Column(scale=2): | |
goal_input = gr.Textbox(label="Goal definition:") | |
goal_output = gr.Textbox(label="Goal output:") | |
intermediate_steps = gr.JSON(label="Intermediate Steps") | |
run_btn = gr.Button("Run") | |
# Evento de ejecuci贸n | |
run_btn.click( | |
fn=get_response, | |
inputs=[goal_input, google_cse_id, google_api_key, openai_api_key], | |
outputs=[goal_output, intermediate_steps] | |
) | |
# 1) Configura la cola antes de lanzar | |
demo.queue(max_size=20, default_concurrency_limit=1) | |
# 2) Lanza solo con par谩metros permitidos | |
demo.launch( | |
server_port=7860 # puerto de escucha | |
) |