Update moderations.py
Browse files- moderations.py +35 -38
moderations.py
CHANGED
@@ -81,7 +81,41 @@ def register_service(client,service_name,service_ip,service_port,cluster_name,he
|
|
81 |
class ModerationsRequest(BaseModel):
|
82 |
model: str = "text-moderation-latest" # or "text-moderation-stable"
|
83 |
input: Union[str, list[str]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
|
|
|
|
|
|
|
85 |
@app.post("/v1/moderations")
|
86 |
async def moderations(request: ModerationsRequest):
|
87 |
results = {
|
@@ -155,41 +189,4 @@ async def predict(text, model, tokenizer):
|
|
155 |
# Main
|
156 |
if __name__ == "__main__":
|
157 |
|
158 |
-
|
159 |
-
config = configparser.ConfigParser()
|
160 |
-
# 读取配置文件
|
161 |
-
if not config.read('config.ini'):
|
162 |
-
raise RuntimeError("配置文件不存在")
|
163 |
-
# Nacos server and other configurations
|
164 |
-
NACOS_SERVER = config['nacos']['nacos_server']
|
165 |
-
NAMESPACE = config['nacos']['namespace']
|
166 |
-
CLUSTER_NAME = config['nacos']['cluster_name']
|
167 |
-
client = nacos.NacosClient(NACOS_SERVER, namespace=NAMESPACE, username=config['nacos']['username'], password=config['nacos']['password'])
|
168 |
-
SERVICE_NAME = config['nacos']['service_name']
|
169 |
-
HEALTH_CHECK_INTERVAL = int(config['nacos']['health_check_interval'])
|
170 |
-
if config.has_option('nacos', 'weight'):
|
171 |
-
WEIGHT = int(config.get('nacos', 'weight'))
|
172 |
-
else:
|
173 |
-
WEIGHT = 1
|
174 |
-
HTTP_PROXY = config.getboolean('server', 'http_proxy')
|
175 |
-
DOMAIN = config['server']['domain']
|
176 |
-
PROTOCOL = config['server']['protocol']
|
177 |
-
DIRECT_DOMAIN = config.getboolean('server', 'direct_domain')
|
178 |
-
# Parse AutoDLServiceURL
|
179 |
-
autodl_url = os.environ.get('AutoDLServiceURL')
|
180 |
-
|
181 |
-
if not autodl_url:
|
182 |
-
raise RuntimeError("Error: AutoDLServiceURL environment variable is not set.")
|
183 |
-
|
184 |
-
parsed_url = urlparse(autodl_url)
|
185 |
-
SERVICE_IP = parsed_url.hostname
|
186 |
-
SERVICE_PORT = parsed_url.port
|
187 |
-
if not SERVICE_IP or not SERVICE_PORT:
|
188 |
-
raise RuntimeError("Error: Invalid AutoDLServiceURL format.")
|
189 |
-
|
190 |
-
print(f"Service will be registered with IP: {SERVICE_IP} and Port: {SERVICE_PORT}")
|
191 |
-
if not register_service(client,SERVICE_NAME,SERVICE_IP,SERVICE_PORT,CLUSTER_NAME,HEALTH_CHECK_INTERVAL,WEIGHT,HTTP_PROXY,DOMAIN,PROTOCOL,DIRECT_DOMAIN):
|
192 |
-
raise RuntimeError("Service is healthy but failed to register.")
|
193 |
-
app.register_model('text-moderations-latest', 'text-moderations-stable')
|
194 |
-
app.register_model('text-moderations-005', 'text-moderations-ifmain')
|
195 |
-
uvicorn.run(app, host="0.0.0.0", port=6006)
|
|
|
81 |
class ModerationsRequest(BaseModel):
|
82 |
model: str = "text-moderation-latest" # or "text-moderation-stable"
|
83 |
input: Union[str, list[str]]
|
84 |
+
@app.on_event("startup")
|
85 |
+
def startup_event():
|
86 |
+
# 创建配置解析器
|
87 |
+
config = configparser.ConfigParser()
|
88 |
+
# 读取配置文件
|
89 |
+
if not config.read('config.ini'):
|
90 |
+
raise RuntimeError("配置文件不存在")
|
91 |
+
|
92 |
+
# Nacos server and other configurations
|
93 |
+
NACOS_SERVER = config['nacos']['nacos_server']
|
94 |
+
NAMESPACE = config['nacos']['namespace']
|
95 |
+
CLUSTER_NAME = config['nacos']['cluster_name']
|
96 |
+
client = nacos.NacosClient(NACOS_SERVER, namespace=NAMESPACE, username=config['nacos']['username'], password=config['nacos']['password'])
|
97 |
+
SERVICE_NAME = config['nacos']['service_name']
|
98 |
+
HEALTH_CHECK_INTERVAL = int(config['nacos']['health_check_interval'])
|
99 |
+
WEIGHT = int(config.get('nacos', 'weight', fallback='1'))
|
100 |
+
HTTP_PROXY = config.getboolean('server', 'http_proxy')
|
101 |
+
DOMAIN = config['server']['domain']
|
102 |
+
PROTOCOL = config['server']['protocol']
|
103 |
+
DIRECT_DOMAIN = config.getboolean('server', 'direct_domain')
|
104 |
+
|
105 |
+
# Parse AutoDLServiceURL
|
106 |
+
autodl_url = os.environ.get('AutoDLServiceURL')
|
107 |
+
if not autodl_url:
|
108 |
+
raise RuntimeError("Error: AutoDLServiceURL environment variable is not set.")
|
109 |
+
|
110 |
+
parsed_url = urlparse(autodl_url)
|
111 |
+
SERVICE_IP = parsed_url.hostname
|
112 |
+
SERVICE_PORT = parsed_url.port
|
113 |
+
if not SERVICE_IP or not SERVICE_PORT:
|
114 |
+
raise RuntimeError("Error: Invalid AutoDLServiceURL format.")
|
115 |
|
116 |
+
# Register service with Nacos
|
117 |
+
if not register_service(client, SERVICE_NAME, SERVICE_IP, SERVICE_PORT, CLUSTER_NAME, HEALTH_CHECK_INTERVAL, WEIGHT, HTTP_PROXY, DOMAIN, PROTOCOL, DIRECT_DOMAIN):
|
118 |
+
raise RuntimeError("Service is healthy but failed to register.")
|
119 |
@app.post("/v1/moderations")
|
120 |
async def moderations(request: ModerationsRequest):
|
121 |
results = {
|
|
|
189 |
# Main
|
190 |
if __name__ == "__main__":
|
191 |
|
192 |
+
uvicorn.run("moderations:app", host="0.0.0.0", port=6006, reload=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|