|
| 1 | +import json |
| 2 | +import argparse |
| 3 | +import pandas as pd |
| 4 | +import gradio as gr |
| 5 | + |
| 6 | +from vllm import LLM, SamplingParams |
| 7 | + |
| 8 | +from vllm_fusion_caption import StructuralCaptionDataset |
| 9 | + |
| 10 | +parser = argparse.ArgumentParser() |
| 11 | +parser.add_argument("--fusioncaptioner_model_path", default=None, type=str) |
| 12 | +parser.add_argument("--tensor_parallel_size", type=int, default=2) |
| 13 | +args = parser.parse_args() |
| 14 | + |
| 15 | +example_input = """ |
| 16 | +{ |
| 17 | + "subjects": [ |
| 18 | + { |
| 19 | + "TYPES": { |
| 20 | + "type": "Human", |
| 21 | + "sub_type": "Woman" |
| 22 | + }, |
| 23 | + "appearance": "Long, straight black hair with bangs, wearing a sparkling choker necklace and a dark-colored top or dress with a visible strap over her shoulder.", |
| 24 | + "action": "A woman wearing a sparkling choker necklace and earrings is sitting in a car, looking to her left and speaking. A man, dressed in a suit, is sitting next to her, attentively watching her.", |
| 25 | + "expression": "The individual in the video exhibits a neutral facial expression, characterized by slightly open lips and a gentle, soft-focus gaze. There are no noticeable signs of sadness or distress evident in their demeanor.", |
| 26 | + "position": "Seated in the foreground of the car, facing slightly to the right.", |
| 27 | + "is_main_subject": true |
| 28 | + }, |
| 29 | + { |
| 30 | + "TYPES": { |
| 31 | + "type": "Human", |
| 32 | + "sub_type": "Man" |
| 33 | + }, |
| 34 | + "appearance": "Short hair, wearing a dark-colored suit with a white shirt.", |
| 35 | + "action": "", |
| 36 | + "expression": "", |
| 37 | + "position": "Seated in the background of the car, facing the woman.", |
| 38 | + "is_main_subject": false |
| 39 | + } |
| 40 | + ], |
| 41 | + "shot_type": "close_up", |
| 42 | + "shot_angle": "eye_level", |
| 43 | + "shot_position": "side_view", |
| 44 | + "camera_motion": "", |
| 45 | + "environment": "Interior of a car with a dark color scheme.", |
| 46 | + "lighting": "Soft and natural lighting, suggesting daytime." |
| 47 | +} |
| 48 | +""" |
| 49 | + |
| 50 | +class FusionCaptioner: |
| 51 | + def __init__(self, model_path, tensor_parallel_size): |
| 52 | + self.model = LLM(model=model_path, |
| 53 | + gpu_memory_utilization=0.9, |
| 54 | + max_model_len=4096, |
| 55 | + tensor_parallel_size=tensor_parallel_size) |
| 56 | + self.sampling_params = SamplingParams( |
| 57 | + temperature=0.1, |
| 58 | + max_tokens=512, |
| 59 | + stop=['\n\n'] |
| 60 | + ) |
| 61 | + self.model_path = model_path |
| 62 | + |
| 63 | + def __call__(self, structural_caption, task='t2v'): |
| 64 | + if isinstance(structural_caption, dict): |
| 65 | + structural_caption = json.dumps(structural_caption, ensure_ascii=False) |
| 66 | + else: |
| 67 | + structural_caption = json.dumps(json.loads(structural_caption), ensure_ascii=False) |
| 68 | + meta = pd.DataFrame([structural_caption], columns=['structural_caption']) |
| 69 | + print(f'structural_caption: {structural_caption}') |
| 70 | + print(f'task: {task}') |
| 71 | + dataset = StructuralCaptionDataset(meta, self.model_path, task) |
| 72 | + _, fusion_by_llm, text, original_text, camera_movement = dataset[0] |
| 73 | + llm_original_texts = [] |
| 74 | + if not fusion_by_llm: |
| 75 | + caption = original_text + " " + camera_movement |
| 76 | + return caption |
| 77 | + try: |
| 78 | + outputs = self.model.generate([text], self.sampling_params, use_tqdm=False) |
| 79 | + result = outputs[0].outputs[0].text |
| 80 | + except Exception as e: |
| 81 | + result = llm_original_texts |
| 82 | + |
| 83 | + llm_caption = result + " " + camera_movement |
| 84 | + return llm_caption |
| 85 | + |
| 86 | +def main(): |
| 87 | + fusion_captioner = FusionCaptioner(args.fusioncaptioner_model_path, args.tensor_parallel_size) |
| 88 | + |
| 89 | + def fusion_caption(structural_caption, task): |
| 90 | + caption = fusion_captioner(structural_caption, task) |
| 91 | + return caption |
| 92 | + |
| 93 | + with gr.Blocks() as demo: |
| 94 | + gr.Markdown( |
| 95 | + """ |
| 96 | + <h1 style="text-align: center; font-size: 2em;">SkyCaptioner</h1> |
| 97 | + """, |
| 98 | + elem_id="header" |
| 99 | + ) |
| 100 | + |
| 101 | + with gr.Row(): |
| 102 | + with gr.Column(visible=True): |
| 103 | + with gr.Row(): |
| 104 | + json_input = gr.Code( |
| 105 | + label="Structural Caption", |
| 106 | + language="json", |
| 107 | + lines=25, |
| 108 | + interactive=True |
| 109 | + ) |
| 110 | + with gr.Row(): |
| 111 | + task_input = gr.Radio( |
| 112 | + label="Task", |
| 113 | + choices=["t2v", "i2v"], |
| 114 | + value="t2v", |
| 115 | + interactive=True |
| 116 | + ) |
| 117 | + |
| 118 | + with gr.Column(visible=True): |
| 119 | + text_output = gr.Textbox( |
| 120 | + label="Fusion Caption", |
| 121 | + lines=25, |
| 122 | + interactive=False, |
| 123 | + autoscroll=True |
| 124 | + ) |
| 125 | + |
| 126 | + gr.Button("Generate").click( |
| 127 | + fn=fusion_caption, |
| 128 | + inputs=[json_input, task_input], |
| 129 | + outputs=text_output |
| 130 | + ) |
| 131 | + with gr.Row(): |
| 132 | + gr.Examples( |
| 133 | + examples=[ |
| 134 | + [example_input, "t2v"], |
| 135 | + ], |
| 136 | + inputs=[json_input, task_input], |
| 137 | + label="Example Input" |
| 138 | + ) |
| 139 | + demo.launch( |
| 140 | + server_name="0.0.0.0", |
| 141 | + server_port=7863, |
| 142 | + share=False |
| 143 | + ) |
| 144 | + |
| 145 | +if __name__ == '__main__': |
| 146 | + main() |
0 commit comments