Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -25,169 +25,7 @@ from langchain_google_genai import ChatGoogleGenerativeAI
|
|
| 25 |
|
| 26 |
#load_dotenv()
|
| 27 |
google_api_key = os.environ["GOOGLE_API_KEY"]
|
| 28 |
-
#@@@@@@@@@@@@@@@@@/////////////////////////////////////////////////
|
| 29 |
-
@tool
|
| 30 |
-
def save_and_read_file(content: str, filename: Optional[str] = None) -> str:
|
| 31 |
-
"""
|
| 32 |
-
Save content to a temporary file and return the path.
|
| 33 |
-
Useful for processing files from the GAIA API.
|
| 34 |
-
|
| 35 |
-
Args:
|
| 36 |
-
content: The content to save to the file
|
| 37 |
-
filename: Optional filename, will generate a random name if not provided
|
| 38 |
-
|
| 39 |
-
Returns:
|
| 40 |
-
Path to the saved file
|
| 41 |
-
"""
|
| 42 |
-
temp_dir = tempfile.gettempdir()
|
| 43 |
-
if filename is None:
|
| 44 |
-
temp_file = tempfile.NamedTemporaryFile(delete=False)
|
| 45 |
-
filepath = temp_file.name
|
| 46 |
-
else:
|
| 47 |
-
filepath = os.path.join(temp_dir, filename)
|
| 48 |
-
|
| 49 |
-
# Write content to the file
|
| 50 |
-
with open(filepath, 'w') as f:
|
| 51 |
-
f.write(content)
|
| 52 |
-
|
| 53 |
-
return f"File saved to {filepath}. You can read this file to process its contents."
|
| 54 |
-
|
| 55 |
-
@tool
|
| 56 |
-
def download_file_from_url(url: str, filename: Optional[str] = None) -> str:
|
| 57 |
-
"""
|
| 58 |
-
Download a file from a URL and save it to a temporary location.
|
| 59 |
-
|
| 60 |
-
Args:
|
| 61 |
-
url: The URL to download from
|
| 62 |
-
filename: Optional filename, will generate one based on URL if not provided
|
| 63 |
-
|
| 64 |
-
Returns:
|
| 65 |
-
Path to the downloaded file
|
| 66 |
-
"""
|
| 67 |
-
try:
|
| 68 |
-
# Parse URL to get filename if not provided
|
| 69 |
-
if not filename:
|
| 70 |
-
path = urlparse(url).path
|
| 71 |
-
filename = os.path.basename(path)
|
| 72 |
-
if not filename:
|
| 73 |
-
# Generate a random name if we couldn't extract one
|
| 74 |
-
import uuid
|
| 75 |
-
filename = f"downloaded_{uuid.uuid4().hex[:8]}"
|
| 76 |
-
|
| 77 |
-
# Create temporary file
|
| 78 |
-
temp_dir = tempfile.gettempdir()
|
| 79 |
-
filepath = os.path.join(temp_dir, filename)
|
| 80 |
-
|
| 81 |
-
# Download the file
|
| 82 |
-
response = requests.get(url, stream=True)
|
| 83 |
-
response.raise_for_status()
|
| 84 |
-
|
| 85 |
-
# Save the file
|
| 86 |
-
with open(filepath, 'wb') as f:
|
| 87 |
-
for chunk in response.iter_content(chunk_size=8192):
|
| 88 |
-
f.write(chunk)
|
| 89 |
-
|
| 90 |
-
return f"File downloaded to {filepath}. You can now process this file."
|
| 91 |
-
except Exception as e:
|
| 92 |
-
return f"Error downloading file: {str(e)}"
|
| 93 |
-
|
| 94 |
-
@tool
|
| 95 |
-
def extract_text_from_image(image_path: str) -> str:
|
| 96 |
-
"""
|
| 97 |
-
Extract text from an image using pytesseract (if available).
|
| 98 |
-
|
| 99 |
-
Args:
|
| 100 |
-
image_path: Path to the image file
|
| 101 |
-
|
| 102 |
-
Returns:
|
| 103 |
-
Extracted text or error message
|
| 104 |
-
"""
|
| 105 |
-
try:
|
| 106 |
-
# Try to import pytesseract
|
| 107 |
-
import pytesseract
|
| 108 |
-
from PIL import Image
|
| 109 |
-
|
| 110 |
-
# Open the image
|
| 111 |
-
image = Image.open(image_path)
|
| 112 |
-
|
| 113 |
-
# Extract text
|
| 114 |
-
text = pytesseract.image_to_string(image)
|
| 115 |
-
|
| 116 |
-
return f"Extracted text from image:\n\n{text}"
|
| 117 |
-
except ImportError:
|
| 118 |
-
return "Error: pytesseract is not installed. Please install it with 'pip install pytesseract' and ensure Tesseract OCR is installed on your system."
|
| 119 |
-
except Exception as e:
|
| 120 |
-
return f"Error extracting text from image: {str(e)}"
|
| 121 |
-
|
| 122 |
-
@tool
|
| 123 |
-
def analyze_csv_file(file_path: str, query: str) -> str:
|
| 124 |
-
"""
|
| 125 |
-
Analyze a CSV file using pandas and answer a question about it.
|
| 126 |
-
|
| 127 |
-
Args:
|
| 128 |
-
file_path: Path to the CSV file
|
| 129 |
-
query: Question about the data
|
| 130 |
-
|
| 131 |
-
Returns:
|
| 132 |
-
Analysis result or error message
|
| 133 |
-
"""
|
| 134 |
-
try:
|
| 135 |
-
import pandas as pd
|
| 136 |
-
|
| 137 |
-
# Read the CSV file
|
| 138 |
-
df = pd.read_csv(file_path)
|
| 139 |
-
|
| 140 |
-
# Run various analyses based on the query
|
| 141 |
-
result = f"CSV file loaded with {len(df)} rows and {len(df.columns)} columns.\n"
|
| 142 |
-
result += f"Columns: {', '.join(df.columns)}\n\n"
|
| 143 |
-
|
| 144 |
-
# Add summary statistics
|
| 145 |
-
result += "Summary statistics:\n"
|
| 146 |
-
result += str(df.describe())
|
| 147 |
-
|
| 148 |
-
return result
|
| 149 |
-
except ImportError:
|
| 150 |
-
return "Error: pandas is not installed. Please install it with 'pip install pandas'."
|
| 151 |
-
except Exception as e:
|
| 152 |
-
return f"Error analyzing CSV file: {str(e)}"
|
| 153 |
|
| 154 |
-
@tool
|
| 155 |
-
def analyze_excel_file(file_path: str, query: str) -> str:
|
| 156 |
-
"""
|
| 157 |
-
Analyze an Excel file using pandas and answer a question about it.
|
| 158 |
-
|
| 159 |
-
Args:
|
| 160 |
-
file_path: Path to the Excel file
|
| 161 |
-
query: Question about the data
|
| 162 |
-
|
| 163 |
-
Returns:
|
| 164 |
-
Analysis result or error message
|
| 165 |
-
"""
|
| 166 |
-
try:
|
| 167 |
-
import pandas as pd
|
| 168 |
-
|
| 169 |
-
# Read the Excel file
|
| 170 |
-
df = pd.read_excel(file_path)
|
| 171 |
-
|
| 172 |
-
# Run various analyses based on the query
|
| 173 |
-
result = f"Excel file loaded with {len(df)} rows and {len(df.columns)} columns.\n"
|
| 174 |
-
result += f"Columns: {', '.join(df.columns)}\n\n"
|
| 175 |
-
|
| 176 |
-
# Add summary statistics
|
| 177 |
-
result += "Summary statistics:\n"
|
| 178 |
-
result += str(df.describe())
|
| 179 |
-
|
| 180 |
-
return result
|
| 181 |
-
except ImportError:
|
| 182 |
-
return "Error: pandas and openpyxl are not installed. Please install them with 'pip install pandas openpyxl'."
|
| 183 |
-
except Exception as e:
|
| 184 |
-
return f"Error analyzing Excel file: {str(e)}"
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
#@@@@@@@@@@@@@@@@@@///////////////////////////////////////////////
|
| 191 |
@tool
|
| 192 |
def add(a: int, b: int) -> int:
|
| 193 |
""" Add a and b """
|
|
@@ -266,8 +104,7 @@ If you are asked for a comma separated list, apply the above rules depending of
|
|
| 266 |
system_message = SystemMessage(content=system_prompt)
|
| 267 |
|
| 268 |
tools = [
|
| 269 |
-
add,subtract,multiply,divide,web_search,wikipedia_search,arxiv_search
|
| 270 |
-
save_and_read_file,download_file_from_url,extract_text_from_image,analyze_csv_file,analyze_excel_file
|
| 271 |
]
|
| 272 |
|
| 273 |
|
|
|
|
| 25 |
|
| 26 |
#load_dotenv()
|
| 27 |
google_api_key = os.environ["GOOGLE_API_KEY"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
@tool
|
| 30 |
def add(a: int, b: int) -> int:
|
| 31 |
""" Add a and b """
|
|
|
|
| 104 |
system_message = SystemMessage(content=system_prompt)
|
| 105 |
|
| 106 |
tools = [
|
| 107 |
+
add,subtract,multiply,divide,web_search,wikipedia_search,arxiv_search
|
|
|
|
| 108 |
]
|
| 109 |
|
| 110 |
|