Spaces:
Sleeping
Sleeping
Commit ·
cbc2dad
1
Parent(s): 6e2fb37
add python_interpreter tool
Browse files
agent.py
CHANGED
|
@@ -23,10 +23,11 @@ from youtube_transcript_api import YouTubeTranscriptApi
|
|
| 23 |
from PIL import Image
|
| 24 |
import pytesseract
|
| 25 |
import fitz # PyMuPDF
|
|
|
|
|
|
|
| 26 |
|
| 27 |
# Load environment variables from .env file
|
| 28 |
# in HF Spaces, the .env file is saved in Variables and secrets in settings
|
| 29 |
-
from dotenv import load_dotenv
|
| 30 |
load_dotenv()
|
| 31 |
|
| 32 |
# === System Prompt ===
|
|
@@ -179,11 +180,38 @@ def read_jsonl(jsonl_path: str) -> str:
|
|
| 179 |
except Exception as e:
|
| 180 |
return f"Error reading JSONL file: {e}"
|
| 181 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
# Update tools list
|
| 183 |
tools: List[StructuredTool] = [
|
| 184 |
calculate, web_search, wikipedia_search, image_recognition,
|
| 185 |
read_pdf, read_csv, read_spreadsheet, transcribe_audio,
|
| 186 |
-
youtube_transcript_tool, youtube_transcript_api, read_jsonl
|
|
|
|
| 187 |
]
|
| 188 |
|
| 189 |
class AgentState(TypedDict):
|
|
|
|
| 23 |
from PIL import Image
|
| 24 |
import pytesseract
|
| 25 |
import fitz # PyMuPDF
|
| 26 |
+
from dotenv import load_dotenv
|
| 27 |
+
from contextlib import redirect_stdout
|
| 28 |
|
| 29 |
# Load environment variables from .env file
|
| 30 |
# in HF Spaces, the .env file is saved in Variables and secrets in settings
|
|
|
|
| 31 |
load_dotenv()
|
| 32 |
|
| 33 |
# === System Prompt ===
|
|
|
|
| 180 |
except Exception as e:
|
| 181 |
return f"Error reading JSONL file: {e}"
|
| 182 |
|
| 183 |
+
@tool
|
| 184 |
+
def python_interpreter(code: str) -> str:
|
| 185 |
+
"""Execute Python code and return the output. Supports data analysis, plotting, and general Python operations."""
|
| 186 |
+
try:
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
# Set up a safe globals environment
|
| 190 |
+
safe_globals = {
|
| 191 |
+
'pd': __import__('pandas'),
|
| 192 |
+
'np': __import__('numpy'),
|
| 193 |
+
'plt': __import__('matplotlib.pyplot'),
|
| 194 |
+
'json': __import__('json'),
|
| 195 |
+
're': __import__('re'),
|
| 196 |
+
'math': __import__('math'),
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
# Capture output
|
| 200 |
+
buffer = io.StringIO()
|
| 201 |
+
with redirect_stdout(buffer):
|
| 202 |
+
# Execute the code in a safe environment
|
| 203 |
+
exec(code, safe_globals)
|
| 204 |
+
|
| 205 |
+
return buffer.getvalue() or "Code executed successfully (no output)"
|
| 206 |
+
except Exception as e:
|
| 207 |
+
return f"Error executing Python code: {e}"
|
| 208 |
+
|
| 209 |
# Update tools list
|
| 210 |
tools: List[StructuredTool] = [
|
| 211 |
calculate, web_search, wikipedia_search, image_recognition,
|
| 212 |
read_pdf, read_csv, read_spreadsheet, transcribe_audio,
|
| 213 |
+
youtube_transcript_tool, youtube_transcript_api, read_jsonl,
|
| 214 |
+
python_interpreter # Add python_interpreter
|
| 215 |
]
|
| 216 |
|
| 217 |
class AgentState(TypedDict):
|