Spaces:
Sleeping
Sleeping
Update multiple_tools.py
Browse files- multiple_tools.py +145 -0
multiple_tools.py
CHANGED
|
@@ -4,6 +4,17 @@ from dotenv import load_dotenv
|
|
| 4 |
from llama_index.core.tools import FunctionTool
|
| 5 |
from llama_index.tools.google import GoogleSearchToolSpec
|
| 6 |
from llama_index.tools.wikipedia import WikipediaToolSpec
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
load_dotenv()
|
| 9 |
google_key = os.getenv("GOOGLE_SECRET_KEY")
|
|
@@ -61,3 +72,137 @@ def text_inverter(text: str) -> str:
|
|
| 61 |
decoded = text[::-1]
|
| 62 |
print(decoded)
|
| 63 |
text_inverter_tool = FunctionTool.from_defaults(text_inverter)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from llama_index.core.tools import FunctionTool
|
| 5 |
from llama_index.tools.google import GoogleSearchToolSpec
|
| 6 |
from llama_index.tools.wikipedia import WikipediaToolSpec
|
| 7 |
+
#---------------------------------
|
| 8 |
+
import os
|
| 9 |
+
import tempfile
|
| 10 |
+
import whisper
|
| 11 |
+
import pandas as pd
|
| 12 |
+
import os
|
| 13 |
+
import chess
|
| 14 |
+
import chess.engine
|
| 15 |
+
import tempfile
|
| 16 |
+
from PIL import Image
|
| 17 |
+
#---------------------------------
|
| 18 |
|
| 19 |
load_dotenv()
|
| 20 |
google_key = os.getenv("GOOGLE_SECRET_KEY")
|
|
|
|
| 72 |
decoded = text[::-1]
|
| 73 |
print(decoded)
|
| 74 |
text_inverter_tool = FunctionTool.from_defaults(text_inverter)
|
| 75 |
+
|
| 76 |
+
#---------------------
|
| 77 |
+
MODEL_NAME = "base"
|
| 78 |
+
whisper_model = whisper.load_model(MODEL_NAME)
|
| 79 |
+
|
| 80 |
+
def transcribe_audio(audio_file_path: str) -> str:
|
| 81 |
+
"""
|
| 82 |
+
Transcribes speech from an audio file using OpenAI Whisper.
|
| 83 |
+
Args:
|
| 84 |
+
audio_file_path (str): Path to the local audio file (.mp3, .wav, etc.).
|
| 85 |
+
Returns:
|
| 86 |
+
str: Transcribed text or error message.
|
| 87 |
+
"""
|
| 88 |
+
try:
|
| 89 |
+
result = whisper_model.transcribe(audio_file_path)
|
| 90 |
+
return result["text"].strip()
|
| 91 |
+
except Exception as e:
|
| 92 |
+
return f"Transcription error: {str(e)}"
|
| 93 |
+
transcribe_audio_tool = FunctionTool.from_defaults(transcribe_audio)
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
def excel_food_sales_sum(file_path: str) -> str:
|
| 97 |
+
"""
|
| 98 |
+
Parses the Excel file and returns total sales of items classified as food.
|
| 99 |
+
Assumes 'Item Type' and 'Sales USD' columns.
|
| 100 |
+
"""
|
| 101 |
+
try:
|
| 102 |
+
df = pd.read_excel(file_path)
|
| 103 |
+
df.columns = [col.strip().lower() for col in df.columns]
|
| 104 |
+
food_rows = df[df['item type'].str.lower().str.contains("food")]
|
| 105 |
+
total = food_rows['sales usd'].sum()
|
| 106 |
+
return f"{total:.2f}"
|
| 107 |
+
except Exception as e:
|
| 108 |
+
return f"Excel parsing failed: {str(e)}"
|
| 109 |
+
excel_food_sales_sum_tool = FunctionTool.from_defaults(excel_food_sales_sum)
|
| 110 |
+
|
| 111 |
+
def parse_file_and_summarize(file_path: str, query: str = "") -> str:
|
| 112 |
+
"""
|
| 113 |
+
Reads a CSV or Excel file and optionally answers a simple question about it.
|
| 114 |
+
Args:
|
| 115 |
+
file_path (str): Path to the file (.csv or .xlsx).
|
| 116 |
+
query (str): Optional freeform instruction (e.g. "total food sales").
|
| 117 |
+
Returns:
|
| 118 |
+
str: Summary or result from the file.
|
| 119 |
+
"""
|
| 120 |
+
try:
|
| 121 |
+
_, ext = os.path.splitext(file_path.lower())
|
| 122 |
+
if ext == ".csv":
|
| 123 |
+
df = pd.read_csv(file_path)
|
| 124 |
+
elif ext in [".xls", ".xlsx"]:
|
| 125 |
+
df = pd.read_excel(file_path)
|
| 126 |
+
else:
|
| 127 |
+
return "Unsupported file format. Please upload CSV or Excel."
|
| 128 |
+
|
| 129 |
+
if df.empty:
|
| 130 |
+
return "The file is empty or unreadable."
|
| 131 |
+
|
| 132 |
+
if not query:
|
| 133 |
+
return f"Loaded file with {df.shape[0]} rows and {df.shape[1]} columns.\nColumns: {', '.join(df.columns)}"
|
| 134 |
+
|
| 135 |
+
# Very basic natural language query handling (expand with LLM if needed)
|
| 136 |
+
if "total" in query.lower() and "food" in query.lower():
|
| 137 |
+
food_rows = df[df['category'].str.lower() == "food"]
|
| 138 |
+
if "sales" in df.columns:
|
| 139 |
+
total = food_rows["sales"].sum()
|
| 140 |
+
return f"Total food sales: ${total:.2f}"
|
| 141 |
+
else:
|
| 142 |
+
return "Could not find 'sales' column in the file."
|
| 143 |
+
else:
|
| 144 |
+
return "Query not supported. Please specify a clearer question."
|
| 145 |
+
|
| 146 |
+
except Exception as e:
|
| 147 |
+
return f"File parsing error: {str(e)}"
|
| 148 |
+
parse_file_and_summarize_tool = FunctionTool.from_defaults(parse_file_and_summarize)
|
| 149 |
+
|
| 150 |
+
# Path to your Stockfish binary (update if needed)
|
| 151 |
+
STOCKFISH_PATH = "/usr/bin/stockfish"
|
| 152 |
+
def analyze_position_from_fen(fen: str, time_limit: float = 1.0) -> str:
|
| 153 |
+
"""
|
| 154 |
+
Uses Stockfish to analyze the best move from a given FEN string.
|
| 155 |
+
Args:
|
| 156 |
+
fen (str): Forsyth–Edwards Notation of the board.
|
| 157 |
+
time_limit (float): Time to let Stockfish think.
|
| 158 |
+
Returns:
|
| 159 |
+
str: Best move in algebraic notation.
|
| 160 |
+
"""
|
| 161 |
+
try:
|
| 162 |
+
board = chess.Board(fen)
|
| 163 |
+
engine = chess.engine.SimpleEngine.popen_uci(STOCKFISH_PATH)
|
| 164 |
+
result = engine.play(board, chess.engine.Limit(time=time_limit))
|
| 165 |
+
engine.quit()
|
| 166 |
+
return board.san(result.move)
|
| 167 |
+
except Exception as e:
|
| 168 |
+
return f"Stockfish error: {e}"
|
| 169 |
+
|
| 170 |
+
def solve_chess_image(image_path: str) -> str:
|
| 171 |
+
"""
|
| 172 |
+
Stub function for image-to-FEN. Replace with actual OCR/vision logic.
|
| 173 |
+
|
| 174 |
+
Args:
|
| 175 |
+
image_path (str): Path to chessboard image.
|
| 176 |
+
Returns:
|
| 177 |
+
str: Best move or error.
|
| 178 |
+
"""
|
| 179 |
+
# Placeholder FEN for development (e.g., black to move, guaranteed mate)
|
| 180 |
+
sample_fen = "6k1/5ppp/8/8/8/8/5PPP/6K1 b - - 0 1"
|
| 181 |
+
|
| 182 |
+
try:
|
| 183 |
+
print(f"Simulating FEN extraction from image: {image_path}")
|
| 184 |
+
# Replace the above with actual OCR image-to-FEN logic
|
| 185 |
+
best_move = analyze_position_from_fen(sample_fen)
|
| 186 |
+
return f"Detected FEN: {sample_fen}\nBest move for Black: {best_move}"
|
| 187 |
+
except Exception as e:
|
| 188 |
+
return f"Image analysis error: {e}"
|
| 189 |
+
|
| 190 |
+
solve_chess_image_tool = FunctionTool.from_defaults(solve_chess_image)
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
def vegetable_classifier(question: str) -> str:
|
| 194 |
+
"""
|
| 195 |
+
Classifies common grocery items from a Wikipedia-based classification.
|
| 196 |
+
Returns a comma-separated list of vegetables excluding all botanical fruits.
|
| 197 |
+
"""
|
| 198 |
+
known_vegetables = {
|
| 199 |
+
"broccoli", "celery", "lettuce", "zucchini", "green beans",
|
| 200 |
+
"sweet potatoes", "corn", "acorns", "peanuts", "rice", "flour"
|
| 201 |
+
}
|
| 202 |
+
# Accept question but only extract known food items
|
| 203 |
+
input_items = [item.strip().lower() for item in question.split(',')]
|
| 204 |
+
found = sorted([item for item in input_items if item in known_vegetables])
|
| 205 |
+
return ", ".join(found)
|
| 206 |
+
vegetable_classifier_tool = FunctionTool.from_defaults(vegetable_classifier)
|
| 207 |
+
|
| 208 |
+
|