Princekumar commited on
Commit ·
94f2387
1
Parent(s): 4223c89
Fixing lost tool code
Browse files
tools.py
CHANGED
|
@@ -1,20 +1,12 @@
|
|
| 1 |
-
import base64
|
| 2 |
import os
|
| 3 |
from typing import ClassVar
|
| 4 |
-
from urllib.parse import urlparse
|
| 5 |
-
import requests
|
| 6 |
from smolagents import Tool
|
| 7 |
-
import math
|
| 8 |
import datetime
|
| 9 |
from PIL import Image
|
| 10 |
import pandas as pd
|
| 11 |
-
import litellm
|
| 12 |
-
import yt_dlp
|
| 13 |
from prompts import SYSTEM_PROMPT
|
| 14 |
-
from pytube import YouTube
|
| 15 |
from PIL import Image
|
| 16 |
-
import
|
| 17 |
-
from smolagents import DuckDuckGoSearchTool, WikipediaSearchTool
|
| 18 |
from dotenv import load_dotenv
|
| 19 |
import time
|
| 20 |
from langchain_google_community import GoogleSearchAPIWrapper
|
|
@@ -152,7 +144,7 @@ class ExcelAndCSVTableInspectorTool(Tool):
|
|
| 152 |
class YouTubeVideoAnalyzerTool(Tool):
|
| 153 |
name: ClassVar[str] = "youtube_video_analyzer"
|
| 154 |
description: ClassVar[str] = (
|
| 155 |
-
"Given a YouTube URL,
|
| 156 |
)
|
| 157 |
inputs: ClassVar[dict] = {
|
| 158 |
"url": {"type": "string", "description": "Full YouTube video URL"},
|
|
@@ -164,51 +156,31 @@ class YouTubeVideoAnalyzerTool(Tool):
|
|
| 164 |
output_type: ClassVar[str] = "string"
|
| 165 |
|
| 166 |
def forward(self, url: str, user_prompt: str):
|
|
|
|
|
|
|
|
|
|
| 167 |
try:
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
- Provide the answer as per system prompt.
|
| 188 |
-
"""
|
| 189 |
-
|
| 190 |
-
response = litellm.completion(
|
| 191 |
-
api_key=os.getenv("GEMINI_API_KEY"),
|
| 192 |
-
model=os.getenv("GEMINI_MODEL"),
|
| 193 |
-
messages=[
|
| 194 |
-
{"role": "system", "content": SYSTEM_PROMPT},
|
| 195 |
-
{
|
| 196 |
-
"role": "user",
|
| 197 |
-
"content": user_final_prompt.format(
|
| 198 |
-
user_prompt=user_prompt, url=url
|
| 199 |
-
),
|
| 200 |
-
},
|
| 201 |
-
],
|
| 202 |
-
)
|
| 203 |
-
|
| 204 |
-
return response["choices"][0]["message"]["content"]
|
| 205 |
-
except Exception as e:
|
| 206 |
-
if "Sign in" in str(e):
|
| 207 |
-
return "This video requires age verification or sign-in. Please provide a different video URL."
|
| 208 |
-
return f"Error accessing video: {str(e)}"
|
| 209 |
-
|
| 210 |
except Exception as e:
|
| 211 |
-
return f"Error analyzing video: {e}"
|
| 212 |
|
| 213 |
|
| 214 |
# --- Math Tools ---
|
|
@@ -500,7 +472,7 @@ class ImageInfoTool(Tool):
|
|
| 500 |
return f"Error loading image: {e}"
|
| 501 |
|
| 502 |
|
| 503 |
-
class
|
| 504 |
name: ClassVar[str] = "wikipedia_search_summary"
|
| 505 |
description: ClassVar[str] = "Search Wikipedia and return max 3 results"
|
| 506 |
inputs: ClassVar[dict] = {
|
|
@@ -637,6 +609,7 @@ agent_tools = [
|
|
| 637 |
SortListTool(),
|
| 638 |
UniqueListTool(),
|
| 639 |
GoogleSearchTool(),
|
| 640 |
-
|
|
|
|
| 641 |
AdvanceGoogleAISearchTool(),
|
| 642 |
]
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
from typing import ClassVar
|
|
|
|
|
|
|
| 3 |
from smolagents import Tool
|
|
|
|
| 4 |
import datetime
|
| 5 |
from PIL import Image
|
| 6 |
import pandas as pd
|
|
|
|
|
|
|
| 7 |
from prompts import SYSTEM_PROMPT
|
|
|
|
| 8 |
from PIL import Image
|
| 9 |
+
from smolagents import WikipediaSearchTool
|
|
|
|
| 10 |
from dotenv import load_dotenv
|
| 11 |
import time
|
| 12 |
from langchain_google_community import GoogleSearchAPIWrapper
|
|
|
|
| 144 |
class YouTubeVideoAnalyzerTool(Tool):
|
| 145 |
name: ClassVar[str] = "youtube_video_analyzer"
|
| 146 |
description: ClassVar[str] = (
|
| 147 |
+
"Given a YouTube URL, analyzes the video content using Gemini (google-genai SDK) and answers user queries about it."
|
| 148 |
)
|
| 149 |
inputs: ClassVar[dict] = {
|
| 150 |
"url": {"type": "string", "description": "Full YouTube video URL"},
|
|
|
|
| 156 |
output_type: ClassVar[str] = "string"
|
| 157 |
|
| 158 |
def forward(self, url: str, user_prompt: str):
|
| 159 |
+
api_key = os.getenv("GEMINI_API_KEY")
|
| 160 |
+
if not api_key:
|
| 161 |
+
return "Error: GEMINI_API_KEY environment variable is not set."
|
| 162 |
try:
|
| 163 |
+
genai.configure(api_key=api_key)
|
| 164 |
+
model_name = os.getenv("GENAI_MODEL", "gemini-1.5-pro")
|
| 165 |
+
model = genai.GenerativeModel(model_name)
|
| 166 |
+
prompt = f"""
|
| 167 |
+
You are an AI video analyzer. A user wants to analyze the following YouTube video.
|
| 168 |
+
|
| 169 |
+
### Video URL
|
| 170 |
+
{url}
|
| 171 |
+
|
| 172 |
+
### User Request
|
| 173 |
+
{user_prompt}
|
| 174 |
+
|
| 175 |
+
### Instructions:
|
| 176 |
+
- Analyze the video content based on the user's request.
|
| 177 |
+
- Identify the main key thing needed to be analyzed.
|
| 178 |
+
- Provide the answer as per system prompt.
|
| 179 |
+
"""
|
| 180 |
+
response = model.generate_content([prompt])
|
| 181 |
+
return response.text if hasattr(response, "text") else str(response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
except Exception as e:
|
| 183 |
+
return f"Error analyzing video with Gemini: {e}"
|
| 184 |
|
| 185 |
|
| 186 |
# --- Math Tools ---
|
|
|
|
| 472 |
return f"Error loading image: {e}"
|
| 473 |
|
| 474 |
|
| 475 |
+
class WikipediaCustomTool(Tool):
|
| 476 |
name: ClassVar[str] = "wikipedia_search_summary"
|
| 477 |
description: ClassVar[str] = "Search Wikipedia and return max 3 results"
|
| 478 |
inputs: ClassVar[dict] = {
|
|
|
|
| 609 |
SortListTool(),
|
| 610 |
UniqueListTool(),
|
| 611 |
GoogleSearchTool(),
|
| 612 |
+
WikipediaCustomTool(),
|
| 613 |
+
ArvixSearchTool(),
|
| 614 |
AdvanceGoogleAISearchTool(),
|
| 615 |
]
|