bstraehle commited on
Commit
617431a
·
verified ·
1 Parent(s): 9e48aeb

Update crew.py

Browse files
Files changed (1) hide show
  1. crew.py +123 -70
crew.py CHANGED
@@ -4,12 +4,12 @@ from crewai.tools import tool
4
  from crewai_tools import (
5
  CodeInterpreterTool,
6
  SerperDevTool,
7
- WebsiteSearchTool,
8
- YoutubeVideoSearchTool
9
  )
10
  from openai import OpenAI
11
  from openinference.instrumentation.crewai import CrewAIInstrumentor
12
  from phoenix.otel import register
 
13
  from util import get_final_answer, get_img_b64
14
 
15
  ## LLMs
@@ -38,6 +38,43 @@ CrewAIInstrumentor().instrument(tracer_provider=tracer_provider)
38
  def run_crew(question, file_path):
39
  # Custom tools
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  @tool("Audio Analysis Tool")
42
  def audio_analysis_tool(question: str, file_path: str) -> str:
43
  """Answer a question about an audio file.
@@ -67,59 +104,72 @@ def run_crew(question, file_path):
67
  return transcript.text
68
  except Exception as e:
69
  raise RuntimeError(f"Failed to process audio: {str(e)}")
70
-
71
- @tool("Image Analysis Tool")
72
- def image_analysis_tool(question: str, file_path: str) -> str:
73
- """Answer a question about an image file.
74
 
75
  Args:
76
- question (str): Question about the image file
77
- file_path (str): Path of the image file
78
 
79
  Returns:
80
- str: Answer to the question about the image file
81
 
82
  Raises:
83
- FileNotFoundError: If the image file does not exist
84
  RuntimeError: If processing fails"""
85
- if not os.path.exists(file_path):
86
- raise FileNotFoundError(f"Image file not found: {file_path}")
87
 
88
- try:
89
- img_b64 = get_img_b64(file_path)
 
 
 
 
 
90
 
 
 
91
  client = OpenAI()
92
 
93
- completion = client.chat.completions.create(
94
- messages=[{"role": "user",
95
- "content": [{"type": "text", "text": question},
96
- {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{img_b64}"}}]}],
97
- model=IMAGE_MODEL
98
  )
99
-
100
- return completion.choices[0].message.content
101
  except Exception as e:
102
- raise RuntimeError(f"Failed to process image: {str(e)}")
103
 
104
- @tool("Video Analysis Tool")
105
- def video_analysis_tool(question: str, file_path: str) -> str:
106
- """Answer a question about a video file.
107
 
108
  Args:
109
- question (str): Question about the video file
110
- file_path (str): Path of the video file
111
 
112
  Returns:
113
- str: Answer to the question about the video file
114
 
115
  Raises:
