Spaces:
Sleeping
Sleeping
More tools + Fix Tavily API key
Browse files
agent.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import os
|
| 2 |
-
from typing import Optional
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
from langgraph.graph import START, StateGraph, MessagesState
|
| 5 |
from langgraph.prebuilt import tools_condition, ToolNode
|
|
@@ -13,6 +13,18 @@ from langchain_community.vectorstores import SupabaseVectorStore
|
|
| 13 |
from langchain.tools.retriever import create_retriever_tool
|
| 14 |
from supabase.client import Client, create_client
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
load_dotenv()
|
| 17 |
|
| 18 |
@tool
|
|
@@ -87,8 +99,7 @@ def web_search(query: str) -> dict:
|
|
| 87 |
Args:
|
| 88 |
query: Text
|
| 89 |
"""
|
| 90 |
-
|
| 91 |
-
search_docs = TavilySearchResults(max_result = 3, api_key = api_key).invoke(query = query)
|
| 92 |
formatted_search_doc = "\n\n---\n\n".join(
|
| 93 |
[
|
| 94 |
f'<Document source="{doc.get("url", "")}" title="{doc.get("title", "")}"/>\n{doc.get("content", "")}\n</Document>' for doc in search_docs
|
|
@@ -149,6 +160,26 @@ def get_youtube_transcript(url: str) -> dict:
|
|
| 149 |
except Exception as e:
|
| 150 |
return {"error": f"Failed to get transcript: {str(e)}"}
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
@tool
|
| 153 |
def download_file_from_url(url: str, filename: Optional[str] = None) -> str:
|
| 154 |
"""
|
|
@@ -182,6 +213,180 @@ def download_file_from_url(url: str, filename: Optional[str] = None) -> str:
|
|
| 182 |
except Exception as e:
|
| 183 |
return f"Error downloading file: {str(e)}"
|
| 184 |
|
|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
|
|
|
|
|
|
|
|
|
| 185 |
# Load system prompt
|
| 186 |
# with open("system_prompt.txt", "r", encoding = 'utf-8') as f:
|
| 187 |
# system_prompt = f.read()
|
|
@@ -222,7 +427,9 @@ retriever_tool = create_retriever_tool(
|
|
| 222 |
tools = [
|
| 223 |
add, subtract, multiply, divide, modulus,
|
| 224 |
web_search, wiki_search, arxiv_search,
|
| 225 |
-
read_file, get_youtube_transcript,
|
|
|
|
|
|
|
| 226 |
]
|
| 227 |
|
| 228 |
# Build graph
|
|
|
|
| 1 |
import os
|
| 2 |
+
from typing import List, Dict, Any, Optional
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
from langgraph.graph import START, StateGraph, MessagesState
|
| 5 |
from langgraph.prebuilt import tools_condition, ToolNode
|
|
|
|
| 13 |
from langchain.tools.retriever import create_retriever_tool
|
| 14 |
from supabase.client import Client, create_client
|
| 15 |
|
| 16 |
+
import tempfile
|
| 17 |
+
import re
|
| 18 |
+
import json
|
| 19 |
+
import requests
|
| 20 |
+
from urllib.parse import urlparse
|
| 21 |
+
import pytesseract
|
| 22 |
+
from PIL import Image, ImageDraw, ImageFont, ImageEnhance, ImageFilter
|
| 23 |
+
import cmath
|
| 24 |
+
import pandas as pd
|
| 25 |
+
import uuid
|
| 26 |
+
import numpy as np
|
| 27 |
+
|
| 28 |
load_dotenv()
|
| 29 |
|
| 30 |
@tool
|
|
|
|
| 99 |
Args:
|
| 100 |
query: Text
|
| 101 |
"""
|
| 102 |
+
search_docs = TavilySearchResults(max_result = 3).invoke(query = query)
|
|
|
|
| 103 |
formatted_search_doc = "\n\n---\n\n".join(
|
| 104 |
[
|
| 105 |
f'<Document source="{doc.get("url", "")}" title="{doc.get("title", "")}"/>\n{doc.get("content", "")}\n</Document>' for doc in search_docs
|
|
|
|
| 160 |
except Exception as e:
|
| 161 |
return {"error": f"Failed to get transcript: {str(e)}"}
|
| 162 |
|
| 163 |
+
@tool
|
| 164 |
+
def save_and_read_file(content: str, filename: Optional[str] = None) -> str:
|
| 165 |
+
"""
|
| 166 |
+
Save content to a file and return the path.
|
| 167 |
+
Args:
|
| 168 |
+
content (str): the content to save to the file
|
| 169 |
+
filename (str, optional): the name of the file. If not provided, a random name file will be created.
|
| 170 |
+
"""
|
| 171 |
+
temp_dir = tempfile.gettempdir()
|
| 172 |
+
if filename is None:
|
| 173 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, dir=temp_dir)
|
| 174 |
+
filepath = temp_file.name
|
| 175 |
+
else:
|
| 176 |
+
filepath = os.path.join(temp_dir, filename)
|
| 177 |
+
|
| 178 |
+
with open(filepath, "w") as f:
|
| 179 |
+
f.write(content)
|
| 180 |
+
|
| 181 |
+
return f"File saved to {filepath}. You can read this file to process its contents."
|
| 182 |
+
|
| 183 |
@tool
|
| 184 |
def download_file_from_url(url: str, filename: Optional[str] = None) -> str:
|
| 185 |
"""
|
|
|
|
| 213 |
except Exception as e:
|
| 214 |
return f"Error downloading file: {str(e)}"
|
| 215 |
|
| 216 |
+
@tool
|
| 217 |
+
def extract_text_from_image(image_path: str) -> str:
|
| 218 |
+
"""
|
| 219 |
+
Extract text from an image using OCR library pytesseract (if available).
|
| 220 |
+
Args:
|
| 221 |
+
image_path (str): the path to the image file.
|
| 222 |
+
"""
|
| 223 |
+
try:
|
| 224 |
+
# Open the image
|
| 225 |
+
image = Image.open(image_path)
|
| 226 |
+
|
| 227 |
+
# Extract text from the image
|
| 228 |
+
text = pytesseract.image_to_string(image)
|
| 229 |
+
|
| 230 |
+
return f"Extracted text from image:\n\n{text}"
|
| 231 |
+
except Exception as e:
|
| 232 |
+
return f"Error extracting text from image: {str(e)}"
|
| 233 |
+
|
| 234 |
+
|
| 235 |
+
@tool
|
| 236 |
+
def analyze_csv_file(file_path: str, query: str) -> str:
|
| 237 |
+
"""
|
| 238 |
+
Analyze a CSV file using pandas and answer a question about it.
|
| 239 |
+
Args:
|
| 240 |
+
file_path (str): the path to the CSV file.
|
| 241 |
+
query (str): Question about the data
|
| 242 |
+
"""
|
| 243 |
+
try:
|
| 244 |
+
# Read the CSV file
|
| 245 |
+
df = pd.read_csv(file_path)
|
| 246 |
+
|
| 247 |
+
# Run various analyses based on the query
|
| 248 |
+
result = f"CSV file loaded with {len(df)} rows and {len(df.columns)} columns.\n"
|
| 249 |
+
result += f"Columns: {', '.join(df.columns)}\n\n"
|
| 250 |
+
|
| 251 |
+
# Add summary statistics
|
| 252 |
+
result += "Summary statistics:\n"
|
| 253 |
+
result += str(df.describe())
|
| 254 |
+
|
| 255 |
+
return result
|
| 256 |
+
|
| 257 |
+
except Exception as e:
|
| 258 |
+
return f"Error analyzing CSV file: {str(e)}"
|
| 259 |
+
|
| 260 |
+
|
| 261 |
+
@tool
|
| 262 |
+
def analyze_excel_file(file_path: str, query: str) -> str:
|
| 263 |
+
"""
|
| 264 |
+
Analyze an Excel file using pandas and answer a question about it.
|
| 265 |
+
Args:
|
| 266 |
+
file_path (str): the path to the Excel file.
|
| 267 |
+
query (str): Question about the data
|
| 268 |
+
"""
|
| 269 |
+
try:
|
| 270 |
+
# Read the Excel file
|
| 271 |
+
df = pd.read_excel(file_path)
|
| 272 |
+
|
| 273 |
+
# Run various analyses based on the query
|
| 274 |
+
result = (
|
| 275 |
+
f"Excel file loaded with {len(df)} rows and {len(df.columns)} columns.\n"
|
| 276 |
+
)
|
| 277 |
+
result += f"Columns: {', '.join(df.columns)}\n\n"
|
| 278 |
+
|
| 279 |
+
# Add summary statistics
|
| 280 |
+
result += "Summary statistics:\n"
|
| 281 |
+
result += str(df.describe())
|
| 282 |
+
|
| 283 |
+
return result
|
| 284 |
+
|
| 285 |
+
except Exception as e:
|
| 286 |
+
return f"Error analyzing Excel file: {str(e)}"
|
| 287 |
+
|
| 288 |
+
@tool
|
| 289 |
+
def analyze_image(image_base64: str) -> Dict[str, Any]:
|
| 290 |
+
"""
|
| 291 |
+
Analyze basic properties of an image (size, mode, color analysis, thumbnail preview).
|
| 292 |
+
Args:
|
| 293 |
+
image_base64 (str): Base64 encoded image string
|
| 294 |
+
Returns:
|
| 295 |
+
Dictionary with analysis result
|
| 296 |
+
"""
|
| 297 |
+
try:
|
| 298 |
+
img = decode_image(image_base64)
|
| 299 |
+
width, height = img.size
|
| 300 |
+
mode = img.mode
|
| 301 |
+
|
| 302 |
+
if mode in ("RGB", "RGBA"):
|
| 303 |
+
arr = np.array(img)
|
| 304 |
+
avg_colors = arr.mean(axis=(0, 1))
|
| 305 |
+
dominant = ["Red", "Green", "Blue"][np.argmax(avg_colors[:3])]
|
| 306 |
+
brightness = avg_colors.mean()
|
| 307 |
+
color_analysis = {
|
| 308 |
+
"average_rgb": avg_colors.tolist(),
|
| 309 |
+
"brightness": brightness,
|
| 310 |
+
"dominant_color": dominant,
|
| 311 |
+
}
|
| 312 |
+
else:
|
| 313 |
+
color_analysis = {"note": f"No color analysis for mode {mode}"}
|
| 314 |
+
|
| 315 |
+
thumbnail = img.copy()
|
| 316 |
+
thumbnail.thumbnail((100, 100))
|
| 317 |
+
thumb_path = save_image(thumbnail, "thumbnails")
|
| 318 |
+
thumbnail_base64 = encode_image(thumb_path)
|
| 319 |
+
|
| 320 |
+
return {
|
| 321 |
+
"dimensions": (width, height),
|
| 322 |
+
"mode": mode,
|
| 323 |
+
"color_analysis": color_analysis,
|
| 324 |
+
"thumbnail": thumbnail_base64,
|
| 325 |
+
}
|
| 326 |
+
except Exception as e:
|
| 327 |
+
return {"error": str(e)}
|
| 328 |
+
|
| 329 |
+
|
| 330 |
+
@tool
|
| 331 |
+
def transform_image(
|
| 332 |
+
image_base64: str, operation: str, params: Optional[Dict[str, Any]] = None
|
| 333 |
+
) -> Dict[str, Any]:
|
| 334 |
+
"""
|
| 335 |
+
Apply transformations: resize, rotate, crop, flip, brightness, contrast, blur, sharpen, grayscale.
|
| 336 |
+
Args:
|
| 337 |
+
image_base64 (str): Base64 encoded input image
|
| 338 |
+
operation (str): Transformation operation
|
| 339 |
+
params (Dict[str, Any], optional): Parameters for the operation
|
| 340 |
+
Returns:
|
| 341 |
+
Dictionary with transformed image (base64)
|
| 342 |
+
"""
|
| 343 |
+
try:
|
| 344 |
+
img = decode_image(image_base64)
|
| 345 |
+
params = params or {}
|
| 346 |
+
|
| 347 |
+
if operation == "resize":
|
| 348 |
+
img = img.resize(
|
| 349 |
+
(
|
| 350 |
+
params.get("width", img.width // 2),
|
| 351 |
+
params.get("height", img.height // 2),
|
| 352 |
+
)
|
| 353 |
+
)
|
| 354 |
+
elif operation == "rotate":
|
| 355 |
+
img = img.rotate(params.get("angle", 90), expand=True)
|
| 356 |
+
elif operation == "crop":
|
| 357 |
+
img = img.crop(
|
| 358 |
+
(
|
| 359 |
+
params.get("left", 0),
|
| 360 |
+
params.get("top", 0),
|
| 361 |
+
params.get("right", img.width),
|
| 362 |
+
params.get("bottom", img.height),
|
| 363 |
+
)
|
| 364 |
+
)
|
| 365 |
+
elif operation == "flip":
|
| 366 |
+
if params.get("direction", "horizontal") == "horizontal":
|
| 367 |
+
img = img.transpose(Image.FLIP_LEFT_RIGHT)
|
| 368 |
+
else:
|
| 369 |
+
img = img.transpose(Image.FLIP_TOP_BOTTOM)
|
| 370 |
+
elif operation == "adjust_brightness":
|
| 371 |
+
img = ImageEnhance.Brightness(img).enhance(params.get("factor", 1.5))
|
| 372 |
+
elif operation == "adjust_contrast":
|
| 373 |
+
img = ImageEnhance.Contrast(img).enhance(params.get("factor", 1.5))
|
| 374 |
+
elif operation == "blur":
|
| 375 |
+
img = img.filter(ImageFilter.GaussianBlur(params.get("radius", 2)))
|
| 376 |
+
elif operation == "sharpen":
|
| 377 |
+
img = img.filter(ImageFilter.SHARPEN)
|
| 378 |
+
elif operation == "grayscale":
|
| 379 |
+
img = img.convert("L")
|
| 380 |
+
else:
|
| 381 |
+
return {"error": f"Unknown operation: {operation}"}
|
| 382 |
+
|
| 383 |
+
result_path = save_image(img)
|
| 384 |
+
result_base64 = encode_image(result_path)
|
| 385 |
+
return {"transformed_image": result_base64}
|
| 386 |
+
|
| 387 |
+
except Exception as e:
|
| 388 |
+
return {"error": str(e)}
|
| 389 |
+
|
| 390 |
# Load system prompt
|
| 391 |
# with open("system_prompt.txt", "r", encoding = 'utf-8') as f:
|
| 392 |
# system_prompt = f.read()
|
|
|
|
| 427 |
tools = [
|
| 428 |
add, subtract, multiply, divide, modulus,
|
| 429 |
web_search, wiki_search, arxiv_search,
|
| 430 |
+
read_file, get_youtube_transcript,
|
| 431 |
+
download_file_from_url, save_and_read_file,
|
| 432 |
+
extract_text_from_image, analyze_csv_file, analyze_excel_file, analyze_image, transform_image
|
| 433 |
]
|
| 434 |
|
| 435 |
# Build graph
|