|
2 | 2 | # Copyright (C) 2023-2025 Intel Corporation |
3 | 3 | # SPDX-License-Identifier: Apache-2.0 |
4 | 4 |
|
| 5 | +def create_base_prompt(json_data, key='prompt'): |
| 6 | + prompt_data = {} |
| 7 | + if key in json_data: |
| 8 | + if json_data[key] != "": |
| 9 | + prompt_data = json_data[key] |
| 10 | + else: raise RuntimeError(f"== {key} should not be empty string ==") |
| 11 | + else: raise RuntimeError(f"== key word '{key}' does not exist ==") |
| 12 | + return prompt_data |
5 | 13 |
|
6 | 14 | def parse_text_json_data(json_data_list): |
7 | 15 | text_param_list = [] |
8 | 16 | for json_data in json_data_list: |
9 | | - if 'prompt' in json_data: |
10 | | - if json_data['prompt'] != '': |
11 | | - text_param_list.append(json_data['prompt']) |
12 | | - else: |
13 | | - raise RuntimeError('== prompt should not be empty string ==') |
14 | | - else: |
15 | | - raise RuntimeError('== key word "prompt" does not exist ==') |
| 17 | + prompt_data = create_base_prompt(json_data) |
| 18 | + text_param_list.append(prompt_data["prompt"]) |
16 | 19 | return text_param_list |
17 | 20 |
|
18 | 21 |
|
19 | 22 | def parse_vlm_json_data(json_data_list): |
20 | 23 | text_param_list = [] |
21 | 24 | for json_data in json_data_list: |
22 | | - prompt_data = {} |
23 | | - if 'prompt' in json_data: |
24 | | - if json_data['prompt'] != '': |
25 | | - prompt_data["prompt"] = json_data['prompt'] |
26 | | - else: |
27 | | - raise RuntimeError('== prompt should not be empty string ==') |
28 | | - else: |
29 | | - raise RuntimeError('== key word "prompt" does not exist ==') |
| 25 | + prompt_data = create_base_prompt(json_data) |
| 26 | + assert ("media" in json_data) ^ ("video" in json_data) |
30 | 27 | if "media" in json_data: |
31 | 28 | prompt_data["media"] = json_data["media"] |
| 29 | + if "video" in json_data: |
| 30 | + prompt_data["video"] = json_data["video"] |
32 | 31 | text_param_list.append(prompt_data) |
33 | 32 | return text_param_list |
34 | 33 |
|
35 | 34 |
|
36 | 35 | def parse_image_json_data(json_data_list): |
37 | 36 | image_param_list = [] |
38 | | - for data in json_data_list: |
39 | | - image_param = {} |
40 | | - if 'prompt' in data: |
41 | | - if data['prompt'] != '': |
42 | | - image_param['prompt'] = data['prompt'] |
43 | | - else: |
44 | | - raise RuntimeError('== prompt should not be empty string ==') |
45 | | - else: |
46 | | - raise RuntimeError('== key word "prompt" does not exist in prompt file ==') |
47 | | - if 'width' in data: |
48 | | - image_param['width'] = int(data['width']) |
49 | | - if 'height' in data: |
50 | | - image_param['height'] = int(data['height']) |
51 | | - if 'steps' in data: |
52 | | - image_param['steps'] = int(data['steps']) |
53 | | - if 'guidance_scale' in data: |
54 | | - image_param['guidance_scale'] = float(data['guidance_scale']) |
55 | | - if 'media' in data: |
56 | | - image_param['media'] = data['media'] |
57 | | - if 'mask_image' in data: |
58 | | - image_param['mask_image'] = data['mask_image'] |
| 37 | + for json_data in json_data_list: |
| 38 | + image_param = create_base_prompt(json_data) |
| 39 | + if 'width' in json_data: |
| 40 | + image_param['width'] = int(json_data['width']) |
| 41 | + if 'height' in json_data: |
| 42 | + image_param['height'] = int(json_data['height']) |
| 43 | + if 'steps' in json_data: |
| 44 | + image_param['steps'] = int(json_data['steps']) |
| 45 | + if 'guidance_scale' in json_data: |
| 46 | + image_param['guidance_scale'] = float(json_data['guidance_scale']) |
| 47 | + if 'media' in json_data: |
| 48 | + image_param['media'] = json_data['media'] |
| 49 | + if 'mask_image' in json_data: |
| 50 | + image_param['mask_image'] = json_data['mask_image'] |
59 | 51 | image_param_list.append(image_param) |
60 | 52 | return image_param_list |
61 | 53 |
|
62 | 54 |
|
63 | 55 | def parse_speech_json_data(json_data_list): |
64 | 56 | speech_param_list = [] |
65 | 57 | for json_data in json_data_list: |
66 | | - speech_param = {} |
67 | | - if 'media' in json_data: |
68 | | - if json_data['media'] != '': |
69 | | - speech_param['media'] = json_data['media'] |
70 | | - else: |
71 | | - raise RuntimeError('== media path should not be empty string ==') |
72 | | - else: |
73 | | - raise RuntimeError('== key word "media" does not exist ==') |
74 | | - if 'language' in json_data: |
75 | | - speech_param['language'] = json_data['language'] |
76 | | - if 'timestamp' in json_data: |
77 | | - speech_param['timestamp'] = json_data['timestamp'] |
| 58 | + speech_param = create_base_prompt(json_data, "media") |
| 59 | + if "language" in json_data: |
| 60 | + speech_param["language"] = json_data["language"] |
| 61 | + if "timestamp" in json_data: |
| 62 | + speech_param["timestamp"] = json_data["timestamp"] |
78 | 63 | speech_param_list.append(speech_param) |
79 | 64 | return speech_param_list |
0 commit comments