ORromu commited on
Commit
40375fc
·
verified ·
1 Parent(s): 8286586

Update tool.py

Browse files
Files changed (1) hide show
  1. tool.py +129 -1
tool.py CHANGED
@@ -119,4 +119,132 @@ def PubmedSearchTool(query: str) -> str:
119
  f'<Document source="{doc.metadata["uid"]}" title="{doc.metadata["Title"]}"/>\n{doc.page_content[:1000]}\n</Document>'
120
  for doc in search_docs
121
  ])
122
- return {"pubmed_results": formatted_search_docs}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  f'<Document source="{doc.metadata["uid"]}" title="{doc.metadata["Title"]}"/>\n{doc.page_content[:1000]}\n</Document>'
120
  for doc in search_docs
121
  ])
122
+ return {"pubmed_results": formatted_search_docs}
123
+
124
+
125
+ @tool
126
+ def save_and_read_file(content: str, filename: Optional[str] = None) -> str:
127
+ """Save content to a file and return the path.
128
+
129
+ Args:
130
+ content (str): the content to save to the file
131
+ filename (str, optional): the name of the file. If not provided, a random name file will be created.
132
+ """
133
+ temp_dir = tempfile.gettempdir()
134
+ if filename is None:
135
+ temp_file = tempfile.NamedTemporaryFile(delete=False, dir=temp_dir)
136
+ filepath = temp_file.name
137
+ else:
138
+ filepath = os.path.join(temp_dir, filename)
139
+
140
+ with open(filepath, "w") as f:
141
+ f.write(content)
142
+
143
+ return f"File saved to {filepath}. You can read this file to process its contents."
144
+
145
+
146
+ @tool
147
+ def download_file_from_url(url: str, filename: Optional[str] = None) -> str:
148
+ """Download a file from a URL and save it to a temporary location.
149
+
150
+ Args:
151
+ url (str): the URL of the file to download.
152
+ filename (str, optional): the name of the file. If not provided, a random name file will be created.
153
+ """
154
+ try:
155
+ # Parse URL to get filename if not provided
156
+ if not filename:
157
+ path = urlparse(url).path
158
+ filename = os.path.basename(path)
159
+ if not filename:
160
+ filename = f"downloaded_{uuid.uuid4().hex[:8]}"
161
+
162
+ # Create temporary file
163
+ temp_dir = tempfile.gettempdir()
164
+ filepath = os.path.join(temp_dir, filename)
165
+
166
+ # Download the file
167
+ response = requests.get(url, stream=True)
168
+ response.raise_for_status()
169
+
170
+ # Save the file
171
+ with open(filepath, "wb") as f:
172
+ for chunk in response.iter_content(chunk_size=8192):
173
+ f.write(chunk)
174
+
175
+ return f"File downloaded to {filepath}. You can read this file to process its contents."
176
+ except Exception as e:
177
+ return f"Error downloading file: {str(e)}"
178
+
179
+
180
+ @tool
181
+ def extract_text_from_image(image_path: str) -> str:
182
+ """Extract text from an image using OCR library pytesseract (if available).
183
+
184
+ Args:
185
+ image_path (str): the path to the image file.
186
+ """
187
+ try:
188
+ # Open the image
189
+ image = Image.open(image_path)
190
+
191
+ # Extract text from the image
192
+ text = pytesseract.image_to_string(image)
193
+
194
+ return f"Extracted text from image:\n\n{text}"
195
+ except Exception as e:
196
+ return f"Error extracting text from image: {str(e)}"
197
+
198
+
199
+ @tool
200
+ def analyze_csv_file(file_path: str, query: str) -> str:
201
+ """Analyze a CSV file using pandas and answer a question about it.
202
+
203
+ Args:
204
+ file_path (str): the path to the CSV file.
205
+ query (str): Question about the data
206
+ """
207
+ try:
208
+ # Read the CSV file
209
+ df = pd.read_csv(file_path)
210
+
211
+ # Run various analyses based on the query
212
+ result = f"CSV file loaded with {len(df)} rows and {len(df.columns)} columns.\n"
213
+ result += f"Columns: {', '.join(df.columns)}\n\n"
214
+
215
+ # Add summary statistics
216
+ result += "Summary statistics:\n"
217
+ result += str(df.describe())
218
+
219
+ return result
220
+
221
+ except Exception as e:
222
+ return f"Error analyzing CSV file: {str(e)}"
223
+
224
+
225
+ @tool
226
+ def analyze_excel_file(file_path: str, query: str) -> str:
227
+ """Analyze an Excel file using pandas and answer a question about it.
228
+
229
+ Args:
230
+ file_path (str): the path to the Excel file.
231
+ query (str): Question about the data
232
+ """
233
+ try:
234
+ # Read the Excel file
235
+ df = pd.read_excel(file_path)
236
+
237
+ # Run various analyses based on the query
238
+ result = (
239
+ f"Excel file loaded with {len(df)} rows and {len(df.columns)} columns.\n"
240
+ )
241
+ result += f"Columns: {', '.join(df.columns)}\n\n"
242
+
243
+ # Add summary statistics
244
+ result += "Summary statistics:\n"
245
+ result += str(df.describe())
246
+
247
+ return result
248
+
249
+ except Exception as e:
250
+ return f"Error analyzing Excel file: {str(e)}"