|
from tools.final_answer import FinalAnswerTool as FinalAnswer |
|
from tools.classify_topic import SimpleTool as ClassifyTopic |
|
from tools.extract_news_article_content import SimpleTool as ExtractNewsArticleContent |
|
from tools.summarize_news import SimpleTool as SummarizeNews |
|
from tools.fetch_lastest_news_titles_and_urls import SimpleTool as FetchLastestNewsTitlesAndUrls |
|
import yaml |
|
import spaces |
|
import os |
|
from smolagents import CodeAgent, TransformersModel |
|
from gradio_ui import GradioUI |
|
import torch |
|
|
|
|
|
def set_seed(seed: int): |
|
torch.manual_seed(seed) |
|
torch.cuda.manual_seed_all(seed) |
|
torch.cuda.manual_seed(seed) |
|
torch.backends.cudnn.deterministic = True |
|
torch.backends.cudnn.benchmark = False |
|
|
|
|
|
set_seed(42) |
|
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) |
|
|
|
|
|
fetch_lastest_news_titles_and_urls = FetchLastestNewsTitlesAndUrls() |
|
summarize_news = SummarizeNews() |
|
extract_news_article_content = ExtractNewsArticleContent() |
|
classify_topic = ClassifyTopic() |
|
final_answer = FinalAnswer() |
|
|
|
model = TransformersModel( |
|
max_new_tokens=2000, |
|
model_id='Qwen/Qwen2.5-Coder-3B-Instruct', |
|
) |
|
|
|
with open(os.path.join(CURRENT_DIR, "prompts.yaml"), 'r') as stream: |
|
prompt_templates = yaml.safe_load(stream) |
|
|
|
|
|
agent_news_agent = CodeAgent( |
|
model=model, |
|
tools=[fetch_lastest_news_titles_and_urls, summarize_news, |
|
extract_news_article_content, classify_topic], |
|
managed_agents=[], |
|
max_steps=20, |
|
verbosity_level=2, |
|
grammar=None, |
|
planning_interval=None, |
|
name='news_agent', |
|
description="This agent is a smart news aggregator that fetches, summarizes, and classifies real-time news updates.", |
|
executor_type='local', |
|
executor_kwargs={}, |
|
max_print_outputs_length=None, |
|
prompt_templates=prompt_templates |
|
) |
|
|
|
|
|
@spaces.GPU |
|
def run_agent(): |
|
GradioUI(agent_news_agent).launch() |
|
|
|
|
|
if __name__ == "__main__": |
|
run_agent() |
|
|