Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,9 +8,15 @@ from smolagents import DuckDuckGoSearchTool, VisitWebpageTool
|
|
| 8 |
import time # Added: For sleep functionality
|
| 9 |
from functools import lru_cache # Added: For caching search results
|
| 10 |
|
|
|
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
search_tool = DuckDuckGoSearchTool()
|
| 13 |
visit_webpage = VisitWebpageTool()
|
|
|
|
| 14 |
|
| 15 |
# (Keep Constants as is)
|
| 16 |
# --- Constants ---
|
|
@@ -92,7 +98,58 @@ class BasicAgent:
|
|
| 92 |
|
| 93 |
def _get_fallback_answer(self, question):
|
| 94 |
return f"Based on the information available, I cannot provide a specific answer to your question about {question.split()[0:3]}..."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 98 |
"""
|
|
@@ -115,7 +172,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 115 |
|
| 116 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 117 |
try:
|
| 118 |
-
agent = BasicAgent(model='
|
| 119 |
except Exception as e:
|
| 120 |
print(f"Error instantiating agent: {e}")
|
| 121 |
return f"Error initializing agent: {e}", None
|
|
|
|
| 8 |
import time # Added: For sleep functionality
|
| 9 |
from functools import lru_cache # Added: For caching search results
|
| 10 |
|
| 11 |
+
pip install youtube-transcript-api
|
| 12 |
|
| 13 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
| 14 |
+
import re
|
| 15 |
+
|
| 16 |
+
# TOOLS
|
| 17 |
search_tool = DuckDuckGoSearchTool()
|
| 18 |
visit_webpage = VisitWebpageTool()
|
| 19 |
+
youtube_tool = YouTubeVideoTool()
|
| 20 |
|
| 21 |
# (Keep Constants as is)
|
| 22 |
# --- Constants ---
|
|
|
|
| 98 |
|
| 99 |
def _get_fallback_answer(self, question):
|
| 100 |
return f"Based on the information available, I cannot provide a specific answer to your question about {question.split()[0:3]}..."
|
| 101 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
| 102 |
+
import re
|
| 103 |
+
|
| 104 |
+
class YouTubeVideoTool:
|
| 105 |
+
def __init__(self):
|
| 106 |
+
self.name = "youtube_video_tool"
|
| 107 |
+
|
| 108 |
+
def __call__(self, query):
|
| 109 |
+
"""
|
| 110 |
+
Extract information from a YouTube video.
|
| 111 |
|
| 112 |
+
Args:
|
| 113 |
+
query: Either a YouTube URL or video ID
|
| 114 |
+
|
| 115 |
+
Returns:
|
| 116 |
+
String with the transcript of the video
|
| 117 |
+
"""
|
| 118 |
+
try:
|
| 119 |
+
# Extract video ID from URL if needed
|
| 120 |
+
video_id = self._extract_video_id(query)
|
| 121 |
+
if not video_id:
|
| 122 |
+
return "Could not extract a valid YouTube video ID"
|
| 123 |
+
|
| 124 |
+
# Get the transcript
|
| 125 |
+
transcript_list = YouTubeTranscriptApi.get_transcript(video_id)
|
| 126 |
+
|
| 127 |
+
# Combine the transcript text
|
| 128 |
+
transcript_text = " ".join([item['text'] for item in transcript_list])
|
| 129 |
+
|
| 130 |
+
return f"Transcript from YouTube video {video_id}:\n{transcript_text}"
|
| 131 |
+
except Exception as e:
|
| 132 |
+
return f"Error processing YouTube video: {str(e)}"
|
| 133 |
+
|
| 134 |
+
def _extract_video_id(self, url_or_id):
|
| 135 |
+
"""Extract YouTube video ID from various URL formats or return the ID if already provided."""
|
| 136 |
+
# Handle direct video ID
|
| 137 |
+
if len(url_or_id) == 11 and re.match(r'^[A-Za-z0-9_-]{11}$', url_or_id):
|
| 138 |
+
return url_or_id
|
| 139 |
+
|
| 140 |
+
# Common YouTube URL patterns
|
| 141 |
+
patterns = [
|
| 142 |
+
r'(?:youtube\.com\/watch\?v=|youtu\.be\/|youtube\.com\/embed\/|youtube\.com\/v\/)([A-Za-z0-9_-]{11})',
|
| 143 |
+
r'youtube\.com\/watch\?.*v=([A-Za-z0-9_-]{11})',
|
| 144 |
+
r'youtube\.com\/shorts\/([A-Za-z0-9_-]{11})'
|
| 145 |
+
]
|
| 146 |
+
|
| 147 |
+
for pattern in patterns:
|
| 148 |
+
match = re.search(pattern, url_or_id)
|
| 149 |
+
if match:
|
| 150 |
+
return match.group(1)
|
| 151 |
+
|
| 152 |
+
return None
|
| 153 |
|
| 154 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 155 |
"""
|
|
|
|
| 172 |
|
| 173 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 174 |
try:
|
| 175 |
+
agent = BasicAgent(model= LiteLLMModel('gemini/gemini-2.0-flash-exp'), tools=[search_tool, visit_webpage, youtube_tool])
|
| 176 |
except Exception as e:
|
| 177 |
print(f"Error instantiating agent: {e}")
|
| 178 |
return f"Error initializing agent: {e}", None
|