File size: 3,175 Bytes
ac0f9ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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'