Unit_4_Final_Assignment / video_question_answer.py
edwin_ang_ph
test upload
f454e61
Raw
History Blame Contribute Delete
2.27 kB
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?")'''