File size: 3,438 Bytes
d1479d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
117
118
119
120
121
122
123
124
125
126
127
import os
from smolagents import Tool, tool
from huggingface_hub import HfApi
from dotenv import load_dotenv
from huggingface_hub import InferenceClient
from utils import upload_file
load_dotenv()
HF_TOKEN = os.environ.get("HF_TOKEN")

api = HfApi()
client = InferenceClient(
    provider="hf-inference",
    api_key=HF_TOKEN,
)

# --- Constants ---
local_data_path = "../data"

if not os.path.exists(local_data_path):
    os.makedirs(local_data_path)



@tool
def image_question_answering(image_path: str, prompt: str) -> str:
    """
    This function takes a image path and a prompt, and returns the answer to the question.
    Args:
        image_path: The path to the image file
        prompt: The prompt to the question
    Returns:
        The answer to the question
    """
    file_extension = image_path.split(".")[-1]
    if file_extension in [".mp4", ".avi", ".mov", ".wmv", ".mkv", ".webm"]:
        return "Media type not supported. Please upload an image."
   
    if image_path.startswith("http"):
        media_url = image_path
    else:
        media_url = upload_file(image_path)

    
    messages = [
                {
                    "role": "user",
                    "content": [
                        {
                            "type": "text",
                            "text": prompt,
                        },
                        {
                            "type": "image_url",
                            "image_url": {"url": media_url},
                        }
                    ],
                }
            ]

    completion = client.chat.completions.create(
            model="meta-llama/Llama-3.2-11B-Vision-Instruct",
            messages=messages,
    )
    return completion.choices[0].message


@tool
def transcribe_audio(file_local_path: str) -> str:
    """
    Transcribe the audio file and return the transcript
    Args:
        file_local_path: The local path to the audio file
    Returns:
        The transcript of the audio file
    """
    file_url = upload_file(file_local_path)

    asr_tool = Tool.from_space(
        "hf-audio/whisper-large-v3",
        api_name="/predict_1", # from file
        name="transcribe_audio",
        description="Use this tool to transcribe the audio"
    )

    transcript = asr_tool(file_url)
    return transcript


class GetFileTool(Tool):
    name = "get_file"
    description = "Download a file from the given file name"
    inputs = {
        "file_name": {
            "type": "string",
            "description": "Download the file from the given file name and outputs the local path"
        }

    }
    output_type = "string"

    def forward(self, file_name: str) -> str:
        import requests

        if file_name == "":
            return "No file name provided"

        task_id = file_name.split(".")[0]
        url = f"https://agents-course-unit4-scoring.hf.space/files/{task_id}"
        headers = {
            "accept": "application/json"
        }
        req = requests.get(url, headers=headers)

        if req.status_code != 200:
            return "File not found, please check the file name and try again."
        
        local_file_path = local_data_path + "/" + file_name

        with open(local_file_path, "wb") as f:
            f.write(req.content)
        
        print(f"File saved to {local_file_path}. You can read this file to process its contents.")
        return local_file_path