File size: 2,273 Bytes
4c76dbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import time
import os
from dotenv import load_dotenv
from google import genai
from google.genai import types

load_dotenv()

class VideoQuestionAnswer:
    def __init__(self):
        self.client = genai.Client(api_key=os.getenv("GEMINI_API_KEY"))

    def answer(self, youtube_url: str, question: str) -> str:
        print(f"Procesando video de YouTube: {youtube_url}")
        print(f"Pregunta: {question}")

        response = self.client.models.generate_content(
            model="gemini-2.0-flash",
            contents=types.Content(
                parts=[
                    types.Part(
                        file_data=types.FileData(file_uri=youtube_url)
                    ),
                    types.Part(text=question)
                ]
            )
        )

        print(f"Respuesta: {response.text}")
        return response.text

'''if __name__ == "__main__":
    video_question_answer = VideoQuestionAnswer()
    video_question_answer.answer("https://www.youtube.com/watch?v=L1vXCYZAYYM", "what is the highest number of bird species to be on camera simultaneously?")'''

'''class VideoQuestionAnswer:
    def __init__(self):
        self.client = genai.Client(api_key=os.getenv("GEMINI_API_KEY"))

    def answer(self, video_path: str, question: str) -> str:
        print(f"Uploading video to Google GenAI: {video_path}")
        print(f"Question: {question}")
        myfile = self.client.files.upload(file=video_path)

        # Check whether the file is ready to be used.
        while myfile.state.name == "PROCESSING":
            print("Waiting for file to be processed...")
            time.sleep(10)
            myfile = self.client.files.get(name=myfile.name)

        if myfile.state.name == "FAILED":
            print("File processing failed.")
            raise ValueError(myfile.state.name)

        response = self.client.models.generate_content(
            model="gemini-2.0-flash", contents=[myfile, question]
        )

        print(f"Video question answer: {response.text}")
        return response.text'''
    
'''if __name__ == "__main__":
    video_question_answer = VideoQuestionAnswer()
    video_question_answer.answer("birds_video.mp4", "what is the highest number of bird species to be on camera simultaneously?")'''