lyclyc52's picture
Update: add distributed inference code
f168581
raw
history blame
11.4 kB
import json
sys_instructions = {
'question_rephrase': 'Given the some sentences, rewrite it into a regular format. It contain some pairs of questions and answers. For each pair, the output should be formulated as: \n' + \
'Q: <question>\n' + \
'A: <answer>\n' + \
'Please do not output anything else. The output should only contain the pairs of questions and answers. \n' + \
'Here are some examples.\n' +\
'Example 1: \n' + \
'Input: \n' + \
"### Question 1:\nWhat equipment can be found in the operating room?\n\n**Answer:** The operating room contains anesthesia equipment, an instrument table, and a secondary table.\n\n---\n\n### Question 2:\nWhat is positioned in the center of the operating room?\n\n**Answer:** There's an instrument table in the center of the operating room. It is typically used to keep surgical instruments in a readily accessible location doing to an operation.\n\n---\n\n### Question 3:\nIs there any anesthesia equipment present in the operating room?\n\n**Answer:** Yes, there is anesthesia equipment present in the operating room, which is used to administer anesthesia to patients in order to keep them unconscious or pain-free during surgical procedures.\n" + \
'Output: \n' + \
'Q: What equipment can be found in the operating room?\n' + \
'A: The operating room contains anesthesia equipment, an instrument table, and a secondary table.\n\n' + \
'Q: What is positioned in the center of the operating room?\n' + \
'A: There\'s an instrument table in the center of the operating room. It is typically used to keep surgical instruments in a readily accessible location doing to an operation.\n\n' + \
'Q: Is there any anesthesia equipment present in the operating room?\n' + \
'A: Yes, there is anesthesia equipment present in the operating room, which is used to administer anesthesia to patients in order to keep them unconscious or pain-free during surgical procedures.\n' + \
'Example 2: \n' + \
'Input: \n' + \
"Sure, I'll describe the current phase and generate related questions and answers that pertain to maintaining the sterile field during surgery.\n\n### Description\nIn the image, the medical staff are wearing blue sterile gowns, gloves, masks, and caps, indicating adherence to strict aseptic techniques. There is equipment carefully covered with sterile coverings, and positions are maintained near the required instruments such as the instrument table and anesthesia equipment. Staff members seem to be in diligent preparation and monitoring phases prior to more active surgical intervention.\n\n### Questions and Answers about the Sterile Phase\n\n#### Question 1:\nWhat is the current surgical phase, and how should it be maintained?\n##### Answer:\nThe current surgical phase is sterile. It is crucial to maintain strict aseptic techniques: all personnel must continue to wear sterile gowns, gloves, and masks while thoroughly disinfecting the surgical site using sterilized instruments and materials. The team should minimize unnecessary movement and contact to reduce contamination risks. Clear communication within the team is important to promptly address any breaches in sterility.\n\n#### Question 2:\nWhy is it essential to maintain the sterile phase during surgery?\n##### Answer:\nMaintaining the sterile phase during surgery is essential to prevent infections. It ensures the surgical site and instruments remain free from harmful bacteria and other microorganisms. Proper sterilization reduces the risk of postoperative complications, speeds up patient recovery, and enhances the overall success of the surgical procedure.\n\n#### Question 3:\nWhat suggestions could help surgeons improve the quality of surgery during the sterile phase?\n##### Answer:\nTo improve the quality of surgery during the sterile phase, it is important to:\n1. Ensure a thorough hand washing and use of sterilizing solutions before scrubbing in.\n2. Use sterile drapes to isolate the surgical site efficiently.\n3. Maintain constant surveillance of sterilization procedures.\n4. Label and organize sterile instruments properly on the instrument table.\n5. Implement a clear protocol for notifying the team if the sterile field is compromised, so corrective actions can be taken immediately.\n6. Avoid over-cluttering the sterile field with non-essential equipment.\n\nBy following these suggestions, the sterility of the procedure can be maintained, resulting in higher surgery quality and better patient outcomes." + \
'Output: \n' + \
'Q: What is the current surgical phase, and how should it be maintained?\n' + \
'A: The current surgical phase is sterile. It is crucial to maintain strict aseptic techniques: all personnel must continue to wear sterile gowns, gloves, and masks while thoroughly disinfecting the surgical site using sterilized instruments and materials. The team should minimize unnecessary movement and contact to reduce contamination risks. Clear communication within the team is important to promptly address any breaches in sterility.\n\n' + \
'Q: Why is it essential to maintain the sterile phase during surgery?\n' + \
'A: Maintaining the sterile phase during surgery is essential to prevent infections. It ensures the surgical site and instruments remain free from harmful bacteria and other microorganisms. Proper sterilization reduces the risk of postoperative complications, speeds up patient recovery, and enhances the overall success of the surgical procedure.\n\n' + \
'Q: What suggestions could help surgeons improve the quality of surgery during the sterile phase?\n' + \
'A: To improve the quality of surgery during the sterile phase, it is important to:\n1. Ensure a thorough hand washing and use of sterilizing solutions before scrubbing in.\n2. Use sterile drapes to isolate the surgical site efficiently.\n3. Maintain constant surveillance of sterilization procedures.\n4. Label and organize sterile instruments properly on the instrument table.\n5. Implement a clear protocol for notifying the team if the sterile field is compromised, so corrective actions can be taken immediately.\n6. Avoid over-cluttering the sterile field with non-essential equipment.\n' + \
'Example 3: \n' + \
'Input: \n' + \
"Question: What equipment can be found in the operating room?\nAnswer: Anesthesia equipment.\n\nQuestion: What is placed on the instrument table?\nAnswer: Various surgical instruments needed for the procedure are typically placed on the instrument table for easy access by the surgical team.\n\nQuestion: Where can the secondary table be found, and what might it be used for?\nAnswer: The secondary table is part of the auxiliary equipment in the operating room and may be used to hold additional surgical instruments or supplies needed during the procedure." + \
'Output: \n' + \
'Q: What equipment can be found in the operating room?\n' + \
'A: Anesthesia equipment.\n\n' + \
'Q: What is placed on the instrument table?\n' + \
'A: Various surgical instruments needed for the procedure are typically placed on the instrument table for easy access by the surgical team.\n\n' + \
'Q: Where can the secondary table be found, and what might it be used for?\n' + \
'A: The secondary table is part of the auxiliary equipment in the operating room and may be used to hold additional surgical instruments or supplies needed during the procedure.\n',
'count_generate': 'You are an AI visual assistant, and you are looking at a picture of many surgical tools.'
}
user_prompt = {
'count_generate': """
## Information
- You will receive a list of dictionaries of annotated tools that can be seen on the table.
- Note that each dictionary contains "name":"bbox", "name is the name of the surgical tool that can be seen on the table, and "bbox" is the numerical value of the corresponding surgical tool position (top left x, top left y, bottom right x, bottom right y.
- The list is as follows:
```json
{position}
```
## Task
Based on the list, Your task is to generate several questions and corresponding answers about counting surgical tools on the table.
## Example:
- "Question: How many scalpals are on the table? Answer: Two scalpels are on the table",
- "Question: How many forceps are on the table? Answer: There is no forcep on the table",
- "Question: How many surgical tools in total are on the table? Answer: There are 2 scalpals on the table and 3 tweezers on the table",
## Constraints
- Remeber, all the questions must can be clearly answered based on the information of given lists.
- Do not make up any questions and answers without solid evidence in the given lists.
- Importantly, you do not need to give any reasoning process, just give a straightforward answers.
- Do not use coordinates to generate answers and questions.
Now take a deep breath and start your response step by step.
"""
}
def repharse(results):
try:
results = results.split('\n\n')
repharse_list = []
for r in results:
split_temp = r.split('\n')
if len(split_temp) != 2 or \
split_temp[0][:2] != 'Q:' or \
split_temp[1][:2] != 'A:':
continue
r = r.split('Q: ')[1]
r = r.split('A: ')
qa_pair = {}
for i in range(len(r)):
if i == 0:
qa_pair['question'] = r[i][:-1]
elif i == 1:
qa_pair['answer'] = r[i]
repharse_list.append(qa_pair)
except:
repharse_list = []
return repharse_list
def caption_repharse(results):
try:
caption = ""
results = results.split('\n\n')
if len(results) == 2:
caption = results[1]
return caption
except:
return ""
def get_output_data(data, output_data, indices, instruction):
if instruction == 'question_rephrase':
return data[:indices[-1] + 1].copy()
elif instruction == 'count_generate':
return output_data.copy()
def clean_caption(file):
output_name = file.replace('.json', '_rephrased.json')
with open(file, 'r') as f:
data = json.load(f)
for i in range(len(data)):
caption = data[i]['caption']
caption = caption.replace('\n\n', ' ').replace('\n', ' ')
caption = caption.replace('\u2019', '\'').replace('\u2013', '-')
caption = caption.replace('### ', '').replace('## ', '').replace('###', '').replace('##', '')
caption = caption.replace('**', '')
caption = caption.replace('- ', '')
data[i]['caption'] = caption
with open(output_name, 'w') as f:
json.dump(data[:i+1], f, indent=4)
if __name__=='__main__':
file_list = ['./data/caption_dataset_pwiseg_0710.json']
for file in file_list:
clean_caption(file)