first cimmit

This commit is contained in:
zcr
2026-01-26 11:40:11 +08:00
parent 71400cff38
commit dc2c1ddb03
11 changed files with 27 additions and 27 deletions

View File

@@ -17,7 +17,10 @@ class Settings(BaseSettings):
extra='ignore' # 忽略环境变量中多余的键
)
# 启动端口
SERVE_PROD: int = Field(default=8000, description='')
SERVE_PORT: int = Field(default=10090, description='')
FLUX2_KLEIN_MODEL_PATH: str = Field(default="/models", description='')
# 调试配饰
LOCAL: int = Field(default=0, description="是否在本地运行1表示本地运行0表示生产环境运行")

50
app/logging_env.py Normal file
View File

@@ -0,0 +1,50 @@
LOGS_PATH = './logs/'
LOGGER_CONFIG_DICT = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'simple': {'format': '%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s %(message)s'}
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'level': 'INFO',
'formatter': 'simple',
'stream': 'ext://sys.stdout',
},
'info_file_handler': {
'class': 'logging.handlers.RotatingFileHandler',
'level': 'INFO',
'formatter': 'simple',
'filename': f'{LOGS_PATH}info.log',
'maxBytes': 10485760,
'backupCount': 50,
'encoding': 'utf8',
},
'error_file_handler': {
'class': 'logging.handlers.RotatingFileHandler',
'level': 'ERROR',
'formatter': 'simple',
'filename': f'{LOGS_PATH}error.log',
'maxBytes': 10485760,
'backupCount': 20,
'encoding': 'utf8',
},
'debug_file_handler': {
'class': 'logging.handlers.RotatingFileHandler',
'level': 'DEBUG',
'formatter': 'simple',
'filename': f'{LOGS_PATH}debug.log',
'maxBytes': 10485760,
'backupCount': 50,
'encoding': 'utf8',
},
},
'loggers': {
'my_module': {'level': 'INFO', 'handlers': ['console'], 'propagate': 'no'}
},
'root': {
'level': 'INFO',
'handlers': ['error_file_handler', 'info_file_handler', 'debug_file_handler', 'console'],
},
}

24
app/main.py Normal file
View File

@@ -0,0 +1,24 @@
import logging
import os
import litserve as ls
from app.logging_env import LOGGER_CONFIG_DICT
from app.server.generate_image.flux2_klein.server import Flux2KleinServer
logger = logging.getLogger(__name__)
# 判断目录是否存在
logs = 'logs'
if not os.path.exists(logs):
# 不存在则创建目录parents=True 允许创建多级目录exist_ok=True 避免目录已存在时报错)
os.makedirs(logs, exist_ok=True)
logger.info(f"目录 {logs} 创建成功")
else:
logger.info(f"目录 {logs} 已存在")
logging.config.dictConfig(LOGGER_CONFIG_DICT)
if __name__ == '__main__':
to_product = Flux2KleinServer(api_path='/api/v1/to_product')
server = ls.LitServer([to_product])
server.run(port=8888)

View File

@@ -9,5 +9,5 @@ request_data = {
"infer_step":4,
"tasks_id":"123456-123"
}
response = requests.post("http://127.0.0.1:8012//api/v1/to_product", json=request_data)
response = requests.post("http://127.0.0.1:10090//api/v1/to_product", json=request_data)
print(f"Status: {response.status_code}\nResponse:\n {response.text}")

View File

@@ -1,4 +1,6 @@
import io
import os
import torch
import litserve as ls
from diffusers import Flux2KleinPipeline
@@ -15,7 +17,8 @@ class Flux2KleinServer(ls.LitAPI):
# Load the model
dtype = torch.bfloat16
self.device = device
self.model = Flux2KleinPipeline.from_pretrained("black-forest-labs/FLUX.2-klein-4B", torch_dtype=dtype, is_distilled=False)
model_path = os.path.join(settings.FLUX2_KLEIN_MODEL_PATH, "FLUX.2-klein-4B")
self.model = Flux2KleinPipeline.from_pretrained(model_path, torch_dtype=dtype, is_distilled=False)
self.model.to(device) # save some VRAM by offloading the model to CPU
def decode_request(self, request):