File size: 22,051 Bytes
a5eaebe |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 |
'''
Copyright 2024-2025 Infosys Ltd.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'''
import pickle
import torch
import os
import time
import logging
from flask import Flask, render_template, request, jsonify
from flask import g
from datetime import datetime
import json
from transformers import AutoModelForSequenceClassification, AutoTokenizer
from sentence_transformers import SentenceTransformer,util
from detoxify import Detoxify
from presidio_analyzer import AnalyzerEngine, RecognizerRegistry
from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline
from dao.AdminDb import Results
from werkzeug.exceptions import HTTPException,BadRequest,UnprocessableEntity,InternalServerError
from tqdm.auto import tqdm
from fastapi.encoders import jsonable_encoder
import numpy as np
import traceback
import uuid
from waitress import serve
from mapper.mapper import *
import contextvars
app = Flask(__name__)
print("before loading model")
request_id_var = contextvars.ContextVar("request_id_var")
#pipe = StableDiffusionPipeline.from_pretrained('/model/stablediffusion/fp32/model')
device = "cuda"
registry = RecognizerRegistry()
registry.load_predefined_recognizers()
analyzer_engine = AnalyzerEngine(registry=registry)
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
gpu=0 if torch.cuda.is_available() else -1
check_point = 'toxic_debiased-c7548aa0.ckpt'
toxicityModel = Detoxify(checkpoint='../models/detoxify/'+ check_point,
device=device,
huggingface_config_path='../models/detoxify')
PromptModel_dberta = AutoModelForSequenceClassification.from_pretrained("../models/dbertaInjection").to(device)
Prompttokens_dberta = AutoTokenizer.from_pretrained("../models/dbertaInjection")
topictokenizer_Facebook = AutoTokenizer.from_pretrained("../models/facebook")
topicmodel_Facebook = AutoModelForSequenceClassification.from_pretrained("../models/facebook").to(device)
topictokenizer_dberta = AutoTokenizer.from_pretrained("../models/restricted-dberta-large-zeroshot")
topicmodel_dberta = AutoModelForSequenceClassification.from_pretrained("../models/restricted-dberta-large-zeroshot").to(device)
# classifier = pipeline("zero-shot-classification",model="../models/facebook",device=device)
# classifier2 = pipeline("zero-shot-classification",model="../models/restricted-dberta-large-zeroshot",device=device)
encoder = SentenceTransformer("../models/multi-qa-mpnet-base-dot-v1").to(device)
jailbreakModel = encoder
similarity_model =encoder
request_id_var.set("Startup")
log_dict={}
print("model loaded")
@app.errorhandler(HTTPException)
def handle_exception(e):
"""Return JSON instead of HTML for HTTP errors."""
# start with the correct headers and status code from the error
response = e.get_response()
# replace the body with JSON
response.data = json.dumps({
"code": e.code,
"details": e.description,
})
response.content_type = "application/json"
return response
@app.errorhandler(UnprocessableEntity)
def validation_error_handler(exc):
"""Return JSON instead of HTML for HTTP errors."""
# start with the correct headers and status code from the error
response = exc.get_response()
print(response)
# replace the body with JSON
exc_code_desc=exc.description.split("-")
exc_code=int(exc_code_desc[0])
exc_desc=exc_code_desc[1]
response.data = json.dumps({
"code": exc_code,
"details": exc_desc,
})
response.content_type = "application/json"
return response
@app.errorhandler(InternalServerError)
def validation_error_handler(exc):
"""Return JSON instead of HTML for HTTP errors."""
# start with the correct headers and status code from the error
response = exc.get_response()
print(response)
# replace the body with JSON
response.data = json.dumps({
"code": 500,
"details": "Some Error Occurred ,Please try Later",
})
response.content_type = "application/json"
return response
@app.route("/rai/v2test/raimoderationmodels/detoxifymodel",methods=[ 'POST'])
def toxic_model():
st=time.time()
try:
id=uuid.uuid4().hex
payload=request.get_json()
request_id_var.set(id)
logging.info("before invoking toxic_model service ")
log_dict[request_id_var.get()]=[]
if payload['text'] is None or (payload['text'] is not None and len(payload['text'])==0):
raise UnprocessableEntity("1021-Input Text should not be empty ")
response = toxicity_check(payload,id)
logging.info("after invoking toxic_model service ")
er=log_dict[request_id_var.get()]
logobj = {"_id":id,"error":er}
if len(er)!=0:
Results.createlog(logobj)
del log_dict[id]
logging.debug("response : " + str(response))
logging.info("exit toxic_model routing method")
logging.info(f"Time taken by toxicity {time.time()-st}")
return jsonable_encoder(response)
except UnprocessableEntity as cie:
logging.error(cie.__dict__)
logging.info("exit toxic_model routing method")
raise UnprocessableEntity(**cie.__dict__)
except Exception as cie:
logging.error(cie.__dict__)
logging.info("exit toxic_model routing method")
raise HTTPException()
@app.route("/rai/v2test/raimoderationmodels/privacy",methods=[ 'POST'])
def pii_check():
st=time.time()
logging.info("Entered pii_check routing method")
try:
id=uuid.uuid4().hex
payload=request.get_json()
request_id_var.set(id)
logging.info("before invoking create usecase service ")
log_dict[request_id_var.get()]=[]
if payload['text'] is None or (payload['text'] is not None and len(payload['text'])==0) or payload['entitiesselected'] is None or (payload['entitiesselected'] is not None and len(payload['entitiesselected'])==0):
raise UnprocessableEntity("1021-invalid input!")
response = privacy(id,payload['text'],payload['entitiesselected'])
logging.info("after invoking create usecase service ")
er=log_dict[request_id_var.get()]
logobj = {"_id":id,"error":er}
if len(er)!=0:
Results.createlog(logobj)
del log_dict[id]
logging.debug("response : " + str(response))
# logging.debug("response : " + str(response))
logging.info("exit pii_check routing method")
logging.info(f"Time taken by privacy {time.time()-st}")
return jsonable_encoder(response)
except Exception as cie:
logging.error(cie.__dict__)
logging.info("exit pii_check routing method")
raise HTTPException()
@app.route("/rai/v2test/raimoderationmodels/promptinjectionmodel",methods=[ 'POST'])
def prompt_model():
st=time.time()
logging.info("Entered prompt_model routing method")
try:
id=uuid.uuid4().hex
payload=request.get_json()
request_id_var.set(id)
logging.info("before invoking prompt_model service")
log_dict[request_id_var.get()]=[]
if payload['text'] is None or (payload['text'] is not None and len(payload['text'])==0):
raise UnprocessableEntity("1021-Input Text should not be empty ")
response = promptInjection_check(payload['text'],id)
logging.info("after invoking prompt_model service ")
er=log_dict[request_id_var.get()]
logobj = {"_id":id,"error":er}
if len(er)!=0:
Results.createlog(logobj)
del log_dict[id]
logging.debug("response : " + str(response))
# logging.debug("response : " + str(response))
logging.info("exit prompt_model routing method")
logging.info(f"Time taken by promptinjection {time.time()-st}")
return jsonable_encoder(response)
except Exception as cie:
logging.error(cie.__dict__)
logging.info("exit prompt_model routing method")
raise HTTPException()
@app.route("/rai/v2test/raimoderationmodels/restrictedtopicmodel",methods=[ 'POST'])
def restrictedTopic_model():
st=time.time()
logging.info("Entered restrictedTopic_model routing method")
try:
id=uuid.uuid4().hex
payload=request.get_json()
request_id_var.set(id)
logging.info("before invoking restrictedTopic_model service ")
log_dict[request_id_var.get()]=[]
label_cond = payload['labels'] is None or (payload['labels'] is not None and len(payload['labels'])==0)
model_cond=False
# print("--")
if("model" in payload):
model_cond = payload['model'] is None or (payload['model'] is not None and len(payload['model'])==0)
# print("==")
if payload['text'] is None or (payload['text'] is not None and len(payload['text'])==0) or label_cond or model_cond:
raise UnprocessableEntity("1021-invalid input ")
response = restricttopic_check(payload,id)
logging.info("after invoking restrictedTopic_model service ")
er=log_dict[request_id_var.get()]
logobj = {"_id":id,"error":er}
if len(er)!=0:
Results.createlog(logobj)
del log_dict[id]
logging.debug("response : " + str(response))
# logging.debug("response : " + str(response))
logging.info("exit restrictedTopic_model routing method")
logging.info(f"Time taken by RestrictedTopic{time.time()-st}")
# print(type(response))
# print(type(jsonable_encoder(response)))
return jsonable_encoder(response)
except Exception as cie:
logging.error(cie.__dict__)
logging.info("exit restrictedTopic_model routing method")
raise HTTPException()
@app.route("/rai/v2test/raimoderationmodels/multi_q_net_embedding",methods=[ 'POST'])
def embedding_model():
st=time.time()
logging.info("Entered embedding_model routing method")
try:
id=uuid.uuid4().hex
payload=request.get_json()
request_id_var.set(id)
logging.info("before invoking embedding_model service ")
log_dict[request_id_var.get()]=[]
if payload['text'] is None or (payload['text'] is not None and len(payload['text'])==0):
raise UnprocessableEntity("1021-Input Text should not be empty ")
response = multi_q_net_embedding(id,payload['text'])
logging.info("after invoking embedding_model service ")
er=log_dict[request_id_var.get()]
logobj = {"_id":id,"error":er}
if len(er)!=0:
Results.createlog(logobj)
del log_dict[id]
logging.debug("response : " + str(response))
# logging.debug("response : " + str(response))
logging.info("exit embedding_model routing method")
logging.info(f"Time taken by Jailbreak {time.time()-st}")
return jsonable_encoder(response)
except Exception as cie:
logging.error(cie.__dict__)
logging.info("exit embedding_model routing method")
raise HTTPException()
@app.route("/rai/v2test/raimoderationmodels/multi-qa-mpnet-model_similarity",methods=[ 'POST'])
def similarity_model():
st=time.time()
logging.info("Entered similarity_model routing method")
try:
id=uuid.uuid4().hex
request_id_var.set(id)
logging.info("before invoking similarity_model service ")
payload=request.get_json()
log_dict[request_id_var.get()]=[]
text1_cond = payload['text1'] is None or (payload['text1'] is not None and len(payload['text1'])==0)
text2_cond = payload['text2'] is None or (payload['text2'] is not None and len(payload['text2'])==0)
emb1_cond = payload['emb1'] is None or (payload['emb1'] is not None and len(payload['emb1'])==0)
emb2_cond = payload['emb2'] is None or (payload['emb2'] is not None and len(payload['emb2'])==0)
if text1_cond or text2_cond or emb1_cond or emb2_cond:
raise UnprocessableEntity("1021-Input Text should not be empty ")
response = multi_q_net_similarity(id,payload['text1'],payload['text2'],payload['emb1'],payload['emb2'])
logging.info("after invoking similarity_model service ")
er=log_dict[request_id_var.get()]
logobj = {"_id":id,"error":er}
if len(er)!=0:
Results.createlog(logobj)
del log_dict[id]
logging.debug("response : " + str(response))
# logging.debug("response : " + str(response))
logging.info("exit similarity_model routing method")
logging.info(f"Time taken by similary{time.time()-st}")
return jsonable_encoder(response)
except Exception as cie:
logging.error(cie.__dict__)
logging.info("exit similarity_model routing method")
raise HTTPException()
def privacy(id,text,PIIenities_selected=None):
try:
analyzer_results = analyzer_engine.analyze(text=text, language="en",entities=PIIenities_selected)
entityList= []
anyz_res = jsonable_encoder(analyzer_results)
for i in anyz_res:
entityList.append(i['entity_type'])
return anyz_res,jsonable_encoder(entityList)
except Exception as e:
logging.error("Error occured in privacy")
logging.error(f"Exception: {e}")
log_dict[request_id_var.get()].append({"Line number":str(traceback.extract_tb(e.__traceback__)[0].lineno),"Error":str(e),
"Error Module":"Failed at privacy call"})
raise InternalServerError()
def multi_q_net_similarity(id,text1=None,text2=None,emb1=None,emb2=None):
try:
if text1:
with torch.no_grad():
emb1 = jailbreakModel.encode(text1, convert_to_tensor=True,device=device)
if text2:
with torch.no_grad():
emb2 = jailbreakModel.encode(text2, convert_to_tensor=True,device=device)
emb = util.pytorch_cos_sim(emb1, emb2).to("cpu").numpy().tolist()
del emb1
del emb2
#torch.cuda.empty_cache()
return emb
except Exception as e:
logging.error("Error occured in multi_q_net_similarity")
logging.error(f"Exception: {e}")
log_dict[request_id_var.get()].append({"Line number":str(traceback.extract_tb(e.__traceback__)[0].lineno),"Error":str(e),
"Error Module":"Failed at multi_q_net_similarity call"})
raise InternalServerError()
def multi_q_net_embedding(id,lst):
try:
res = []
for text in lst:
with torch.no_grad():
text_embedding = jailbreakModel.encode(text, convert_to_tensor=True,device=device)
res.append(text_embedding.to("cpu").numpy().tolist())
del text_embedding
#torch.cuda.empty_cache()
return res
# return text_embedding.numpy().tolist()
except Exception as e:
logging.error("Error occured in multi_q_net text embedding")
logging.error(f"Exception: {e}")
log_dict[request_id_var.get()].append({"Line number":str(traceback.extract_tb(e.__traceback__)[0].lineno),"Error":str(e),
"Error Module":"Failed at multi_q_net text embedding call"})
raise InternalServerError()
def restricttopic_check(payload,id):
try:
# topicmodel = topicmodel_Facebook
# topictokenizer = topictokenizer_Facebook
# nlp = pipeline('zero-shot-classification', model=classifier, tokenizer=topictokenizer)
text=payload['text']
labels=payload['labels']
model =payload['model'] if hasattr(payload, 'model') else "facebook"
if model==None:
model="dberta"
if model=="facebook":
# nlp = classifier
nlp = pipeline('zero-shot-classification', model=topicmodel_Facebook, tokenizer=topictokenizer_Facebook, device=gpu)
elif model=="dberta":
# nlp = classifier2
nlp = pipeline('zero-shot-classification', model=topicmodel_dberta, tokenizer=topictokenizer_dberta,device=gpu)
with torch.no_grad():
output=nlp(text, labels,multi_label=True)
for i in range(len(output["scores"])):
output["scores"][i] = round(output["scores"][i],4)
del nlp
#torch.cuda.empty_cache()
return output
except Exception as e:
logging.error("Error occured in restricttopic_check")
logging.error(f"Exception: {e}")
log_dict[request_id_var.get()].append({"Line number":str(traceback.extract_tb(e.__traceback__)[0].lineno),"Error":str(e),
"Error Module":"Failed at restricttopic_check call"})
raise InternalServerError()
def toxicity_check(payload,id) :
try:
text = payload['text']
with torch.no_grad():
output = toxicityModel.predict(text)
List_profanity_score = []
obj_profanityScore_toxic = profanityScore(metricName='toxicity',
metricScore=output['toxicity'])
obj_profanityScore_severe_toxic = profanityScore(metricName='severe_toxicity',
metricScore=output['severe_toxicity'])
obj_profanityScore_obscene = profanityScore(metricName='obscene',
metricScore=output['obscene'])
obj_profanityScore_threat = profanityScore(metricName='threat',
metricScore=output['threat'])
obj_profanityScore_insult = profanityScore(metricName='insult',
metricScore=output['insult'])
obj_profanityScore_identity_attack = profanityScore(metricName='identity_attack',
metricScore=output['identity_attack'])
obj_profanityScore_sexual_explicit = profanityScore(metricName='sexual_explicit',
metricScore=output['sexual_explicit'])
List_profanity_score.append(obj_profanityScore_toxic)
List_profanity_score.append(obj_profanityScore_severe_toxic)
List_profanity_score.append(obj_profanityScore_obscene)
List_profanity_score.append(obj_profanityScore_threat)
List_profanity_score.append(obj_profanityScore_insult)
List_profanity_score.append(obj_profanityScore_identity_attack)
List_profanity_score.append(obj_profanityScore_sexual_explicit)
objProfanityAnalyzeResponse = {}
objProfanityAnalyzeResponse['toxicScore'] = List_profanity_score
#torch.cuda.empty_cache()
return objProfanityAnalyzeResponse
except Exception as e:
logging.error("Error occured in toxicity_check")
logging.error(f"Exception: {e}")
log_dict[request_id_var.get()].append({"Line number":str(traceback.extract_tb(e.__traceback__)[0].lineno),"Error":str(e),
"Error Module":"Failed at toxicity_check call"})
raise InternalServerError()
def promptInjection_check(text,id):
try:
Prompttokens = Prompttokens_dberta
PromptModel = PromptModel_dberta
tokens = Prompttokens.encode_plus(text, truncation=True, padding=True, return_tensors="pt").to(device)
with torch.no_grad():
outputs = PromptModel(**tokens)
predicted_label = outputs.logits.argmax().item()
label_names = PromptModel.config.id2label
predicted_label_name = label_names[predicted_label]
predicted_probabilities = outputs.logits.softmax(dim=1)[0, predicted_label].item()
del tokens
#torch.cuda.empty_cache()
# #torch.cuda.empty_cache()
return predicted_label_name,predicted_probabilities
except Exception as e:
logging.error("Error occured in promptInjection_check")
logging.error(f"Exception: {e}")
log_dict[request_id_var.get()].append({"Line number":str(traceback.extract_tb(e.__traceback__)[0].lineno),"Error":str(e),
"Error Module":"Failed at promptInjection_check call"})
raise InternalServerError()
@app.route("/")
def hello_world():
return "<h1>Hello, world!</h1>"
if __name__ == "__main__":
serve(app, host='0.0.0.0', port=8000, threads=int(os.getenv('THREADS',1)),connection_limit=int(os.getenv('CONNECTION_LIMIT',500)), channel_timeout=int(os.getenv('CHANNEL_TIMEOUT',120)))
#app.run()
|