Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -29,6 +29,55 @@ def search_youtube_guitar_tutorials(song_name: str) -> List[Dict[str, Any]]:
|
|
| 29 |
api_key = os.getenv("YOUTUBE_API_KEY")
|
| 30 |
if not api_key:
|
| 31 |
raise ValueError("YouTube API key not found. Please set the YOUTUBE_API_KEY environment variable.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
# Initialize tools
|
| 34 |
final_answer = FinalAnswerTool()
|
|
@@ -49,7 +98,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 49 |
agent = CodeAgent(
|
| 50 |
model=model,
|
| 51 |
tools=[
|
| 52 |
-
|
| 53 |
final_answer
|
| 54 |
],
|
| 55 |
max_steps=6,
|
|
|
|
| 29 |
api_key = os.getenv("YOUTUBE_API_KEY")
|
| 30 |
if not api_key:
|
| 31 |
raise ValueError("YouTube API key not found. Please set the YOUTUBE_API_KEY environment variable.")
|
| 32 |
+
|
| 33 |
+
base_url = "https://www.googleapis.com/youtube/v3/search"
|
| 34 |
+
search_params = {
|
| 35 |
+
'part': 'snippet',
|
| 36 |
+
'q': f"{song_name} guitar tutorial",
|
| 37 |
+
'type': 'video',
|
| 38 |
+
'maxResults': 5,
|
| 39 |
+
'key': api_key
|
| 40 |
+
}
|
| 41 |
+
search_url = f"{base_url}?{urlencode(search_params)}"
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
try:
|
| 45 |
+
search_response = requests.get(search_url)
|
| 46 |
+
search_response.raise_for_status()
|
| 47 |
+
search_data = search_response.json()
|
| 48 |
+
|
| 49 |
+
# Get video IDs
|
| 50 |
+
video_ids = [item['id']['videoId'] for item in search_data.get('items', [])]
|
| 51 |
+
if not video_ids:
|
| 52 |
+
return []
|
| 53 |
+
|
| 54 |
+
# Get video details (including view counts)
|
| 55 |
+
video_url = "https://www.googleapis.com/youtube/v3/videos"
|
| 56 |
+
video_params = {
|
| 57 |
+
'part': 'snippet,statistics',
|
| 58 |
+
'id': ','.join(video_ids),
|
| 59 |
+
'key': api_key
|
| 60 |
+
}
|
| 61 |
+
video_details_url = f"{video_url}?{urlencode(video_params)}"
|
| 62 |
+
video_response = requests.get(video_details_url)
|
| 63 |
+
video_response.raise_for_status()
|
| 64 |
+
video_data = video_response.json()
|
| 65 |
+
|
| 66 |
+
results = []
|
| 67 |
+
for item in video_data.get('items', [])[:3]: # Limit to top 3
|
| 68 |
+
results.append({
|
| 69 |
+
'title': item['snippet']['title'],
|
| 70 |
+
'channel': item['snippet']['channelTitle'],
|
| 71 |
+
'views': int(item['statistics'].get('viewCount', 0)),
|
| 72 |
+
'url': f"https://www.youtube.com/watch?v={item['id']}"
|
| 73 |
+
})
|
| 74 |
+
|
| 75 |
+
return results
|
| 76 |
+
|
| 77 |
+
except requests.exceptions.RequestException as e:
|
| 78 |
+
print(f"Error searching YouTube: {e}")
|
| 79 |
+
return []
|
| 80 |
+
|
| 81 |
|
| 82 |
# Initialize tools
|
| 83 |
final_answer = FinalAnswerTool()
|
|
|
|
| 98 |
agent = CodeAgent(
|
| 99 |
model=model,
|
| 100 |
tools=[
|
| 101 |
+
search_youtube_guitar_tutorials,
|
| 102 |
final_answer
|
| 103 |
],
|
| 104 |
max_steps=6,
|