Spaces:
Sleeping
Sleeping
File size: 2,580 Bytes
3454130 128c031 deafbd7 128c031 ffabec6 128c031 ffabec6 128c031 ffabec6 128c031 ffabec6 128c031 3454130 128c031 3454130 128c031 3454130 deafbd7 128c031 deafbd7 128c031 |
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
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()` |