Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files
tools.py
CHANGED
@@ -17,6 +17,7 @@ class PythonRunTool(Tool):
|
|
17 |
inputs = {
|
18 |
"code": {"type": str, "description": "Python code to execute", "required": True}
|
19 |
}
|
|
|
20 |
|
21 |
def forward(self, code: str) -> str:
|
22 |
buf, ns = io.StringIO(), {}
|
@@ -43,15 +44,19 @@ class ExcelLoaderTool(Tool):
|
|
43 |
"type": str,
|
44 |
"description": "Sheet name or index (optional, required for Excel files only)",
|
45 |
"required": False,
|
46 |
-
"default":
|
47 |
}
|
48 |
}
|
|
|
49 |
|
50 |
def forward(self, path: str, sheet: str | int | None = None) -> List[Dict[str, Any]]:
|
51 |
import pandas as pd
|
52 |
if not os.path.isfile(path):
|
53 |
raise FileNotFoundError(path)
|
54 |
ext = os.path.splitext(path)[1].lower()
|
|
|
|
|
|
|
55 |
if ext == ".csv":
|
56 |
df = pd.read_csv(path)
|
57 |
else:
|
@@ -67,6 +72,7 @@ class YouTubeTranscriptTool(Tool):
|
|
67 |
"url": {"type": str, "description": "YouTube URL", "required": True},
|
68 |
"lang": {"type": str, "description": "Transcript language (default: en)", "required": False, "default": "en"}
|
69 |
}
|
|
|
70 |
|
71 |
def forward(self, url: str, lang: str = "en") -> str:
|
72 |
from urllib.parse import urlparse, parse_qs
|
@@ -83,19 +89,16 @@ class AudioTranscriptionTool(Tool):
|
|
83 |
"path": {"type": str, "description": "Path to audio file", "required": True},
|
84 |
"model": {"type": str, "description": "Model name for transcription (default: whisper-1)", "required": False, "default": "whisper-1"}
|
85 |
}
|
|
|
86 |
|
87 |
def forward(self, path: str, model: str = "whisper-1") -> str:
|
88 |
import openai
|
89 |
if not os.path.isfile(path):
|
90 |
raise FileNotFoundError(path)
|
91 |
-
openai.api_key
|
92 |
-
if not hasattr(openai, "Audio"):
|
93 |
-
raise ImportError(
|
94 |
-
"Your OpenAI package does not support Audio. "
|
95 |
-
"Please upgrade it with: pip install --upgrade openai"
|
96 |
-
)
|
97 |
with open(path, "rb") as fp:
|
98 |
-
|
|
|
99 |
|
100 |
# ---- 5. SimpleOCRTool ------------------------------------------------------
|
101 |
class SimpleOCRTool(Tool):
|
@@ -104,6 +107,7 @@ class SimpleOCRTool(Tool):
|
|
104 |
inputs = {
|
105 |
"path": {"type": str, "description": "Path to image file", "required": True}
|
106 |
}
|
|
|
107 |
|
108 |
def forward(self, path: str) -> str:
|
109 |
from PIL import Image
|
|
|
17 |
inputs = {
|
18 |
"code": {"type": str, "description": "Python code to execute", "required": True}
|
19 |
}
|
20 |
+
output_type = "str"
|
21 |
|
22 |
def forward(self, code: str) -> str:
|
23 |
buf, ns = io.StringIO(), {}
|
|
|
44 |
"type": str,
|
45 |
"description": "Sheet name or index (optional, required for Excel files only)",
|
46 |
"required": False,
|
47 |
+
"default": "",
|
48 |
}
|
49 |
}
|
50 |
+
output_type = "list"
|
51 |
|
52 |
def forward(self, path: str, sheet: str | int | None = None) -> List[Dict[str, Any]]:
|
53 |
import pandas as pd
|
54 |
if not os.path.isfile(path):
|
55 |
raise FileNotFoundError(path)
|
56 |
ext = os.path.splitext(path)[1].lower()
|
57 |
+
# Handle empty string as None for sheet
|
58 |
+
if sheet == "":
|
59 |
+
sheet = None
|
60 |
if ext == ".csv":
|
61 |
df = pd.read_csv(path)
|
62 |
else:
|
|
|
72 |
"url": {"type": str, "description": "YouTube URL", "required": True},
|
73 |
"lang": {"type": str, "description": "Transcript language (default: en)", "required": False, "default": "en"}
|
74 |
}
|
75 |
+
output_type = "str"
|
76 |
|
77 |
def forward(self, url: str, lang: str = "en") -> str:
|
78 |
from urllib.parse import urlparse, parse_qs
|
|
|
89 |
"path": {"type": str, "description": "Path to audio file", "required": True},
|
90 |
"model": {"type": str, "description": "Model name for transcription (default: whisper-1)", "required": False, "default": "whisper-1"}
|
91 |
}
|
92 |
+
output_type = "str"
|
93 |
|
94 |
def forward(self, path: str, model: str = "whisper-1") -> str:
|
95 |
import openai
|
96 |
if not os.path.isfile(path):
|
97 |
raise FileNotFoundError(path)
|
98 |
+
client = openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
|
|
|
|
|
|
|
|
|
|
99 |
with open(path, "rb") as fp:
|
100 |
+
transcript = client.audio.transcriptions.create(model=model, file=fp)
|
101 |
+
return transcript.text.strip()
|
102 |
|
103 |
# ---- 5. SimpleOCRTool ------------------------------------------------------
|
104 |
class SimpleOCRTool(Tool):
|
|
|
107 |
inputs = {
|
108 |
"path": {"type": str, "description": "Path to image file", "required": True}
|
109 |
}
|
110 |
+
output_type = "str"
|
111 |
|
112 |
def forward(self, path: str) -> str:
|
113 |
from PIL import Image
|