Spaces:
Runtime error
Runtime error
mriusero commited on
Commit ·
6078833
1
Parent(s): 4489760
feat: tools complete (1st version)
Browse files- requirements.txt +5 -1
- src/inference.py +29 -3
- src/tools/__init__.py +7 -0
- src/tools/analyze_chess.py +52 -0
- src/tools/analyze_document.py +72 -0
- src/tools/analyze_excel.py +35 -0
- src/tools/classify_food.py +56 -0
- src/tools/execute_code.py +32 -0
- src/tools/reverse_text.py +19 -0
- src/tools/transcript_audio.py +30 -0
- src/tools/web_search.py +1 -1
- src/utils/__init__.py +1 -2
- src/workflow.py +3 -10
- tools.json +150 -0
requirements.txt
CHANGED
|
@@ -5,4 +5,8 @@ smolagents
|
|
| 5 |
wikipedia
|
| 6 |
openpyxl
|
| 7 |
pydub
|
| 8 |
-
mistralai
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
wikipedia
|
| 6 |
openpyxl
|
| 7 |
pydub
|
| 8 |
+
mistralai
|
| 9 |
+
opencv-python
|
| 10 |
+
chess
|
| 11 |
+
SpeechRecognition
|
| 12 |
+
pypdf2
|
src/inference.py
CHANGED
|
@@ -10,6 +10,13 @@ from src.tools import (
|
|
| 10 |
web_search,
|
| 11 |
visit_webpage,
|
| 12 |
load_file,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
)
|
| 14 |
|
| 15 |
load_dotenv()
|
|
@@ -24,13 +31,32 @@ class Agent:
|
|
| 24 |
"web_search": web_search,
|
| 25 |
"visit_webpage": visit_webpage,
|
| 26 |
"load_file": load_file,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
}
|
| 28 |
self.conversation_log = []
|
| 29 |
|
| 30 |
def get_tools(self):
|
| 31 |
"""Generate the tools.json file with the tools to be used by the agent."""
|
| 32 |
return generate_tools_json(
|
| 33 |
-
[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
def make_initial_request(self, input):
|
| 36 |
"""Make the initial request to the agent with the given input."""
|
|
@@ -115,7 +141,7 @@ class Agent:
|
|
| 115 |
file, ensure_ascii=False, indent=4
|
| 116 |
)
|
| 117 |
|
| 118 |
-
def run(self, input, task_id, truth, max_steps=
|
| 119 |
"""Run the agent with the given input and process the response."""
|
| 120 |
print("\n... Asking the agent ...\n")
|
| 121 |
response, messages = self.make_initial_request(input)
|
|
@@ -125,7 +151,7 @@ class Agent:
|
|
| 125 |
steps += 1
|
| 126 |
thought_result = self.thought(response)
|
| 127 |
|
| 128 |
-
final_answer_match = re.search(r'
|
| 129 |
if final_answer_match:
|
| 130 |
self.save_conversation(task_id, truth, final_answer_match.group(1).strip())
|
| 131 |
return final_answer_match.group(1).strip()
|
|
|
|
| 10 |
web_search,
|
| 11 |
visit_webpage,
|
| 12 |
load_file,
|
| 13 |
+
reverse_text,
|
| 14 |
+
analyze_chess,
|
| 15 |
+
analyze_document,
|
| 16 |
+
classify_foods,
|
| 17 |
+
transcribe_audio,
|
| 18 |
+
execute_code,
|
| 19 |
+
analyze_excel,
|
| 20 |
)
|
| 21 |
|
| 22 |
load_dotenv()
|
|
|
|
| 31 |
"web_search": web_search,
|
| 32 |
"visit_webpage": visit_webpage,
|
| 33 |
"load_file": load_file,
|
| 34 |
+
"reverse_text": reverse_text,
|
| 35 |
+
"analyze_chess": analyze_chess,
|
| 36 |
+
"analyze_document": analyze_document,
|
| 37 |
+
"classify_foods": classify_foods,
|
| 38 |
+
"transcribe_audio": transcribe_audio,
|
| 39 |
+
"execute_code": execute_code,
|
| 40 |
+
"analyze_excel": analyze_excel,
|
| 41 |
}
|
| 42 |
self.conversation_log = []
|
| 43 |
|
| 44 |
def get_tools(self):
|
| 45 |
"""Generate the tools.json file with the tools to be used by the agent."""
|
| 46 |
return generate_tools_json(
|
| 47 |
+
[
|
| 48 |
+
web_search,
|
| 49 |
+
visit_webpage,
|
| 50 |
+
load_file,
|
| 51 |
+
reverse_text,
|
| 52 |
+
analyze_chess,
|
| 53 |
+
analyze_document,
|
| 54 |
+
classify_foods,
|
| 55 |
+
transcribe_audio,
|
| 56 |
+
execute_code,
|
| 57 |
+
analyze_excel,
|
| 58 |
+
]
|
| 59 |
+
).get('tools')
|
| 60 |
|
| 61 |
def make_initial_request(self, input):
|
| 62 |
"""Make the initial request to the agent with the given input."""
|
|
|
|
| 141 |
file, ensure_ascii=False, indent=4
|
| 142 |
)
|
| 143 |
|
| 144 |
+
def run(self, input, task_id, truth, max_steps=100):
|
| 145 |
"""Run the agent with the given input and process the response."""
|
| 146 |
print("\n... Asking the agent ...\n")
|
| 147 |
response, messages = self.make_initial_request(input)
|
|
|
|
| 151 |
steps += 1
|
| 152 |
thought_result = self.thought(response)
|
| 153 |
|
| 154 |
+
final_answer_match = re.search(r'FINAL ANSWER:(.*)', thought_result, re.DOTALL)
|
| 155 |
if final_answer_match:
|
| 156 |
self.save_conversation(task_id, truth, final_answer_match.group(1).strip())
|
| 157 |
return final_answer_match.group(1).strip()
|
src/tools/__init__.py
CHANGED
|
@@ -1,3 +1,10 @@
|
|
| 1 |
from .web_search import web_search
|
| 2 |
from .visit_webpage import visit_webpage
|
| 3 |
from .load_file import load_file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from .web_search import web_search
|
| 2 |
from .visit_webpage import visit_webpage
|
| 3 |
from .load_file import load_file
|
| 4 |
+
from .reverse_text import reverse_text
|
| 5 |
+
from .analyze_chess import analyze_chess
|
| 6 |
+
from .analyze_document import analyze_document
|
| 7 |
+
from .classify_food import classify_foods
|
| 8 |
+
from .transcript_audio import transcribe_audio
|
| 9 |
+
from .execute_code import execute_code
|
| 10 |
+
from .analyze_excel import analyze_excel
|
src/tools/analyze_chess.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from src.utils.tooling import tool
|
| 2 |
+
|
| 3 |
+
@tool
|
| 4 |
+
def analyze_chess(image_path: str) -> str:
|
| 5 |
+
"""
|
| 6 |
+
Analyzes a chess position from an image and determines the best next move.
|
| 7 |
+
Args:
|
| 8 |
+
image_path (str): The path to the image file containing the chess position.
|
| 9 |
+
Returns:
|
| 10 |
+
str: The recommended move in algebraic notation.
|
| 11 |
+
"""
|
| 12 |
+
try:
|
| 13 |
+
import chess.engine
|
| 14 |
+
from PIL import Image
|
| 15 |
+
import cv2
|
| 16 |
+
import numpy as np
|
| 17 |
+
except ImportError as e:
|
| 18 |
+
raise ImportError(
|
| 19 |
+
"You must install packages `python-chess`, `Pillow`, and `opencv-python` to run this tool."
|
| 20 |
+
"For instance, run `pip install chess pillow opencv-python`."
|
| 21 |
+
) from e
|
| 22 |
+
|
| 23 |
+
def preprocess_image(image_path):
|
| 24 |
+
image = Image.open(image_path)
|
| 25 |
+
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 26 |
+
return image
|
| 27 |
+
|
| 28 |
+
def detect_board_position(image):
|
| 29 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # For example, use contour detection to find the chessboard
|
| 30 |
+
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
|
| 31 |
+
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 32 |
+
board_contour = max(contours, key=cv2.contourArea) # Assume the largest contour is the chessboard
|
| 33 |
+
return board_contour
|
| 34 |
+
|
| 35 |
+
def extract_fen_from_image(image):
|
| 36 |
+
fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" # Placeholder FEN string
|
| 37 |
+
return fen
|
| 38 |
+
|
| 39 |
+
def get_best_move(fen):
|
| 40 |
+
|
| 41 |
+
engine = chess.engine.SimpleEngine.popen_uci("/opt/homebrew/bin/stockfish") # Initialize the chess engine
|
| 42 |
+
board = chess.Board(fen) # Create a board from the FEN string
|
| 43 |
+
result = engine.play(board, chess.engine.Limit(time=2.0)) # Get the best move
|
| 44 |
+
engine.quit()
|
| 45 |
+
return result.move.uci()
|
| 46 |
+
|
| 47 |
+
image = preprocess_image(image_path) # Preprocess the image
|
| 48 |
+
board_contour = detect_board_position(image) # Detect the board position
|
| 49 |
+
fen = extract_fen_from_image(image) # Extract the FEN string from the image
|
| 50 |
+
best_move = get_best_move(fen) # Get the best move
|
| 51 |
+
|
| 52 |
+
return best_move
|
src/tools/analyze_document.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from src.utils.tooling import tool
|
| 2 |
+
import PyPDF2
|
| 3 |
+
import re
|
| 4 |
+
|
| 5 |
+
@tool
|
| 6 |
+
def analyze_document(file_path: str, keywords: list) -> str:
|
| 7 |
+
"""
|
| 8 |
+
Extracts specific information from a PDF or text document based on given keywords.
|
| 9 |
+
Args:
|
| 10 |
+
file_path (str): The path to the PDF or text document to analyze.
|
| 11 |
+
keywords (list): A list of keywords to search for in the document.
|
| 12 |
+
Returns:
|
| 13 |
+
str: The extracted information as text.
|
| 14 |
+
"""
|
| 15 |
+
def extract_text_from_pdf(file_path: str) -> str:
|
| 16 |
+
"""
|
| 17 |
+
Extracts text from a PDF file.
|
| 18 |
+
Args:
|
| 19 |
+
file_path (str): The path to the PDF file.
|
| 20 |
+
Returns:
|
| 21 |
+
str: The extracted text from the PDF.
|
| 22 |
+
"""
|
| 23 |
+
try:
|
| 24 |
+
with open(file_path, 'rb') as file:
|
| 25 |
+
reader = PyPDF2.PdfFileReader(file)
|
| 26 |
+
text = ''
|
| 27 |
+
for page_num in range(reader.numPages):
|
| 28 |
+
page = reader.getPage(page_num)
|
| 29 |
+
text += page.extract_text()
|
| 30 |
+
return text
|
| 31 |
+
except Exception as e:
|
| 32 |
+
raise Exception(f"Error reading PDF file: {e}")
|
| 33 |
+
|
| 34 |
+
def extract_text_from_txt(file_path: str) -> str:
|
| 35 |
+
"""
|
| 36 |
+
Extracts text from a text file.
|
| 37 |
+
Args:
|
| 38 |
+
file_path (str): The path to the text file.
|
| 39 |
+
Returns:
|
| 40 |
+
str: The extracted text from the text file.
|
| 41 |
+
"""
|
| 42 |
+
try:
|
| 43 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
| 44 |
+
return file.read()
|
| 45 |
+
except Exception as e:
|
| 46 |
+
raise Exception(f"Error reading text file: {e}")
|
| 47 |
+
|
| 48 |
+
def extract_information(text: str, keywords: list) -> str:
|
| 49 |
+
"""
|
| 50 |
+
Extracts information based on keywords from the text.
|
| 51 |
+
Args:
|
| 52 |
+
text (str): The text to analyze.
|
| 53 |
+
keywords (list): A list of keywords to search for in the text.
|
| 54 |
+
Returns:
|
| 55 |
+
str: The extracted information as text.
|
| 56 |
+
"""
|
| 57 |
+
extracted_info = []
|
| 58 |
+
for keyword in keywords:
|
| 59 |
+
pattern = re.compile(r'\b{}\b'.format(re.escape(keyword)), re.IGNORECASE)
|
| 60 |
+
matches = pattern.findall(text)
|
| 61 |
+
if matches:
|
| 62 |
+
extracted_info.append(f"Keyword '{keyword}': {', '.join(matches)}")
|
| 63 |
+
return "\n".join(extracted_info)
|
| 64 |
+
|
| 65 |
+
if file_path.lower().endswith('.pdf'):
|
| 66 |
+
text = extract_text_from_pdf(file_path)
|
| 67 |
+
elif file_path.lower().endswith('.txt'):
|
| 68 |
+
text = extract_text_from_txt(file_path)
|
| 69 |
+
else:
|
| 70 |
+
raise ValueError("Unsupported file format. Please provide a PDF or text file.")
|
| 71 |
+
|
| 72 |
+
return extract_information(text, keywords)
|
src/tools/analyze_excel.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from src.utils.tooling import tool
|
| 2 |
+
import pandas as pd
|
| 3 |
+
|
| 4 |
+
@tool
|
| 5 |
+
def analyze_excel(file_path: str, sheet_name: str = None, specific_columns: list = None) -> str:
|
| 6 |
+
"""
|
| 7 |
+
Analyzes data from an Excel file to extract specific information.
|
| 8 |
+
Args:
|
| 9 |
+
file_path (str): The path to the Excel file to analyze.
|
| 10 |
+
sheet_name (str, optional): The name of the sheet to read. If None, the first sheet is used.
|
| 11 |
+
specific_columns (list, optional): A list of column names to extract. If None, all columns are extracted.
|
| 12 |
+
Returns:
|
| 13 |
+
str: Extracted information in text or structured data format.
|
| 14 |
+
"""
|
| 15 |
+
try:
|
| 16 |
+
if sheet_name:
|
| 17 |
+
df = pd.read_excel(file_path, sheet_name=sheet_name)
|
| 18 |
+
else:
|
| 19 |
+
df = pd.read_excel(file_path)
|
| 20 |
+
|
| 21 |
+
if specific_columns:
|
| 22 |
+
df = df[specific_columns]
|
| 23 |
+
|
| 24 |
+
analysis_result = df.describe(include='all').to_string()
|
| 25 |
+
|
| 26 |
+
return f"## Excel Analysis Results\n\n{analysis_result}"
|
| 27 |
+
|
| 28 |
+
except FileNotFoundError:
|
| 29 |
+
return "File not found. Please check the file path."
|
| 30 |
+
except pd.errors.EmptyDataError:
|
| 31 |
+
return "The Excel file is empty."
|
| 32 |
+
except pd.errors.ParserError:
|
| 33 |
+
return "Error parsing the Excel file."
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return f"An error occurred: {str(e)}"
|
src/tools/classify_food.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from src.utils.tooling import tool
|
| 2 |
+
|
| 3 |
+
@tool
|
| 4 |
+
def classify_foods(food_list: list) -> dict:
|
| 5 |
+
"""
|
| 6 |
+
Classifies a list of foods into specific botanical categories.
|
| 7 |
+
Args:
|
| 8 |
+
food_list (list): A list of foods to classify.
|
| 9 |
+
Returns:
|
| 10 |
+
dict: A dictionary with categories as keys and lists of foods as values.
|
| 11 |
+
"""
|
| 12 |
+
categories = {
|
| 13 |
+
"fruits": [
|
| 14 |
+
"apple", "banana", "orange", "grape", "strawberry", "plum", "peach", "pear",
|
| 15 |
+
"cherry", "blueberry", "raspberry", "pineapple", "mango", "kiwi", "lemon",
|
| 16 |
+
"lime", "watermelon", "cantaloupe", "avocado", "tomato", "cucumber", "bell pepper",
|
| 17 |
+
"eggplant", "okra", "zucchini", "pumpkin", "olive"
|
| 18 |
+
],
|
| 19 |
+
"vegetables": [
|
| 20 |
+
"carrot", "broccoli", "spinach", "lettuce", "celery", "fresh basil", "sweet potato",
|
| 21 |
+
"potato", "onion", "garlic", "cabbage", "kale", "cauliflower", "asparagus", "radish",
|
| 22 |
+
"turnip", "beet", "artichoke", "brussels sprouts", "green beans", "peas", "mushroom"
|
| 23 |
+
],
|
| 24 |
+
"grains": [
|
| 25 |
+
"rice", "wheat", "oats", "barley", "quinoa", "corn", "rye", "millet", "sorghum",
|
| 26 |
+
"buckwheat", "flour"
|
| 27 |
+
],
|
| 28 |
+
"nuts": [
|
| 29 |
+
"almond", "walnut", "cashew", "peanut", "hazelnut", "pecan", "pistachio", "macadamia",
|
| 30 |
+
"brazil nut", "chestnut", "acorn"
|
| 31 |
+
],
|
| 32 |
+
"legumes": [
|
| 33 |
+
"lentil", "chickpea", "bean", "pea", "soybean", "black bean", "kidney bean", "pinto bean",
|
| 34 |
+
"navy bean", "lima bean", "green bean"
|
| 35 |
+
],
|
| 36 |
+
"other": [
|
| 37 |
+
"milk", "eggs", "coffee", "Oreos", "allspice", "sugar", "salt", "honey", "maple syrup",
|
| 38 |
+
"vinegar", "oil", "butter", "cheese", "yogurt", "cream", "meat", "fish", "poultry"
|
| 39 |
+
]
|
| 40 |
+
}
|
| 41 |
+
classified_foods = {category: [] for category in categories}
|
| 42 |
+
|
| 43 |
+
for food in food_list:
|
| 44 |
+
food_lower = food.lower()
|
| 45 |
+
classified = False
|
| 46 |
+
for category, examples in categories.items():
|
| 47 |
+
if food_lower in examples:
|
| 48 |
+
classified_foods[category].append(food)
|
| 49 |
+
classified = True
|
| 50 |
+
break
|
| 51 |
+
if not classified:
|
| 52 |
+
classified_foods.setdefault("unknown", []).append(food)
|
| 53 |
+
|
| 54 |
+
classified_foods["vegetables"].sort()
|
| 55 |
+
|
| 56 |
+
return classified_foods
|
src/tools/execute_code.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from src.utils.tooling import tool
|
| 2 |
+
import subprocess
|
| 3 |
+
import tempfile
|
| 4 |
+
|
| 5 |
+
@tool
|
| 6 |
+
def execute_code(file_path: str) -> str:
|
| 7 |
+
"""
|
| 8 |
+
Executes Python code from a file and returns the final result.
|
| 9 |
+
Args:
|
| 10 |
+
file_path (str): The path to the file containing the Python code to execute.
|
| 11 |
+
Returns:
|
| 12 |
+
str: The result of the code execution.
|
| 13 |
+
"""
|
| 14 |
+
try:
|
| 15 |
+
with open(file_path, 'r') as file:
|
| 16 |
+
code = file.read()
|
| 17 |
+
|
| 18 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".py") as temp_file:
|
| 19 |
+
temp_file.write(code.encode('utf-8'))
|
| 20 |
+
temp_file_path = temp_file.name
|
| 21 |
+
|
| 22 |
+
result = subprocess.run(['python', temp_file_path], capture_output=True, text=True)
|
| 23 |
+
|
| 24 |
+
if result.returncode != 0:
|
| 25 |
+
raise Exception(f"Error executing code: {result.stderr}")
|
| 26 |
+
|
| 27 |
+
return result.stdout
|
| 28 |
+
|
| 29 |
+
except FileNotFoundError:
|
| 30 |
+
raise FileNotFoundError(f"The file at {file_path} does not exist.")
|
| 31 |
+
except Exception as e:
|
| 32 |
+
raise Exception(f"An error occurred: {str(e)}")
|
src/tools/reverse_text.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from src.utils.tooling import tool
|
| 2 |
+
|
| 3 |
+
@tool
|
| 4 |
+
def reverse_text(input_text: str) -> str:
|
| 5 |
+
"""
|
| 6 |
+
Reverses an input string to make it readable.
|
| 7 |
+
Args:
|
| 8 |
+
input_text (str): The reversed text string to process.
|
| 9 |
+
"""
|
| 10 |
+
try:
|
| 11 |
+
corrected_text = input_text[::-1]
|
| 12 |
+
|
| 13 |
+
if not corrected_text:
|
| 14 |
+
raise ValueError("The input text is empty! Please provide a valid reversed text string.")
|
| 15 |
+
|
| 16 |
+
return corrected_text
|
| 17 |
+
|
| 18 |
+
except Exception as e:
|
| 19 |
+
raise Exception(f"An error occurred while processing the text: {e}")
|
src/tools/transcript_audio.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from src.utils.tooling import tool
|
| 2 |
+
|
| 3 |
+
@tool
|
| 4 |
+
def transcribe_audio(file_path: str, language: str = 'en') -> str:
|
| 5 |
+
"""
|
| 6 |
+
Transcribes the content of an audio file into text.
|
| 7 |
+
Args:
|
| 8 |
+
file_path (str): The path to the audio file to transcribe.
|
| 9 |
+
language (str, optional): The language of the audio content. Defaults to 'en' (English).
|
| 10 |
+
Returns:
|
| 11 |
+
str: The transcribed text from the audio file.
|
| 12 |
+
"""
|
| 13 |
+
try:
|
| 14 |
+
import speech_recognition as sr
|
| 15 |
+
except ImportError as e:
|
| 16 |
+
raise ImportError(
|
| 17 |
+
"You must install the package `SpeechRecognition` to run this tool. For instance, run `pip install SpeechRecognition`."
|
| 18 |
+
) from e
|
| 19 |
+
|
| 20 |
+
recognizer = sr.Recognizer()
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
with sr.AudioFile(file_path) as source:
|
| 24 |
+
audio_data = recognizer.record(source)
|
| 25 |
+
text = recognizer.recognize_google(audio_data, language=language)
|
| 26 |
+
return text
|
| 27 |
+
except sr.UnknownValueError:
|
| 28 |
+
raise Exception("Speech Recognition could not understand the audio.")
|
| 29 |
+
except sr.RequestError as e:
|
| 30 |
+
raise Exception(f"Could not request results from Speech Recognition service; {e}")
|
src/tools/web_search.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
from src.utils.tooling import tool
|
| 2 |
|
| 3 |
@tool
|
| 4 |
-
def web_search(query: str, max_results: int = 3, timeout: int =
|
| 5 |
"""
|
| 6 |
Performs a web search based on the query and returns the top search results.
|
| 7 |
Args:
|
|
|
|
| 1 |
from src.utils.tooling import tool
|
| 2 |
|
| 3 |
@tool
|
| 4 |
+
def web_search(query: str, max_results: int = 3, timeout: int = 1) -> str:
|
| 5 |
"""
|
| 6 |
Performs a web search based on the query and returns the top search results.
|
| 7 |
Args:
|
src/utils/__init__.py
CHANGED
|
@@ -1,2 +1 @@
|
|
| 1 |
-
from .api import fetch_questions, submit_answers, get_file
|
| 2 |
-
from .prompt import load_prompt
|
|
|
|
| 1 |
+
from .api import fetch_questions, submit_answers, get_file
|
|
|
src/workflow.py
CHANGED
|
@@ -16,7 +16,6 @@ from src.inference import Agent
|
|
| 16 |
|
| 17 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 18 |
console = Console()
|
| 19 |
-
agent = Agent()
|
| 20 |
space_id = os.getenv("SPACE_ID")
|
| 21 |
|
| 22 |
if profile:
|
|
@@ -38,6 +37,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 38 |
answers_payload = []
|
| 39 |
|
| 40 |
for item in questions_data:
|
|
|
|
| 41 |
task_id = item.get("task_id")
|
| 42 |
question_text = item.get("question")
|
| 43 |
file_name = item.get("file_name")
|
|
@@ -60,8 +60,8 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 60 |
item = json.loads(line)
|
| 61 |
if item.get('task_id') == task_id:
|
| 62 |
final_answer = item.get('Final answer')
|
| 63 |
-
console.print(Panel(f"The correct final answer is: [bold]{final_answer}[/bold]"))
|
| 64 |
|
|
|
|
| 65 |
submitted_answer = agent.run(
|
| 66 |
input=question_text + file_context,
|
| 67 |
task_id=task_id,
|
|
@@ -69,18 +69,11 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 69 |
)
|
| 70 |
|
| 71 |
console.print(Panel(f"[bold green]Submitted Answer[/bold green]\n{submitted_answer}", expand=False))
|
|
|
|
| 72 |
|
| 73 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 74 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 75 |
|
| 76 |
-
# Vérification des métadonnées
|
| 77 |
-
with open('./metadata.jsonl', 'r') as file:
|
| 78 |
-
for line in file:
|
| 79 |
-
item = json.loads(line)
|
| 80 |
-
if item.get('task_id') == task_id:
|
| 81 |
-
final_answer = item.get('Final answer')
|
| 82 |
-
console.print(Panel(f"The correct final answer is: [bold]{final_answer}[/bold]"))
|
| 83 |
-
|
| 84 |
except Exception as e:
|
| 85 |
console.print(f"Error: {e}", style="bold red")
|
| 86 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
|
|
|
| 16 |
|
| 17 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 18 |
console = Console()
|
|
|
|
| 19 |
space_id = os.getenv("SPACE_ID")
|
| 20 |
|
| 21 |
if profile:
|
|
|
|
| 37 |
answers_payload = []
|
| 38 |
|
| 39 |
for item in questions_data:
|
| 40 |
+
|
| 41 |
task_id = item.get("task_id")
|
| 42 |
question_text = item.get("question")
|
| 43 |
file_name = item.get("file_name")
|
|
|
|
| 60 |
item = json.loads(line)
|
| 61 |
if item.get('task_id') == task_id:
|
| 62 |
final_answer = item.get('Final answer')
|
|
|
|
| 63 |
|
| 64 |
+
agent = Agent()
|
| 65 |
submitted_answer = agent.run(
|
| 66 |
input=question_text + file_context,
|
| 67 |
task_id=task_id,
|
|
|
|
| 69 |
)
|
| 70 |
|
| 71 |
console.print(Panel(f"[bold green]Submitted Answer[/bold green]\n{submitted_answer}", expand=False))
|
| 72 |
+
console.print(Panel(f"The correct final answer is: [bold]{final_answer}[/bold]"))
|
| 73 |
|
| 74 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 75 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
except Exception as e:
|
| 78 |
console.print(f"Error: {e}", style="bold red")
|
| 79 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
tools.json
CHANGED
|
@@ -63,5 +63,155 @@
|
|
| 63 |
]
|
| 64 |
}
|
| 65 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
}
|
| 67 |
]
|
|
|
|
| 63 |
]
|
| 64 |
}
|
| 65 |
}
|
| 66 |
+
},
|
| 67 |
+
{
|
| 68 |
+
"type": "function",
|
| 69 |
+
"function": {
|
| 70 |
+
"name": "reverse_text",
|
| 71 |
+
"description": "Reverses an input string to make it readable.",
|
| 72 |
+
"parameters": {
|
| 73 |
+
"type": "object",
|
| 74 |
+
"properties": {
|
| 75 |
+
"input_text": {
|
| 76 |
+
"type": "string",
|
| 77 |
+
"description": "The reversed text string to process."
|
| 78 |
+
}
|
| 79 |
+
},
|
| 80 |
+
"required": [
|
| 81 |
+
"input_text"
|
| 82 |
+
]
|
| 83 |
+
}
|
| 84 |
+
}
|
| 85 |
+
},
|
| 86 |
+
{
|
| 87 |
+
"type": "function",
|
| 88 |
+
"function": {
|
| 89 |
+
"name": "analyze_chess",
|
| 90 |
+
"description": "Analyzes a chess position from an image and determines the best next move.",
|
| 91 |
+
"parameters": {
|
| 92 |
+
"type": "object",
|
| 93 |
+
"properties": {
|
| 94 |
+
"image_path": {
|
| 95 |
+
"type": "string",
|
| 96 |
+
"description": "The path to the image file containing the chess position."
|
| 97 |
+
}
|
| 98 |
+
},
|
| 99 |
+
"required": [
|
| 100 |
+
"image_path"
|
| 101 |
+
]
|
| 102 |
+
}
|
| 103 |
+
}
|
| 104 |
+
},
|
| 105 |
+
{
|
| 106 |
+
"type": "function",
|
| 107 |
+
"function": {
|
| 108 |
+
"name": "analyze_document",
|
| 109 |
+
"description": "Extracts specific information from a PDF or text document based on given keywords.",
|
| 110 |
+
"parameters": {
|
| 111 |
+
"type": "object",
|
| 112 |
+
"properties": {
|
| 113 |
+
"file_path": {
|
| 114 |
+
"type": "string",
|
| 115 |
+
"description": "The path to the PDF or text document to analyze."
|
| 116 |
+
},
|
| 117 |
+
"keywords": {
|
| 118 |
+
"type": "array",
|
| 119 |
+
"description": "A list of keywords to search for in the document."
|
| 120 |
+
}
|
| 121 |
+
},
|
| 122 |
+
"required": [
|
| 123 |
+
"file_path",
|
| 124 |
+
"keywords"
|
| 125 |
+
]
|
| 126 |
+
}
|
| 127 |
+
}
|
| 128 |
+
},
|
| 129 |
+
{
|
| 130 |
+
"type": "function",
|
| 131 |
+
"function": {
|
| 132 |
+
"name": "classify_foods",
|
| 133 |
+
"description": "Classifies a list of foods into specific botanical categories.",
|
| 134 |
+
"parameters": {
|
| 135 |
+
"type": "object",
|
| 136 |
+
"properties": {
|
| 137 |
+
"food_list": {
|
| 138 |
+
"type": "array",
|
| 139 |
+
"description": "A list of foods to classify."
|
| 140 |
+
}
|
| 141 |
+
},
|
| 142 |
+
"required": [
|
| 143 |
+
"food_list"
|
| 144 |
+
]
|
| 145 |
+
}
|
| 146 |
+
}
|
| 147 |
+
},
|
| 148 |
+
{
|
| 149 |
+
"type": "function",
|
| 150 |
+
"function": {
|
| 151 |
+
"name": "transcribe_audio",
|
| 152 |
+
"description": "Transcribes the content of an audio file into text.",
|
| 153 |
+
"parameters": {
|
| 154 |
+
"type": "object",
|
| 155 |
+
"properties": {
|
| 156 |
+
"file_path": {
|
| 157 |
+
"type": "string",
|
| 158 |
+
"description": "The path to the audio file to transcribe."
|
| 159 |
+
},
|
| 160 |
+
"language": {
|
| 161 |
+
"type": "string",
|
| 162 |
+
"description": "The language of the audio content. Defaults to 'en' (English)."
|
| 163 |
+
}
|
| 164 |
+
},
|
| 165 |
+
"required": [
|
| 166 |
+
"file_path"
|
| 167 |
+
]
|
| 168 |
+
}
|
| 169 |
+
}
|
| 170 |
+
},
|
| 171 |
+
{
|
| 172 |
+
"type": "function",
|
| 173 |
+
"function": {
|
| 174 |
+
"name": "execute_code",
|
| 175 |
+
"description": "Executes Python code from a file and returns the final result.",
|
| 176 |
+
"parameters": {
|
| 177 |
+
"type": "object",
|
| 178 |
+
"properties": {
|
| 179 |
+
"file_path": {
|
| 180 |
+
"type": "string",
|
| 181 |
+
"description": "The path to the file containing the Python code to execute."
|
| 182 |
+
}
|
| 183 |
+
},
|
| 184 |
+
"required": [
|
| 185 |
+
"file_path"
|
| 186 |
+
]
|
| 187 |
+
}
|
| 188 |
+
}
|
| 189 |
+
},
|
| 190 |
+
{
|
| 191 |
+
"type": "function",
|
| 192 |
+
"function": {
|
| 193 |
+
"name": "analyze_excel",
|
| 194 |
+
"description": "Analyzes data from an Excel file to extract specific information.",
|
| 195 |
+
"parameters": {
|
| 196 |
+
"type": "object",
|
| 197 |
+
"properties": {
|
| 198 |
+
"file_path": {
|
| 199 |
+
"type": "string",
|
| 200 |
+
"description": "The path to the Excel file to analyze."
|
| 201 |
+
},
|
| 202 |
+
"sheet_name": {
|
| 203 |
+
"type": "string",
|
| 204 |
+
"description": "The name of the sheet to read. If None, the first sheet is used."
|
| 205 |
+
},
|
| 206 |
+
"specific_columns": {
|
| 207 |
+
"type": "array",
|
| 208 |
+
"description": "A list of column names to extract. If None, all columns are extracted."
|
| 209 |
+
}
|
| 210 |
+
},
|
| 211 |
+
"required": [
|
| 212 |
+
"file_path"
|
| 213 |
+
]
|
| 214 |
+
}
|
| 215 |
+
}
|
| 216 |
}
|
| 217 |
]
|