116
- FileNotFoundError: If the video file does not exist
117
  RuntimeError: If processing fails"""
118
- if not os.path.exists(file_path):
119
- raise FileNotFoundError(f"Video file not found: {file_path}")
120
-
121
  try:
122
- video = cv2.VideoCapture(file_path)
 
 
 
 
 
 
 
 
 
 
 
 
123
 
124
  base64Frames = []
125
 
@@ -134,7 +184,11 @@ def run_crew(question, file_path):
134
  base64Frames.append(base64.b64encode(buffer).decode("utf-8"))
135
 
136
  video.release()
 
 
137
 
 
 
138
  response = client.responses.create(
139
  input=[{"role": "user",
140
  "content": [{"type": "input_text", "text": (question)},
@@ -148,24 +202,23 @@ def run_crew(question, file_path):
148
 
149
  # Built-in tools
150
 
151
- python_coding_tool = CodeInterpreterTool()
152
  web_search_tool = SerperDevTool()
153
  web_rag_tool = WebsiteSearchTool()
154
- youtube_video_analysis_tool = YoutubeVideoSearchTool()
155
 
156
  # Agents
157
 
158
- audio_analysis_agent = Agent(
159
- role="Audio Analysis Agent",
160
- goal="Analyze audio to help answer question \"{question}\"",
161
- backstory="As an expert audio analysis assistant, you analyze the audio to help answer the question.",
162
  allow_delegation=False,
163
  llm=AGENT_MODEL,
164
  max_iter=3,
165
- tools=[audio_analysis_tool],
166
  verbose=False
167
  )
168
-
169
  image_analysis_agent = Agent(
170
  role="Image Analysis Agent",
171
  goal="Analyze image to help answer question \"{question}\"",
@@ -176,40 +229,29 @@ def run_crew(question, file_path):
176
  tools=[image_analysis_tool],
177
  verbose=False
178
  )
179
-
180
- python_coding_agent = Agent(
181
- role="Python Coding Agent",
182
- goal="Write and/or execute Python code to help answer question \"{question}\"",
183
- backstory="As an expert Python coding assistant, you write and/or execute Python code to help answer the question.",
184
- allow_delegation=False,
185
- llm=AGENT_MODEL,
186
- max_iter=10,
187
- tools=[python_coding_tool],
188
- verbose=False
189
- )
190
 
191
- video_analysis_agent = Agent(
192
- role="Video Analysis Agent",
193
- goal="Analyze video to help answer question \"{question}\"",
194
- backstory="As an expert video analysis assistant, you analyze the video to help answer the question.",
195
  allow_delegation=False,
196
  llm=AGENT_MODEL,
197
  max_iter=3,
198
- tools=[video_analysis_tool],
199
  verbose=False
200
  )
201
 
202
- web_search_agent = Agent(
203
- role="Web Search Agent",
204
- goal="Search the web to help answer question \"{question}\", then scrape the most relevant web page.",
205
- backstory="As an expert web search assistant, you search the web to help answer the question.",
206
  allow_delegation=False,
207
  llm=AGENT_MODEL,
208
  max_iter=3,
209
- tools=[web_search_tool, web_rag_tool],
210
  verbose=False
211
  )
212
-
213
  youtube_video_analysis_agent = Agent(
214
  role="YouTube Video Analysis Agent",
215
  goal="Analyze YouTube video to help answer question \"{question}\"",
@@ -217,10 +259,21 @@ def run_crew(question, file_path):
217
  allow_delegation=False,
218
  llm=AGENT_MODEL,
219
  max_iter=3,
220
- tools=[youtube_video_analysis_tool],
221
  verbose=False
222
  )
223
-
 
 
 
 
 
 
 
 
 
 
 
224
  manager_agent = Agent(
225
  role="Manager Agent",
226
  goal="Try to answer the following question. If needed, delegate to one or more of your coworkers for help. "
@@ -244,12 +297,12 @@ def run_crew(question, file_path):
244
  # Crew
245
 
246
  crew = Crew(
247
- agents=[audio_analysis_agent,
248
  image_analysis_agent,
249
- python_coding_agent,
250
- video_analysis_agent,
251
- web_search_agent,
252
- youtube_video_analysis_agent],
253
  manager_agent=manager_agent,
254
  tasks=[manager_task],
255
  verbose=True
 
4
  from crewai_tools import (
5
  CodeInterpreterTool,
6
  SerperDevTool,
7
+ WebsiteSearchTool
 
8
  )
9
  from openai import OpenAI
10
  from openinference.instrumentation.crewai import CrewAIInstrumentor
11
  from phoenix.otel import register
12
+ from pytube import YouTube
13
  from util import get_final_answer, get_img_b64
14
 
15
  ## LLMs
 
38
  def run_crew(question, file_path):
39
  # Custom tools
40
 
41
+ @tool("Image Analysis Tool")
42
+ def image_analysis_tool(question: str, file_path: str) -> str:
43
+ """Answer a question about an image file.
44
+
45
+ Args:
46
+ question (str): Question about the image file
47
+ file_path (str): Path of the image file
48
+
49
+ Returns:
50
+ str: Answer to the question about the image file
51
+
52
+ Raises:
53
+ FileNotFoundError: If the image file does not exist
54
+ RuntimeError: If processing fails"""
55
+ if not os.path.exists(file_path):
56
+ raise FileNotFoundError(f"Image file not found: {file_path}")
57
+
58
+ try:
59
+ # Get image
60
+
61
+ img_b64 = get_img_b64(file_path)
62
+
63
+ # OpenAI
64
+
65
+ client = OpenAI()
66
+
67
+ completion = client.chat.completions.create(
68
+ messages=[{"role": "user",
69
+ "content": [{"type": "text", "text": question},
70
+ {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{img_b64}"}}]}],
71
+ model=IMAGE_MODEL
72
+ )
73
+
74
+ return completion.choices[0].message.content
75
+ except Exception as e:
76
+ raise RuntimeError(f"Failed to process image: {str(e)}")
77
+
78
  @tool("Audio Analysis Tool")
79
  def audio_analysis_tool(question: str, file_path: str) -> str:
80
  """Answer a question about an audio file.
 
104
  return transcript.text
105
  except Exception as e:
106
  raise RuntimeError(f"Failed to process audio: {str(e)}")
107
+
108
+ @tool("YouTube Audio Analysis Tool")
109
+ def youtube_audio_analysis_tool(question: str, url: str) -> str:
110
+ """Answer a question about YouTube audio.
111
 
112
  Args:
113
+ question (str): Question about YouTube audio
114
+ url (str): YouTube URL
115
 
116
  Returns:
117
+ str: Answer to the question about YouTube audio
118
 
119
  Raises:
 
120
  RuntimeError: If processing fails"""
121
+ try:
122
+ # YouTube
123
 
124
+ file = "audio.mp4"
125
+
126
+ yt = YouTube(url)
127
+
128
+ stream = yt.streams.filter(only_audio=True).first()
129
+
130
+ stream.download(output_path=".", filename=file)
131
 
132
+ # OpenAI
133
+
134
  client = OpenAI()
