Spaces:
Sleeping
Sleeping
File size: 4,647 Bytes
9b5b26a c19d193 6aae614 8fe992b 9b5b26a 8912d86 f244277 8912d86 9b5b26a 5df72d6 9b5b26a 8912d86 9b5b26a 8912d86 9b5b26a 8912d86 9b5b26a 8912d86 9b5b26a 8c01ffb 8912d86 404b3ec 8912d86 404b3ec 8912d86 9d69367 8912d86 f244277 8912d86 450662f f244277 450662f f244277 8912d86 450662f 8c01ffb 6aae614 f244277 404b3ec f244277 8c01ffb 9b5b26a 8c01ffb 861422e 9b5b26a 8c01ffb 8fe992b 7da0b86 8c01ffb 861422e 8fe992b 9b5b26a 8c01ffb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | 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() |