Spaces:
Sleeping
Sleeping
Update Gradio_UI.py
Browse files- Gradio_UI.py +61 -56
Gradio_UI.py
CHANGED
|
@@ -1,68 +1,73 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
class GradioUI:
|
| 4 |
-
def __init__(self, agent):
|
| 5 |
-
|
| 6 |
-
self.interface = self._create_interface()
|
| 7 |
-
|
| 8 |
-
def _create_interface(self):
|
| 9 |
-
# Define custom CSS to hide the resize arrows and style the output
|
| 10 |
-
custom_css = """
|
| 11 |
-
/* Hide the resize arrows */
|
| 12 |
-
.output-container .svelte-1g9btlg { display: none !important; }
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
background-color: #f9f9f9; /* Light background */
|
| 22 |
-
}
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
text-decoration: none; /* Remove underline */
|
| 28 |
-
font-weight: bold; /* Bold links */
|
| 29 |
-
}
|
| 30 |
-
#output-panel a:hover {
|
| 31 |
-
text-decoration: underline; /* Underline on hover */
|
| 32 |
-
}
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
list-style-type: none; /* Remove default bullets */
|
| 37 |
-
padding-left: 0; /* Remove default padding */
|
| 38 |
-
}
|
| 39 |
-
#output-panel li {
|
| 40 |
-
margin-bottom: 10px; /* Space between list items */
|
| 41 |
-
}
|
| 42 |
"""
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
with gr.Row():
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
submit_btn.click(
|
| 59 |
-
fn=
|
| 60 |
-
|
| 61 |
-
),
|
| 62 |
-
inputs=[lat_min, lon_min, lat_max, lon_max, start_date, end_date],
|
| 63 |
outputs=output
|
| 64 |
)
|
| 65 |
-
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
|
| 68 |
-
self.interface.launch(**kwargs)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from smolagents import CodeAgent
|
| 3 |
|
| 4 |
class GradioUI:
|
| 5 |
+
def __init__(self, agent: CodeAgent):
|
| 6 |
+
"""Initialize the Gradio UI with the provided agent.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
Args:
|
| 9 |
+
agent: An instance of CodeAgent from smolagents.
|
| 10 |
+
"""
|
| 11 |
+
self.agent = agent
|
| 12 |
+
|
| 13 |
+
def run_agent(self, player_name: str, role: str) -> str:
|
| 14 |
+
"""Run the agent with the cricketer analysis tool.
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
Args:
|
| 17 |
+
player_name: The name of the cricketer to analyze.
|
| 18 |
+
role: The role of the cricketer (e.g., 'batter', 'bowler').
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
Returns:
|
| 21 |
+
A string with the analysis result or an error message.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
"""
|
| 23 |
+
# Construct the query for the agent
|
| 24 |
+
query = f"Analyze the form of cricketer '{player_name}' as a '{role}'"
|
| 25 |
+
try:
|
| 26 |
+
# Execute the agent's run method with the query
|
| 27 |
+
response = self.agent.run(query)
|
| 28 |
+
return response
|
| 29 |
+
except Exception as e:
|
| 30 |
+
return f"Error running analysis: {str(e)}"
|
| 31 |
+
|
| 32 |
+
def launch(self):
|
| 33 |
+
"""Launch the Gradio interface."""
|
| 34 |
+
# Define the UI layout using Gradio Blocks
|
| 35 |
+
with gr.Blocks(title="Cricketer Form Analyzer") as interface:
|
| 36 |
+
# Header
|
| 37 |
+
gr.Markdown("# Cricketer Form Analyzer")
|
| 38 |
+
gr.Markdown("Enter a cricketer's name and select their role to analyze their recent form and career stats.")
|
| 39 |
+
|
| 40 |
+
# Input section
|
| 41 |
with gr.Row():
|
| 42 |
+
player_input = gr.Textbox(
|
| 43 |
+
label="Player Name",
|
| 44 |
+
placeholder="e.g., Virat Kohli",
|
| 45 |
+
lines=1
|
| 46 |
+
)
|
| 47 |
+
role_input = gr.Dropdown(
|
| 48 |
+
choices=["batter", "bowler", "wicket_keeper", "fielder"],
|
| 49 |
+
label="Role",
|
| 50 |
+
value="batter" # Default selection
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# Submit button
|
| 54 |
+
submit_btn = gr.Button("Analyze")
|
| 55 |
+
|
| 56 |
+
# Output section
|
| 57 |
+
output = gr.Textbox(
|
| 58 |
+
label="Analysis Result",
|
| 59 |
+
lines=10,
|
| 60 |
+
placeholder="Analysis will appear here..."
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
# Connect the button to the run_agent function
|
| 64 |
submit_btn.click(
|
| 65 |
+
fn=self.run_agent,
|
| 66 |
+
inputs=[player_input, role_input],
|
|
|
|
|
|
|
| 67 |
outputs=output
|
| 68 |
)
|
| 69 |
+
|
| 70 |
+
# Launch the interface
|
| 71 |
+
interface.launch()
|
| 72 |
|
| 73 |
+
# Note: This is imported and used in app.py as `GradioUI(agent).launch()`
|
|
|