File size: 1,285 Bytes
f454e61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from langchain_core.tools import BaseTool
from video_question_answer import VideoQuestionAnswer
from pydantic import BaseModel, Field
from typing import Type
import os
from youtube_video_downloader import YoutubeVideoDownloader

class YoutubeVideoQuestionAnswerToolArgs(BaseModel):
    url: str = Field(description="The url of the youtube video")
    question: str = Field(description="The question to answer")

class YoutubeVideoQuestionAnswerTool(BaseTool):
    name: str = "youtube_video_question_answer"
    description: str = "Answer the question based on the youtube video."
    args_schema: Type[BaseModel] = YoutubeVideoQuestionAnswerToolArgs

    def _run(self, url: str, question: str) -> str:
        if url is None:
            return "Error: Video not found."
        # Answer the question
        answer = VideoQuestionAnswer().answer(url, question)
        return answer

    '''def _run(self, url: str, question: str) -> str:
        if url is None:
            return "Error: Video not found."
        # Download the video
        video_path = YoutubeVideoDownloader().download_video(url)
        # Answer the question
        answer = VideoQuestionAnswer().answer(video_path, question)
        # Delete the video
        os.remove(video_path)
        return answer'''