Spaces:
Sleeping
Sleeping
fix some tool errors
Browse files- requirements.txt +3 -3
- tools.py +35 -2
requirements.txt
CHANGED
|
@@ -1,8 +1,10 @@
|
|
| 1 |
duckduckgo-search
|
| 2 |
gradio
|
| 3 |
gradio[oauth]
|
| 4 |
-
|
| 5 |
numpy
|
|
|
|
|
|
|
| 6 |
openpyxl
|
| 7 |
pandas
|
| 8 |
pillow
|
|
@@ -15,5 +17,3 @@ smolagents[transformers]
|
|
| 15 |
transformers
|
| 16 |
wikipedia-api
|
| 17 |
youtube-transcript-api
|
| 18 |
-
openai-whisper
|
| 19 |
-
openai
|
|
|
|
| 1 |
duckduckgo-search
|
| 2 |
gradio
|
| 3 |
gradio[oauth]
|
| 4 |
+
markdownify
|
| 5 |
numpy
|
| 6 |
+
openai
|
| 7 |
+
openai-whisper
|
| 8 |
openpyxl
|
| 9 |
pandas
|
| 10 |
pillow
|
|
|
|
| 17 |
transformers
|
| 18 |
wikipedia-api
|
| 19 |
youtube-transcript-api
|
|
|
|
|
|
tools.py
CHANGED
|
@@ -3,6 +3,9 @@ import logging
|
|
| 3 |
from typing import List
|
| 4 |
import os
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
from smolagents import Tool, PythonInterpreterTool, VisitWebpageTool, WikipediaSearchTool
|
| 7 |
from duckduckgo_search import DDGS
|
| 8 |
|
|
@@ -32,6 +35,10 @@ class ReadFileTool(Tool):
|
|
| 32 |
}
|
| 33 |
output_type = 'string'
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
def forward(self, file_path: str) -> str:
|
| 36 |
try:
|
| 37 |
with open(file_path, 'r') as file:
|
|
@@ -59,6 +66,10 @@ class OpenAISpeechToTextTool(Tool):
|
|
| 59 |
}
|
| 60 |
output_type = 'string'
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
def forward(self, audio_path: str) -> str:
|
| 63 |
try:
|
| 64 |
model = whisper.load_model('small')
|
|
@@ -90,13 +101,16 @@ class YouTubeTranscriptionTool(Tool):
|
|
| 90 |
}
|
| 91 |
output_type = 'string'
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
def forward(self, video_url: str) -> str:
|
| 94 |
video_id = video_url.strip().split('v=')[-1]
|
| 95 |
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
| 96 |
return ' '.join([entry['text'] for entry in transcript])
|
| 97 |
|
| 98 |
|
| 99 |
-
client = OpenAI()
|
| 100 |
|
| 101 |
|
| 102 |
class DescribeImageTool(Tool):
|
|
@@ -130,6 +144,19 @@ class DescribeImageTool(Tool):
|
|
| 130 |
}
|
| 131 |
output_type = 'string'
|
| 132 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
def encode_image(self, image_path: str) -> str:
|
| 134 |
"""Encode image to base64 string."""
|
| 135 |
with open(image_path, 'rb') as image_file:
|
|
@@ -211,6 +238,7 @@ class ReliableDuckDuckGoSearchTool(Tool):
|
|
| 211 |
|
| 212 |
|
| 213 |
def __init__(self, num_results=3):
|
|
|
|
| 214 |
self.num_results = num_results
|
| 215 |
|
| 216 |
def forward(self, query: str) -> str:
|
|
@@ -230,9 +258,14 @@ def get_tools() -> List[Tool]:
|
|
| 230 |
Returns:
|
| 231 |
List[Tool]: List of initialized tool instances.
|
| 232 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
try:
|
| 234 |
tools = [ReliableDuckDuckGoSearchTool(),
|
| 235 |
-
PythonInterpreterTool(),
|
| 236 |
WikipediaSearchTool(),
|
| 237 |
VisitWebpageTool(),
|
| 238 |
OpenAISpeechToTextTool(),
|
|
|
|
| 3 |
from typing import List
|
| 4 |
import os
|
| 5 |
|
| 6 |
+
import markdownify
|
| 7 |
+
import requests
|
| 8 |
+
|
| 9 |
from smolagents import Tool, PythonInterpreterTool, VisitWebpageTool, WikipediaSearchTool
|
| 10 |
from duckduckgo_search import DDGS
|
| 11 |
|
|
|
|
| 35 |
}
|
| 36 |
output_type = 'string'
|
| 37 |
|
| 38 |
+
def __init__():
|
| 39 |
+
"""Nothing to do for this class here, but kick the constructor of the base class."""
|
| 40 |
+
super().__init__()
|
| 41 |
+
|
| 42 |
def forward(self, file_path: str) -> str:
|
| 43 |
try:
|
| 44 |
with open(file_path, 'r') as file:
|
|
|
|
| 66 |
}
|
| 67 |
output_type = 'string'
|
| 68 |
|
| 69 |
+
def __init__():
|
| 70 |
+
"""Nothing to do for this class here, but kick the constructor of the base class."""
|
| 71 |
+
super().__init__()
|
| 72 |
+
|
| 73 |
def forward(self, audio_path: str) -> str:
|
| 74 |
try:
|
| 75 |
model = whisper.load_model('small')
|
|
|
|
| 101 |
}
|
| 102 |
output_type = 'string'
|
| 103 |
|
| 104 |
+
def __init__():
|
| 105 |
+
"""Nothing to do for this class here, but kick the constructor of the base class."""
|
| 106 |
+
super().__init__()
|
| 107 |
+
|
| 108 |
def forward(self, video_url: str) -> str:
|
| 109 |
video_id = video_url.strip().split('v=')[-1]
|
| 110 |
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
| 111 |
return ' '.join([entry['text'] for entry in transcript])
|
| 112 |
|
| 113 |
|
|
|
|
| 114 |
|
| 115 |
|
| 116 |
class DescribeImageTool(Tool):
|
|
|
|
| 144 |
}
|
| 145 |
output_type = 'string'
|
| 146 |
|
| 147 |
+
client = None
|
| 148 |
+
|
| 149 |
+
def __init__():
|
| 150 |
+
"""ensure, that the super class is instantiated properly and creat an OpenAI client (if not already done)."""
|
| 151 |
+
super().__init__()
|
| 152 |
+
|
| 153 |
+
if not self.cls.client:
|
| 154 |
+
openai_api_key = os.environ.get('REAL_OPENAI_API_KEY', # just in case we run with other openAI compatibel providers
|
| 155 |
+
os.environ.get('OPENAI_API_KEY')
|
| 156 |
+
)
|
| 157 |
+
client = OpenAI(api_key=openai_api_key)
|
| 158 |
+
|
| 159 |
+
|
| 160 |
def encode_image(self, image_path: str) -> str:
|
| 161 |
"""Encode image to base64 string."""
|
| 162 |
with open(image_path, 'rb') as image_file:
|
|
|
|
| 238 |
|
| 239 |
|
| 240 |
def __init__(self, num_results=3):
|
| 241 |
+
super().__init__()
|
| 242 |
self.num_results = num_results
|
| 243 |
|
| 244 |
def forward(self, query: str) -> str:
|
|
|
|
| 258 |
Returns:
|
| 259 |
List[Tool]: List of initialized tool instances.
|
| 260 |
"""
|
| 261 |
+
|
| 262 |
+
authorized_imports = ['requests',
|
| 263 |
+
'markdownify',
|
| 264 |
+
]
|
| 265 |
+
|
| 266 |
try:
|
| 267 |
tools = [ReliableDuckDuckGoSearchTool(),
|
| 268 |
+
PythonInterpreterTool(authorized_imports=authorized_imports),
|
| 269 |
WikipediaSearchTool(),
|
| 270 |
VisitWebpageTool(),
|
| 271 |
OpenAISpeechToTextTool(),
|