Spaces:
Runtime error
Runtime error
Update tools.py
Browse files
tools.py
CHANGED
|
@@ -1,8 +1,14 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import openai
|
| 3 |
-
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 7 |
|
| 8 |
SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template:
|
|
@@ -25,7 +31,6 @@ class AnswerTool(Tool):
|
|
| 25 |
{"role": "system", "content": SYSTEM_PROMPT},
|
| 26 |
{"role": "user", "content": question},
|
| 27 |
]
|
| 28 |
-
# Chiamata all'API v1
|
| 29 |
resp = openai.chat.completions.create(
|
| 30 |
model="gpt-4o",
|
| 31 |
messages=messages,
|
|
@@ -36,3 +41,51 @@ class AnswerTool(Tool):
|
|
| 36 |
if "FINAL ANSWER:" in text:
|
| 37 |
return text.split("FINAL ANSWER:")[-1].strip()
|
| 38 |
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")
|
| 13 |
|
| 14 |
SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template:
|
|
|
|
| 31 |
{"role": "system", "content": SYSTEM_PROMPT},
|
| 32 |
{"role": "user", "content": question},
|
| 33 |
]
|
|
|
|
| 34 |
resp = openai.chat.completions.create(
|
| 35 |
model="gpt-4o",
|
| 36 |
messages=messages,
|
|
|
|
| 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)
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
|