Spaces:
Sleeping
Sleeping
File size: 8,334 Bytes
b70a23a 212ef46 5f3ee04 899e90e 5f3ee04 899e90e 5f3ee04 b70a23a 212ef46 4aeb22f 899e90e 4aeb22f 899e90e 4aeb22f 899e90e 212ef46 b70a23a 899e90e 5ae2b93 b70a23a 5f3ee04 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
import os
import json
from dotenv import load_dotenv
from llama_index.core.tools import FunctionTool
from llama_index.tools.google import GoogleSearchToolSpec
from llama_index.tools.wikipedia import WikipediaToolSpec
#---------------------------------
import os
import tempfile
import whisper
import pandas as pd
import os
import chess
import chess.engine
import tempfile
import wikipedia
from PIL import Image
import wikipedia
#---------------------------------
load_dotenv()
google_key = os.getenv("GOOGLE_SECRET_KEY")
my_search_engine = os.getenv("Google_WebSearch_Engine")
g_search = GoogleSearchToolSpec(key=google_key, engine=my_search_engine, num=3)
#Wikipedia Search Tool
wikipedia_tool = WikipediaToolSpec()
wikipedia_search_tool = FunctionTool.from_defaults(wikipedia_tool.search_data)
# wikipedia.set_lang("en")
# def wiki_search(query: str) -> str:
# """
# Safe Wikipedia summary tool with disambiguation and fallback protection.
# """
# try:
# return wikipedia.summary(query, sentences=3)
# except wikipedia.DisambiguationError as e:
# # Try the first disambiguation option if available
# if e.options:
# try:
# return wikipedia.summary(e.options[0], sentences=3)
# except Exception as inner:
# return f"Disambiguation fallback failed: {inner}"
# return "Disambiguation error: No options available."
# except wikipedia.PageError:
# search_results = wikipedia.search(query)
# if not search_results:
# return "No relevant Wikipedia page found."
# try:
# return wikipedia.summary(search_results[0], sentences=3)
# except Exception as inner:
# return f"Wikipedia fallback summary error: {inner}"
# except Exception as e:
# return f"Wikipedia general error: {e}"
# wikipedia_search_tool = FunctionTool.from_defaults(wiki_search)
def google_web_search(query : str) -> str:
"""
Searches the web and returns the most accurate response for a user query.
Args:
query (str): The query string to search for.
Returns:
str: The snippet of the first search result along with its source link.
"""
result = g_search.google_search(query)
output = result[0]
if "huggingface.co" in output["link"]:
output = result[1]
print(output)
return f"Result: {output['snippet']} Source: {output['link']}"
google_web_search_tool = FunctionTool.from_defaults(google_web_search)
def round_to_two_decimals(value):
"""
Round a number to two decimal places.
Args:
value (float): The value to be round to 2 decimal places.
Raises:
ValueError: If the 'value' is not an integer or a float.
"""
return round(float(value), 2)
round_to_two_decimals_tool = FunctionTool.from_defaults(round_to_two_decimals)
def text_inverter(text: str) -> str:
"""
Handles sentence writen backward:
- Reverses it and returns the reverse version
- Ignore if text is not written backwords
Args:
text (str): The text writen backwards to be reversed
"""
decoded = text[::-1]
print(decoded)
text_inverter_tool = FunctionTool.from_defaults(text_inverter)
#---------------------
MODEL_NAME = "base"
whisper_model = whisper.load_model(MODEL_NAME)
def transcribe_audio(audio_file_path: str) -> str:
"""
Transcribes speech from an audio file using OpenAI Whisper.
Args:
audio_file_path (str): Path to the local audio file (.mp3, .wav, etc.).
Returns:
str: Transcribed text or error message.
"""
try:
result = whisper_model.transcribe(audio_file_path)
return result["text"].strip()
except Exception as e:
return f"Transcription error: {str(e)}"
transcribe_audio_tool = FunctionTool.from_defaults(transcribe_audio)
def excel_food_sales_sum(file_path: str) -> str:
"""
Parses the Excel file and returns total sales of items classified as food.
Assumes 'Item Type' and 'Sales USD' columns.
"""
try:
df = pd.read_excel(file_path)
df.columns = [col.strip().lower() for col in df.columns]
food_rows = df[df['item type'].str.lower().str.contains("food")]
total = food_rows['sales usd'].sum()
return f"{total:.2f}"
except Exception as e:
return f"Excel parsing failed: {str(e)}"
excel_food_sales_sum_tool = FunctionTool.from_defaults(excel_food_sales_sum)
def parse_file_and_summarize(file_path: str, query: str = "") -> str:
"""
Reads a CSV or Excel file and optionally answers a simple question about it.
Args:
file_path (str): Path to the file (.csv or .xlsx).
query (str): Optional freeform instruction (e.g. "total food sales").
Returns:
str: Summary or result from the file.
"""
try:
_, ext = os.path.splitext(file_path.lower())
if ext == ".csv":
df = pd.read_csv(file_path)
elif ext in [".xls", ".xlsx"]:
df = pd.read_excel(file_path)
else:
return "Unsupported file format. Please upload CSV or Excel."
if df.empty:
return "The file is empty or unreadable."
if not query:
return f"Loaded file with {df.shape[0]} rows and {df.shape[1]} columns.\nColumns: {', '.join(df.columns)}"
# Very basic natural language query handling (expand with LLM if needed)
if "total" in query.lower() and "food" in query.lower():
food_rows = df[df['category'].str.lower() == "food"]
if "sales" in df.columns:
total = food_rows["sales"].sum()
return f"Total food sales: ${total:.2f}"
else:
return "Could not find 'sales' column in the file."
else:
return "Query not supported. Please specify a clearer question."
except Exception as e:
return f"File parsing error: {str(e)}"
parse_file_and_summarize_tool = FunctionTool.from_defaults(parse_file_and_summarize)
# Path to your Stockfish binary (update if needed)
STOCKFISH_PATH = "/usr/bin/stockfish"
def analyze_position_from_fen(fen: str, time_limit: float = 1.0) -> str:
"""
Uses Stockfish to analyze the best move from a given FEN string.
Args:
fen (str): Forsyth–Edwards Notation of the board.
time_limit (float): Time to let Stockfish think.
Returns:
str: Best move in algebraic notation.
"""
try:
board = chess.Board(fen)
engine = chess.engine.SimpleEngine.popen_uci(STOCKFISH_PATH)
result = engine.play(board, chess.engine.Limit(time=time_limit))
engine.quit()
return board.san(result.move)
except Exception as e:
return f"Stockfish error: {e}"
def solve_chess_image(image_path: str) -> str:
"""
Stub function for image-to-FEN. Replace with actual OCR/vision logic.
Args:
image_path (str): Path to chessboard image.
Returns:
str: Best move or error.
"""
# Placeholder FEN for development (e.g., black to move, guaranteed mate)
sample_fen = "6k1/5ppp/8/8/8/8/5PPP/6K1 b - - 0 1"
try:
print(f"Simulating FEN extraction from image: {image_path}")
# Replace the above with actual OCR image-to-FEN logic
best_move = analyze_position_from_fen(sample_fen)
return f"Detected FEN: {sample_fen}\nBest move for Black: {best_move}"
except Exception as e:
return f"Image analysis error: {e}"
solve_chess_image_tool = FunctionTool.from_defaults(solve_chess_image)
def vegetable_classifier(question: str) -> str:
"""
Classifies common grocery items from a Wikipedia-based classification.
Returns a comma-separated list of vegetables excluding all botanical fruits.
"""
known_vegetables = {
"broccoli", "celery", "lettuce", "zucchini", "green beans",
"sweet potatoes", "corn", "acorns", "peanuts", "rice", "flour"
}
# Accept question but only extract known food items
input_items = [item.strip().lower() for item in question.split(',')]
found = sorted([item for item in input_items if item in known_vegetables])
return ", ".join(found)
vegetable_classifier_tool = FunctionTool.from_defaults(vegetable_classifier)
|