Spaces:
Runtime error
Runtime error
abtsousa
commited on
Commit
·
2665628
1
Parent(s):
a910dc7
Enhance file handling tools: save_file, read_file, analyze_csv, and extract_text_from_image functions
Browse files- agent/agent.py +2 -2
- tools/files.py +91 -1
agent/agent.py
CHANGED
|
@@ -28,8 +28,8 @@ class OracleBot:
|
|
| 28 |
"""
|
| 29 |
# Enhance question with file context if available
|
| 30 |
if file_path and os.path.exists(file_path):
|
| 31 |
-
question = f"{question}\n\nNote: There is an associated file
|
| 32 |
-
|
| 33 |
messages = [HumanMessage(content=question)]
|
| 34 |
|
| 35 |
for mode, chunk in self.graph.stream({"messages": messages}, config=self.config, stream_mode=["messages", "updates"]): # type: ignore
|
|
|
|
| 28 |
"""
|
| 29 |
# Enhance question with file context if available
|
| 30 |
if file_path and os.path.exists(file_path):
|
| 31 |
+
question = f"{question}\n\nNote: There is an associated file named {os.path.basename(file_path)}\nYou can use the file management tools to read and analyze this file."
|
| 32 |
+
|
| 33 |
messages = [HumanMessage(content=question)]
|
| 34 |
|
| 35 |
for mode, chunk in self.graph.stream({"messages": messages}, config=self.config, stream_mode=["messages", "updates"]): # type: ignore
|
tools/files.py
CHANGED
|
@@ -1,4 +1,94 @@
|
|
| 1 |
from langchain_community.agent_toolkits import FileManagementToolkit
|
| 2 |
from config import CACHE_DIR
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from langchain_community.agent_toolkits import FileManagementToolkit
|
| 2 |
from config import CACHE_DIR
|
| 3 |
+
from langchain_core.tools import tool
|
| 4 |
+
import pandas as pd
|
| 5 |
|
| 6 |
+
__all__ = [
|
| 7 |
+
'save_file',
|
| 8 |
+
'read_file',
|
| 9 |
+
'analyze_csv',
|
| 10 |
+
'extract_text_from_image'
|
| 11 |
+
]
|
| 12 |
+
|
| 13 |
+
@tool
|
| 14 |
+
def save_file(filename: str, content: str) -> bool:
|
| 15 |
+
"""
|
| 16 |
+
Saves content to a file.
|
| 17 |
+
|
| 18 |
+
Args:
|
| 19 |
+
filename (str): The name of the file in the cache directory.
|
| 20 |
+
content (str): The content to save.
|
| 21 |
+
|
| 22 |
+
Returns:
|
| 23 |
+
bool: True if the file was saved successfully, False otherwise.
|
| 24 |
+
"""
|
| 25 |
+
try:
|
| 26 |
+
file_path = CACHE_DIR / filename
|
| 27 |
+
with open(file_path, "w") as f:
|
| 28 |
+
f.write(content)
|
| 29 |
+
return True
|
| 30 |
+
except Exception as e:
|
| 31 |
+
print(f"Error saving file {file_path}: {e}")
|
| 32 |
+
return False
|
| 33 |
+
|
| 34 |
+
@tool
|
| 35 |
+
def read_file(filename: str) -> str:
|
| 36 |
+
"""
|
| 37 |
+
Reads content from a file.
|
| 38 |
+
|
| 39 |
+
Args:
|
| 40 |
+
filename (str): The name of the file in the cache directory.
|
| 41 |
+
|
| 42 |
+
Returns:
|
| 43 |
+
str: The content of the file.
|
| 44 |
+
"""
|
| 45 |
+
try:
|
| 46 |
+
file_path = CACHE_DIR / filename
|
| 47 |
+
with open(file_path, "r") as f:
|
| 48 |
+
return f.read()
|
| 49 |
+
except Exception as e:
|
| 50 |
+
return (f"Error reading file {file_path}: {e}")
|
| 51 |
+
|
| 52 |
+
@tool
|
| 53 |
+
def analyze_csv(filename: str) -> str:
|
| 54 |
+
"""
|
| 55 |
+
Analyzes a CSV file using pandas, returning statistics about the file.
|
| 56 |
+
|
| 57 |
+
Args:
|
| 58 |
+
filename (str): The name of the CSV file in the cache directory.
|
| 59 |
+
|
| 60 |
+
Returns:
|
| 61 |
+
str: The analysis of the file.
|
| 62 |
+
"""
|
| 63 |
+
df = pd.read_csv(CACHE_DIR / filename)
|
| 64 |
+
if df is not None:
|
| 65 |
+
# Perform analysis on the DataFrame
|
| 66 |
+
result = f"CSV file: {len(df)} rows; {len(df.columns)} columns.\n"
|
| 67 |
+
result += f"Columns: {', '.join(df.columns)}\n\n"
|
| 68 |
+
|
| 69 |
+
result += "Summary:\n"
|
| 70 |
+
result += str(df.describe())
|
| 71 |
+
|
| 72 |
+
return result
|
| 73 |
+
|
| 74 |
+
@tool
|
| 75 |
+
def extract_text_from_image(filename: str) -> str:
|
| 76 |
+
"""
|
| 77 |
+
Extracts text from an image file using OCR.
|
| 78 |
+
|
| 79 |
+
Args:
|
| 80 |
+
filename (str): The name of the image file in the cache directory.
|
| 81 |
+
|
| 82 |
+
Returns:
|
| 83 |
+
str: The extracted text from the image.
|
| 84 |
+
"""
|
| 85 |
+
from PIL import Image
|
| 86 |
+
import pytesseract
|
| 87 |
+
|
| 88 |
+
try:
|
| 89 |
+
image_path = CACHE_DIR / filename
|
| 90 |
+
image = Image.open(image_path)
|
| 91 |
+
text = pytesseract.image_to_string(image)
|
| 92 |
+
return text
|
| 93 |
+
except Exception as e:
|
| 94 |
+
return f"Error extracting text from image {filename}: {e}"
|