Omnitopia commited on
Commit
9ddc718
·
verified ·
1 Parent(s): 1819218

添加媒体分析工具,tavily_search

Browse files
Files changed (1) hide show
  1. my_tools.py +66 -2
my_tools.py CHANGED
@@ -1,5 +1,7 @@
1
- from smolagents import DuckDuckGoSearchTool, Tool,tool
2
  import wikipediaapi
 
 
3
 
4
  class WikipediaSearchTool(Tool):
5
  name = "wikipedia_search"
@@ -8,9 +10,11 @@ class WikipediaSearchTool(Tool):
8
  "query": {"type": "string", "description": "维基百科搜索关键词,例如人名/专名"}
9
  }
10
  output_type = "string"
 
11
  def __init__(self, lang="en"):
12
  super().__init__()
13
  self.wiki = wikipediaapi.Wikipedia(language=lang, user_agent="celum")
 
14
  def forward(self, query: str):
15
  page = self.wiki.page(query)
16
  if not page.exists():
@@ -60,8 +64,68 @@ def tavily_search(query: str) -> str:
60
  except Exception as e:
61
  return f"Tavily search error: {str(e)}"
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  my_tool_list = [
64
  WikipediaSearchTool(),
65
- DuckDuckGoSearchTool()
66
  tavily_search,
 
67
  ]
 
1
+ from smolagents import DuckDuckGoSearchTool, Tool, tool
2
  import wikipediaapi
3
+ import os
4
+ import requests
5
 
6
  class WikipediaSearchTool(Tool):
7
  name = "wikipedia_search"
 
10
  "query": {"type": "string", "description": "维基百科搜索关键词,例如人名/专名"}
11
  }
12
  output_type = "string"
13
+
14
  def __init__(self, lang="en"):
15
  super().__init__()
16
  self.wiki = wikipediaapi.Wikipedia(language=lang, user_agent="celum")
17
+
18
  def forward(self, query: str):
19
  page = self.wiki.page(query)
20
  if not page.exists():
 
64
  except Exception as e:
65
  return f"Tavily search error: {str(e)}"
66
 
67
+ # 添加媒体分析工具
68
+ @tool
69
+ def analyze_media_file(file_path: str) -> str:
70
+ """
71
+ 分析媒体文件并返回基本信息
72
+ Args:
73
+ file_path: 文件路径
74
+ Returns:
75
+ 文件信息和分析结果
76
+ """
77
+ if not os.path.exists(file_path):
78
+ return f"File not found: {file_path}"
79
+
80
+ try:
81
+ file_size = os.path.getsize(file_path)
82
+ file_ext = os.path.splitext(file_path)[1].lower()
83
+
84
+ result = f"File: {os.path.basename(file_path)}\n"
85
+ result += f"Size: {file_size} bytes\n"
86
+ result += f"Extension: {file_ext}\n"
87
+
88
+ # 根据文件类型进行不同的分析
89
+ if file_ext in ['.png', '.jpg', '.jpeg', '.gif', '.bmp']:
90
+ try:
91
+ from PIL import Image
92
+ with Image.open(file_path) as img:
93
+ result += f"Image dimensions: {img.size[0]}x{img.size[1]}\n"
94
+ result += f"Image mode: {img.mode}\n"
95
+ result += f"Image format: {img.format}\n"
96
+
97
+ # 检测是否可能是棋盘
98
+ if abs(img.size[0] - img.size[1]) < 50:
99
+ result += "Note: Square aspect ratio - possibly a chess board\n"
100
+ except Exception as e:
101
+ result += f"Image analysis error: {str(e)}\n"
102
+
103
+ elif file_ext in ['.mp4', '.avi', '.mov', '.mkv']:
104
+ result += "Video file detected\n"
105
+ elif file_ext in ['.mp3', '.wav', '.m4a', '.flac']:
106
+ result += "Audio file detected\n"
107
+ elif file_ext in ['.pdf']:
108
+ result += "PDF file detected\n"
109
+ elif file_ext in ['.txt', '.csv', '.json']:
110
+ try:
111
+ with open(file_path, 'r', encoding='utf-8') as f:
112
+ content = f.read()
113
+ result += f"Text length: {len(content)} characters\n"
114
+ result += f"Line count: {len(content.splitlines())}\n"
115
+ preview = content[:200].replace('\n', ' ')
116
+ result += f"Content preview: {preview}...\n"
117
+ except Exception as e:
118
+ result += f"Text analysis error: {str(e)}\n"
119
+
120
+ return result
121
+
122
+ except Exception as e:
123
+ return f"Error analyzing file: {str(e)}"
124
+
125
+
126
  my_tool_list = [
127
  WikipediaSearchTool(),
128
+ DuckDuckGoSearchTool(),
129
  tavily_search,
130
+ analyze_media_file,
131
  ]