| 6 | from tqdm import tqdm |
| 7 | |
| 8 | def process_entry(model, client, entry): |
| 9 | messages = [ |
| 10 | { |
| 11 | "role": "system", |
| 12 | "content": "You are an expert in affective computing and very good at handling tasks related to emotion recognition." |
| 13 | }, |
| 14 | { |
| 15 | "role": "user", |
| 16 | "content": f"Given the following answer about the emotion conveyed in a video, " |
| 17 | f"please help me extract several key steps from the answer. Each step should be as concise as " |
| 18 | f"possible and prefixed with Step X. In the last step, showcase the predicted emotion. If there " |
| 19 | f"are no reasoning steps, please showcase the predicted emotion at Step 1. Enclose your result " |
| 20 | f"within <step></step> tags.\n" |
| 21 | f"answer: '{entry['model_response']}'\n" |
| 22 | f"Example 1:\n" |
| 23 | f"answer: In the video, an elderly man wearing a green shirt is in an outdoor nighttime setting. He appears focused and serious, with slightly " |
| 24 | f"furrowed brows and a serious expression. His eyes are scanning the other person, suggesting he wants to convey something important or ask about " |
| 25 | f"a certain thing. As time passes, his gaze shifts from scanning to direct engagement, eventually relaxing and showing a hint of happiness, " |
| 26 | f"indicating fluctuations in his emotions. Overall, the elderly man experiences a moderate intensity of neutral emotion, mixed with slight " |
| 27 | f"positive fluctuations.\n" |
| 28 | f"result: <step>Step 1: Observe the setting and characters in the video, noting any relevant details such as location and participants' appearance. " |
| 29 | f"Step 2: Analyze the facial expressions and movements, such as furrowed brows and downturned eyes, looking for indicators of specific emotions. " |
| 30 | f"Step 3: Consider verbal and non-verbal cues, including mouth movements and gaze direction, that signify communication and emotional responses. " |
| 31 | f"Step 4: Synthesize observations to understand the overall emotional state conveyed by the participant. " |
| 32 | f"Step 5: Based on the analysis, determine that the predicted emotion is neutral.</step>\n" |
| 33 | f"Example 2:\n" |
| 34 | f"answer: happy\n" |
| 35 | f"result: <step>Step 1: The predicted emotion is happy.</step>" |
| 36 | } |
| 37 | ] |
| 38 | |
| 39 | max_retries = 10 |
| 40 | for attempt in range(max_retries): |
| 41 | try: |
| 42 | response = client.chat.completions.create( |
| 43 | model=model, |
| 44 | messages=messages, |
| 45 | temperature=0.0 |
| 46 | ) |
| 47 | result = response.choices[0].message.content |
| 48 | entry['step'] = result |
| 49 | return True |
| 50 | |
| 51 | except Exception as e: |
| 52 | if attempt < max_retries - 1: |
| 53 | time.sleep(2) |
| 54 | else: |
| 55 | entry['step'] = f"Error: {str(e)}" |
| 56 | return False |
| 57 | return False |
| 58 | |
| 59 | def process_json_file(input_file, output_file, model_name, api_key, base_url): |
| 60 | |