lyclyc52's picture
Update: add distributed inference code
f168581
from openai import OpenAI
from utils import repharse
from tqdm import tqdm
import json
import os
def get_result(prompt, instruction, client, base64_image=None, ):
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": instruction},
{"role": "user", "content":
[
{"type": "text", "text": prompt}
]
},
],
stream=False,
temperature=0.0
)
return response.choices[0].message.content
def get_result_video(prompt, instruction, client, base64_image=None, ):
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are generating a video summary. Please provide a summary of the video. Respond in Markdown."},
{"role": "user", "content": [
"These are the frames from the video.",
*map(lambda x: {"type": "image_url",
"image_url": {"url": f'data:image/jpg;base64,{x}', "detail": "low"}}, base64Frames)
],
}
],
temperature=0,
)
def main():
instruction = f'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'
client = OpenAI(api_key="sk-sqilImjoPeMWLosGC7EbB5Dc215d4320BbDa49C59f73Eb85",
base_url="https://vip.yi-zhan.top/v1")
file_list = ['./data/4dor_recognition_dataset_0702.json']
for file in file_list:
with open(file, 'r') as f:
data = json.load(f)
output_name = file.replace('.json', '_rephrased.json')
existing_data = []
if os.path.exists(output_name):
with open(output_name, 'r') as f:
existing_data = json.load(f)
# data[:len(existing_data)] = existing_data
# start_index = len(existing_data)
start_index = 0
for i in tqdm(range(start_index, len(data))):
prompt = data[i]['caption']
results = get_result(prompt, instruction, client)
data[i]['caption'] = repharse(results)
exit()
with open(output_name, 'w') as f:
json.dump(data[:i+1], f, indent=4)
if __name__ == '__main__':
main()