Spaces:
Sleeping
Sleeping
| import re | |
| import requests | |
| from markdownify import markdownify | |
| from requests.exceptions import RequestException | |
| from smolagents import tool, CodeAgent, ToolCallingAgent, HfApiModel, DuckDuckGoSearchTool, Tool | |
| from huggingface_hub import login | |
| import os | |
| # Создание модели | |
| model_id = "Qwen/Qwen2.5-Coder-32B-Instruct" | |
| hf_token = os.getenv('hf_token') | |
| model = HfApiModel(model_id, token=hf_token) | |
| class WebpageVisitorTool(tool.Tool): | |
| def __init__(self): | |
| self.name = "Webpage Visitor" | |
| def visit_webpage(self, url: str) -> str: | |
| """Visits a webpage at the given URL and returns its content as a markdown string. | |
| Args: | |
| url: The URL of the webpage to visit. | |
| Returns: | |
| The content of the webpage converted to Markdown, or an error message if the request fails. | |
| """ | |
| try: | |
| # Send a GET request to the URL | |
| response = requests.get(url) | |
| response.raise_for_status() # Raise an exception for bad status codes | |
| # Convert the HTML content to Markdown | |
| markdown_content = markdownify(response.text).strip() | |
| # Remove multiple line breaks | |
| markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content) | |
| return markdown_content | |
| except RequestException as e: | |
| return f"Error fetching the webpage: {str(e)}" | |
| except Exception as e: | |
| return f"An unexpected error occurred: {str(e)}" | |
| print(visit_webpage("https://en.wikipedia.org/wiki/Hugging_Face")[:500]) | |
| # Создание веб-агента | |
| web_visitor_tool = WebpageVisitorTool() | |
| web_agent = ToolCallingAgent( | |
| tools=[DuckDuckGoSearchTool(), web_visitor_tool], | |
| model=model, | |
| max_steps=10, | |
| name="web_search_agent", | |
| description="Выполняет поиск в интернете" | |
| ) | |
| # Создание менеджера-агента | |
| manager_agent = CodeAgent( | |
| tools=[], | |
| model=model, | |
| managed_agents=[web_agent], | |
| additional_authorized_imports=["time", "numpy", "pandas"] | |
| ) | |
| # Запуск системы | |
| answer = manager_agent.run("Если обучение моделей языка продолжит масштабироваться с текущим темпом до 2030 года, какова будет потребляемая электрическая мощность в ГВт, необходимая для питания крупнейших тренировочных запусков к 2030 году? Что это будет соответствовать, по сравнению с некоторыми странами? Пожалуйста, предоставьте источник для любых использованных чисел.") | |
| print(answer) | |