Update core_agent.py

#4
Files changed (1) hide show
  1. core_agent.py +140 -0
core_agent.py CHANGED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from smolagents import (
2
+ CodeAgent,
3
+ DuckDuckGoSearchTool,
4
+ HfApiModel,
5
+ LiteLLMModel,
6
+ OpenAIServerModel,
7
+ PythonInterpreterTool,
8
+ tool,
9
+ InferenceClientModel
10
+ )
11
+
12
+ import requests
13
+
14
+ @tool
15
+ def save_file(content :str,fileName:Optional[str]=None) -> str:
16
+ """Save content to a temporary file and return the path.
17
+ Useful for processing files from the GAIA API.
18
+
19
+ Args:
20
+ content: The content to save to the file
21
+ filename: Optional filename, will generate a random name if not provided
22
+
23
+ Returns:
24
+ Path to the saved file
25
+ """
26
+ temp_dir = tempfile.gettempdir()
27
+ if fileName is None:
28
+ temp_file = tempfile.NamedTemporaryFile(delete=False)
29
+ filepath=temp_file.name
30
+ else:
31
+ filepath=os.path.join(temp_dir,fileName)
32
+
33
+ #write content to file
34
+ with open(filepath,'w') as f:
35
+ f.write(content)
36
+
37
+ return f"File saved to {filepath}. You can read this file to process its contents."
38
+
39
+
40
+
41
+ @tool
42
+ def download_file_from_url(url: str, fileName: Optional[str]=None) -> str:
43
+ """
44
+ Download a file from a URL and save it to a temporary location.
45
+
46
+ Args:
47
+ url: The URL to download from
48
+ filename: Optional filename, will generate one based on URL if not provided
49
+
50
+ Returns:
51
+ Path to the downloaded file
52
+ """
53
+ try:
54
+ if not fileName:
55
+ path=urlparse(url).path
56
+ fileName=os.path.basename(path)
57
+ if not fileName:
58
+ import uuid
59
+ fileName= f"downloaded_{uuid.uuid4().hex[:8]}"
60
+
61
+ temp_dir=tempfile.gettempdir()
62
+ filepath=os.path.join(temp_dir,fileName)
63
+
64
+ #Download the file
65
+ response=requests.get(url,stream=True)
66
+ response.raise_for_status()
67
+
68
+ with open(filepath,'wb') as f:
69
+ for chunk in response.iter_content(chunk_size=8192):
70
+ f.write(chunk)
71
+
72
+ return f"File downloaded to {filepath}. You can now process the file."
73
+ except Exception as e:
74
+ return f"Error downloading file: {str(e)}"
75
+
76
+ @tool
77
+ def extract_text_from_image(image_path: str) -> str:
78
+ """
79
+ Extract text from an image.
80
+
81
+ Args:
82
+ image_path: Path to the image file
83
+
84
+ Returns:
85
+ Extracted text or error message
86
+ """
87
+ try:
88
+ from langchain.document_loaders import UnstructuredImageLoader
89
+ loader = UnstructuredImageLoader(image_path, strategy="ocr_only")
90
+ docs = loader.load()
91
+ # Join the text from all documents into a single string
92
+ extracted_text = "\n".join(doc.page_content for doc in docs)
93
+ return extracted_text
94
+ except ImportError:
95
+ return "Error: pytesseract is not installed. Please install it with 'pip install pytesseract' and ensure Tesseract OCR is installed on your system."
96
+ except Exception as e:
97
+ return f"Error extracting text from image: {str(e)}"
98
+
99
+ @tool
100
+ def analyze_csv_file(file_path: str, query: str) -> str:
101
+ """
102
+ Analyze a CSV file using pandas and answer a question about it.
103
+
104
+ Args:
105
+ file_path: Path to the CSV file
106
+ query: Question about the data
107
+
108
+ Returns:
109
+ Analysis result or error message
110
+ """
111
+ try:
112
+ import pandas as pd
113
+
114
+ # Read the CSV file
115
+ df = pd.read_csv(file_path)
116
+
117
+ # Run various analyses based on the query
118
+ result = f"CSV file loaded with {len(df)} rows and {len(df.columns)} columns.\n"
119
+ result += f"Columns: {', '.join(df.columns)}\n\n"
120
+
121
+ # Add summary statistics
122
+ result += "Summary statistics:\n"
123
+ result += str(df.describe())
124
+
125
+ return result
126
+ except ImportError:
127
+ return "Error: pandas is not installed. Please install it with 'pip install pandas'."
128
+ except Exception as e:
129
+ return f"Error analyzing CSV file: {str(e)}"
130
+
131
+
132
+
133
+
134
+
135
+
136
+
137
+
138
+
139
+
140
+