puckettventures commited on
Commit
e7316aa
·
verified ·
1 Parent(s): 972cbb6

Attempting to use youtubesearchtool

Browse files
Files changed (1) hide show
  1. app.py +37 -1
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
  import datetime
3
  import requests
4
  import pytz
@@ -7,6 +7,42 @@ from tools.final_answer import FinalAnswerTool
7
 
8
  from Gradio_UI import GradioUI
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  @tool
12
  def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
 
1
+ from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool,YouTubeSearchTool
2
  import datetime
3
  import requests
4
  import pytz
 
7
 
8
  from Gradio_UI import GradioUI
9
 
10
+ from smolagents import tool
11
+ from langchain_community.tools.youtube_search.tool import YouTubeSearchTool
12
+
13
+ @tool
14
+ def youtube_search_langchain(query: str) -> str:
15
+ """
16
+ Uses LangChain's YouTubeSearchTool to search YouTube and returns embedded players for the top 3 results.
17
+ Args:
18
+ query: The search query.
19
+ """
20
+ # Instantiate the tool (will use API key from env: YOUTUBE_API_KEY)
21
+ search_tool = YouTubeSearchTool()
22
+ try:
23
+ # Search returns a string summary. For richer data, see LangChain's API.
24
+ search_results = search_tool._run(query)
25
+
26
+ # Extract video IDs using regex (hacky, but their result is a summary string)
27
+ import re
28
+ video_ids = re.findall(r"youtube\.com/watch\?v=([A-Za-z0-9_\-]+)", search_results)
29
+ if not video_ids:
30
+ return "No videos found."
31
+
32
+ # Limit to top 3 and generate embed HTML
33
+ html_blocks = []
34
+ for vid in video_ids[:3]:
35
+ embed_html = f"""
36
+ <div style="margin-bottom:16px">
37
+ <iframe width="360" height="203" src="https://www.youtube.com/embed/{vid}" frameborder="0" allowfullscreen></iframe>
38
+ </div>
39
+ """
40
+ html_blocks.append(embed_html)
41
+ return "".join(html_blocks)
42
+ except Exception as e:
43
+ return f"Error using LangChain's YouTubeSearchTool: {str(e)}"
44
+
45
+
46
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
47
  @tool
48
  def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type