Final_Assignment / direct_youtube_test.py
tonthatthienvu's picture
Clean repository without binary files
37cadfb
raw
history blame
4.22 kB
#!/usr/bin/env python3
"""
Direct test for YouTube video analysis tool
"""
import os
import sys
import gaia_tools
import re
# YouTube URL regex pattern
YOUTUBE_URL_PATTERN = r'(https?://)?(www\.)?(youtube\.com|youtu\.?be)/.+?(?=\s|$)'
def extract_youtube_url(text):
"""Extract YouTube URL from text"""
match = re.search(YOUTUBE_URL_PATTERN, text)
if match:
return match.group(0)
return None
# Save original function
original_analyze_youtube_video = gaia_tools.analyze_youtube_video
# Create mock function
def mock_analyze_youtube_video(video_url, question, max_frames=10):
"""Mock implementation that returns a predefined answer for bird species question"""
print(f"🎬 Mock analyzing video: {video_url}")
return """
Video Analysis Results:
Video Title: Bird Identification Challenge: Backyard Birds in Spring
Duration: 3:42
Analysis:
After careful frame-by-frame analysis of the video, the highest number of different bird species visible simultaneously is 3.
This occurs at approximately 1:23 into the video, where we can see:
1. American Robin
2. Northern Cardinal
3. Blue Jay
These three species are clearly visible in the same frame at this timestamp.
"""
def main():
"""Run direct test of YouTube video analysis"""
# Import here to avoid circular imports - needs to be done before mock setup
from question_classifier import QuestionClassifier
from main import GAIASolver
# Replace with mock - must be done after imports
gaia_tools.analyze_youtube_video = mock_analyze_youtube_video
try:
# Test question
question_text = "In the video https://www.youtube.com/watch?v=L1vXCYZAYYM, what is the highest number of bird species to be on camera simultaneously?"
# Extract URL
youtube_url = extract_youtube_url(question_text)
if not youtube_url:
print("❌ Failed to extract YouTube URL")
return
print(f"πŸ” Extracted URL: {youtube_url}")
# First check the classifier
print("🧩 Testing classifier...")
classifier = QuestionClassifier()
classification = classifier.classify_question(question_text)
print(f"πŸ“‹ Classification: {classification['primary_agent']}")
print(f"πŸ”§ Tools needed: {classification.get('tools_needed', [])}")
# Check if YouTube tool is prioritized
if "analyze_youtube_video" in classification.get('tools_needed', []):
print("βœ… PASS: analyze_youtube_video is selected as a tool")
# Check if it's the first tool
if classification.get('tools_needed', [])[0] == "analyze_youtube_video":
print("βœ… PASS: analyze_youtube_video is the FIRST tool")
else:
print("⚠️ WARN: analyze_youtube_video is not the first tool")
else:
print("❌ FAIL: analyze_youtube_video not selected for YouTube URL")
# Now test with the solver
print("\nπŸ€– Testing with full GAIASolver...")
try:
# Initialize solver
solver = GAIASolver()
# Create a simple question object
question = {
'task_id': 'youtube_direct_test',
'question': question_text
}
# Process with solver
print("πŸ“Š Solving question...")
result = solver.solve_question(question)
print("\nπŸ“ Result:")
print("-" * 50)
print(result)
print("-" * 50)
# Extract answer
if "3" in result:
print("\nβœ… Success! Found expected answer '3'")
else:
print("\n❌ Failed! Expected answer not found")
except Exception as e:
print(f"\n❌ Error initializing or running solver: {e}")
finally:
# Restore original function
gaia_tools.analyze_youtube_video = original_analyze_youtube_video
print("\nπŸ”„ Original function restored")
if __name__ == "__main__":
main()