File size: 3,886 Bytes
6bae69e
 
 
 
 
 
 
 
 
da66358
4d799f7
6bae69e
 
62ed5af
6bae69e
 
 
 
 
 
 
 
 
 
 
 
 
 
62ed5af
6bae69e
62ed5af
6bae69e
 
 
 
 
 
 
 
 
 
62ed5af
6bae69e
 
62ed5af
6bae69e
 
 
 
 
62ed5af
 
6bae69e
 
62ed5af
6bae69e
62ed5af
6bae69e
 
62ed5af
6bae69e
 
 
 
 
62ed5af
6bae69e
 
 
62ed5af
da66358
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62ed5af
4ad672b
6bae69e
 
 
 
 
 
 
 
 
 
 
4ad672b
6bae69e
 
 
 
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
from langchain_core.tools import tool
import dotenv
dotenv.load_dotenv()
from langchain_tavily import TavilySearch, TavilyExtract
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage
from langchain_community.tools.riza.command import ExecPython
from langchain_community.tools import BraveSearch
from langchain_community.document_loaders import AssemblyAIAudioTranscriptLoader
from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
from typing import Optional
import base64
import pandas as pd

@tool
def describe_image_tool(image_path: str, prompt: str = "Describe the following image:") -> str:
    """
    Describe an image using a vision language model.
    Args:
        image_path (str): Path to the image file.
        prompt (str): Prompt to guide the description.
    Returns:
        str: Description of the image.
    """
    print(f"Describing image: {image_path} with prompt: {prompt}")
    vision_llm = ChatOpenAI(model="gpt-4o")
    with open(image_path, "rb") as image_file:
        image_content = image_file.read()

    image_base64 = base64.b64encode(image_content).decode('utf-8')

    message = [
        HumanMessage(
            content=[
                {"type": "text", "text": prompt},
                {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_base64}"}}
            ]
        )
    ]
    response = vision_llm.invoke(message)
    return response.content if response else f"[Image description for {image_path} with prompt '{prompt}' not available."

@tool
def parse_excel_tool(file_path: str) -> str:
    """
    Parse an Excel file and return its content as a string.
    Args:
        file_path (str): Path to the Excel file.
    Returns:
        str: Content of the Excel file as a string.
    """
    try:
        df = pd.read_excel(io=file_path)
        return df.to_string() if not df.empty else "The Excel file is empty."
    except Exception as e:
        return f"Error parsing Excel file: {e}"

@tool
def audio_file_transcriber(file_path: str) -> str :
    """
    Transcribe an audio file
    Args:
        file_path (str): the file path of the audio file
    Returns:
        str: A transcription of the audio
    """
    loader = AssemblyAIAudioTranscriptLoader(file_path=file_path)
    docs = loader.load()
    return docs[0].page_content

@tool
def get_youtube_transcript(video_url: str, lang: Optional[str] = 'en') -> str:
    """Extracts and returns the transcript of a YouTube video.

    Args:
        video_url (str): The full YouTube video URL.
        lang (Optional[str]): The language of the transcript. Defaults to 'en'.

    Returns:
        str: The full transcript as a string, or a message if not available.
    """
    import re

    video_id_match = re.search(r"(?:v=|youtu.be/)([\w-]{11})", video_url)
    if not video_id_match:
        return "Invalid YouTube URL"

    video_id = video_id_match.group(1)

    try:
        transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
        transcript = transcript_list.find_transcript([lang])
        transcript_text = " ".join([entry['text'] for entry in transcript.fetch()])
        return transcript_text
    except TranscriptsDisabled:
        return "Transcript is disabled for this video."
    except NoTranscriptFound:
        return f"No transcript found in language: {lang}"
    except Exception as e:
        return f"Error retrieving transcript: {str(e)}"

brave_web_search = BraveSearch.from_search_kwargs({"max_results": 4})


# search_tool = TavilySearch(
#     max_results=5,
#     include_answer="basic",
#     include_images=True,
#     include_image_descriptions=True,
#     search_depth="advanced"
# )

webpage_extraction_tool = TavilyExtract(
    extract_depth="basic",
    include_images=True
)

python_code_interpreter_tool = ExecPython()