135
 
136
+ transcription = client.audio.transcriptions.create(
137
+ file=open(file, "rb"),
138
+ model=AUDIO_MODEL,
139
+ prompt=question
 
140
  )
141
+
142
+ return transcription.text
143
  except Exception as e:
144
+ raise RuntimeError(f"Failed to process video: {str(e)}")
145
 
146
+ @tool("YouTube Video Analysis Tool")
147
+ def youtube_video_analysis_tool(question: str, url: str) -> str:
148
+ """Answer a question about YouTube video.
149
 
150
  Args:
151
+ question (str): Question about YouTube video
152
+ url (str): YouTube URL
153
 
154
  Returns:
155
+ str: Answer to the question about YouTube video
156
 
157
  Raises:
 
158
  RuntimeError: If processing fails"""
 
 
 
159
  try:
160
+ # YouTube
161
+
162
+ file = "video.mp4"
163
+
164
+ yt = YouTube(url)
165
+
166
+ stream = yt.streams.get_highest_resolution()
167
+
168
+ stream.download(output_path=".", filename=file)
169
+
170
+ # Get video TODOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOo
171
+
172
+ video = cv2.VideoCapture(file)
173
 
174
  base64Frames = []
175
 
 
184
  base64Frames.append(base64.b64encode(buffer).decode("utf-8"))
185
 
186
  video.release()
187
+
188
+ # OpenAI
189
 
190
+ client = OpenAI()
191
+
192
  response = client.responses.create(
193
  input=[{"role": "user",
194
  "content": [{"type": "input_text", "text": (question)},
 
202
 
203
  # Built-in tools
204
 
 
205
  web_search_tool = SerperDevTool()
206
  web_rag_tool = WebsiteSearchTool()
207
+ python_coding_tool = CodeInterpreterTool()
208
 
209
  # Agents
210
 
211
+ web_search_agent = Agent(
212
+ role="Web Search Agent",
213
+ goal="Search the web to help answer question \"{question}\", then scrape the most relevant web page.",
214
+ backstory="As an expert web search assistant, you search the web to help answer the question.",
215
  allow_delegation=False,
216
  llm=AGENT_MODEL,
217
  max_iter=3,
218
+ tools=[web_search_tool, web_rag_tool],
219
  verbose=False
220
  )
221
+
222
  image_analysis_agent = Agent(
223
  role="Image Analysis Agent",
224
  goal="Analyze image to help answer question \"{question}\"",
 
229
  tools=[image_analysis_tool],
230
  verbose=False
231
  )
 
 
 
 
 
 
 
 
 
 
 
232
 
233
+ audio_analysis_agent = Agent(
234
+ role="Audio Analysis Agent",
235
+ goal="Analyze audio to help answer question \"{question}\"",
236
+ backstory="As an expert audio analysis assistant, you analyze the audio to help answer the question.",
237
  allow_delegation=False,
238
  llm=AGENT_MODEL,
239
  max_iter=3,
240
+ tools=[audio_analysis_tool],
241
  verbose=False
242
  )
243
 
244
+ youtube_audio_analysis_agent = Agent(
245
+ role="YouTube Audio Analysis Agent",
246
+ goal="Analyze YouTube audio to help answer question \"{question}\"",
247
+ backstory="As an expert YouTube audio analysis assistant, you analyze the audio to help answer the question.",
248
  allow_delegation=False,
249
  llm=AGENT_MODEL,
250
  max_iter=3,
251
+ tools=[video_analysis_tool],
252
  verbose=False
253
  )
254
+
255
  youtube_video_analysis_agent = Agent(
256
  role="YouTube Video Analysis Agent",
257
  goal="Analyze YouTube video to help answer question \"{question}\"",
 
259
  allow_delegation=False,
260
  llm=AGENT_MODEL,
261
  max_iter=3,
262
+ tools=[video_analysis_tool],
263
  verbose=False
264
  )
265
+
266
+ python_coding_agent = Agent(
267
+ role="Python Coding Agent",
268
+ goal="Write and/or execute Python code to help answer question \"{question}\"",
269
+ backstory="As an expert Python coding assistant, you write and/or execute Python code to help answer the question.",
270
+ allow_delegation=False,
271
+ llm=AGENT_MODEL,
272
+ max_iter=10,
273
+ tools=[python_coding_tool],
274
+ verbose=False
275
+ )
276
+
277
  manager_agent = Agent(
278
  role="Manager Agent",
279
  goal="Try to answer the following question. If needed, delegate to one or more of your coworkers for help. "
 
297
  # Crew
298
 
299
  crew = Crew(
300
+ agents=[web_search_agent,
301
  image_analysis_agent,
302
+ audio_analysis_agent,
303
+ youtube_audio_analysis_agent,
304
+ youtube_video_analysis_agent,
305
+ python_coding_agent],
306
  manager_agent=manager_agent,
307
  tasks=[manager_task],
308
  verbose=True