Skip to content

Commit ab13f82

Browse files
authored
Add files via upload
1 parent 71cd5a0 commit ab13f82

File tree

1 file changed

+27
-70
lines changed

1 file changed

+27
-70
lines changed

compute_health_score.py

+27-70
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import openai, os
2-
import pandas as pd
1+
import openai
2+
import os
33
import re
4-
from Hopfield import retrieval_info
4+
import csv
55
from llama_index import GPTVectorStoreIndex, SimpleDirectoryReader
66
from llama_index import Prompt
77
from llama_index import StorageContext, load_index_from_storage
@@ -10,20 +10,16 @@
1010
HumanMessagePromptTemplate,
1111
SystemMessagePromptTemplate,
1212
)
13-
import csv
14-
15-
16-
1713

18-
def answer_from_gpt(ques, context, work):
1914

15+
def answer_from_gpt(ques, context):
2016
storage_context = StorageContext.from_defaults(persist_dir='./storage')
2117
index = load_index_from_storage(storage_context, index_id="index_health")
2218
list_score = []
2319

2420
t = 0
2521
for i in ques:
26-
my_context = context + work[t]
22+
my_context = context
2723
QA_PROMPT = get_systemprompt_template(my_context)
2824
query_engine = index.as_query_engine(text_qa_template=QA_PROMPT)
2925
response = query_engine.query(i)
@@ -36,23 +32,21 @@ def answer_from_gpt(ques, context, work):
3632
return list_score
3733

3834

39-
4035
def get_systemprompt_template(exist_context):
41-
4236
chat_text_qa_msgs = [
4337
SystemMessagePromptTemplate.from_template(
4438
exist_context
4539
),
4640
HumanMessagePromptTemplate.from_template(
47-
"Give the answer in jason format with only one number between 0 and 1 that is: 'score'\n"
48-
"The score number must be an decimals\n"
49-
"This is the rule of answer: 0-0.2 is mild or none, 0.3-0.6 is moderate, and above 0.7 is severe.\n"
50-
"This is a patient‘s medical record. Context information in below\n"
51-
"---------------------\n"
52-
"{context_str}"
53-
"Given the context information, you are a helpful health consultant "
54-
"answer the question: {query_str}\n"
55-
)
41+
"Give the answer in jason format with only one number between 0 and 1 that is: 'score'\n"
42+
"The score number must be an decimals\n"
43+
"This is the rule of answer: 0-0.2 is mild or none, 0.3-0.6 is moderate, and above 0.7 is severe.\n"
44+
"This is a patient‘s medical record. Context information in below\n"
45+
"---------------------\n"
46+
"{context_str}"
47+
"Given the context information, you are a helpful health consultant "
48+
"answer the question: {query_str}\n"
49+
)
5650
]
5751
chat_text_qa_msgs_lc = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
5852
text_qa_template = Prompt.from_langchain_prompt(chat_text_qa_msgs_lc)
@@ -79,13 +73,9 @@ def generate_question(path):
7973
question = []
8074
for i in my_feature_list:
8175
sentence = f"Does the person described in the case have {i} symptoms? Do you think it is serious?"
82-
list_sentence = [sentence]
83-
retrieval = retrieval_info(list_sentence, '/Users/jmy/Desktop/ai_for_health_final/', 1)
8476
question.append(sentence)
85-
related_work.append(retrieval[0])
86-
print(retrieval[0])
8777

88-
return question, related_work, my_feature_list
78+
return question, my_feature_list
8979

9080

9181
def count_subfolders(folder_path):
@@ -96,65 +86,39 @@ def count_subfolders(folder_path):
9686
if root != folder_path:
9787
subfolder_count += 1
9888

99-
basepath = '/Users/jmy/Desktop/ai_for_health_final/dataset_folder/health_report_'
89+
basepath = '/Users/chongzhang/PycharmProjects/ai_for_health_final/dataset_folder/health_report_'
10090
for i in range(subfolder_count):
101-
path_rr = basepath+str({i})
91+
path_rr = basepath + str({i})
10292
subfolder_paths.append(path_rr)
10393

10494
return subfolder_count, subfolder_paths
10595

10696

107-
# def answer_from_gpt(ques,context):
108-
# # rebuild storage context
109-
# storage_context = StorageContext.from_defaults(persist_dir='./storage')
110-
# # load index
111-
# index = load_index_from_storage(storage_context, index_id="index_health")
112-
#
113-
# list_score = []
114-
# for i in ques:
115-
# QA_PROMPT = get_systemprompt_template(context)
116-
# query_engine = index.as_query_engine(text_qa_template=QA_PROMPT)
117-
# response = query_engine.query(i)
118-
# stt = str(response)
119-
#
120-
# score = extract_score(stt)
121-
# list_score.append(score)
122-
# print(score)
123-
#
124-
# return list_score
125-
126-
127-
def load_doc(folder_path,question,work):
128-
print(len(work))
129-
count,dict = count_subfolders(folder_path)
97+
def load_doc(folder_path, question):
98+
count, dict = count_subfolders(folder_path)
13099
list_k = []
100+
openai.api_key = "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"
131101
context = 'Here is some additional professional health knowledge that can help you better analyze the report'
132-
for i in range(0,10000):
102+
for i in range(0, 1):
133103
documents = SimpleDirectoryReader(dict[i]).load_data()
134104
index = GPTVectorStoreIndex.from_documents(documents)
135105
index.set_index_id("index_health")
136106
index.storage_context.persist('./storage')
137107
content = context
138-
list = answer_from_gpt(question, content, work)
108+
list = answer_from_gpt(question, content)
139109
list_k.append(list)
140110
return list_k
141111

142112

143-
144113
if __name__ == '__main__':
145114

146-
147115
openai.api_key = os.environ.get("OPENAI_API_KEY")
148-
path = '/Users/jmy/Desktop/ai_for_health_final/label and feature/input_feature.txt'
149-
question, related_work, features_list = generate_question(path)
150-
folder_path = '/Users/jmy/Desktop/ai_for_health_final/dataset_folder'
151-
list = load_doc(folder_path, question, related_work)
116+
path = '/Users/chongzhang/PycharmProjects/ai_for_health_final/label and feature/input_feature.txt'
117+
question, features_list = generate_question(path)
118+
folder_path = '/Users/chongzhang/PycharmProjects/ai_for_health_final/dataset_folder'
119+
list = load_doc(folder_path, question)
152120

153-
with open('training/train.txt', 'w') as file:
154-
for item in list:
155-
file.write(''.join(str(item)) + '\n\n')
156-
157-
with open('training/combined7.csv', 'w', newline='') as file:
121+
with open(f'training2/combined10.csv', 'w', newline='') as file:
158122
writer = csv.writer(file)
159123
# 首先写入特征行
160124
writer.writerow(features_list)
@@ -163,10 +127,3 @@ def load_doc(folder_path,question,work):
163127
writer.writerow(row)
164128

165129
print("CSV file has been created successfully.")
166-
167-
168-
169-
170-
171-
172-

0 commit comments

Comments
 (0)