bstraehle commited on
Commit
148f2b3
·
verified ·
1 Parent(s): 81edcab

Update crew.py

Browse files
Files changed (1) hide show
  1. crew.py +54 -5
crew.py CHANGED
@@ -1,4 +1,4 @@
1
- import os
2
  from crewai import Agent, Crew, Process, Task
3
  from crewai.tools import tool
4
  from crewai_tools import (
@@ -99,13 +99,46 @@ def run_crew(question, file_path):
99
  return completion.choices[0].message.content
100
  except Exception as e:
101
  raise RuntimeError(f"Failed to process image: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
 
103
  # Built-in tools
104
 
105
  python_coding_tool = CodeInterpreterTool()
106
- video_analysis_tool = YoutubeVideoSearchTool()
107
  web_search_tool = SerperDevTool()
108
  web_rag_tool = WebsiteSearchTool()
 
109
 
110
  # Agents
111
 
@@ -163,10 +196,21 @@ def run_crew(question, file_path):
163
  tools=[web_search_tool, web_rag_tool],
164
  verbose=False
165
  )
 
 
 
 
 
 
 
 
 
 
 
166
 
167
  manager_agent = Agent(
168
  role="Manager Agent",
169
- goal="Try to answer the following question. If needed, delegate to **one** of your coworkers, Image Analysis Agent, Python Coding Agent, Web Search Agent, or YouTube Analysis Agent for help. "
170
  "If there is no good coworker, delegate to the Python Coding Agent to implement a tool for the task. "
171
  "Question: \"{question}\"",
172
  backstory="As an expert manager assistant, you answer the question.",
@@ -180,14 +224,19 @@ def run_crew(question, file_path):
180
 
181
  manager_task = Task(
182
  agent=manager_agent,
183
- description="Try to answer the following question. If needed, delegate to **one** of your coworkers, Image Analysis Agent, Python Coding Agent, Web Search Agent, or YouTube Analysis Agent for help. Question: \"{question}\"",
184
  expected_output="The answer to the question."
185
  )
186
 
187
  # Crew
188
 
189
  crew = Crew(
190
- agents=[audio_analysis_agent, image_analysis_agent, python_coding_agent, video_analysis_agent, web_search_agent],
 
 
 
 
 
191
  manager_agent=manager_agent,
192
  tasks=[manager_task],
193
  verbose=True
 
1
+ import cv2, os
2
  from crewai import Agent, Crew, Process, Task
3
  from crewai.tools import tool
4
  from crewai_tools import (
 
99
  return completion.choices[0].message.content
100
  except Exception as e:
101
  raise RuntimeError(f"Failed to process image: {str(e)}")
102
+
103
+ @tool("Video Analysis Tool")
104
+ def video_analysis_tool(question: str, file_path: str) -> str:
105
+ """Answer a question about a video file.
106
+
107
+ Args:
108
+ question (str): Question about the video file
109
+ file_path (str): Path of the video file
110
+
111
+ Returns:
112
+ str: Answer to the question about the video file
113
+
114
+ Raises:
115
+ FileNotFoundError: If the video file does not exist
116
+ RuntimeError: If processing fails"""
117
+ if not os.path.exists(file_path):
118
+ raise FileNotFoundError(f"Image file not found: {file_path}")
119
+
120
+ try:
121
+ img_b64 = get_img_b64(file_path)
122
+
123
+ client = OpenAI()
124
+
125
+ completion = client.chat.completions.create(
126
+ messages=[{"role": "user",
127
+ "content": [{"type": "text", "text": question},
128
+ {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{img_b64}"}}]}],
129
+ model=IMAGE_MODEL
130
+ )
131
+
132
+ return completion.choices[0].message.content
133
+ except Exception as e:
134
+ raise RuntimeError(f"Failed to process image: {str(e)}")
135
 
136
  # Built-in tools
137
 
138
  python_coding_tool = CodeInterpreterTool()
 
139
  web_search_tool = SerperDevTool()
140
  web_rag_tool = WebsiteSearchTool()
141
+ youtube_video_analysis_tool = YoutubeVideoSearchTool()
142
 
143
  # Agents
144
 
 
196
  tools=[web_search_tool, web_rag_tool],
197
  verbose=False
198
  )
199
+
200
+ youtube_video_analysis_agent = Agent(
201
+ role="YouTube Video Analysis Agent",
202
+ goal="Analyze YouTube video to help answer question \"{question}\"",
203
+ backstory="As an expert YouTube video analysis assistant, you analyze the video to help answer the question.",
204
+ allow_delegation=False,
205
+ llm=AGENT_MODEL,
206
+ max_iter=3,
207
+ tools=[youtube_video_analysis_tool],
208
+ verbose=False
209
+ )
210
 
211
  manager_agent = Agent(
212
  role="Manager Agent",
213
+ goal="Try to answer the following question. If needed, delegate to one or more of your coworkers for help. "
214
  "If there is no good coworker, delegate to the Python Coding Agent to implement a tool for the task. "
215
  "Question: \"{question}\"",
216
  backstory="As an expert manager assistant, you answer the question.",
 
224
 
225
  manager_task = Task(
226
  agent=manager_agent,
227
+ description="Try to answer the following question. If needed, delegate to one or more of your coworkers for help. Question: \"{question}\"",
228
  expected_output="The answer to the question."
229
  )
230
 
231
  # Crew
232
 
233
  crew = Crew(
234
+ agents=[audio_analysis_agent,
235
+ image_analysis_agent,
236
+ python_coding_agent,
237
+ video_analysis_agent,
238
+ web_search_agent,
239
+ youtube_video_analysis_agent],
240
  manager_agent=manager_agent,
241
  tasks=[manager_task],
242
  verbose=True