from smolagents import Tool, tool from langchain_community.tools.tavily_search import TavilySearchResults import requests import inspect import pandas as pd from PIL import Image from io import BytesIO import base64 from src.final_assignment_template.models import videoLiteLLm, imageLiteLLm DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" travily_tool = Tool.from_langchain(TavilySearchResults(max_results=25,)) from smolagents import Tool # class SearchTool(Tool): # name = "SearchTool" # description = """ # This is a tool that returns the most downloaded model of a given task on the Hugging Face Hub. # It returns the name of the checkpoint.""" # inputs = { # "task": { # "type": "string", # "description": "the task category (such as text-classification, depth-estimation, etc)", # } # } # output_type = "string" # def forward(self, task: str): # from huggingface_hub import list_models # model = next(iter(list_models(filter=task, sort="downloads", direction=-1))) # return model.id # model_downloads_tool = HFModelDownloadsTool() @tool def Video_understanding_tool(query:str)->str: """ This tool for understanding or finding something in the video link. Args: query: link with your query. """ print("processcing vidoe ",query) messages =[ {"role": "user", "content": [{"type": "text", "text": query}]} ] resp = videoLiteLLm(messages) return resp.content or 'No data' @tool def get_task_file(task_id:str)->requests.models.Response: """ This tool is for get the task file using task_id. it will return the request response and then this response will be used for other tools. Args: task_id: Task ID """ url = f"{DEFAULT_API_URL}/files/{task_id}" print(url) headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36" } response = requests.get(url,headers=headers) return response @tool def image_understanding_tool(query:str,response:requests.models.Response)->str: """ This tool for understanding or perform any query on the image. Provide the image base64 image data Args: query: Query for the image. response : The return value from the get_task_file which returns the response. """ print("processcing image ") image = Image.open(BytesIO(response.content)).convert("RGB") buffered = BytesIO() image.save(buffered, format="PNG") # change format if necessary img_bytes = buffered.getvalue() img_b64 = base64.b64encode(img_bytes).decode('utf-8') print(img_b64) messages =[ { "role": "user", "content": [ {"type": "text", "text": query}, { "type": "image_url", "image_url": { "url": img_b64, "format": "image/png" # Adjust MIME type if necessary } } ] } ] resp = imageLiteLLm(messages) print(resp.content) return resp.content or 'No data'