server.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import requests
  2. from nltk.tokenize import sent_tokenize
  3. from fastapi import FastAPI
  4. from return_result import get_ner_result
  5. from return_result import get_re_result
  6. from pdfReader import extract_text_from_pdf
  7. from pdfReader import split_sentences
  8. from threading import Lock
  9. app = FastAPI()
  10. lock = Lock()
  11. @app.post("/extractKnowledge")
  12. async def process_doc(data: dict):
  13. # 尝试获取线程锁,如果已经被其他线程获取,则等待
  14. locked = lock.acquire(blocking=False)
  15. if not locked:
  16. return {"code": 500,
  17. "msg": "Task is already running. Please try again later."}
  18. try:
  19. # 在锁的保护下执行任务
  20. doc_address = data.get("docAddress")
  21. if not doc_address.endswith(".pdf"):
  22. return {"code": 500,
  23. "msg": "Invalid file format"}
  24. text = extract_text_from_pdf(doc_address)
  25. sentences = split_sentences(text)
  26. results = []
  27. for raw_text in sentences:
  28. entities = get_ner_result(raw_text)
  29. result = get_re_result(entities, raw_text)
  30. for i in result:
  31. results.append(i)
  32. jsonRes = {
  33. "docInfo": {
  34. "docId": data.get("docId"),
  35. "taskId": data.get("taskId"),
  36. "subTaskId": data.get("subTaskId"),
  37. "docName": data.get("docName"),
  38. "docAddress":data.get("docAddress")
  39. },
  40. "knowledgeList": results,
  41. "code": 200,
  42. "msg": "success"
  43. }
  44. url = "http://example.com/kg/saveKnowledge"
  45. headers = {"Content-Type": "application/json"}
  46. data = {"key1": "value1", "key2": "value2"} # 替换为您的json数据
  47. try:
  48. response = requests.post(url, headers=headers, data=jsonRes)
  49. response.raise_for_status() # 如果响应状态码不是 200,则会抛出异常
  50. if response.json()["code"] != 200:
  51. result = {"code": 500, "msg": response.json()["msg"]}
  52. else:
  53. result = {"code": 200, "msg": "成功"}
  54. except requests.exceptions.RequestException as e:
  55. result = {"code": 500, "msg": str(e)}
  56. return result
  57. except requests.exceptions.RequestException as e:
  58. return {"code": 500, "msg": str(e)}
  59. finally:
  60. # 释放线程锁
  61. lock.release()
  62. if __name__ == '__main__':
  63. import uvicorn
  64. uvicorn.run(app=app,
  65. host="0.0.0.0",
  66. port=9999,
  67. workers=1)