read_write_excel.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import json
  2. from socket import timeout
  3. import pandas as pd
  4. import random
  5. import requests
  6. from db.session import get_db
  7. from service.kg_edge_service import KGEdgeService
  8. from service.kg_node_service import KGNodeService
  9. from tests.DeepseekUtil import chat
  10. from utils.agent import call_chat_api,get_conversation_id
  11. # 读取指定路径的xlsx文件
  12. def read_write_excel(file_path):
  13. nodeService = KGNodeService(next(get_db()))
  14. edgeService = KGEdgeService(next(get_db()))
  15. app_id = "256fd853-60b0-4357-b11b-8114b4e90ae0"
  16. df = pd.read_excel(file_path)
  17. for index, row in df.iterrows():
  18. try:
  19. disease_name = str(row[0])
  20. node = nodeService.get_node_by_name_category(disease_name, '疾病')
  21. if node is None:
  22. df.at[index, '主诉和现病史'] = '未找到该疾病'
  23. continue
  24. edges = edgeService.get_edges_by_nodes(src_id=node['id'], category='疾病相关症状')
  25. if edges is None or len(edges) == 0:
  26. df.at[index, '疾病相关症状'] = '未找到疾病相关症状'
  27. continue
  28. else:
  29. symptoms = [edge['dest_node']['name'] for edge in edges]
  30. df.at[index, '疾病相关症状'] = ' '.join(symptoms)
  31. question = f'你是一个全科医生,帮忙按照开病历的格式,生成“{disease_name}”的主诉和现病史。\n要求:\n1、主诉和现病史需要包含“{disease_name}”的主要症状。\n2、内容简短,不要有除了“主诉”和“现病史”以外多余的信息。\n以json的格式输出,格式如下:\n{{\"主诉\": \"面部红色丘疹、脓疱伴瘙痒3天\", \"现病史\": \"患者3天前面部出现红色丘疹,逐渐发展为脓疱,伴轻度瘙痒,无发热。自行外用“红霉素软膏”效果不佳,脓疱增多,遂就诊。既往体健,无类似病史,无药物过敏史。\"}}'
  32. answer = chat(question).strip("\n```json")
  33. json_data = json.loads(answer)
  34. df.at[index, '主诉和现病史'] = answer
  35. url = f'http://173.18.12.205:8001/knowledge/disease/recommend?chief={json_data['主诉']}&present_illness={json_data['现病史']}'
  36. response = requests.request("GET", url,timeout=60)
  37. data = json.loads(response.text)['data']
  38. df.at[index, '主诉和现病史中的症状'] = data['症状']
  39. if any(disease_name in d['name'] for d in data['可能诊断']):
  40. df.at[index, '疾病是否命中'] = '是'
  41. else:
  42. df.at[index, '疾病是否命中'] = '否'
  43. df.at[index, '可能诊断'] = data['可能诊断']
  44. except Exception as e:
  45. print(f"处理Excel文件时发生错误: {e}")
  46. # 保存修改后的文件
  47. df.to_excel(file_path, index=False)
  48. if __name__ == "__main__":
  49. # 指定xlsx文件路径
  50. file_path = "C:\\Users\\17664\\Desktop\\疾病列表.xlsx"
  51. read_write_excel(file_path)