| import os |
| import sys |
| import json |
| import base64 |
| import re |
| import asyncio |
| import aiofiles |
| from tqdm.asyncio import tqdm_asyncio |
| import boto3 |
| from rouge import Rouge |
|
|
| Test_Model = "Claude" |
|
|
| |
| TEST_JSON_PATH = "/code/CogReasoner/Test/VisualWebBench_webqa.json" |
| MODEL_NAME = "us.anthropic.claude-sonnet-4-20250514-v1:0" |
| MAX_SAMPLE = 245 |
| MAX_CONCURRENT_REQUESTS = 1 |
| ACCURACY_PRINT_INTERVAL = 10 |
| OUTPUT_JSON_PATH = f"/code/CogReasoner/Code/Evalaute/Result/Test-{Test_Model}-VisualWebBench_WebQA.json" |
| FIXED_PROMPT = ( |
| "You are given a screenshot of a webpage. Please generate the main text within the screenshot, " |
| "which can be regarded as the heading of the webpage.\n\n" |
| "You should directly tell me the main content, and do not output any explanation or any other contents." |
| ) |
| |
| class BedrockClaudeClient: |
| """ |
| A client for interacting with the Claude model on AWS Bedrock. |
| AWS credentials are configured directly in this code (for demonstration). |
| """ |
| def __init__(self, access_key, secret_key, region_name, model_id): |
| """ |
| Initializes the Bedrock runtime client with provided keys and region info. |
| """ |
| self.model_id = model_id |
| |
| try: |
| self.bedrock_client = boto3.client( |
| service_name='bedrock-runtime', |
| region_name=region_name, |
| aws_access_key_id=access_key, |
| aws_secret_access_key=secret_key |
| ) |
| print(f"Boto3 client successfully created in region '{region_name}' for model '{self.model_id}'!") |
| except Exception as e: |
| raise ConnectionError(f"Failed to create Bedrock client: {e}. Please check your AWS credentials and region name.") |
|
|
| def _parse_data_url(self, data_url): |
| """ |
| Parses a Data URL (e.g., data:image/png;base64,iVBOR...) |
| Extracts media_type and Base64 data. |
| """ |
| if not data_url.startswith("data:"): |
| print(f"Warning: Not a standard Data URL format: {data_url}") |
| return None, None |
|
|
| parts = data_url.split(',', 1) |
| if len(parts) < 2: |
| print(f"Warning: Incomplete Data URL format: {data_url}") |
| return None, None |
|
|
| metadata = parts[0][len("data:"):].split(';') |
| media_type = metadata[0] |
| base64_data = parts[1] |
|
|
| if "base64" not in metadata: |
| print(f"Warning: Data URL does not contain 'base64' encoding identifier: {data_url}") |
| return None, None |
|
|
| return base64_data, media_type |
|
|
| |
| |
| def chat(self, messages, max_tokens=1024, temperature=0.7): |
| """ |
| Sends messages to the Claude model and gets a reply. |
| This function is now fully compatible with OpenAI-format message lists, |
| including handling system messages and embedded base64 image_url. |
| """ |
| if not hasattr(self, 'bedrock_client'): |
| raise RuntimeError("Bedrock client not successfully initialized.") |
|
|
| claude_system_message = None |
| claude_messages_payload = [] |
|
|
| |
| for openai_msg in messages: |
| role = openai_msg.get("role") |
| content = openai_msg.get("content") |
|
|
| if role == "system": |
| claude_system_message = content |
| elif role in ["user", "assistant"]: |
| claude_content_blocks = [] |
| if isinstance(content, str): |
| claude_content_blocks.append({"type": "text", "text": content}) |
| elif isinstance(content, list): |
| for item in content: |
| if item.get("type") == "text": |
| claude_content_blocks.append({"type": "text", "text": item.get("text", "")}) |
| elif item.get("type") == "image_url": |
| image_url_dict = item.get("image_url", {}) |
| url = image_url_dict.get("url") |
| |
| if url: |
| base64_data, media_type = self._parse_data_url(url) |
| if base64_data and media_type: |
| claude_content_blocks.append({ |
| "type": "image", |
| "source": { |
| "type": "base64", |
| "media_type": media_type, |
| "data": base64_data |
| } |
| }) |
| else: |
| print(f"Warning: Could not parse image data from Data URL {url}, skipping this content block.") |
| else: |
| print(f"Warning: Unsupported OpenAI content type: {item.get('type')}. Skipping this content block.") |
| |
| if claude_content_blocks: |
| claude_messages_payload.append({"role": role, "content": claude_content_blocks}) |
| else: |
| print(f"Warning: '{role}' role message has no valid content, skipping.") |
|
|
| else: |
| print(f"Warning: Unsupported OpenAI message role: {role}. Skipping this message.") |
|
|
| if not claude_messages_payload: |
| raise ValueError("No valid 'user' or 'assistant' role messages to send to Claude after conversion.") |
|
|
| |
| body = { |
| "anthropic_version": "bedrock-2023-05-31", |
| "max_tokens": max_tokens, |
| "temperature": temperature, |
| "messages": claude_messages_payload |
| } |
| |
| if claude_system_message: |
| body["system"] = claude_system_message |
|
|
| try: |
| response = self.bedrock_client.invoke_model( |
| modelId=self.model_id, |
| body=json.dumps(body) |
| ) |
|
|
| response_body = json.loads(response.get('body').read()) |
| |
| response_text = "" |
| if response_body.get('content'): |
| for content_block in response_body['content']: |
| if content_block.get('type') == 'text': |
| response_text += content_block['text'] |
|
|
| usage = response_body.get('usage', {}) |
| prompt_tokens = usage.get('input_tokens', 0) |
| completion_tokens = usage.get('output_tokens', 0) |
|
|
| return { |
| "response_text": response_text, |
| "prompt_tokens": prompt_tokens, |
| "completion_tokens": completion_tokens |
| } |
| except Exception as e: |
| error_message = str(e) |
| if hasattr(e, 'response') and 'Error' in e.response: |
| error_message = f"{e.response['Error'].get('Code', '')}: {e.response['Error'].get('Message', '')}" |
| raise RuntimeError(f"Error calling Claude model: {error_message}") |
|
|
|
|
| def eval_webqa(preds, golds, **kwargs): |
| """ |
| 计算 WebQA 的 F1 分数。 |
| preds: 预测答案的列表。 |
| golds: 参考答案的列表的列表 (每个问题可以有多个参考答案)。 |
| """ |
| assert len(preds) == len(golds), "预测数量和参考答案数量必须一致" |
| f1_scores = [] |
| |
| rouge = Rouge(metrics=['rouge-1']) |
| for pred, gold_list in zip(preds, golds): |
| if not pred: |
| pred = " " |
| |
| |
| |
| try: |
| current_f1 = max([rouge.get_scores([pred], [gold], avg=True)['rouge-1']['f'] for gold in gold_list]) |
| f1_scores.append(current_f1) |
| except Exception as e: |
| |
| print(f"Warning: Could not compute F1 score for pred='{pred}' and gold_list='{gold_list}'. Error: {e}") |
| f1_scores.append(0.0) |
|
|
| |
| if not f1_scores: |
| return dict(f1=0.0) |
|
|
| return dict( |
| f1=sum(f1_scores) / len(f1_scores) * 100 |
| ) |
|
|
|
|
|
|
| |
| async def process_item(index, item, sem, claude_client_instance, stats): |
| async with sem: |
| image_path = item["images"][0] |
| |
| |
| |
| |
| ground_truth = [item["messages"][1]["content"].strip()] |
| |
| user_prompt = item["messages"][0]["content"] |
|
|
| |
| async with aiofiles.open(image_path, "rb") as f: |
| content = await f.read() |
| encoded_image = base64.b64encode(content).decode("utf-8") |
| image_data_uri = f"data:image/png;base64,{encoded_image}" |
| prompt_text = user_prompt.replace("<image>\n", "").strip() |
| |
| messages =[ |
| {"role": "system", "content": "You are a helpful assistant."}, |
| { |
| "role": "user", |
| "content": [ |
| {"type": "image_url", "image_url": {"url": image_data_uri}}, |
| {"type": "text", "text": prompt_text}, |
| ], |
| }, |
| ] |
| try: |
| |
| |
| |
| response_data = await asyncio.to_thread( |
| claude_client_instance.chat, |
| messages = messages, |
| max_tokens=2048, |
| temperature=0.1 |
| ) |
| pred_text = response_data['response_text'].strip() |
| except Exception as e: |
| pred_text = f"[ERROR] {str(e)}" |
| await asyncio.sleep(10) |
| |
| return { |
| "image": image_path, |
| "ground_truth": ground_truth, |
| "prediction": pred_text, |
| } |
|
|
| |
| async def main(): |
| """ |
| Main execution function, responsible for loading test data, creating and running |
| asynchronous tasks, collecting results, and saving them. |
| """ |
| |
| |
| |
| aws_access_key_id = "AKIAYEDGY53YI74GRHPL" |
| aws_secret_access_key = "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" |
| aws_region_name = "us-east-1" |
| aws_model_id = MODEL_NAME |
|
|
| |
| try: |
| claude_client = BedrockClaudeClient( |
| access_key=aws_access_key_id, |
| secret_key=aws_secret_access_key, |
| region_name=aws_region_name, |
| model_id=aws_model_id |
| ) |
| except Exception as e: |
| print(f"Failed to initialize Bedrock Claude client: {e}") |
| sys.exit(1) |
|
|
| |
| with open(TEST_JSON_PATH, "r", encoding="utf-8") as f: |
| test_data = json.load(f)[:MAX_SAMPLE] |
|
|
| sem = asyncio.Semaphore(MAX_CONCURRENT_REQUESTS) |
| stats = {"total": 0, "correct": 0} |
|
|
| tasks = [process_item(i, item, sem, claude_client, stats) for i, item in enumerate(test_data)] |
|
|
| print(f"\n🚀 Starting evaluation for WebQA on {len(tasks)} samples...\n") |
| results = await tqdm_asyncio.gather(*tasks) |
|
|
| predictions = [r["prediction"] for r in results] |
| references = [r["ground_truth"] for r in results] |
|
|
| |
| |
| metrics = eval_webqa(predictions, references) |
|
|
| output = { |
| "task": "WebQA", |
| "model": Test_Model, |
| "metrics": metrics, |
| "results": results, |
| } |
|
|
| |
| os.makedirs(os.path.dirname(OUTPUT_JSON_PATH), exist_ok=True) |
| with open(OUTPUT_JSON_PATH, "w", encoding="utf-8") as f: |
| json.dump(output, f, indent=2, ensure_ascii=False) |
|
|
| print(f"\n✅ Evaluation Complete!") |
| print(f"📊 Metrics: {json.dumps(metrics, indent=2)}") |
| print(f"📁 Results saved at: {OUTPUT_JSON_PATH}") |
|
|
|
|
| |
| if __name__ == "__main__": |
| asyncio.run(main()) |
| sys.exit(0) |