Skip to content

Commit

Permalink
feat: use utc time for log (#471)
Browse files Browse the repository at this point in the history
use utc time for log

Signed-off-by: Keming <[email protected]>
  • Loading branch information
kemingy authored Dec 5, 2023
1 parent 4c40ca6 commit 8b5fc31
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mosec"
version = "0.8.1"
version = "0.8.2"
authors = ["Keming <[email protected]>", "Zichen <[email protected]>"]
edition = "2021"
license = "Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class StableDiffusion(MsgpackMixin, Worker):
return images
```

> **Note**
> [!NOTE]
>
> (a) In this example we return an image in the binary format, which JSON does not support (unless encoded with base64 that makes the payload larger). Hence, msgpack suits our need better. If we do not inherit `MsgpackMixin`, JSON will be used by default. In other words, the protocol of the service request/response can be either msgpack, JSON, or any other format (check our [mixins](https://mosecorg.github.io/mosec/reference/interface.html#module-mosec.mixin)).
>
Expand Down
11 changes: 4 additions & 7 deletions examples/embedding/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,13 @@

import os

import openai

openai.api_key = ""
openai.api_base = "http://127.0.0.1:8000/"
from openai import Client

DEFAULT_MODEL = "thenlper/gte-base"


emb = openai.Embedding.create(
client = Client(api_key="fake", base_url="http://127.0.0.1:8000/")
emb = client.embeddings.create(
model=os.environ.get("EMB_MODEL", DEFAULT_MODEL),
input="Hello world!",
)
print(emb["data"][0]["embedding"]) # type: ignore
print(emb.data[0].embedding) # type: ignore
5 changes: 4 additions & 1 deletion examples/embedding/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ def __init__(self):
self.model = self.model.to(self.device)
self.model.eval()

def get_embedding_with_token_count(self, sentences: Union[str, List[str]]):
def get_embedding_with_token_count(
self, sentences: Union[str, List[Union[str, List[int]]]]
):
# Mean Pooling - Take attention mask into account for correct averaging
def mean_pooling(model_output, attention_mask):
# First element of model_output contains all token embeddings
Expand All @@ -54,6 +56,7 @@ def mean_pooling(model_output, attention_mask):
)

# Tokenize sentences
# TODO: support `List[List[int]]` input
encoded_input = self.tokenizer(
sentences, padding=True, truncation=True, return_tensors="pt"
)
Expand Down
6 changes: 4 additions & 2 deletions mosec/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ class MosecFormat(logging.Formatter):
This class uses `datetime` instead of `localtime` to get accurate milliseconds.
"""

default_time_format = "%Y-%m-%dT%H:%M:%S.%f%z"
# `%z` will be empty string if the object is naive, so we use `Z` to make it
# compatible with rfc3339
default_time_format = "%Y-%m-%dT%H:%M:%S.%fZ "

def formatTime(self, record: logging.LogRecord, datefmt=None) -> str:
"""Convert to datetime with timezone."""
time = datetime.fromtimestamp(record.created).astimezone()
time = datetime.fromtimestamp(record.created).utcnow()
return datetime.strftime(time, datefmt if datefmt else self.default_time_format)


Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use axum::routing::{get, post};
use axum::Router;
use tokio::signal::unix::{signal, SignalKind};
use tracing::{debug, info};
use tracing_subscriber::fmt::time::OffsetTime;
use tracing_subscriber::fmt::time::UtcTime;
use tracing_subscriber::prelude::*;
use tracing_subscriber::{filter, Layer};
use utoipa::OpenApi;
Expand Down Expand Up @@ -115,7 +115,7 @@ fn main() {
let conf: Config = serde_json::from_str(&config_str).expect("parse config failure");

// this has to be defined before tokio multi-threads
let timer = OffsetTime::local_rfc_3339().expect("local time offset");
let timer = UtcTime::rfc_3339();
if conf.log_level == "debug" {
// use colorful log for debug
let output = tracing_subscriber::fmt::layer().compact().with_timer(timer);
Expand Down

0 comments on commit 8b5fc31

Please sign in to comment.