Skip to content

Commit

Permalink
fix: support disable rich console (#38)
Browse files Browse the repository at this point in the history
feat: support disable rich console

Signed-off-by: Keming <kemingyang@tensorchord.ai>
  • Loading branch information
kemingy authored Sep 4, 2023
1 parent 9e41a37 commit 9e25bc1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
3 changes: 1 addition & 2 deletions modelz/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
from urllib.parse import urljoin

import httpx
from rich.console import Console

from modelz.console import console
from modelz.env import EnvConfig
from modelz.serde import Serde, SerdeEnum, TextSerde

TIMEOUT = httpx.Timeout(5, read=300, write=300)
console = Console()
config = EnvConfig()
DEFAULT_RESP_SERDE = TextSerde()
DEFAULT_RETRY = 3
Expand Down
6 changes: 1 addition & 5 deletions modelz/cmd.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import sys
from typing import Dict, Optional

from rich.console import Console

from modelz.args import build_argument_parser, parse_arguments
from modelz.client import ModelzClient

console = Console()
from modelz.console import console


def inference(
Expand All @@ -29,7 +26,6 @@ def inference(
write_file: write response to the file
key: API key, will try to read from env ``MODELZ_API_KEY`` if not provided
serde: serilize/deserilize method, choose from [json|msgpack|raw]
output: output target, choose from [console|file]
"""
data = params
if read_stdin:
Expand Down
32 changes: 32 additions & 0 deletions modelz/console.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import re

from rich.console import Console

from modelz.env import EnvConfig

config = EnvConfig()


class ModelzConsole:
class Status:
def __init__(self, msg) -> None:
self.msg = msg

def __enter__(self):
print(self.msg)

def __exit__(self, exc_type, exc_value, traceback):
pass

def __init__(self) -> None:
self._remove_style = re.compile(r"\[.*?\]")
self.formatter = lambda msg: self._remove_style.sub("", msg)

def status(self, msg: str):
return self.Status(self.formatter(msg))

def print(self, msg: str):
print(self.formatter(msg))


console = ModelzConsole() if config.disable_rich else Console()
6 changes: 4 additions & 2 deletions modelz/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ class EnvConfig:
def __init__(self) -> None:
self.api_key: str
self.host: str = "https://{}.modelz.io/"
self.disable_rich: bool = True
self.update_from_env()

def update_from_env(self):
for key in ("api_key", "host"):
for key in ("api_key", "host", "disable_rich"):
val = os.environ.get(f"{PREFIX}{key.upper()}")
if val is not None:
setattr(self, key, val)
setattr(self, key, type(key)(val))
print(f"update {key} from env: {val}")

0 comments on commit 9e25bc1

Please sign in to comment.