Spaces:
Sleeping
Sleeping
import gradio as gr | |
from smolagents import CodeAgent | |
class GradioUI: | |
def __init__(self, agent: CodeAgent): | |
"""Initialize the Gradio UI with the provided agent. | |
Args: | |
agent: An instance of CodeAgent from smolagents. | |
""" | |
self.agent = agent | |
def run_agent(self, player_name: str, role: str) -> str: | |
"""Run the agent with the cricketer analysis tool. | |
Args: | |
player_name: The name of the cricketer to analyze. | |
role: The role of the cricketer (e.g., 'batter', 'bowler'). | |
Returns: | |
A string with the analysis result or an error message. | |
""" | |
# Construct the query for the agent | |
query = f"Analyze the form of cricketer '{player_name}' as a '{role}'" | |
try: | |
# Execute the agent's run method with the query | |
response = self.agent.run(query) | |
return response | |
except Exception as e: | |
return f"Error running analysis: {str(e)}" | |
def launch(self): | |
"""Launch the Gradio interface.""" | |
# Define the UI layout using Gradio Blocks | |
with gr.Blocks(title="Cricketer Form Analyzer") as interface: | |
# Header | |
gr.Markdown("# Cricketer Form Analyzer") | |
gr.Markdown("Enter a cricketer's name and select their role to analyze their recent form and career stats.") | |
# Input section | |
with gr.Row(): | |
player_input = gr.Textbox( | |
label="Player Name", | |
placeholder="e.g., Virat Kohli", | |
lines=1 | |
) | |
role_input = gr.Dropdown( | |
choices=["batter", "bowler", "wicket_keeper", "fielder"], | |
label="Role", | |
value="batter" # Default selection | |
) | |
# Submit button | |
submit_btn = gr.Button("Analyze") | |
# Output section | |
output = gr.Textbox( | |
label="Analysis Result", | |
lines=10, | |
placeholder="Analysis will appear here..." | |
) | |
# Connect the button to the run_agent function | |
submit_btn.click( | |
fn=self.run_agent, | |
inputs=[player_input, role_input], | |
outputs=output | |
) | |
# Launch the interface | |
interface.launch() | |
# Note: This is imported and used in app.py as `GradioUI(agent).launch()` |