Skip to content

Commit

Permalink
FEAT: [BEAUTIFY in GPT4Vision][Disable logging in __init__ of swarms]
Browse files Browse the repository at this point in the history
  • Loading branch information
Kye committed Nov 25, 2023
1 parent 51c82cf commit f895497
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 20 deletions.
16 changes: 2 additions & 14 deletions swarms/__init__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
import logging
import os
import warnings
from swarms.utils.disable_logging import disable_logging

warnings.filterwarnings("ignore", category=UserWarning)

# disable tensorflow warnings
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"

try:
log = logging.getLogger("pytorch")
log.propagate = False
log.setLevel(logging.ERROR)
except Exception as error:
print(f"Pytorch logging not disabled: {error}")
disable_logging()

from swarms.agents import * # noqa: E402, F403
from swarms.swarms import * # noqa: E402, F403
Expand Down
8 changes: 7 additions & 1 deletion swarms/models/base_multimodal_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def __init__(
max_workers: Optional[int] = 10,
top_p: Optional[int] = 1,
top_k: Optional[int] = 50,
beautify: Optional[bool] = False,
device: Optional[str] = "cuda",
max_new_tokens: Optional[int] = 500,
retries: Optional[int] = 3,
Expand All @@ -30,6 +31,7 @@ def __init__(
self.max_workers = max_workers
self.top_p = top_p
self.top_k = top_k
self.beautify = beautify
self.device = device
self.max_new_tokens = max_new_tokens
self.retries = retries
Expand Down Expand Up @@ -206,4 +208,8 @@ def get_unique_chat_history_length(self):
def get_chat_history_tokens(self):
"""Get the chat history tokens"""
return self._num_tokens()


def print_beautiful(self, content: str, color: str = "cyan"):
"""Print Beautifully with termcolor"""
content = colored(content, color)
print(content)
29 changes: 24 additions & 5 deletions swarms/models/gpt4_vision_api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import asyncio
import base64
import concurrent.futures
Expand Down Expand Up @@ -54,16 +55,27 @@ def __init__(
self,
openai_api_key: str = openai_api_key,
model_name: str = "gpt-4-vision-preview",
logging_enabled: bool = False,
max_workers: int = 10,
max_tokens: str = 300,
openai_proxy: str = "https://api.openai.com/v1/chat/completions",
beautify: bool = False
):
super().__init__()
self.openai_api_key = openai_api_key
self.logging_enabled = logging_enabled
self.model_name = model_name
self.max_workers = max_workers
self.max_tokens = max_tokens
self.openai_proxy = openai_proxy
self.beautify = beautify

if self.logging_enabled:
logging.basicConfig(level=logging.DEBUG)
else:
# Disable debug logs for requests and urllib3
logging.getLogger("requests").setLevel(logging.WARNING)
logging.getLogger("urllib3").setLevel(logging.WARNING)

def encode_image(self, img: str):
"""Encode image to base64."""
Expand All @@ -83,7 +95,7 @@ def run(self, task: str, img: str):
"Authorization": f"Bearer {openai_api_key}",
}
payload = {
"model": self.model_name,
"model": "gpt-4-vision-preview",
"messages": [
{
"role": "user",
Expand All @@ -103,14 +115,18 @@ def run(self, task: str, img: str):
"max_tokens": self.max_tokens,
}
response = requests.post(
"https://api.openai.com/v1/chat/completions",
self.openai_proxy,
headers=headers,
json=payload,
)

out = response.json()
content = out["choices"][0]["message"]["content"]
print(content)

if self.beautify:
content = colored(content, "cyan")
else:
print(content)
except Exception as error:
print(f"Error with the request: {error}")
raise error
Expand Down Expand Up @@ -151,11 +167,14 @@ def __call__(self, task: str, img: str):

out = response.json()
content = out["choices"][0]["message"]["content"]
print(content)

if self.beautify:
content = colored(content, "cyan")
else:
print(content)
except Exception as error:
print(f"Error with the request: {error}")
raise error
# Function to handle vision tasks

def run_many(
self,
Expand Down
25 changes: 25 additions & 0 deletions swarms/utils/disable_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

import logging
import os
import warnings

def disable_logging():
warnings.filterwarnings("ignore", category=UserWarning)

# disable tensorflow warnings
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"


# Set the logging level for the entire module
logging.basicConfig(level=logging.WARNING)

try:
log = logging.getLogger("pytorch")
log.propagate = False
log.setLevel(logging.ERROR)
except Exception as error:
print(f"Pytorch logging not disabled: {error}")

for logger_name in ['tensorflow', 'h5py', 'numexpr', 'git', 'wandb.docker.auth']:
logger = logging.getLogger(logger_name)
logger.setLevel(logging.WARNING) # Supress DEBUG and info logs

0 comments on commit f895497

Please sign in to comment.