111
This commit is contained in:
343
app/core/config_20.1.1.33.py
Normal file
343
app/core/config_20.1.1.33.py
Normal file
@@ -0,0 +1,343 @@
|
|||||||
|
import logging
|
||||||
|
import socket
|
||||||
|
from typing import Dict, Any
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
from pydantic import Field
|
||||||
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||||
|
from v2.nacos import ClientConfigBuilder, GRPCConfig, NacosConfigService, ConfigParam, NacosNamingService, RegisterInstanceParam, DeregisterInstanceParam
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
# ====================== Nacos 配置 ======================
|
||||||
|
NACOS_SERVER_ADDRESSES = "18.167.251.121:28848"
|
||||||
|
NACOS_NAMESPACE = "zcr"
|
||||||
|
NACOS_USERNAME = "nacos"
|
||||||
|
NACOS_PASSWORD = "Aidlab123123!"
|
||||||
|
NACOS_GROUP = "LOCAL"
|
||||||
|
NACOS_DATA_ID = "aida.python"
|
||||||
|
SERVICE_NAME = "fastapi-service" # ←←← 必须修改!建议格式:项目名-环境,例如 ai-image-service-dev
|
||||||
|
|
||||||
|
|
||||||
|
class Settings(BaseSettings):
|
||||||
|
"""
|
||||||
|
应用配置类。Pydantic Settings 会自动从环境变量和 .env 文件中加载这些值。
|
||||||
|
"""
|
||||||
|
model_config = SettingsConfigDict(
|
||||||
|
env_file='.env',
|
||||||
|
env_file_encoding='utf-8',
|
||||||
|
# extra='ignore' # 忽略环境变量中多余的键
|
||||||
|
)
|
||||||
|
# --- 服务端口配置信息 ---
|
||||||
|
PORT: int = Field(default=8001, description="")
|
||||||
|
# --- 服务环境 配置信息 ---
|
||||||
|
SERVE_ENV: str = Field(default='', description="")
|
||||||
|
# --- 开发状态 配置信息 ---
|
||||||
|
DEBUG: bool = Field(default=False, description="")
|
||||||
|
# --- 千问api 配置信息 ---
|
||||||
|
QWEN_API_KEY: str = Field(default="", description="")
|
||||||
|
|
||||||
|
# --- ComfyUI 配置信息 ---
|
||||||
|
COMFYUI_SERVER_ADDRESS: str = Field(default='', description="")
|
||||||
|
|
||||||
|
# --- minio 配置信息 ---
|
||||||
|
MINIO_URL: str = Field(default='', description="")
|
||||||
|
MINIO_ACCESS: str = Field(default='', description="")
|
||||||
|
MINIO_SECRET: str = Field(default='', description="")
|
||||||
|
MINIO_SECURE: bool = Field(default=True, description="")
|
||||||
|
|
||||||
|
# --- redis 配置信息 ---
|
||||||
|
REDIS_HOST: str = Field(default='', description="")
|
||||||
|
REDIS_PORT: str = Field(default='', description="")
|
||||||
|
REDIS_DB: int = Field(default=0, description="")
|
||||||
|
|
||||||
|
# --- mysql 配置信息 ---
|
||||||
|
MYSQL_HOST: str = Field(default='', description="")
|
||||||
|
MYSQL_PORT: int = Field(default=3306, description="")
|
||||||
|
MYSQL_USER: str = Field(default='', description="")
|
||||||
|
MYSQL_PASSWORD: str = Field(default='', description="")
|
||||||
|
MYSQL_DB: str = Field(default='', description="")
|
||||||
|
MYSQL_CHARSET: str = Field(default='utf8mb4', description="")
|
||||||
|
|
||||||
|
# --- rabbit-mq 配置信息 ---
|
||||||
|
MQ_HOST: str = Field(default='', description="")
|
||||||
|
MQ_PORT: str = Field(default='', description="")
|
||||||
|
MQ_USERNAME: str = Field(default='', description="")
|
||||||
|
MQ_PASSWORD: str = Field(default='', description="")
|
||||||
|
MQ_VIRTUAL_HOST: str = Field(default='/', description="")
|
||||||
|
MQ_ENV: str = Field(default='', description="")
|
||||||
|
|
||||||
|
# --- milvus 配置信息 ---
|
||||||
|
MILVUS_URL: str = Field(default='', description="")
|
||||||
|
MILVUS_TOKEN: str = Field(default='', description="")
|
||||||
|
MILVUS_ALIAS: str = Field(default='', description="")
|
||||||
|
|
||||||
|
# --- ollama 配置信息 ---
|
||||||
|
CHROMADB_PATH: str = Field(default='', description="")
|
||||||
|
|
||||||
|
# --- ollama 配置信息 ---
|
||||||
|
OLLAMA_URL: str = Field(default='', description="")
|
||||||
|
|
||||||
|
# --- Design Callback Java 接口 ---
|
||||||
|
JAVA_STREAM_API_URL: str = Field(default='', description="")
|
||||||
|
|
||||||
|
# --- flux2 klein model url ---
|
||||||
|
FLUX2_GEN_IMG_MODEL_URL: str = Field(default='', description="")
|
||||||
|
|
||||||
|
# --- 服务器IP ---
|
||||||
|
A6000_SERVICE_HOST: str = Field(default='', description="")
|
||||||
|
B_4_X_4090_SERVICE_HOST: str = Field(default='', description="")
|
||||||
|
|
||||||
|
# --- 其他配置信息 以下均为Docker容器内配置---
|
||||||
|
LOGS_PATH: str = Field(default="/logs/", description="")
|
||||||
|
CATEGORY_PATH: str = Field(default="/app/service/attribute/config/descriptor/category/category_dis.csv", description="")
|
||||||
|
SEG_CACHE_PATH: str = Field(default="/seg_cache/", description="")
|
||||||
|
RECOMMEND_PATH_PREFIX: str = Field(default="/app/service/recommend/", description="")
|
||||||
|
SERVE_PORT: int = Field(default=2010, description="")
|
||||||
|
|
||||||
|
|
||||||
|
settings = Settings()
|
||||||
|
|
||||||
|
# ====================== Nacos 配置管理 ======================
|
||||||
|
|
||||||
|
client_config = (ClientConfigBuilder()
|
||||||
|
.server_address(NACOS_SERVER_ADDRESSES)
|
||||||
|
.username(NACOS_USERNAME)
|
||||||
|
.password(NACOS_PASSWORD)
|
||||||
|
.namespace_id(NACOS_NAMESPACE)
|
||||||
|
.log_level('INFO')
|
||||||
|
.grpc_config(GRPCConfig(grpc_timeout=5000))
|
||||||
|
.build())
|
||||||
|
|
||||||
|
# ====================== Nacos 配置管理 ======================
|
||||||
|
nacos_config_data: Dict[str, Any] = {}
|
||||||
|
nacos_config_client = None
|
||||||
|
async def load_nacos_config() -> None:
|
||||||
|
"""初始化 Nacos 配置并监听变化"""
|
||||||
|
global nacos_config_data, settings
|
||||||
|
|
||||||
|
try:
|
||||||
|
client = await NacosConfigService.create_config_service(client_config)
|
||||||
|
|
||||||
|
# 1. 第一次获取配置
|
||||||
|
content = await client.get_config(ConfigParam(
|
||||||
|
data_id=NACOS_DATA_ID,
|
||||||
|
group=NACOS_GROUP
|
||||||
|
))
|
||||||
|
|
||||||
|
if content:
|
||||||
|
loaded = yaml.safe_load(content) or {}
|
||||||
|
nacos_config_data = loaded
|
||||||
|
# 用 Nacos 配置覆盖 settings
|
||||||
|
for key, value in loaded.items():
|
||||||
|
if hasattr(settings, key):
|
||||||
|
setattr(settings, key, value)
|
||||||
|
logger.info(f"✅ Nacos 配置加载成功: {NACOS_DATA_ID} | 覆盖字段数量: {len(loaded)}")
|
||||||
|
else:
|
||||||
|
logger.warning("Nacos 返回配置为空,使用 .env + 默认值")
|
||||||
|
|
||||||
|
# 2. 注册动态监听器(配置变更自动刷新)
|
||||||
|
async def listener(tenant: str, data_id: str, group: str, content: str):
|
||||||
|
global nacos_config_data, settings
|
||||||
|
try:
|
||||||
|
new_config = yaml.safe_load(content) if content else {}
|
||||||
|
nacos_config_data = new_config
|
||||||
|
# 实时覆盖 settings
|
||||||
|
for key, value in new_config.items():
|
||||||
|
if hasattr(settings, key):
|
||||||
|
old_val = getattr(settings, key)
|
||||||
|
setattr(settings, key, value)
|
||||||
|
if old_val != value:
|
||||||
|
logger.info(f"🔄 配置更新 → {key}: {old_val} → {value}")
|
||||||
|
logger.info(f"【Nacos 动态更新】{NACOS_DATA_ID}")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Nacos 配置解析失败: {e}")
|
||||||
|
|
||||||
|
await client.add_listener(NACOS_DATA_ID, NACOS_GROUP, listener)
|
||||||
|
logger.info("✅ Nacos 配置监听器已注册(支持热更新)")
|
||||||
|
|
||||||
|
await register_service_to_nacos()
|
||||||
|
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"❌ Nacos 初始化失败: {e},将仅使用 .env 配置")
|
||||||
|
|
||||||
|
|
||||||
|
async def register_service_to_nacos():
|
||||||
|
"""启动时把服务注册到 Nacos"""
|
||||||
|
global nacos_config_client
|
||||||
|
|
||||||
|
nacos_config_client = await NacosConfigService.create_config_service(client_config)
|
||||||
|
|
||||||
|
if not nacos_config_client: # 如果配置客户端都没连上,就不注册
|
||||||
|
logger.warning("Nacos 配置客户端未初始化,跳过服务注册")
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
nacos_naming_client = await NacosNamingService.create_naming_service(client_config)
|
||||||
|
|
||||||
|
# 获取服务 IP(生产环境建议通过环境变量传入,避免 Docker/K8s 内获取错误)
|
||||||
|
host_ip = socket.gethostbyname(socket.gethostname())
|
||||||
|
if not host_ip or host_ip.startswith('127.'):
|
||||||
|
host_ip = "127.0.0.1" # 本地测试用
|
||||||
|
|
||||||
|
param = RegisterInstanceParam(
|
||||||
|
service_name="aida.python",
|
||||||
|
group_name=NACOS_GROUP,
|
||||||
|
ip=host_ip,
|
||||||
|
port=settings.PORT, # 使用你 settings 中的 PORT
|
||||||
|
cluster_name="DEFAULT",
|
||||||
|
weight=1.0,
|
||||||
|
metadata={
|
||||||
|
"version": "1.0.0",
|
||||||
|
"env": settings.SERVE_ENV,
|
||||||
|
"framework": "fastapi",
|
||||||
|
"debug": str(settings.DEBUG),
|
||||||
|
},
|
||||||
|
enabled=True,
|
||||||
|
healthy=True,
|
||||||
|
ephemeral=True, # 临时实例,推荐生产使用
|
||||||
|
)
|
||||||
|
|
||||||
|
await nacos_naming_client.register_instance(request=param)
|
||||||
|
logger.info(f"✅ 服务已成功注册到 Nacos! → {SERVICE_NAME} | {host_ip}:{settings.PORT} | env={settings.SERVE_ENV}")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"❌ 服务注册到 Nacos 失败: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
async def deregister_service_from_nacos():
|
||||||
|
"""服务关闭时优雅注销(防止 Nacos 长时间显示不健康实例)"""
|
||||||
|
try:
|
||||||
|
nacos_naming_client = await NacosNamingService.create_naming_service(client_config)
|
||||||
|
|
||||||
|
host_ip = socket.gethostbyname(socket.gethostname()) or "127.0.0.1"
|
||||||
|
param = DeregisterInstanceParam(
|
||||||
|
service_name=SERVICE_NAME,
|
||||||
|
group_name=NACOS_GROUP,
|
||||||
|
ip=host_ip,
|
||||||
|
port=settings.PORT,
|
||||||
|
cluster_name='c1',
|
||||||
|
ephemeral=True,
|
||||||
|
)
|
||||||
|
await nacos_naming_client.deregister_instance(request=param)
|
||||||
|
logger.info(f"✅ 服务已从 Nacos 注销 → {SERVICE_NAME}")
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"服务注销时出现异常(通常可忽略): {e}")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# 提供给 FastAPI 的依赖
|
||||||
|
def get_settings() -> Settings:
|
||||||
|
return settings
|
||||||
|
|
||||||
|
|
||||||
|
"""Design 服务"""
|
||||||
|
# 推荐服装类别映射
|
||||||
|
TABLE_CATEGORIES = {
|
||||||
|
"female_dress": "female/dress",
|
||||||
|
"female_outwear": "female/outwear",
|
||||||
|
"female_trousers": "female/trousers",
|
||||||
|
"female_skirt": "female/skirt",
|
||||||
|
"female_blouse": "female/blouse",
|
||||||
|
"male_tops": "male/tops",
|
||||||
|
"male_bottoms": "male/bottoms",
|
||||||
|
"male_outwear": "male/outwear"
|
||||||
|
}
|
||||||
|
# Design前后排优先级
|
||||||
|
PRIORITY_DICT = {
|
||||||
|
'earring_front': 99,
|
||||||
|
'bag_front': 98,
|
||||||
|
'hairstyle_front': 97,
|
||||||
|
'outwear_front': 20,
|
||||||
|
'tops_front': 19,
|
||||||
|
'dress_front': 18,
|
||||||
|
'blouse_front': 17,
|
||||||
|
'skirt_front': 16,
|
||||||
|
'trousers_front': 15,
|
||||||
|
'bottoms_front': 14,
|
||||||
|
'shoes_right': 1,
|
||||||
|
'shoes_left': 1,
|
||||||
|
'body': 0,
|
||||||
|
'bottoms_back': -14,
|
||||||
|
'trousers_back': -15,
|
||||||
|
'skirt_back': -16,
|
||||||
|
'blouse_back': -17,
|
||||||
|
'dress_back': -18,
|
||||||
|
'tops_back': -19,
|
||||||
|
'outwear_back': -20,
|
||||||
|
'hairstyle_back': -97,
|
||||||
|
'bag_back': -98,
|
||||||
|
'earring_back': -99,
|
||||||
|
}
|
||||||
|
# Design 关键点字段
|
||||||
|
KEYPOINT_RESULT_TABLE_FIELD_SET = ('neckline_left', 'neckline_right', 'shoulder_left', 'shoulder_right', 'armpit_left', 'armpit_right', 'cuff_left_in', 'cuff_left_out', 'cuff_right_in', 'cuff_right_out', 'waistband_left', 'waistband_right')
|
||||||
|
# milvus配置信息
|
||||||
|
MILVUS_TABLE_KEYPOINT = "keypoint_cache_2"
|
||||||
|
|
||||||
|
# ollama 地址
|
||||||
|
OLLAMA_URL = f"http://{settings.A6000_SERVICE_HOST}:11434/api/embeddings"
|
||||||
|
|
||||||
|
"""Triton Server Config"""
|
||||||
|
# Design
|
||||||
|
DESIGN_MODEL_URL = f'{settings.A6000_SERVICE_HOST}:10000'
|
||||||
|
DESIGN_MODEL_NAME = 'seg_knet'
|
||||||
|
# Seg Product
|
||||||
|
SEG_PRODUCT_MODEL_URL = f'{settings.B_4_X_4090_SERVICE_HOST}:30000'
|
||||||
|
# Generate Image
|
||||||
|
GI_MODEL_URL = f'{settings.A6000_SERVICE_HOST}:10061'
|
||||||
|
GI_MODEL_NAME = 'flux'
|
||||||
|
# Generate Single Logo
|
||||||
|
GSL_MODEL_URL = f'{settings.B_4_X_4090_SERVICE_HOST}:10041'
|
||||||
|
GSL_MODEL_NAME = 'stable_diffusion_xl_transparent'
|
||||||
|
# Generate Product (整套和单品)
|
||||||
|
GPI_MODEL_URL = f'{settings.B_4_X_4090_SERVICE_HOST}:10051'
|
||||||
|
GPI_MODEL_NAME_OVERALL = 'diffusion_ensemble_all'
|
||||||
|
GPI_MODEL_NAME_SINGLE = 'stable_diffusion_1_5_cnet'
|
||||||
|
|
||||||
|
# 以下停用中...*************
|
||||||
|
# 多视角生成
|
||||||
|
GMV_MODEL_URL = f'{settings.B_4_X_4090_SERVICE_HOST}:10081'
|
||||||
|
GMV_MODEL_NAME = 'multi_view'
|
||||||
|
# 超分
|
||||||
|
SR_MODEL_NAME = "super_resolution"
|
||||||
|
SR_TRITON_URL = f"{settings.A6000_SERVICE_HOST}:10031"
|
||||||
|
# 打光
|
||||||
|
GRI_MODEL_URL = f'{settings.A6000_SERVICE_HOST}:10051'
|
||||||
|
GRI_MODEL_NAME_OVERALL = 'diffusion_relight_ensemble'
|
||||||
|
GRI_MODEL_NAME_SINGLE = 'stable_diffusion_1_5_relight'
|
||||||
|
# agent 图片生成
|
||||||
|
FAST_GI_MODEL_URL = f'{settings.B_4_X_4090_SERVICE_HOST}:10011'
|
||||||
|
FAST_GI_MODEL_NAME = 'stable_diffusion_xl'
|
||||||
|
# 图转视频 triton版
|
||||||
|
PT_MODEL_URL = f'{settings.B_4_X_4090_SERVICE_HOST}:10061'
|
||||||
|
|
||||||
|
# *************
|
||||||
|
|
||||||
|
"""MQ 队列信息"""
|
||||||
|
# 生成图片 moodboard printboard sketchboard
|
||||||
|
GI_RABBITMQ_QUEUES = f"GenerateImage-{settings.SERVE_ENV}"
|
||||||
|
# 生成slogan
|
||||||
|
SLOGAN_RABBITMQ_QUEUES = f"Slogan-{settings.SERVE_ENV}"
|
||||||
|
# 转产品图
|
||||||
|
GPI_RABBITMQ_QUEUES = f"ToProductImage-{settings.SERVE_ENV}"
|
||||||
|
# 产品图转视频
|
||||||
|
PS_RABBITMQ_QUEUES = f"PoseTransform-{settings.SERVE_ENV}"
|
||||||
|
|
||||||
|
# 以下停用中...*************
|
||||||
|
# 产品图打光
|
||||||
|
GRI_RABBITMQ_QUEUES = f"Relight-{settings.SERVE_ENV}"
|
||||||
|
# 超分
|
||||||
|
SR_RABBITMQ_QUEUES = f"SuperResolution-{settings.SERVE_ENV}"
|
||||||
|
# 生成多视图
|
||||||
|
GMV_RABBITMQ_QUEUES = f"GenerateMultiView-{settings.SERVE_ENV}"
|
||||||
|
# 批量转产品图
|
||||||
|
BATCH_GPI_RABBITMQ_QUEUES = f"BatchToProductImage-{settings.SERVE_ENV}"
|
||||||
|
# 批量打光
|
||||||
|
BATCH_GRI_RABBITMQ_QUEUES = f"BatchRelight-{settings.SERVE_ENV}"
|
||||||
|
# 批量图片转视频
|
||||||
|
BATCH_PS_RABBITMQ_QUEUES = f"BatchPoseTransform-{settings.SERVE_ENV}"
|
||||||
|
# 批量design
|
||||||
|
BATCH_DESIGN_RABBITMQ_QUEUES = f"DesignBatch-{settings.SERVE_ENV}"
|
||||||
|
# *************
|
||||||
108
app/core/nacos_config.py
Normal file
108
app/core/nacos_config.py
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
import logging
|
||||||
|
import yaml
|
||||||
|
import nacos
|
||||||
|
|
||||||
|
from app.core.config import settings
|
||||||
|
|
||||||
|
logger = logging.getLogger("nacos")
|
||||||
|
|
||||||
|
# client config
|
||||||
|
NACOS_SERVER_ADDRESSES = "18.167.251.121:28848"
|
||||||
|
NACOS_NAMESPACE = "zcr"
|
||||||
|
NACOS_USERNAME = "nacos"
|
||||||
|
NACOS_PASSWORD = "Aidlab123123!"
|
||||||
|
|
||||||
|
# nacos config info
|
||||||
|
NACOS_CONFIG_GROUP = "LOCAL"
|
||||||
|
NACOS_CONFIG_DATA_ID = "aida.python"
|
||||||
|
|
||||||
|
# nacos server config
|
||||||
|
NACOS_SERVICE_NAME = "AiDA-DEV" # ←←← 必须修改!建议格式:项目名-环境,例如 ai-image-service-dev
|
||||||
|
NACOS_SERVICE_IP = "127.0.0.1"
|
||||||
|
NACOS_SERVICE_PORT = 8445
|
||||||
|
|
||||||
|
# nacos client
|
||||||
|
client = nacos.NacosClient(
|
||||||
|
server_addresses=NACOS_SERVER_ADDRESSES,
|
||||||
|
namespace=NACOS_NAMESPACE,
|
||||||
|
username=NACOS_USERNAME,
|
||||||
|
password=NACOS_PASSWORD
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def listener_config_callback(args):
|
||||||
|
data_id = args['data_id']
|
||||||
|
namespace = args['namespace']
|
||||||
|
group = args['group']
|
||||||
|
content = args['content']
|
||||||
|
logger.info("【Nacos】配置")
|
||||||
|
try:
|
||||||
|
logger.info(f"【Nacos】 动态更新 : data_id : {data_id} | namespace : {namespace} | group_name: {group}")
|
||||||
|
new_config = yaml.safe_load(content) if content else {}
|
||||||
|
for key, value in new_config.items():
|
||||||
|
if hasattr(settings, key):
|
||||||
|
old_val = getattr(settings, key)
|
||||||
|
setattr(settings, key, value)
|
||||||
|
if old_val != value:
|
||||||
|
logger.info(f"🔄 配置更新 → {key}: {old_val} → {value}")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"【Nacos】 配置解析失败: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
def remove_config_callback(args):
|
||||||
|
data_id = args['data_id']
|
||||||
|
namespace = args['namespace']
|
||||||
|
print(f" remove_config_callback : {data_id} | namespace : {namespace}")
|
||||||
|
|
||||||
|
|
||||||
|
def load_nacos_config():
|
||||||
|
"""初始化 Nacos 配置并监听变化"""
|
||||||
|
logger.info(f"【Nacos】 配置订阅 - 初次获取配置信息")
|
||||||
|
|
||||||
|
try:
|
||||||
|
# 1. 第一次获取配置
|
||||||
|
content = client.get_config(data_id=NACOS_CONFIG_DATA_ID, group=NACOS_CONFIG_GROUP)
|
||||||
|
if content:
|
||||||
|
loaded = yaml.safe_load(content) or {}
|
||||||
|
for key, value in loaded.items():
|
||||||
|
if hasattr(settings, key):
|
||||||
|
setattr(settings, key, value)
|
||||||
|
logger.info(f"【Nacos】✅ 配置加载成功: {NACOS_CONFIG_DATA_ID} | 覆盖字段数量: {len(loaded)}")
|
||||||
|
else:
|
||||||
|
logger.warning("【Nacos】 返回配置为空,使用 .env + 默认值")
|
||||||
|
|
||||||
|
client.add_config_watcher(data_id=NACOS_CONFIG_DATA_ID, group=NACOS_CONFIG_GROUP, cb=listener_config_callback)
|
||||||
|
logger.info("【Nacos】✅ 配置监听器已注册(支持热更新)")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"【Nacos】❌ 初始化失败: {e},将仅使用 .env 配置")
|
||||||
|
finally:
|
||||||
|
client.remove_config_watcher(
|
||||||
|
data_id=NACOS_CONFIG_DATA_ID,
|
||||||
|
group=NACOS_CONFIG_GROUP,
|
||||||
|
cb=remove_config_callback
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def register_server():
|
||||||
|
logger.info(f"nacos 服务注册")
|
||||||
|
try:
|
||||||
|
client.add_naming_instance(
|
||||||
|
service_name=NACOS_SERVICE_NAME,
|
||||||
|
ip=NACOS_SERVICE_IP,
|
||||||
|
port=NACOS_SERVICE_PORT,
|
||||||
|
metadata={"status": "ok"},
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"【Nacos】❌ 服务注册失败 : {e}")
|
||||||
|
|
||||||
|
|
||||||
|
def deregister_server():
|
||||||
|
logger.info(f"nacos 服务注册")
|
||||||
|
try:
|
||||||
|
client.remove_naming_instance(
|
||||||
|
service_name=NACOS_SERVICE_NAME,
|
||||||
|
ip=NACOS_SERVICE_IP,
|
||||||
|
port=NACOS_SERVICE_PORT
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"【Nacos】❌ 服务注销失败 : {e}")
|
||||||
@@ -0,0 +1,170 @@
|
|||||||
|
import json
|
||||||
|
import logging
|
||||||
|
import time
|
||||||
|
|
||||||
|
import requests
|
||||||
|
from dashscope import Generation
|
||||||
|
from requests import RequestException
|
||||||
|
from retry import retry
|
||||||
|
|
||||||
|
from app.core.config import settings
|
||||||
|
from app.service.chat_robot.script.prompt import GET_LANGUAGE_PREFIX
|
||||||
|
from app.service.prompt_generation.util import minio_util
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def get_assistant_response(messages):
|
||||||
|
response = Generation.call(
|
||||||
|
model='qwen-max',
|
||||||
|
api_key=settings.QWEN_API_KEY,
|
||||||
|
messages=messages,
|
||||||
|
# seed=random.randint(1, 10000), # 设置随机数种子seed,如果没有设置,则随机数种子默认为1234
|
||||||
|
result_format='message', # 将输出设置为message形式
|
||||||
|
enable_search='false'
|
||||||
|
)
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
def get_language(message: str) -> str:
|
||||||
|
messages = [
|
||||||
|
{
|
||||||
|
"content": GET_LANGUAGE_PREFIX, # ai message
|
||||||
|
"role": "system"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"content": "Tree", # 用户message
|
||||||
|
"role": "user"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"content": "English", # 用户message
|
||||||
|
"role": "assistant"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"content": "玩具", # 用户message
|
||||||
|
"role": "user"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"content": "Chinese", # 用户message
|
||||||
|
"role": "assistant"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"content": message, # 用户message
|
||||||
|
"role": "user"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
first_response = get_assistant_response(messages)
|
||||||
|
assistant_output = first_response.output.choices[0].message.content
|
||||||
|
logging.info(f"大模型输出信息:{first_response}\n判断用户输入的语言为:{assistant_output}")
|
||||||
|
# print(f"大模型输出信息:{first_response}\n判断用户输入的语言为:{assistant_output}")
|
||||||
|
return assistant_output
|
||||||
|
|
||||||
|
|
||||||
|
@retry(exceptions=RequestException, tries=3, delay=1)
|
||||||
|
def get_response(messages):
|
||||||
|
response = Generation.call(
|
||||||
|
model='qwen-turbo',
|
||||||
|
api_key=settings.QWEN_API_KEY,
|
||||||
|
messages=messages,
|
||||||
|
# seed=random.randint(1, 10000), # 设置随机数种子seed,如果没有设置,则随机数种子默认为1234
|
||||||
|
result_format='message', # 将输出设置为message形式
|
||||||
|
enable_search='True'
|
||||||
|
)
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
def get_translation_from_llama3(text):
|
||||||
|
start_time = time.time()
|
||||||
|
url = f"http://{settings.A6000_SERVICE_HOST}:12434/api/generate"
|
||||||
|
# 先获取用户输入文本的语言
|
||||||
|
language = get_language(text)
|
||||||
|
|
||||||
|
if 'English' in language:
|
||||||
|
return text
|
||||||
|
|
||||||
|
# 创建请求的负载 translator是自定义的翻译模型
|
||||||
|
payload = {
|
||||||
|
"model": "AiDA-translator:latest",
|
||||||
|
"prompt": f"[{text}]",
|
||||||
|
"stream": False
|
||||||
|
}
|
||||||
|
# 将负载转换为 JSON 格式
|
||||||
|
headers = {'Content-Type': 'application/json'}
|
||||||
|
response = requests.post(url, data=json.dumps(payload), headers=headers)
|
||||||
|
# 处理响应
|
||||||
|
if response.status_code == 200:
|
||||||
|
# print("Response from server:")
|
||||||
|
# print(response.json())
|
||||||
|
resp = json.loads(response.content).get("response")
|
||||||
|
logger.info(f"translation server runtime is {time.time() - start_time} , response is {resp}")
|
||||||
|
print("input : {}, translate result : {}".format(text, resp))
|
||||||
|
return resp
|
||||||
|
else:
|
||||||
|
logger.info(f"translation server runtime is {time.time() - start_time} , response is {response.content}")
|
||||||
|
print(f"Request failed with status code {response.status_code}")
|
||||||
|
print(response.text)
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
# 在llama3中创建一个翻译模型
|
||||||
|
# def create_model_with_llama(text):
|
||||||
|
# url = "http://localhost:11434/api/create"
|
||||||
|
# # url = "http://20.1.1.43:1143/api/generate"
|
||||||
|
#
|
||||||
|
# # prompt = f"System: {prefix_for_llama}\nUser:[{text}]"
|
||||||
|
#
|
||||||
|
# # 创建翻译器的配置文件
|
||||||
|
# payload = {
|
||||||
|
# "model": "translator",
|
||||||
|
# "modelfile": "FROM llama3\nSYSTEM Translate everything within the brackets [] into English."
|
||||||
|
# "Never translate or modify any English input."
|
||||||
|
# "The input must be fully translated into coherent English sentences."
|
||||||
|
# }
|
||||||
|
#
|
||||||
|
# # 将负载转换为 JSON 格式
|
||||||
|
# headers = {'Content-Type': 'application/json'}
|
||||||
|
# response = requests.post(url, data=json.dumps(payload), headers=headers)
|
||||||
|
|
||||||
|
|
||||||
|
def get_prompt_from_image(image_path, text):
|
||||||
|
start_time = time.time()
|
||||||
|
# url = "http://localhost:11434/api/generate"
|
||||||
|
url = f"http://{settings.B_4_X_4090_SERVICE_HOST}:11434/api/generate"
|
||||||
|
|
||||||
|
image_base64 = minio_util.minio_url_to_base64(image_path.img)
|
||||||
|
# image_base64 = minio_url_to_base64(image_path)
|
||||||
|
|
||||||
|
# 创建请求的负载 translator是自定义的翻译模型
|
||||||
|
payload = {
|
||||||
|
"model": "llama3.2-vision",
|
||||||
|
"images": [image_base64],
|
||||||
|
"prompt": f"{text}",
|
||||||
|
"stream": False
|
||||||
|
}
|
||||||
|
# 将负载转换为 JSON 格式
|
||||||
|
headers = {'Content-Type': 'application/json'}
|
||||||
|
response = requests.post(url, data=json.dumps(payload), headers=headers)
|
||||||
|
# 处理响应
|
||||||
|
if response.status_code == 200:
|
||||||
|
# print("Response from server:")
|
||||||
|
# print(response.json())
|
||||||
|
resp = json.loads(response.content).get("response")
|
||||||
|
logger.info(f"sketch re-generate server runtime is {time.time() - start_time} \n, response is {resp}")
|
||||||
|
# print("input : {}, sketch re-generate result : {}".format(text, resp))
|
||||||
|
return resp
|
||||||
|
else:
|
||||||
|
logger.info(f"sketch re-generate server runtime is {time.time() - start_time} , response is {response.content}")
|
||||||
|
print(f"Request failed with status code {response.status_code}")
|
||||||
|
print(response.text)
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
"""Main function"""
|
||||||
|
text = get_translation_from_llama3("[火焰]")
|
||||||
|
print(text)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
60
nacos-data/snapshot/aida.python+LOCAL+zcr
Normal file
60
nacos-data/snapshot/aida.python+LOCAL+zcr
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
# ====================== 应用基础配置 ======================
|
||||||
|
PORT: 8088
|
||||||
|
SERVE_ENV: "dev"
|
||||||
|
DEBUG: true
|
||||||
|
|
||||||
|
# ====================== API 密钥 ======================
|
||||||
|
QWEN_API_KEY: "sk-f31c29e61ac2498ba5e307aaa6dc10e0"
|
||||||
|
|
||||||
|
# ====================== ComfyUI ======================
|
||||||
|
COMFYUI_SERVER_ADDRESS: "20.1.1.40:8188"
|
||||||
|
|
||||||
|
# ====================== MinIO ======================
|
||||||
|
MINIO_URL: "www.minio-api.aida.com.hk"
|
||||||
|
MINIO_ACCESS: "vXKFLSJkYeEq2DrSZvkB"
|
||||||
|
MINIO_SECRET: "uKTZT3x7C43WvPN9QTc99DiRkwddWZrG9Uh3JVlR"
|
||||||
|
MINIO_SECURE: true
|
||||||
|
|
||||||
|
# ====================== Redis ======================
|
||||||
|
REDIS_HOST: "127.0.0.1"
|
||||||
|
REDIS_PORT: "6379"
|
||||||
|
REDIS_DB: 2
|
||||||
|
|
||||||
|
# ====================== MySQL ======================
|
||||||
|
MYSQL_HOST: "18.167.251.121"
|
||||||
|
MYSQL_PORT: 33008
|
||||||
|
MYSQL_USER: "aida_con"
|
||||||
|
MYSQL_PASSWORD: "123456"
|
||||||
|
MYSQL_DB: "aida_back"
|
||||||
|
|
||||||
|
# ====================== RabbitMQ ======================
|
||||||
|
MQ_HOST: "18.167.251.121"
|
||||||
|
MQ_PORT: "5672"
|
||||||
|
MQ_USERNAME: "rabbit"
|
||||||
|
MQ_PASSWORD: "123456"
|
||||||
|
MQ_VIRTUAL_HOST: "/"
|
||||||
|
MQ_ENV: "dev"
|
||||||
|
|
||||||
|
# ====================== 其他服务 ======================
|
||||||
|
MILVUS_URL: "http://20.1.1.43:19530"
|
||||||
|
MILVUS_TOKEN: "root:Milvus"
|
||||||
|
MILVUS_ALIAS: "default"
|
||||||
|
|
||||||
|
|
||||||
|
OLLAMA_URL: "http://20.1.1.43:11434"
|
||||||
|
|
||||||
|
CHROMADB_PATH: "/mnt/data/workspace/Code/trinity_client_aida/app/chromadb"
|
||||||
|
|
||||||
|
JAVA_STREAM_API_URL: "http://your-java-service:8080"
|
||||||
|
|
||||||
|
FLUX2_GEN_IMG_MODEL_URL: "20.1.1.33:14202"
|
||||||
|
|
||||||
|
A6000_SERVICE_HOST: "20.1.1.43"
|
||||||
|
B_4_X_4090_SERVICE_HOST: "20.1.1.40"
|
||||||
|
|
||||||
|
# ====================== 路径配置 ======================
|
||||||
|
LOGS_PATH: "/mnt/data/workspace/Code/trinity_client_aida/app/logs"
|
||||||
|
CATEGORY_PATH: "/mnt/data/workspace/Code/trinity_client_aida/app/service/attribute/config/descriptor/category/category_dis.csv"
|
||||||
|
SEG_CACHE_PATH: "/seg_cache/"
|
||||||
|
RECOMMEND_PATH_PREFIX: "/mnt/data/workspace/Code/trinity_client_aida/app/service/recommend/"
|
||||||
|
SERVE_PORT: 2010
|
||||||
Reference in New Issue
Block a user