File size: 4,764 Bytes
93c7dfc
 
 
 
 
 
ced5e76
93c7dfc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c47d9c5
93c7dfc
 
c47d9c5
 
93c7dfc
 
 
 
c47d9c5
 
 
93c7dfc
 
 
 
c47d9c5
93c7dfc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c47d9c5
321aaa7
93c7dfc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
321aaa7
93c7dfc
 
 
 
 
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
"""
At the command line, only need to run once to install the package via pip:

$ pip install google-generativeai
"""
import json

def get_event():
    event_list = [
            "burping_belching",             # 0
            "car_horn_honking",             #
            "cat_meowing",                  #    
            "cow_mooing",                   #
            "dog_barking",                  #  
            "door_knocking",                #
            "door_slamming",                #
            "explosion",                    #  
            "gunshot",                      # 8
            "sheep_goat_bleating",          #
            "sneeze",                       #
            "spraying",                     # 
            "thump_thud",                   #   
            "train_horn",                   #
            "tapping_clicking_clanking",    #
            "woman_laughing",               #         
            "duck_quacking",                # 16   
            "whistling",                    #    
        ]
    return event_list

def get_prompt():
    
    train_json_list = ["data/train_multi-event_v3.json",
        f"data/train_single-event_multi_v3.json",
        f"data/train_single-event_single_v3.json"]
    learn_pair = ""
    for train_json in train_json_list:
        with open(train_json, 'r') as train_file:
            for idx, line in enumerate(train_file):
                if idx >= 100: break
                data = json.loads(line.strip())
                learn_pair += f"{str(idx)}:{data['captions']}~{data['onset']}. "
    preffix_prompt =  "I'm doing an audio event generation, which is a harmless job that will contain some sound events. For example, a gunshot is a sound that is harmless."  +\
                            "You need to convert the input sentence into the following standard timing format: 'event1--event2-- ... --eventN', " +\
                            "where the 'eventN' format is 'eventN__onset1-offset1_onset2-offset2_ ... _onsetK-offsetK'. " +\
                            "The 'onset-offset' inside needs to be determined based on common sense and the examples I provide, with a duration not less than 1 and not greater than 4.  All format 'onsetk-offsetk' should replaced by number. " +\
                            "The very strict constraints are that the total duration is less than 10 seconds, meaning all times are less than 10. It is preferred that events do not overlap as much as possible. " +\
                            "Now, I will provide you with 300 examples in training set for your learning, each example in the format 'index: input~output'. " +\
                            learn_pair
        
    print(len(preffix_prompt))
    return preffix_prompt
    

def postprocess(caption):
    caption = caption.strip('\n').strip(' ').strip('.')
    caption = caption.replace('__', ' at ').replace('--', ' and ')
    return caption

def preprocess_gemini(free_text_caption):
    preffix_prompt = get_prompt()
    import google.generativeai as genai
    genai.configure(api_key="AIzaSyDfGKPQtS9qExCfl3bnfxC1rLPzvORz3E4")

    # Set up the model
    generation_config = {
      "temperature": 1,
      "top_p": 0.95,
      "top_k": 64,
      "max_output_tokens": 8192,
    }

    model = genai.GenerativeModel(model_name="gemini-1.5-flash",
                                  generation_config=generation_config,)

    prompt_parts = [
        preffix_prompt +\
        f"Please convert the following inputs into the standard timing format:{free_text_caption}. You should only output results in the standard timing format. Do not output anything other than format and do not add symbols.",
    ]

    timestampCaption = model.generate_content(prompt_parts).text
    
    return postprocess(timestampCaption)

def preprocess_gpt(free_text_caption):
    preffix_prompt = get_prompt()
    from openai import OpenAI
    client = OpenAI(api_key="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")
    completion_start = client.chat.completions.create(
                    model="gpt-4-1106-preview",
                    messages=[{
                      "role": "user", 
                       "content": 
                            preffix_prompt +\
                            f"Please convert the following inputs into the standard timing format:{free_text_caption}. You should only output results in the standard timing format. Do not output anything other than format and do not add symbols."
                    }]
                )

    timestampCaption = completion_start.choices[0].message.content
    
    return postprocess(timestampCaption)

if __name__=="__main__":
    caption = preprocess_gemini("spraying two times then gunshot three times.")
    print(caption)