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 from newsapi import NewsApiClient from smolagents.models import OpenAIServerModel # Init NEWSAPI_API_KEY='cbcbfa28426a47b2bda9d246cefbf588' newsapi = NewsApiClient(api_key=NEWSAPI_API_KEY) def retrieve_news(topic, api_key, page_size=3): url = f"https://newsapi.org/v2/everything?q={topic}&apiKey={api_key}&language=en&sortBy=publishedAt&pageSize={page_size}" response = requests.get(url) return response.json() # Below is an example of a tool that does nothing. Amaze us with your creativity ! @tool def get_news(topic:str)-> str: #it's import to specify the return type #Keep this format for the description / args / args description but feel free to modify the tool """A tool that returns the first news about a given topic Args: topic: the topic you want to get news about. Should be one word """ news = newsapi.get_top_headlines(q=topic, language='en') articles = "\n\n".join([f"Title: {article['title']}\nDescription: {article['description']}\nContent: {article['content']}\nLink: {article['url']}" for article in news['articles']]) return articles @tool def get_current_time_in_timezone(timezone: str) -> str: """A tool that fetches the current local time in a specified timezone. Args: timezone: A string representing a valid timezone (e.g., 'America/New_York'). """ try: # Create timezone object tz = pytz.timezone(timezone) # Get current time in that timezone local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S") return f"The current local time in {timezone} is: {local_time}" except Exception as e: return f"Error fetching time for timezone '{timezone}': {str(e)}" from tts_gen import TTSGenerator, translit2rus import random sample_rate = 16000 tts = TTSGenerator(sample_rate) silero_voices = tts.get_all_voices() import tempfile, os, uuid def get_random_voice(voices): return voices[random.randint(0, len(voices)-1)] def silero_voice_generator(sentence): voice_id = get_random_voice(silero_voices) return tts.generate_voice(text = sentence, speaker = voice_id) @tool def text_to_voice(sentence: str) -> bytes: """ A tool that converts a sentence into a voice audio file and returns its binary content. Args: sentence: The sentence to convert to speech. Returns: A link to audio file to download. """ voice = silero_voice_generator(sentence) temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") temp_file.write(voice.read()) temp_file.close() try: with open(temp_file.name, "rb") as f: response = requests.post( "https://catbox.moe/user/api.php", data={"reqtype": "fileupload"}, files={"fileToUpload": f} ) public_url = response.text.strip() except Exception as e: public_url = f"Upload failed, error: {str(e)}" os.remove(temp_file.name) return public_url final_answer = FinalAnswerTool() #model = HfApiModel( # max_tokens=2096, # temperature=0.5, # #model_id='https://wxknx1kg971u7k1n.us-east-1.aws.endpoints.huggingface.cloud',# it is possible that this model may be overloaded # model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded # #model_id='mistralai/Mistral-Small-24B-Instruct-2501', # #model_id="deepseek-ai/DeepSeek-V3",# it is possible that this model may be overloaded # custom_role_conversions=None, #) mistral_api_key = os.environ.get("MISTRAL_API_KEY", "NZmDhwQK9aiz8qj95ZHdWnKTEKd2vUk7") # Set your API key in the environment model = OpenAIServerModel(model_id='mistral-small-latest', api_base='https://api.mistral.ai/v1', api_key=mistral_api_key) # Import tool from Hub image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True) with open("prompts.yaml", 'r') as stream: prompt_templates = yaml.safe_load(stream) agent = CodeAgent( model=model, tools=[final_answer, image_generation_tool, get_current_time_in_timezone, get_news, text_to_voice], ## add your tools here (don't remove final answer) max_steps=6, verbosity_level=1, grammar=None, planning_interval=None, name=None, description=None, prompt_templates=prompt_templates ) GradioUI(agent).launch()