DamDD/Solar-system-expert
0
1import os2from huggingface_hub import login3from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool4import datetime5import requests6import pytz7import yaml8from tools.final_answer import FinalAnswerTool9 10from Gradio_UI import GradioUI11 12login(token = os.getenv('AgentCourseJD'))13 14@tool15def get_body_info(body_id: str) -> str:16 """Fetch general information about a given solar system object.17 18 Args:19 body_id: Name of the body (e.g., "Phobos").20 21 Returns:22 A summary of the body's details or an error message.23 """24 url = f"https://api.le-systeme-solaire.net/rest.php/bodies/{body_id}"25 26 try:27 response = requests.get(url)28 if response.status_code == 200:29 data = response.json()30 # Extract details31 name = data.get("name", "Unknown")32 density = data.get("density", "Unknown")33 gravity = data.get("gravity", "Unknown")34 35 #moons = ", ".join(data.get("moons", {}).values()) if "moons" in data else "None"36 37 return (f"Name: {name}\n"38 f"Density: {density}\n"39 f"Gravity: {gravity}\n")40 41 else:42 return f"Error: Could not retrieve data for {body_id} (Status: {response.status_code})"43 44 except Exception as e:45 return f"Error fetching data: {str(e)}"46 47@tool48def get_current_time_in_timezone(timezone: str) -> str:49 """A tool that fetches the current local time in a specified timezone.50 Args:51 timezone: A string representing a valid timezone (e.g., 'America/New_York').52 """53 try:54 # Create timezone object55 tz = pytz.timezone(timezone)56 # Get current time in that timezone57 local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")58 return f"The current local time in {timezone} is: {local_time}"59 except Exception as e:60 return f"Error fetching time for timezone '{timezone}': {str(e)}"61 62 63final_answer = FinalAnswerTool()64 65# 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:66# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' 67 68model = HfApiModel(69max_tokens=2096,70temperature=0.5,71model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded72custom_role_conversions=None,73)74 75 76# Import tool from Hub77image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)78 79with open("prompts.yaml", 'r') as stream:80 prompt_templates = yaml.safe_load(stream)81 82agent = CodeAgent(83 model=model,84 tools=[final_answer,get_current_time_in_timezone,image_generation_tool,get_body_info], ## add your tools here (don't remove final answer)85 max_steps=6,86 verbosity_level=1,87 grammar=None,88 planning_interval=None,89 name=None,90 description=None,91 prompt_templates=prompt_templates92)93 94 95GradioUI(agent).launch()