Spaces:
Sleeping
Sleeping
mohammadreza pakzadian commited on
Commit ·
6442393
1
Parent(s): 0ee0361
add youtube transcript tool
Browse files- app.py +5 -3
- requirements.txt +1 -0
- tools/youtube_transcript.py +27 -0
app.py
CHANGED
|
@@ -6,6 +6,7 @@ from smolagents import CodeAgent, OpenAIServerModel, HfApiModel
|
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
from tools.visit_webpage import VisitWebpageTool
|
| 8 |
from tools.web_search import DuckDuckGoSearchTool
|
|
|
|
| 9 |
|
| 10 |
# (Keep Constants as is)
|
| 11 |
# --- Constants ---
|
|
@@ -19,15 +20,16 @@ class BasicAgent:
|
|
| 19 |
final_answer = FinalAnswerTool()
|
| 20 |
visit_webpage = VisitWebpageTool()
|
| 21 |
web_search = DuckDuckGoSearchTool()
|
|
|
|
| 22 |
# model = OpenAIServerModel(model_id="gpt-4.1", api_key=os.getenv("OPENAI_API_KEY"))
|
| 23 |
model = HfApiModel(
|
| 24 |
max_tokens=2096,
|
| 25 |
temperature=0.5,
|
| 26 |
-
model_id='Qwen/Qwen2.5-Coder-
|
| 27 |
)
|
| 28 |
self.agent = CodeAgent(
|
| 29 |
model=model,
|
| 30 |
-
tools=[visit_webpage, web_search, final_answer],
|
| 31 |
max_steps=5,
|
| 32 |
verbosity_level=1,
|
| 33 |
add_base_tools=True
|
|
@@ -40,7 +42,7 @@ class BasicAgent:
|
|
| 40 |
[YOUR FINAL ANSWER] should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
|
| 41 |
If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise.
|
| 42 |
If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.
|
| 43 |
-
If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
|
| 44 |
IMPORTANT NOTES TO LIMIT COSTS AND PREVENT ERRORS:
|
| 45 |
- Use web search sparingly and only when absolutely necessary.
|
| 46 |
- Limit to 1-2 web searches per question.
|
|
|
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
from tools.visit_webpage import VisitWebpageTool
|
| 8 |
from tools.web_search import DuckDuckGoSearchTool
|
| 9 |
+
from tools.youtube_transcript import YoutubeTranscriptTool
|
| 10 |
|
| 11 |
# (Keep Constants as is)
|
| 12 |
# --- Constants ---
|
|
|
|
| 20 |
final_answer = FinalAnswerTool()
|
| 21 |
visit_webpage = VisitWebpageTool()
|
| 22 |
web_search = DuckDuckGoSearchTool()
|
| 23 |
+
youtube_transcript = YoutubeTranscriptTool()
|
| 24 |
# model = OpenAIServerModel(model_id="gpt-4.1", api_key=os.getenv("OPENAI_API_KEY"))
|
| 25 |
model = HfApiModel(
|
| 26 |
max_tokens=2096,
|
| 27 |
temperature=0.5,
|
| 28 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct', custom_role_conversions=None,
|
| 29 |
)
|
| 30 |
self.agent = CodeAgent(
|
| 31 |
model=model,
|
| 32 |
+
tools=[visit_webpage, web_search, final_answer, youtube_transcript],
|
| 33 |
max_steps=5,
|
| 34 |
verbosity_level=1,
|
| 35 |
add_base_tools=True
|
|
|
|
| 42 |
[YOUR FINAL ANSWER] should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
|
| 43 |
If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise.
|
| 44 |
If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.
|
| 45 |
+
If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string and return a string that contains the comma separated values.
|
| 46 |
IMPORTANT NOTES TO LIMIT COSTS AND PREVENT ERRORS:
|
| 47 |
- Use web search sparingly and only when absolutely necessary.
|
| 48 |
- Limit to 1-2 web searches per question.
|
requirements.txt
CHANGED
|
@@ -6,3 +6,4 @@ smolagents[openai]
|
|
| 6 |
requests
|
| 7 |
duckduckgo_search
|
| 8 |
pandas
|
|
|
|
|
|
| 6 |
requests
|
| 7 |
duckduckgo_search
|
| 8 |
pandas
|
| 9 |
+
youtube-transcript-api
|
tools/youtube_transcript.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any, Optional
|
| 2 |
+
from smolagents.tools import Tool
|
| 3 |
+
import duckduckgo_search
|
| 4 |
+
|
| 5 |
+
class YoutubeTranscriptTool(Tool):
|
| 6 |
+
name = "youtube_transcript"
|
| 7 |
+
description = "This tool allows you to retrieve the transcript/subtitles for a given YouTube video."
|
| 8 |
+
inputs = {'video_id': {'type': 'string', 'description': 'The Id of YouTube video'}}
|
| 9 |
+
output_type = "list[dict]"
|
| 10 |
+
|
| 11 |
+
def __init__(self, max_results=10, **kwargs):
|
| 12 |
+
super().__init__()
|
| 13 |
+
self.max_results = max_results
|
| 14 |
+
try:
|
| 15 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
| 16 |
+
except ImportError as e:
|
| 17 |
+
raise ImportError(
|
| 18 |
+
"You must install package `duckduckgo_search` to run this tool: for instance run `pip install duckduckgo-search`."
|
| 19 |
+
) from e
|
| 20 |
+
self.ytt_api = YouTubeTranscriptApi()
|
| 21 |
+
|
| 22 |
+
def forward(self, video_id: str) -> list[dict] | str:
|
| 23 |
+
try:
|
| 24 |
+
result = self.ytt_api.fetch(video_id)
|
| 25 |
+
return result.to_raw_data()
|
| 26 |
+
except Exception as e:
|
| 27 |
+
return f"An unexpected error occurred: {str(e)}"
|