parent
4a9a0ba3ef
commit
54dfa692a8
@ -1,49 +0,0 @@
|
|||||||
import concurrent.futures
|
|
||||||
from typing import List, Tuple, Any, Dict, Union, Callable
|
|
||||||
|
|
||||||
|
|
||||||
def execute_concurrently(
|
|
||||||
callable_functions: List[
|
|
||||||
Tuple[Callable, Tuple[Any, ...], Dict[str, Any]]
|
|
||||||
],
|
|
||||||
max_workers: int = 5,
|
|
||||||
) -> List[Union[Any, Exception]]:
|
|
||||||
"""
|
|
||||||
Executes callable functions concurrently using multithreading.
|
|
||||||
|
|
||||||
Parameters:
|
|
||||||
- callable_functions: A list of tuples, each containing the callable function and its arguments.
|
|
||||||
For example: [(function1, (arg1, arg2), {'kwarg1': val1}), (function2, (), {})]
|
|
||||||
- max_workers: The maximum number of threads to use.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
- results: A list of results returned by the callable functions. If an error occurs in any function,
|
|
||||||
the exception object will be placed at the corresponding index in the list.
|
|
||||||
"""
|
|
||||||
results = [None] * len(callable_functions)
|
|
||||||
|
|
||||||
def worker(
|
|
||||||
fn: Callable,
|
|
||||||
args: Tuple[Any, ...],
|
|
||||||
kwargs: Dict[str, Any],
|
|
||||||
index: int,
|
|
||||||
) -> None:
|
|
||||||
try:
|
|
||||||
result = fn(*args, **kwargs)
|
|
||||||
results[index] = result
|
|
||||||
except Exception as e:
|
|
||||||
results[index] = e
|
|
||||||
|
|
||||||
with concurrent.futures.ThreadPoolExecutor(
|
|
||||||
max_workers=max_workers
|
|
||||||
) as executor:
|
|
||||||
futures = []
|
|
||||||
for i, (fn, args, kwargs) in enumerate(callable_functions):
|
|
||||||
futures.append(
|
|
||||||
executor.submit(worker, fn, args, kwargs, i)
|
|
||||||
)
|
|
||||||
|
|
||||||
# Wait for all threads to complete
|
|
||||||
concurrent.futures.wait(futures)
|
|
||||||
|
|
||||||
return results
|
|
@ -1,80 +0,0 @@
|
|||||||
import functools
|
|
||||||
import logging
|
|
||||||
import threading
|
|
||||||
import warnings
|
|
||||||
|
|
||||||
|
|
||||||
def retry_decorator(max_retries: int = 5):
|
|
||||||
"""
|
|
||||||
Decorator that retries a function a specified number of times if an exception occurs.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
max_retries (int): The maximum number of times to retry the function.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
function: The decorated function.
|
|
||||||
|
|
||||||
"""
|
|
||||||
|
|
||||||
def decorator(func):
|
|
||||||
@functools.wraps(func)
|
|
||||||
def wrapper(*args, **kwargs):
|
|
||||||
for _ in range(max_retries):
|
|
||||||
try:
|
|
||||||
return func(*args, **kwargs)
|
|
||||||
except Exception as error:
|
|
||||||
logging.error(
|
|
||||||
f" Error in {func.__name__}:"
|
|
||||||
f" {str(error)} Retrying ...."
|
|
||||||
)
|
|
||||||
return func(*args, **kwargs)
|
|
||||||
|
|
||||||
return wrapper
|
|
||||||
|
|
||||||
return decorator
|
|
||||||
|
|
||||||
|
|
||||||
def singleton_decorator(cls):
|
|
||||||
instances = {}
|
|
||||||
|
|
||||||
def wrapper(*args, **kwargs):
|
|
||||||
if cls not in instances:
|
|
||||||
instances[cls] = cls(*args, **kwargs)
|
|
||||||
return instances[cls]
|
|
||||||
|
|
||||||
return wrapper
|
|
||||||
|
|
||||||
|
|
||||||
def synchronized_decorator(func):
|
|
||||||
func.__lock__ = threading.Lock()
|
|
||||||
|
|
||||||
def wrapper(*args, **kwargs):
|
|
||||||
with func.__lock__:
|
|
||||||
return func(*args, **kwargs)
|
|
||||||
|
|
||||||
return wrapper
|
|
||||||
|
|
||||||
|
|
||||||
def deprecated_decorator(func):
|
|
||||||
@functools.wraps(func)
|
|
||||||
def wrapper(*args, **kwargs):
|
|
||||||
warnings.warn(
|
|
||||||
f"{func.__name__} is deprecated",
|
|
||||||
category=DeprecationWarning,
|
|
||||||
)
|
|
||||||
return func(*args, **kwargs)
|
|
||||||
|
|
||||||
return wrapper
|
|
||||||
|
|
||||||
|
|
||||||
def validate_inputs_decorator(validator):
|
|
||||||
def decorator(func):
|
|
||||||
@functools.wraps(func)
|
|
||||||
def wrapper(*args, **kwargs):
|
|
||||||
if not validator(*args, **kwargs):
|
|
||||||
raise ValueError("Invalid Inputs")
|
|
||||||
return func(*args, **kwargs)
|
|
||||||
|
|
||||||
return wrapper
|
|
||||||
|
|
||||||
return decorator
|
|
@ -1,23 +1,33 @@
|
|||||||
import os
|
import os
|
||||||
|
import uuid
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
|
||||||
|
|
||||||
WORKSPACE_DIR = os.getenv("WORKSPACE_DIR")
|
def initialize_logger(log_folder: str = "logs"):
|
||||||
|
|
||||||
logger.add(
|
WORKSPACE_DIR = os.getenv("WORKSPACE_DIR")
|
||||||
os.path.join(WORKSPACE_DIR, "swarms.log"),
|
if not os.path.exists(WORKSPACE_DIR):
|
||||||
level="INFO",
|
os.makedirs(WORKSPACE_DIR)
|
||||||
colorize=True,
|
|
||||||
backtrace=True,
|
|
||||||
diagnose=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
# Create a folder within the workspace_dir
|
||||||
|
log_folder_path = os.path.join(WORKSPACE_DIR, log_folder)
|
||||||
|
if not os.path.exists(log_folder_path):
|
||||||
|
os.makedirs(log_folder_path)
|
||||||
|
|
||||||
def loguru_logger(file_path: str = "swarms.log"):
|
# Generate a unique identifier for the log file
|
||||||
return logger.add(
|
uuid_for_log = str(uuid.uuid4())
|
||||||
os.path.join(WORKSPACE_DIR, file_path),
|
log_file_path = os.path.join(
|
||||||
|
log_folder_path, f"{log_folder}_{uuid_for_log}.log"
|
||||||
|
)
|
||||||
|
|
||||||
|
logger.add(
|
||||||
|
log_file_path,
|
||||||
level="INFO",
|
level="INFO",
|
||||||
colorize=True,
|
colorize=True,
|
||||||
backtrace=True,
|
backtrace=True,
|
||||||
diagnose=True,
|
diagnose=True,
|
||||||
|
enqueue=True,
|
||||||
|
retention="10 days",
|
||||||
|
compression="zip",
|
||||||
)
|
)
|
||||||
|
return logger
|
||||||
|
@ -1,108 +0,0 @@
|
|||||||
import datetime
|
|
||||||
import os
|
|
||||||
import platform
|
|
||||||
import traceback
|
|
||||||
|
|
||||||
from loguru import logger
|
|
||||||
|
|
||||||
# Remove default logger configuration
|
|
||||||
logger.remove()
|
|
||||||
|
|
||||||
# Define the path for the log folder
|
|
||||||
log_folder = os.path.join(os.getcwd(), "errors")
|
|
||||||
|
|
||||||
try:
|
|
||||||
# Create the log folder if it doesn't exist
|
|
||||||
os.makedirs(log_folder, exist_ok=True)
|
|
||||||
except PermissionError:
|
|
||||||
logger.error(f"Permission denied: '{log_folder}'")
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(
|
|
||||||
f"An error occurred while creating the log folder: {e}"
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
# If the folder was created successfully, add a new logger
|
|
||||||
logger.add(
|
|
||||||
os.path.join(log_folder, "error_{time}.log"),
|
|
||||||
level="ERROR",
|
|
||||||
format="<red>{time}</red> - <level>{level}</level> - <level>{message}</level>",
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def report_error(error: Exception):
|
|
||||||
"""
|
|
||||||
Logs an error message and provides instructions for reporting the issue on Swarms GitHub
|
|
||||||
or joining the community on Discord for real-time support.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
error (Exception): The exception that occurred.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
None
|
|
||||||
|
|
||||||
Raises:
|
|
||||||
None
|
|
||||||
"""
|
|
||||||
# Gather extensive context information
|
|
||||||
context_info = {
|
|
||||||
"exception_type": type(error).__name__,
|
|
||||||
"exception_message": str(error),
|
|
||||||
"stack_trace": traceback.format_exc(),
|
|
||||||
"timestamp": datetime.datetime.now().isoformat(),
|
|
||||||
"python_version": platform.python_version(),
|
|
||||||
"platform": platform.platform(),
|
|
||||||
"machine": platform.machine(),
|
|
||||||
"processor": platform.processor(),
|
|
||||||
"user": os.getenv("USER") or os.getenv("USERNAME"),
|
|
||||||
"current_working_directory": os.getcwd(),
|
|
||||||
}
|
|
||||||
|
|
||||||
error_message = (
|
|
||||||
f"\n"
|
|
||||||
f"------------------Error: {error}-----------------------\n"
|
|
||||||
f"#########################################\n"
|
|
||||||
f"# #\n"
|
|
||||||
f"# ERROR DETECTED! #\n"
|
|
||||||
f"# #\n"
|
|
||||||
f"# #\n"
|
|
||||||
f"# #\n"
|
|
||||||
f"# #\n"
|
|
||||||
f"#########################################\n"
|
|
||||||
f"\n"
|
|
||||||
f"Error Message: {context_info['exception_message']} ({context_info['exception_type']})\n"
|
|
||||||
f"\n"
|
|
||||||
f"Stack Trace:\n{context_info['stack_trace']}\n"
|
|
||||||
f"\n"
|
|
||||||
f"Context Information:\n"
|
|
||||||
f"-----------------------------------------\n"
|
|
||||||
f"Timestamp: {context_info['timestamp']}\n"
|
|
||||||
f"Python Version: {context_info['python_version']}\n"
|
|
||||||
f"Platform: {context_info['platform']}\n"
|
|
||||||
f"Machine: {context_info['machine']}\n"
|
|
||||||
f"Processor: {context_info['processor']}\n"
|
|
||||||
f"User: {context_info['user']}\n"
|
|
||||||
f"Current Working Directory: {context_info['current_working_directory']}\n"
|
|
||||||
f"-----------------------------------------\n"
|
|
||||||
f"\n"
|
|
||||||
"Support"
|
|
||||||
f"\n"
|
|
||||||
f"\n"
|
|
||||||
f"To report this issue, please visit the Swarms GitHub Issues page:\n"
|
|
||||||
f"https://github.com/kyegomez/swarms/issues\n"
|
|
||||||
f"\n"
|
|
||||||
f"You can also join the Swarms community on Discord for real-time support:\n"
|
|
||||||
f"https://discord.com/servers/agora-999382051935506503\n"
|
|
||||||
f"\n"
|
|
||||||
f"#########################################\n"
|
|
||||||
f"-----------------------------------------\n"
|
|
||||||
)
|
|
||||||
|
|
||||||
return logger.error(error_message)
|
|
||||||
|
|
||||||
|
|
||||||
# # Example usage:
|
|
||||||
# try:
|
|
||||||
# # Simulate an error
|
|
||||||
# raise ValueError("An example error")
|
|
||||||
# except Exception as e:
|
|
||||||
# report_error(e)
|
|
@ -1,34 +0,0 @@
|
|||||||
from typing import Union, Dict, List
|
|
||||||
from swarms.artifacts.main_artifact import Artifact
|
|
||||||
|
|
||||||
|
|
||||||
def handle_artifact_outputs(
|
|
||||||
file_path: str,
|
|
||||||
data: Union[str, Dict, List],
|
|
||||||
output_type: str = "txt",
|
|
||||||
folder_path: str = "./artifacts",
|
|
||||||
) -> str:
|
|
||||||
"""
|
|
||||||
Handle different types of data and create files in various formats.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
file_path: Path where the file should be saved
|
|
||||||
data: Input data that can be string, dict or list
|
|
||||||
output_type: Type of output file (txt, md, pdf, csv, json)
|
|
||||||
folder_path: Folder to save artifacts
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
str: Path to the created file
|
|
||||||
"""
|
|
||||||
# Create artifact with appropriate file type
|
|
||||||
artifact = Artifact(
|
|
||||||
folder_path=folder_path,
|
|
||||||
file_path=file_path,
|
|
||||||
file_type=output_type,
|
|
||||||
contents=data,
|
|
||||||
edit_count=0,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Save the file
|
|
||||||
# artifact.save()
|
|
||||||
artifact.save_as(output_format=output_type)
|
|
Loading…
Reference in new issue