from smolagents import CodeAgent, HfApiModel, load_tool, tool
import datetime
import pytz
import yaml
import requests
from tools.final_answer import FinalAnswerTool
from tools.web_search import DuckDuckGoSearchTool
from tools.visit_webpage import VisitWebpageTool
from smolagents.default_tools import UserInputTool

from Gradio_UI import GradioUI


@tool
def search_arxiv(query: str) -> str:
    """A tool that searches arXiv for papers matching the query using web search
    Args:
        query: The search query string
    """
    try:
        # Initialize tools
        web_search = DuckDuckGoSearchTool(max_results=5)
        webpage_reader = VisitWebpageTool()
        
        # Perform web search with site:arxiv.org to restrict to arXiv
        search_query = f"site:arxiv.org {query}"
        search_results = web_search.forward(search_query)
        
        # Extract URLs and visit each page
        import re
        arxiv_urls = re.findall(r'\((https://arxiv\.org/[^)]+)\)', search_results)
        
        if not arxiv_urls:
            return "No arXiv papers found for the given query."
            
        papers_content = []
        for url in arxiv_urls[:3]:  # Limit to first 3 papers
            try:
                content = webpage_reader.forward(url)
                papers_content.append(f"## Paper from {url}\n{content}\n")
            except Exception as e:
                papers_content.append(f"Failed to fetch content from {url}: {str(e)}")
        
        return "\n\n".join(papers_content)
    except Exception as e:
        return f"Error searching arXiv: {str(e)}"

final_answer = FinalAnswerTool()
user_input = UserInputTool()

# 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:
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'

model = HfApiModel(
    max_tokens=2096,
    temperature=0.5,
    model_id="Qwen/Qwen2.5-Coder-32B-Instruct",  # it is possible that this model may be overloaded
    custom_role_conversions=None,
)


# 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, user_input, search_arxiv],  ## 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()