lucasnseq commited on
Commit
32bb869
·
verified ·
1 Parent(s): f17bad1

Update tools.py

Browse files
Files changed (1) hide show
  1. tools.py +123 -47
tools.py CHANGED
@@ -1,47 +1,123 @@
1
- # Libs
2
- import os
3
- import requests
4
- import pandas as pd
5
- from smolagents import Tool
6
-
7
- # Local
8
- from consts import DEFAULT_API_URL
9
-
10
- class GetTaskFileTool(Tool):
11
- name = "get_task_file_tool"
12
- description = """This tool downloads the file content associated with the given task_id if exists. Returns absolute file path"""
13
- inputs = {
14
- "task_id": {"type": "string", "description": "Task id"},
15
- "file_name": {"type": "string", "description": "File name"},
16
- }
17
- output_type = "string"
18
-
19
- def forward(self, task_id: str, file_name: str) -> str:
20
- response = requests.get(f"{DEFAULT_API_URL}/files/{task_id}", timeout=15)
21
- response.raise_for_status()
22
- with open(file_name, 'wb') as file:
23
- file.write(response.content)
24
- return os.path.abspath(file_name)
25
-
26
- class LoadXlsxFileTool(Tool):
27
- name = "load_xlsx_file_tool"
28
- description = """This tool loads xlsx file into pandas and returns it"""
29
- inputs = {
30
- "file_path": {"type": "string", "description": "File path"}
31
- }
32
- output_type = "object"
33
-
34
- def forward(self, file_path: str) -> object:
35
- return pd.read_excel(file_path)
36
-
37
- class LoadTextFileTool(Tool):
38
- name = "load_text_file_tool"
39
- description = """This tool loads any text file"""
40
- inputs = {
41
- "file_path": {"type": "string", "description": "File path"}
42
- }
43
- output_type = "string"
44
-
45
- def forward(self, file_path: str) -> object:
46
- with open(file_path, 'r', encoding='utf-8') as file:
47
- return file.read()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Libs
2
+ import os
3
+ import requests
4
+ import pandas as pd
5
+ import google.genai as genai
6
+ import base64
7
+ from openai import OpenAI
8
+ from smolagents import Tool
9
+
10
+ # Local
11
+ from consts import DEFAULT_API_URL
12
+
13
+ # Dynamic model ID
14
+ try:
15
+ from app import _SELECTED_MODEL_ID
16
+ if not _SELECTED_MODEL_ID:
17
+ raise ImportError("Model ID not set in app.py")
18
+ except ImportError:
19
+ _SELECTED_MODEL_ID = "gpt-4.1-mini"
20
+
21
+ class GetTaskFileTool(Tool):
22
+ name = "get_task_file_tool"
23
+ description = """This tool downloads the file content associated with the given task_id if exists. Returns absolute file path"""
24
+ inputs = {
25
+ "task_id": {"type": "string", "description": "Task id"},
26
+ "file_name": {"type": "string", "description": "File name"},
27
+ }
28
+ output_type = "string"
29
+
30
+ def forward(self, task_id: str, file_name: str) -> str:
31
+ response = requests.get(f"{DEFAULT_API_URL}/files/{task_id}", timeout=15)
32
+ response.raise_for_status()
33
+ with open(file_name, 'wb') as file:
34
+ file.write(response.content)
35
+ return os.path.abspath(file_name)
36
+
37
+ class LoadXlsxFileTool(Tool):
38
+ name = "load_xlsx_file_tool"
39
+ description = """This tool loads xlsx file into pandas and returns it"""
40
+ inputs = {
41
+ "file_path": {"type": "string", "description": "File path"}
42
+ }
43
+ output_type = "object"
44
+
45
+ def forward(self, file_path: str) -> object:
46
+ return pd.read_excel(file_path)
47
+
48
+ class LoadTextFileTool(Tool):
49
+ name = "load_text_file_tool"
50
+ description = """This tool loads any text file"""
51
+ inputs = {
52
+ "file_path": {"type": "string", "description": "File path"}
53
+ }
54
+ output_type = "string"
55
+
56
+ def forward(self, file_path: str) -> object:
57
+ with open(file_path, 'r', encoding='utf-8') as file:
58
+ return file.read()
59
+
60
+ class AnalyzeImageTool(Tool):
61
+ name = "analyze_image_tool"
62
+ description = """This tool performs a custom analysis of the provided image and returns the corresponding result."""
63
+ inputs = {
64
+ "image_path": {"type": "string", "description": "Image path"},
65
+ "task": {"type": "string", "description": "Task to perform on the image, be detailed and clear"},
66
+ }
67
+ output_type = "string"
68
+
69
+ def __init__(self, model_id=None):
70
+ super().__init__()
71
+ self.model_id = model_id or "gpt-4.1-mini"
72
+
73
+ def forward(self, image_path: str, task: str) -> str:
74
+ """
75
+ Analyze the image at `image_path` according to `task` and return the textual result.
76
+ """
77
+ header = "Image analysis result:\n\n"
78
+ llm_instruction = (
79
+ "You are a highly capable image analysis tool, designed to examine images and deliver detailed descriptions, "
80
+ "insights, and relevant interpretations based on the task at hand.\n\n"
81
+ "Approach the task methodically and provide a thorough and well-reasoned response to the following:\n\n---\nTask:\n"
82
+ f"{task}\n\n"
83
+ )
84
+ try:
85
+ if "gemini" in self.model_id:
86
+ return header + self._analyze_with_gemini(image_path, llm_instruction)
87
+ return header + self._analyze_with_openai(image_path, llm_instruction)
88
+ except Exception as e:
89
+ return f"Error analyzing image: {e}.\nPlease try again."
90
+
91
+ def _analyze_with_gemini(self, image_path: str, task: str) -> str:
92
+ api_key = os.getenv("GOOGLEAI_API_KEY")
93
+ if not api_key:
94
+ raise ValueError("Environment variable GOOGLEAI_API_KEY is not set.")
95
+ client = genai.Client(api_key=api_key)
96
+
97
+ with open(image_path, "rb") as f:
98
+ image_data = f.read()
99
+
100
+ contents = [
101
+ {"inline_data": {"mime_type": "image/jpeg", "data": image_data}},
102
+ {"text": task},
103
+ ]
104
+ response = client.models.generate_content(model=self.model_id, contents=contents)
105
+ return response.candidates[0].content.parts[0].text
106
+
107
+ def _analyze_with_openai(self, image_path: str, task: str) -> str:
108
+ client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
109
+
110
+ with open(image_path, "rb") as f:
111
+ encoded_image = base64.b64encode(f.read()).decode("utf-8")
112
+
113
+ payload = [
114
+ {
115
+ "role": "user",
116
+ "content": [
117
+ {"type": "input_text", "text": task},
118
+ {"type": "input_image", "image_url": f"data:image/jpeg;base64,{encoded_image}"},
119
+ ],
120
+ }
121
+ ]
122
+ response = client.responses.create(model=self.model_id, input=payload)
123
+ return response.output[0].content[0].text