Spaces:
Sleeping
Sleeping
from smolagents import PythonInterpreterTool, tool | |
import requests | |
import wikipedia | |
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 code 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 WikipediaSearchTool(query: str) -> str: | |
""" | |
Searches Wikipedia and returns page content for a given query. | |
Args: | |
query (str): The search term or page title to look up on Wikipedia | |
Returns: | |
str: The Wikipedia page content, or an error message if not found | |
""" | |
try: | |
page = wikipedia.page(query) | |
return f"Title: {page.title}\nURL: {page.url}\nContent: {page.content[:2000]}..." | |
except wikipedia.exceptions.DisambiguationError as e: | |
try: | |
page = wikipedia.page(e.options[0]) | |
return f"Title: {page.title}\nURL: {page.url}\nContent: {page.content[:2000]}..." | |
except: | |
return f"Multiple pages found: {e.options[:5]}" | |
except wikipedia.exceptions.PageError: | |
try: | |
search_results = wikipedia.search(query, results=3) | |
if search_results: | |
page = wikipedia.page(search_results[0]) | |
return f"Title: {page.title}\nURL: {page.url}\nContent: {page.content[:2000]}..." | |
else: | |
return f"No Wikipedia pages found for: {query}" | |
except: | |
return f"No Wikipedia pages found for: {query}" | |
except Exception as e: | |
return f"Wikipedia search failed: {e}" | |
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] |