Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

instruct_postprocess.py 8.5 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  1. import json
  2. import copy
  3. import random
  4. import argparse
  5. def pairwise(iterable):
  6. a = iter(iterable)
  7. return zip(a, a)
  8. def format_conv(x):
  9. turns = [turn for turn in x['result'].split('User:')]
  10. processed_turns = []
  11. try:
  12. for turn in turns[1:]:
  13. try:
  14. user_msg, assistant_msg = turn.split('Assistant:')
  15. except ValueError as e:
  16. continue
  17. processed_turns.append(user_msg.strip())
  18. processed_turns.append(assistant_msg.strip())
  19. if bool(random.getrandbits(1)):
  20. processed_turns[0] = processed_turns[0]+'\n<image>'
  21. else:
  22. processed_turns[0] = '<image>\n'+processed_turns[0]
  23. except:
  24. raise(Exception(x['result']))
  25. conversations = []
  26. for user_msg, assistant_msg in pairwise(processed_turns):
  27. conversations.extend([
  28. {
  29. 'from': 'human',
  30. 'value': user_msg,
  31. },
  32. {
  33. 'from': 'gpt',
  34. 'value': assistant_msg,
  35. }
  36. ])
  37. return {
  38. 'id': x['pair_id'],
  39. 'image': x['pair_id']+'.jpg',
  40. 'domain': x['domain'],
  41. 'conversations': conversations,
  42. }
  43. def clean_conv(x):
  44. x_copy = copy.deepcopy(x)
  45. conversations = []
  46. for human_turn, gpt_turn in pairwise(x['conversations']):
  47. if any(key in gpt_turn['value'].lower() for key in ['having access', 'without access', 'have access', 'access to the image', 'access to the actual image', 'without the actual image', 'not mentioned in the figure caption', 'not mentioned in the caption', 'not provided in the figure caption', 'not specified in the caption', 'not specified in the image caption', 'without seeing the', 'cannot see the', 'unable to view', 'unable to provide specific details', 'sorry', 'cannot directly view', 'cannot view the image', 'image itself is not available', 'image is not available']):
  48. continue
  49. should_continue = False
  50. for key in ['according to the description, ', 'based on the description, ', 'based on the description provided, ', 'based on the image description, ', 'according to the image description, ', 'while I cannot see the actual image, based on the description, ', 'yes, according to the image description, ', 'no, according to the image description, ', 'i cannot see the image, but based on the description, ', 'based on the figure caption, ', 'based on the caption, ', 'according on the figure caption, ', 'according on the caption, ', 'as an ai, i am unable to view the actual image. However, based on the figure caption, ', 'the figure caption describes ', 'yes, the figure caption mentions ', 'no, the figure caption mentions ', 'the figure caption mentions ', 'the caption mentions ', 'yes, according to the figure caption, ', 'no, according to the figure caption, ', 'according to the figure caption, ', 'the image description suggests that ', 'yes, based on the description, ', 'no, based on the description, ', 'according to the description provided, ', 'yes, according to the description, ', 'no, according to the description, ', 'the outcome mentioned in the description is that ', 'the description suggests that ', 'in the provided context, ', 'based on the provided context, ', 'according to the context provided, ', 'based on the context provided, ', 'yes, the context provided indicates that ', 'no, the context provided indicates that ', 'the context provided indicates that ', 'yes, based on the context provided, ', 'no, based on the context provided, ', 'yes, according to the context provided, ', 'no, according to the context provided, ', 'according to the context provided, ', 'the context provided suggests that ', 'yes, the context provided suggests that ', 'based on the image and the context provided, ', 'the context provided mentions that ', 'yes, the context provided mentions that ', 'yes, the context provided mentions ', 'the context provided mentions ', 'the image and context provided suggest that ', 'according to the provided context, ', 'according to the image and the provided context, ', 'based on the image and the provided context, ', 'yes, according to the figure and the provided context, ', 'according to the figure and the provided context, ', 'the provided context mentions that ', 'no, the image and the provided context indicate that ', 'the image, along with the provided context, suggests that ', 'based on the mri and the provided context, ', 'based on the ct image and the provided context, ', 'the histology image and the provided context suggest that ', 'based on the ct scan and the context provided, ', 'based on the information provided, ', 'based on the histologic section and the provided context, ', 'yes, there is another finding mentioned in the context, ', 'based on the histopathological features and the context provided, ']:
  51. if gpt_turn['value'].lower().startswith(key):
  52. gpt_turn['value'] = gpt_turn['value'][len(key):].capitalize()
  53. if not any(key in gpt_turn['value'] for key in ['description', 'caption']):
  54. conversations.extend([human_turn, gpt_turn])
  55. should_continue = True
  56. break
  57. if should_continue:
  58. continue
  59. for key in [', as mentioned in the description', ', as mentioned in the image description', ', but based on the description', ', as mentioned in the figure caption', ' as mentioned in the figure caption', ' as mentioned in the caption', ' based on the figure caption,', ' mentioned in the figure caption', ' mentioned in the caption', 'In the context of the figure caption, ', ' the image caption suggests that', ' the caption suggests that', ' the figure caption suggests that', 'In the context of the image caption, ', ', as indicated by the image description', ', as indicated in the image description', 'based on the provided context, ', ', as described in the provided context', 'In the context of the image provided, ', 'In the context provided, ', ', as suggested by the context provided', ', based on the context provided', ', but based on the context provided', 'based on the context provided, ', 'It is important to note that the context provided indicates that ', ', as mentioned in the context provided', ', which is the context provided', 'According to the context provided, ', ', which is a characteristic appearance in the context provided', 'the context provided suggests that ', 'The image and context provided suggest that ', ', considering the context provided', ', which is also mentioned in the context provided', 'Based on the context provided, ', 'The context provided only mentions that ', 'The context provided suggests that ', ', given the context provided', ' the context provided mentions', 'The context provided mentions ', ' found in the context provided', 'In the provided context, ', 'Based on the provided context, ', 'According to the provided context, ', ', as mentioned in the provided context', ', according to the provided context', ' can be found in the provided context', ' in the provided context', ', as indicated by the provided context', 'In the context of the provided image, ', 'In the context of the chest X-ray provided, ', 'In the context of the provided information, ', 'In the context of the MRI image provided, ', ', as described in the context', 'the additional context provided indicates that ', 'In the context of the information provided, ', 'In the context of the CT scan provided, ', 'In the context of the provided MRI image, ', ', in the context provided', 'The context provided indicates that ', ' in the context of the provided information', 'In the context of the CT scans provided, ']:
  60. if key in gpt_turn['value']:
  61. gpt_turn['value'] = gpt_turn['value'].replace(key, '')
  62. should_continue = True
  63. # break
  64. if should_continue:
  65. if not any(key in gpt_turn['value'] for key in ['description', 'caption']):
  66. conversations.extend([human_turn, gpt_turn])
  67. continue
  68. if any(key in gpt_turn['value'] for key in ['description', 'caption']):
  69. continue
  70. conversations.extend([human_turn, gpt_turn])
  71. if conversations:
  72. x_copy['conversations'] = conversations
  73. return [x_copy]
  74. return []
  75. def main(args):
  76. input_data = []
  77. with open(args.input_path) as f:
  78. input_data = json.load(f)
  79. result = []
  80. for input in input_data:
  81. formatted_conv = format_conv(input)
  82. result.extend(clean_conv(formatted_conv))
  83. with open(args.output_path, 'w') as f:
  84. json.dump(result, f, indent=2)
  85. if __name__ == '__main__':
  86. parser = argparse.ArgumentParser()
  87. parser.add_argument('--input_path', type=str, default='data/instruct/llava_med_instruct_fig_captions_gen.jsonl')
  88. parser.add_argument('--output_path', type=str, default='data/instruct/llava_med_instruct_fig_captions_post.jsonl')
  89. args = parser.parse_args()
  90. main(args)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...