| import os |
| from huggingface_hub import login |
|
|
| def main(): |
| |
| token = os.getenv("hugging_face_api") |
|
|
| if token is None: |
| raise ValueError("Hugging Face token not found in environment variables.") |
|
|
| |
| login(token=token) |
|
|
| |
| print("Successfully logged in to Hugging Face Hub.") |
|
|
| if __name__ == "__main__": |
| main() |
|
|
| from smolagents import tool , HfApiModel, CodeAgent |
| import gradio as gr |
|
|
|
|
| @tool |
| def area_of_circle(radius:float)->float: |
| """Calculate the area of a circle. |
| |
| Args: |
| radius: The radius of the circle. |
| |
| Returns: |
| The area of the circle. |
| """ |
| return 3.14 * radius * radius |
|
|
| @tool |
| def area_of_rectangle(length:float, width:float)->float: |
| """Calculate the area of a rectangle. |
| |
| Args: |
| length: The length of the rectangle. |
| width: The width of the rectangle. |
| |
| Returns: |
| The area of the rectangle. |
| """ |
| return length * width |
|
|
| @tool |
| def area_of_triangle(base:float, height:float)->float: |
| """Calculate the area of a triangle. |
| |
| Args: |
| base: The base of the triangle. |
| height: The height of the triangle. |
| |
| Returns: |
| The area of the triangle. |
| """ |
| return 0.5 * base * height |
|
|
| @tool |
| def area_of_square(side:float)->float: |
| """Calculate the area of a square. |
| |
| Args: |
| side: The side of the square. |
| |
| Returns: |
| The area of the square. |
| """ |
| return side * side |
|
|
|
|
|
|
| model = HfApiModel( |
| "Qwen/Qwen2.5-72B-Instruct", |
| provider="together", |
| max_tokens=4096, |
| temperature=0.1 |
| ) |
|
|
| agent=CodeAgent( |
| model=model, |
| tools=[area_of_circle, area_of_rectangle, area_of_triangle, area_of_square], |
| max_steps=10, |
| verbosity_level=2.0, |
| additional_authorized_imports=['math','numpy'] |
| ) |
|
|
| def run_agent(task: str) -> str: |
| """Run the agent with the given task and return the result.""" |
| result = agent.run(task) |
| return str(result) |
|
|
| ui = gr.Interface( |
| fn=run_agent, |
| inputs=gr.Textbox(lines=2, placeholder="Enter your task here, e.g., 'Calculate the area of a rectangle with length 5 and width 10'..."), |
| outputs="text", |
| title="Area Calculator Agent", |
| description="Enter a task to calculate the area of a shape." |
| ) |
|
|
| |
| ui.launch() |