Spaces:
Paused
Paused
Update crew.py
Browse files
crew.py
CHANGED
|
@@ -7,7 +7,6 @@ import os
|
|
| 7 |
import pandas as pd
|
| 8 |
from crewai import Agent, Crew, Process, Task
|
| 9 |
from crewai.tools import tool
|
| 10 |
-
from crewai_tools import CodeInterpreterTool
|
| 11 |
from google import genai
|
| 12 |
from google.genai import types
|
| 13 |
from openinference.instrumentation.crewai import CrewAIInstrumentor
|
|
@@ -180,7 +179,34 @@ def run_crew(question, file_path):
|
|
| 180 |
except Exception as e:
|
| 181 |
raise RuntimeError(f"Processing failed: {str(e)}")
|
| 182 |
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
@tool("Code Execution Tool")
|
| 186 |
def code_execution_tool(question: str, file_path: str) -> str:
|
|
@@ -330,18 +356,15 @@ def run_crew(question, file_path):
|
|
| 330 |
|
| 331 |
# Process
|
| 332 |
|
| 333 |
-
|
| 334 |
-
|
| 335 |
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
|
| 342 |
-
if file_path:
|
| 343 |
-
question = f"{question} File path: {file_path}."
|
| 344 |
-
|
| 345 |
print("###")
|
| 346 |
print(question)
|
| 347 |
print("###")
|
|
|
|
| 7 |
import pandas as pd
|
| 8 |
from crewai import Agent, Crew, Process, Task
|
| 9 |
from crewai.tools import tool
|
|
|
|
| 10 |
from google import genai
|
| 11 |
from google.genai import types
|
| 12 |
from openinference.instrumentation.crewai import CrewAIInstrumentor
|
|
|
|
| 179 |
except Exception as e:
|
| 180 |
raise RuntimeError(f"Processing failed: {str(e)}")
|
| 181 |
|
| 182 |
+
@tool("Code Generation Tool")
|
| 183 |
+
def code_generation_tool(question: str) -> str:
|
| 184 |
+
"""Given a question, generate code to answer the question.
|
| 185 |
+
|
| 186 |
+
Args:
|
| 187 |
+
question (str): Question to answer
|
| 188 |
+
|
| 189 |
+
Returns:
|
| 190 |
+
str: Answer to the question
|
| 191 |
+
|
| 192 |
+
Raises:
|
| 193 |
+
RuntimeError: If processing fails"""
|
| 194 |
+
try:
|
| 195 |
+
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
| 196 |
+
|
| 197 |
+
response = client.models.generate_content(
|
| 198 |
+
model=CODE_GENERATION_MODEL,
|
| 199 |
+
contents=[question],
|
| 200 |
+
config=types.GenerateContentConfig(
|
| 201 |
+
tools=[types.Tool(code_execution=types.ToolCodeExecution)]
|
| 202 |
+
),
|
| 203 |
+
)
|
| 204 |
+
|
| 205 |
+
for part in response.candidates[0].content.parts:
|
| 206 |
+
if part.code_execution_result is not None:
|
| 207 |
+
return part.code_execution_result.output
|
| 208 |
+
except Exception as e:
|
| 209 |
+
raise RuntimeError(f"Processing failed: {str(e)}")
|
| 210 |
|
| 211 |
@tool("Code Execution Tool")
|
| 212 |
def code_execution_tool(question: str, file_path: str) -> str:
|
|
|
|
| 356 |
|
| 357 |
# Process
|
| 358 |
|
| 359 |
+
if file_path:
|
| 360 |
+
ext = os.path.splitext(file_path)[1].lower()
|
| 361 |
|
| 362 |
+
if ext in (".csv", ".xls", ".xlsx", ".json", ".jsonl"):
|
| 363 |
+
df = read_file(file_path)
|
| 364 |
+
question = f"{question} File {file_path} data: {df.to_string()}"
|
| 365 |
+
else:
|
| 366 |
+
question = f"{question} File path: {file_path}."
|
| 367 |
|
|
|
|
|
|
|
|
|
|
| 368 |
print("###")
|
| 369 |
print(question)
|
| 370 |
print("###")
|