Gradio Agent
Learn how to create web interfaces for your AI agents using Gradio
Quick Start
- Install Dependencies
First, install the required packages:
bash pip install praisonaiagents gradio - Create Script
Create a new file
app.py:python import gradio as gr from praisonaiagents import Agent, Tools from praisonaiagents import duckduckgo def research(query): agent = Agent(instructions="You are a Research Agent", tools=[duckduckgo]) result = agent.start(query) # Format the result with enhanced markdown formatted_result = f""" {result} ---- *Generated by PraisonAI Research Assistant* """ return formatted_result # Create a simple Gradio interface demo = gr.Interface( fn=research, inputs=gr.Textbox( label="Research Query", placeholder="Enter your research topic...", lines=2 ), outputs=gr.Markdown( show_copy_button=True, height=500, container=True ), title="AI Research Assistant", description="Enter your research query below to get started!", ) if __name__ == "__main__": demo.launch() - Run Application
Run your Gradio app:
bash python app.py
Features
Simple Interface
Create beautiful UIs with minimal code.
Markdown SupportRich text output with built-in markdown rendering.
Copy ButtonOne-click copying of results.
Responsive DesignMobile-friendly interface out of the box.
Understanding the Code
The example demonstrates a simple research assistant with these key components:
- Function Definition:
research()function that processes user input- Agent initialization and execution
- Result formatting with markdown
- Interface Setup:
- Input textbox configuration
- Markdown output with copy button
- Title and description settings
- Launch Configuration:
- Main entry point check
- Server launch with default settings
Customization
You can enhance the UI with additional Gradio components:
# Add multiple input types
demo = gr.Interface(
fn=process_inputs,
inputs=[
gr.Textbox(label="Query"),
gr.File(label="Upload Document"),
gr.Dropdown(choices=["Option 1", "Option 2"])
],
outputs=[
gr.Markdown(label="Results"),
gr.Plot(label="Visualization")
]
)
# Add themes and styling
demo = gr.Interface(
...
theme="default",
css=".gradio-container {background-color: #f0f0f0}"
)
# Add authentication
demo.launch(auth=("username", "password"))Next Steps
- Learn about Prompt Chaining for complex UI workflows
- Explore Evaluator Optimizer for improving responses
- Check out Streamlit Integration for an alternative UI framework