Spaces:
Sleeping
Sleeping
File size: 3,665 Bytes
6afe35e 711208d b8da51c 3839dec 711208d b8da51c 711208d b8da51c 6afe35e 711208d 6afe35e 711208d 6afe35e | 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 | from smolagents import PythonInterpreterTool, tool
import requests
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.core.tools.tool_spec.load_and_search import (
LoadAndSearchToolSpec,
)
from llama_index.tools.wikipedia import WikipediaToolSpec
#from recognizer import predict_fen
from llama_index.readers.youtube_transcript import YoutubeTranscriptReader
from llama_index.readers.file import (
DocxReader,
HWPReader,
PDFReader,
EpubReader,
FlatReader,
HTMLTagReader,
ImageCaptionReader,
ImageReader,
ImageVisionLLMReader,
IPYNBReader,
MarkdownReader,
MboxReader,
PptxReader,
PandasCSVReader,
VideoAudioReader,
UnstructuredReader,
PyMuPDFReader,
ImageTabularChartReader,
XMLReader,
PagedCSVReader,
CSVReader,
RTFReader,
)
def ImageReaderTool(text:str) -> str:
"""Image reader takes image files and parse it for llm to use"""
parser = ImageReader()
file_extractor = {
".jpg": parser,
".jpeg": parser,
".png": parser,
} # Add other image formats as needed
documents = SimpleDirectoryReader(
"./data", file_extractor=file_extractor
).load_data()
return documents
def CSVReaderTool(path: str) -> str:
"""CSV reader takes csv file and parse it for llm to use"""
parser = CSVReader()
file_extractor = {".csv": parser} # Add other CSV formats as needed
documents = SimpleDirectoryReader(
"./data", file_extractor=file_extractor
).load_data()
return documents
def YoutubeTranscript(link: str) -> str:
"""
Get video transcript from youtube link
Args:
link (str): youtube link
Returns:
str: transcript of video to llm
"""
loader = YoutubeTranscriptReader()
documents = loader.load_data(
ytlinks=[link]
)
return documents
wiki_spec = WikipediaToolSpec()
# Get the search wikipedia tool
wiki_tool = wiki_spec.to_tool_list()[1]
""" def GetChessPosition(file_path:str) -> str:
fen = predict_fen("../images/chess_image.png")
return(fen) """
def ReverseTextTool(text: str) -> str:
"""
Reverses a text string character by character.
Args:
text (str): The text to reverse
Returns:
str: The reversed text
"""
return text[::-1]
def RunPythonFileTool(file_path: str) -> str:
"""
Executes a Python script loaded from the specified path using the PythonInterpreterTool.
Args:
file_path (str): The full path to the python (.py) file containing the Python code.
Returns:
str: The output produced by the code execution, or an error message if it fails.
"""
try:
with open(file_path, "r") as f:
code = f.read()
interpreter = PythonInterpreterTool()
result = interpreter.run({"code": code})
return result.get("output", "No output returned.")
except Exception as e:
return f"Execution failed: {e}"
def download_server(url: str, save_path: str) -> str:
"""
Downloads a file from a URL and saves it to the given path.
Args:
url (str): The URL from which to download the file.
save_path (str): The local file path where the downloaded file will be saved.
Returns:
str: A message indicating the result of the download operation.
"""
try:
response = requests.get(url, timeout=30)
response.raise_for_status()
with open(save_path, "wb") as f:
f.write(response.content)
return f"File downloaded to {save_path}"
except Exception as e:
return f"Failed to download: {e}" |