Spaces:
Sleeping
Sleeping
| from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool | |
| import datetime | |
| import requests | |
| import pytz | |
| import yaml | |
| from tools.final_answer import FinalAnswerTool | |
| from Gradio_UI import GradioUI | |
| # Below is an example of a tool that get Advice for Life, Personal Growth, Career, Health ! | |
| def generate_advice(topic: str = None) -> str: | |
| """ | |
| Generates a random piece of advice, optionally based on a given topic. | |
| Args: | |
| topic: Optional topic for the advice (can be None) | |
| Returns: | |
| A string containing a piece of advice | |
| """ | |
| # Predefined advice categories | |
| advice_collections = { | |
| 'life': [ | |
| "Always be kind to yourself and others.", | |
| "Learn something new every day.", | |
| "Take time to appreciate the small moments.", | |
| "Don't be afraid to step out of your comfort zone.", | |
| "Practice gratitude daily." | |
| ], | |
| 'personal_growth': [ | |
| "Read books that challenge your perspective.", | |
| "Set realistic goals and work towards them consistently.", | |
| "Embrace failure as a learning opportunity.", | |
| "Develop a growth mindset.", | |
| "Practice self-reflection regularly." | |
| ], | |
| 'career': [ | |
| "Network with people in your industry.", | |
| "Continuously upgrade your skills.", | |
| "Be open to feedback and constructive criticism.", | |
| "Find a mentor who can guide you.", | |
| "Stay curious and adaptable." | |
| ], | |
| 'health': [ | |
| "Prioritize sleep and rest.", | |
| "Stay hydrated and eat balanced meals.", | |
| "Exercise regularly, even if it's just a short walk.", | |
| "Practice mindfulness and stress management.", | |
| "Listen to your body and its needs." | |
| ] | |
| } | |
| # If a specific topic is provided | |
| if topic and topic.lower() in advice_collections: | |
| return random.choice(advice_collections[topic.lower()]) | |
| # If no topic or invalid topic, choose from all advice | |
| all_advice = [ | |
| advice for category in advice_collections.values() for advice in category | |
| ] | |
| return random.choice(all_advice) | |
| # Load existing prompt templates | |
| with open("prompts.yaml", 'r') as stream: | |
| prompt_templates = yaml.safe_load(stream) | |
| #final_answer = FinalAnswerTool() | |
| # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder: | |
| # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' | |
| # Initialize the model | |
| model = HfApiModel( | |
| max_tokens=1024, # Reduced token limit | |
| temperature=0.7, | |
| model_id='Qwen/Qwen2.5-Coder-32B-Instruct', | |
| custom_role_conversions=None | |
| ) | |
| # Create the agent with air quality tool | |
| agent = CodeAgent( | |
| model=model, | |
| tools=[generate_advice, FinalAnswerTool()], | |
| max_steps=3, # Reduced steps | |
| verbosity_level=1, | |
| grammar=None, | |
| planning_interval=None, | |
| name="Advice Generator Agent", | |
| description="A simple agent that provides random advice on various topics", | |
| prompt_templates=prompt_templates | |
| ) | |
| # Launch the Gradio UI | |
| GradioUI(agent).launch() |