Spaces:
Sleeping
Sleeping
Update tools.py
Browse files
tools.py
CHANGED
@@ -1,7 +1,12 @@
|
|
1 |
-
# tools.py
|
2 |
import os
|
|
|
|
|
|
|
|
|
3 |
import openai
|
4 |
-
|
|
|
|
|
5 |
|
6 |
# Load your OpenAI API key
|
7 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
@@ -13,17 +18,6 @@ YOUR FINAL ANSWER should be:
|
|
13 |
- If you are asked for a number, don't use commas, symbols or units (e.g. %, $, km) unless explicitly asked.
|
14 |
- If you are asked for a string, don't use articles ("a", "the"), abbreviations (e.g. "NYC"), or extra words; write digits in plain text unless specified otherwise.
|
15 |
- If you are asked for a comma separated list, apply the above rules to each element.
|
16 |
-
Examples:
|
17 |
-
Q: How many legs does a spider have?
|
18 |
-
Thought: It's basic biology.
|
19 |
-
FINAL ANSWER: 8
|
20 |
-
Q: What are the primary colors in light?
|
21 |
-
Thought: Physics concept.
|
22 |
-
FINAL ANSWER: red, green, blue
|
23 |
-
Q: Who won the Best Picture Oscar in 1994?
|
24 |
-
Thought: Film history.
|
25 |
-
FINAL ANSWER: Forrest Gump
|
26 |
-
Now it's your turn.
|
27 |
"""
|
28 |
|
29 |
class AnswerTool(Tool):
|
@@ -47,3 +41,48 @@ class AnswerTool(Tool):
|
|
47 |
if "FINAL ANSWER:" in text:
|
48 |
return text.split("FINAL ANSWER:")[-1].strip()
|
49 |
return text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
import re
|
3 |
+
from pathlib import Path
|
4 |
+
import tempfile
|
5 |
+
|
6 |
import openai
|
7 |
+
import pandas as pd
|
8 |
+
from tabulate import tabulate
|
9 |
+
from smolagents.tools import Tool, PipelineTool
|
10 |
|
11 |
# Load your OpenAI API key
|
12 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
|
|
18 |
- If you are asked for a number, don't use commas, symbols or units (e.g. %, $, km) unless explicitly asked.
|
19 |
- If you are asked for a string, don't use articles ("a", "the"), abbreviations (e.g. "NYC"), or extra words; write digits in plain text unless specified otherwise.
|
20 |
- If you are asked for a comma separated list, apply the above rules to each element.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
"""
|
22 |
|
23 |
class AnswerTool(Tool):
|
|
|
41 |
if "FINAL ANSWER:" in text:
|
42 |
return text.split("FINAL ANSWER:")[-1].strip()
|
43 |
return text
|
44 |
+
|
45 |
+
class SpeechToTextTool(PipelineTool):
|
46 |
+
"""Transcribes audio files using OpenAI Whisper."""
|
47 |
+
name = "transcriber"
|
48 |
+
description = "Transcribes a local audio file to text using Whisper API."
|
49 |
+
inputs = {"audio": {"type": "string", "description": "Path to audio file."}}
|
50 |
+
output_type = "string"
|
51 |
+
default_checkpoint = "openai/whisper-1"
|
52 |
+
|
53 |
+
def __call__(self, audio: str) -> str:
|
54 |
+
return self._transcribe(audio)
|
55 |
+
|
56 |
+
@staticmethod
|
57 |
+
def _transcribe(audio_path: str) -> str:
|
58 |
+
path = Path(audio_path).expanduser().resolve()
|
59 |
+
if not path.is_file():
|
60 |
+
raise FileNotFoundError(f"Audio file not found: {path}")
|
61 |
+
with path.open("rb") as fp:
|
62 |
+
response = openai.audio.transcriptions.create(
|
63 |
+
file=fp,
|
64 |
+
model="whisper-1",
|
65 |
+
response_format="text"
|
66 |
+
)
|
67 |
+
return response # already plain text
|
68 |
+
|
69 |
+
class ExcelToTextTool(Tool):
|
70 |
+
"""Renders an Excel worksheet as Markdown text."""
|
71 |
+
name = "excel_to_text"
|
72 |
+
description = "Read an Excel file and return a Markdown table."
|
73 |
+
inputs = {
|
74 |
+
"excel_path": {"type": "string", "description": "Path to Excel file."},
|
75 |
+
"sheet_name": {"type": "string", "description": "Sheet name or index as string.", "nullable": True},
|
76 |
+
}
|
77 |
+
output_type = "string"
|
78 |
+
|
79 |
+
def forward(self, excel_path: str, sheet_name: str = None) -> str:
|
80 |
+
path = Path(excel_path).expanduser().resolve()
|
81 |
+
if not path.exists():
|
82 |
+
return f"Error: Excel file not found at {path}"
|
83 |
+
# Determine sheet
|
84 |
+
sheet = 0 if not sheet_name or sheet_name == "" else (int(sheet_name) if sheet_name.isdigit() else sheet_name)
|
85 |
+
df = pd.read_excel(path, sheet_name=sheet)
|
86 |
+
if hasattr(df, "to_markdown"):
|
87 |
+
return df.to_markdown(index=False)
|
88 |
+
return tabulate(df, headers="keys", tablefmt="github", showindex=False)
|