web.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. @Time : 2024/9/10 18:33
  5. @Author : cao
  6. @File : web2.py.py
  7. @Desc :
  8. """
  9. import json
  10. import uvicorn
  11. from fastapi import FastAPI
  12. from pydantic import BaseModel
  13. from sentence_transformers import SentenceTransformer
  14. import utils
  15. import faiss
  16. from collections import Counter
  17. import logging
  18. from starlette.responses import JSONResponse
  19. import dirty
  20. from prometheus_fastapi_instrumentator import Instrumentator, metrics
  21. # from prometheus_client import Counter as myCounter
  22. # from prometheus_client import REGISTRY,gc_collector, platform_collector, process_collector
  23. logger = logging.getLogger('my_logger')
  24. logger.setLevel(logging.INFO)
  25. console_handler = logging.StreamHandler()
  26. formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  27. logger.addHandler(console_handler)
  28. model = utils.load_model()
  29. index = utils.index
  30. cate = utils.cate
  31. class Sentence(BaseModel):
  32. query: str
  33. sessionId: str
  34. app = FastAPI()
  35. Instrumentator().instrument(app).expose(app)
  36. from registry import REQUEST_COUNT
  37. @app.post('/intention')
  38. def intention(sentence:Sentence):
  39. query, session_id = sentence.query, sentence.sessionId
  40. logger.info(f"intention: session_id:{session_id}, query:{query}")
  41. if query is None or len(query)==0:
  42. return JSONResponse(content={"status":0, "intention": "others", "query": query, "isFaq":False}, media_type="application/json")
  43. is_dirty, mess = dirty.dirty_detect(query)
  44. if is_dirty:
  45. return JSONResponse(content=mess, media_type="application/json")
  46. embed = model.encode([query], normalize_embeddings=True)
  47. D, I = index.search(embed, 3)
  48. index_prob = dict(zip(I[0], D[0]))
  49. out = []
  50. for doc_id, prob in index_prob.items():
  51. ans = cate.get(str(doc_id), {})
  52. ans['prob'] = prob
  53. ans['doc_id'] = doc_id
  54. out.append(ans)
  55. out = sorted(out, key=lambda x: float(x['prob']), reverse=True)
  56. res = {}
  57. intention = [i for i in map(lambda x: x['意图类别'], out)]
  58. first= out[0]['prob']
  59. logger.info(f"intention: session_id:{session_id}, query:{query}, first scores:{first}")
  60. if out[0]['prob'] >0.9 or (out[0]['prob'] >0.7 and (len(set(intention)) ==1 or len(intention)== len(set(intention)))):
  61. res['ans'] = out[0]['FAQ答案']
  62. res['doc_id'] = int(out[0]['doc_id'])
  63. res['score'] = float(out[0]['prob'])
  64. res['intent'] = out[0]['cate']
  65. res['class'] = out[0]['class']
  66. res['subclass'] = out[0]['意图类别']
  67. res['isFaq'] = False if out[0]['cate'] !="FAQ" else True
  68. res['recall'] = "emb"
  69. res['query'] = query
  70. res['status'] = 2
  71. res['intent'] = out[0]['cate']
  72. else:
  73. intention_cate = dict(Counter(intention))
  74. intention_max = sorted(intention_cate.items(), key=lambda x: x[1], reverse=True)[0]
  75. out = [i for i in filter(lambda x: x['意图类别']==intention_max[0], out)]
  76. res['ans'] = out[0]['FAQ答案']
  77. res['doc_id'] = int(out[0]['doc_id'])
  78. res['score'] = float(out[0]['prob'])
  79. res['isFaq'] = False if out[0]['cate'] !="FAQ" else True
  80. res['recall'] = "emb"
  81. res['class'] = out[0]['class']
  82. res['subclass'] = out[0]['意图类别']
  83. res['query'] = query
  84. if out[0]['prob'] < 0.7:
  85. res['intent'] = 'others'
  86. REQUEST_COUNT.inc()
  87. res['status'] = 4
  88. else:
  89. res['intent'] = out[0]['cate']
  90. res['status'] = 3
  91. logger.info(f"intention: session_id:{session_id}: ans:{res}")
  92. return JSONResponse(content=res, media_type="application/json")
  93. if __name__ == '__main__':
  94. uvicorn.run("web:app", host='0.0.0.0', port=50072, workers=4)