Datasets:
Download server/serve.py from jingene/CMESpectrumBench: direct link, hf CLI and curl.
- Browser
- Download file 16 kB
-
https://huggingface.co/datasets/jingene/CMESpectrumBench/resolve/main/server/serve.py
- Command line
-
hf download hf://datasets/jingene/CMESpectrumBench/server/serve.py
-
curl -L -o serve.py https://huggingface.co/datasets/jingene/CMESpectrumBench/resolve/main/server/serve.py
16 kB
| import asyncio | |
| from tqdm import tqdm | |
| from query import ask_model, ask_model_with_code_interpreter # now an async function | |
| from db import append_result, safe_key | |
| import os | |
| import json | |
| import aiofiles | |
| import httpx | |
| async def run_model_prompt_queries( | |
| models: list, | |
| prompts: list, | |
| titles: list, | |
| num_elems: list, | |
| num_repeats, | |
| progress_cb = None, | |
| error_cb = None, | |
| max_workers: int = 100000, | |
| ): | |
| prompt_to_title = dict(zip(prompts, titles)) | |
| cache = {} | |
| # semaphore to cap concurrent ask_model calls | |
| sem = asyncio.Semaphore(max_workers) | |
| async def bound_ask(model, prompt, num_elem, title): | |
| async with sem: | |
| try: | |
| out = await ask_model(model, prompt, cache, title, num_elem) | |
| except Exception as e: | |
| print(f"Error for model={model}, {e}, {title}") | |
| return None, model, prompt | |
| print(f"Model: {model}, out: {out}, title: {title}") | |
| return out, model, prompt | |
| # prepare all (model, prompt, num_elem) triples | |
| if isinstance(num_repeats, list): | |
| all_tasks = [ | |
| (m, p, n, t) | |
| for j, (p, n, t) in enumerate(zip(prompts, num_elems, titles)) | |
| for i, m in enumerate(models) | |
| for _ in range(num_repeats[i][j]) | |
| ] | |
| else: | |
| all_tasks = [ | |
| (m, p, n, t) | |
| for p, n, t in zip(prompts, num_elems, titles) | |
| for _ in range(num_repeats) | |
| for m in models | |
| ] | |
| tasks = [] | |
| for idx, (model, prompt, num_elem, title) in enumerate(all_tasks): | |
| # buffer initial semaphore load | |
| tasks.append(asyncio.create_task(bound_ask(model, prompt, num_elem, title))) | |
| for fut in tqdm(asyncio.as_completed(tasks), total=len(tasks), desc="Processing"): | |
| output, model, prompt = await fut | |
| if output: | |
| title = prompt_to_title[prompt] | |
| try: | |
| # Firebase | |
| append_result(title, model, output) | |
| # Local disk | |
| save_dir = "./outputs" | |
| os.makedirs(save_dir, exist_ok=True) | |
| filename = f"{title}-{safe_key(model)}.jsonl" | |
| filepath = os.path.join(save_dir, filename) | |
| async with aiofiles.open(filepath, mode="a") as f: | |
| await f.write(json.dumps(output) + "\n") | |
| except Exception as e: | |
| print(f"Error saving output for {title} - {model}: {e}") | |
| if progress_cb: | |
| await progress_cb() | |
| else: | |
| if error_cb: | |
| await error_cb() | |
| return cache | |
| def resolve_file_path(file_path: str) -> str: | |
| """ | |
| Resolve file paths to absolute paths. | |
| Relative paths are placed under server_dir/Data_csv/Plots/ | |
| Args: | |
| file_path: Relative (e.g., "A1/file.csv") or absolute file path | |
| Returns: | |
| Absolute file path | |
| """ | |
| # If already absolute, return as-is | |
| if os.path.isabs(file_path): | |
| return file_path | |
| # Get the directory where serve.py is located (server/) | |
| server_dir = os.path.dirname(os.path.abspath(__file__)) | |
| # Prepend Data_csv/Plots/ to relative paths, then make absolute | |
| return os.path.join(server_dir, "Data_csv", "Plots", file_path) | |
| # Define model sets for checking which APIs need file uploads | |
| GEMINI_MODELS = { | |
| "gemini-2.0-flash", | |
| "gemini-2.0-flash-lite-preview-02-05", | |
| "gemini-3-pro-preview", | |
| "gemini-3-flash-preview", | |
| "gemini-2.5-pro", | |
| "gemini-2.5-flash", | |
| } | |
| OPENAI_CODE_INTERPRETER_MODELS = { | |
| "gpt-5", | |
| "o3-low", | |
| "o3-medium", | |
| "o3-high", | |
| "o4-mini-low", | |
| "o4-mini-medium", | |
| "o4-mini-high", | |
| "gpt-4o", | |
| "gpt-4o-mini", | |
| "gpt-4-turbo" | |
| } | |
| QWEN_AGENT_MODELS = { | |
| "qwen3-235b-thinking", | |
| } | |
| CLAUDE_CODE_EXECUTION_MODELS = { | |
| "claude-3-7-sonnet-20250219", | |
| "claude-opus-4-5", | |
| "claude-sonnet-4-5", | |
| } | |
| async def run_model_prompt_queries_with_code_interpreter( | |
| models: list, | |
| prompts: list, | |
| titles: list, | |
| num_elems: list, | |
| file_paths_per_question: list, # Array of arrays - one file list per question | |
| num_repeats, | |
| progress_cb = None, | |
| error_cb = None, | |
| max_workers: int = 100000, | |
| ): | |
| """ | |
| Run queries with code interpreter enabled and file access. | |
| Files are uploaded once at the beginning and reused across all attempts. | |
| Each attempt still gets a fresh container (auto mode ensures isolation). | |
| Args: | |
| file_paths_per_question: List where each element is a list of file paths for that question | |
| Paths can be: | |
| - Absolute: /full/path/to/file.csv | |
| - Relative to server directory: Data_csv/Plots/A1/file.csv | |
| """ | |
| from openai import AsyncOpenAI | |
| from google import genai | |
| prompt_to_title = dict(zip(prompts, titles)) | |
| prompt_to_files = dict(zip(prompts, file_paths_per_question)) | |
| # Step 1: Collect all unique file paths across all questions and resolve them | |
| all_file_paths = set() | |
| for file_list in file_paths_per_question: | |
| for file_path in file_list: | |
| resolved_path = resolve_file_path(file_path) | |
| all_file_paths.add(resolved_path) | |
| # Check if files exist | |
| for file_path in all_file_paths: | |
| if not os.path.exists(file_path): | |
| print(f"β File does not exist: {file_path}") | |
| raise FileNotFoundError(f"File not found: {file_path}") | |
| # Step 2: Determine which APIs need file uploads | |
| has_gemini_models = any(model in GEMINI_MODELS for model in models) | |
| has_openai_models = any(model in OPENAI_CODE_INTERPRETER_MODELS for model in models) | |
| has_qwen_models = any(model in QWEN_AGENT_MODELS for model in models) | |
| has_claude_models = any(model in CLAUDE_CODE_EXECUTION_MODELS for model in models) | |
| print(f"Models requiring file uploads: Gemini={has_gemini_models}, OpenAI={has_openai_models}, Qwen={has_qwen_models}, Claude={has_claude_models}") | |
| # Create nested dictionary structure | |
| file_path_to_id = {} | |
| # Step 3: Upload files to OpenAI if needed | |
| if has_openai_models: | |
| print("\n" + "="*60) | |
| print("Uploading files to OpenAI...") | |
| print("="*60) | |
| client = AsyncOpenAI( | |
| api_key=os.getenv("OPENAI_API_KEY"), | |
| timeout=14400 # 4 hours in seconds | |
| ) | |
| # List existing uploaded files to avoid re-uploading | |
| try: | |
| existing_files_response = await client.files.list(purpose="user_data", limit=10000) | |
| existing_files = existing_files_response.data | |
| existing_file_map = { | |
| (f.filename, f.bytes): f.id | |
| for f in existing_files | |
| } | |
| print(f"Found {len(existing_file_map)} existing OpenAI files") | |
| except Exception as e: | |
| print(f"Warning: Could not list existing files: {e}") | |
| existing_file_map = {} | |
| openai_file_map = {} | |
| files_to_upload = [] | |
| for file_path in all_file_paths: | |
| file_size = os.path.getsize(file_path) | |
| file_basename = os.path.basename(file_path) | |
| file_key = (file_basename, file_size) | |
| if file_key in existing_file_map: | |
| openai_file_map[file_path] = existing_file_map[file_key] | |
| else: | |
| files_to_upload.append(file_path) | |
| print(f"Found {len(openai_file_map)} existing, need to upload {len(files_to_upload)} new files") | |
| if files_to_upload: | |
| FILE_UPLOAD_TIMEOUT = 600 | |
| MAX_CONCURRENT_UPLOADS = 40 | |
| upload_semaphore = asyncio.Semaphore(MAX_CONCURRENT_UPLOADS) | |
| async def upload_openai_file(file_path: str): | |
| async with upload_semaphore: | |
| try: | |
| file_basename = os.path.basename(file_path) | |
| async with aiofiles.open(file_path, 'rb') as f: | |
| file_content = await f.read() | |
| from io import BytesIO | |
| file_obj = await asyncio.wait_for( | |
| client.files.create( | |
| file=(file_basename, BytesIO(file_content)), | |
| purpose="user_data" | |
| ), | |
| timeout=FILE_UPLOAD_TIMEOUT | |
| ) | |
| return file_path, file_obj.id | |
| except Exception as e: | |
| print(f"β Error uploading {file_path} to OpenAI: {e}") | |
| raise | |
| upload_tasks = [upload_openai_file(fp) for fp in files_to_upload] | |
| upload_results = await asyncio.gather(*upload_tasks) | |
| for file_path, file_id in upload_results: | |
| openai_file_map[file_path] = file_id | |
| file_path_to_id["openai"] = openai_file_map | |
| print(f"β All {len(openai_file_map)} OpenAI files ready") | |
| # Step 4: Upload files to Gemini if needed | |
| if has_gemini_models: | |
| print("\n" + "="*60) | |
| print("Uploading files to Gemini...") | |
| print("="*60) | |
| gemini_client = genai.Client() | |
| # List existing Gemini files | |
| try: | |
| existing_gemini_files = list(gemini_client.files.list()) | |
| existing_gemini_map = { | |
| f.display_name: f for f in existing_gemini_files | |
| } | |
| print(f"Found {len(existing_gemini_map)} existing Gemini files") | |
| except Exception as e: | |
| print(f"Warning: Could not list existing Gemini files: {e}") | |
| existing_gemini_map = {} | |
| gemini_file_map = {} | |
| for file_path in all_file_paths: | |
| file_basename = os.path.basename(file_path) | |
| # Check if file already exists in Gemini | |
| if file_basename in existing_gemini_map: | |
| gemini_file_map[file_path] = existing_gemini_map[file_basename] | |
| print(f" β Using existing Gemini file: {file_basename}") | |
| else: | |
| try: | |
| # Upload new file to Gemini | |
| uploaded_file = gemini_client.files.upload(file=file_path) | |
| gemini_file_map[file_path] = uploaded_file | |
| print(f" β Uploaded to Gemini: {file_basename}") | |
| except Exception as e: | |
| print(f" β Error uploading {file_basename} to Gemini: {e}") | |
| raise | |
| file_path_to_id["gemini"] = gemini_file_map | |
| print(f"β All {len(gemini_file_map)} Gemini files ready") | |
| # Step 5: Upload files to Claude if needed | |
| if has_claude_models: | |
| print("\n" + "="*60) | |
| print("Uploading files to Claude (Anthropic Files API)...") | |
| print("="*60) | |
| headers = { | |
| "x-api-key": os.getenv("ANTHROPIC_API_KEY"), | |
| "anthropic-version": "2023-06-01", | |
| "anthropic-beta": "files-api-2025-04-14" | |
| } | |
| claude_file_map = {} | |
| async def upload_claude_file(file_path: str): | |
| try: | |
| file_basename = os.path.basename(file_path) | |
| async with aiofiles.open(file_path, 'rb') as f: | |
| file_content = await f.read() | |
| # Use httpx for file upload | |
| async with httpx.AsyncClient(timeout=600) as client: | |
| files = {'file': (file_basename, file_content)} | |
| resp = await client.post( | |
| "https://api.anthropic.com/v1/files", | |
| headers=headers, | |
| files=files | |
| ) | |
| resp.raise_for_status() | |
| file_data = resp.json() | |
| return file_path, file_data['id'] | |
| except Exception as e: | |
| print(f"β Error uploading {file_basename} to Claude: {e}") | |
| raise | |
| upload_tasks = [upload_claude_file(fp) for fp in all_file_paths] | |
| upload_results = await asyncio.gather(*upload_tasks) | |
| for file_path, file_id in upload_results: | |
| claude_file_map[file_path] = file_id | |
| print(f" β Uploaded to Claude: {os.path.basename(file_path)}") | |
| file_path_to_id["claude"] = claude_file_map | |
| print(f"β All {len(claude_file_map)} Claude files ready") | |
| # Step 6: Handle Qwen Agent (uses local file paths, no upload needed) | |
| if has_qwen_models: | |
| print("\n" + "="*60) | |
| print("Qwen Agent will use local file paths (no upload needed)") | |
| print("="*60) | |
| # Qwen Agent uses local file paths directly, so just map paths to themselves | |
| qwen_file_map = {file_path: file_path for file_path in all_file_paths} | |
| file_path_to_id["qwen"] = qwen_file_map | |
| print(f"β {len(qwen_file_map)} files ready for Qwen Agent") | |
| # Step 7: Run queries with pre-uploaded files | |
| # semaphore to cap concurrent ask_model calls | |
| sem = asyncio.Semaphore(max_workers) | |
| async def bound_ask(model, prompt, num_elem, title, file_paths): | |
| async with sem: | |
| try: | |
| # Resolve file paths before passing to ask_model_with_code_interpreter | |
| resolved_file_paths = [resolve_file_path(fp) for fp in file_paths] | |
| out = await ask_model_with_code_interpreter( | |
| model, prompt, resolved_file_paths, file_path_to_id, title, num_elem | |
| ) | |
| except Exception as e: | |
| print(f"Error for model={model}, {e}, {title}") | |
| return None, model, prompt | |
| print(f"Model: {model}, out: {out}, title: {title}") | |
| return out, model, prompt | |
| # prepare all (model, prompt, num_elem, file_paths) tuples | |
| if isinstance(num_repeats, list): | |
| all_tasks = [ | |
| (m, p, n, t, prompt_to_files[p]) | |
| for j, (p, n, t) in enumerate(zip(prompts, num_elems, titles)) | |
| for i, m in enumerate(models) | |
| for _ in range(num_repeats[i][j]) | |
| ] | |
| else: | |
| all_tasks = [ | |
| (m, p, n, t, prompt_to_files[p]) | |
| for p, n, t in zip(prompts, num_elems, titles) | |
| for _ in range(num_repeats) | |
| for m in models | |
| ] | |
| tasks = [] | |
| for idx, (model, prompt, num_elem, title, file_paths) in enumerate(all_tasks): | |
| tasks.append(asyncio.create_task(bound_ask(model, prompt, num_elem, title, file_paths))) | |
| for fut in tqdm(asyncio.as_completed(tasks), total=len(tasks), desc="Processing with Code Interpreter"): | |
| output, model, prompt = await fut | |
| if output: | |
| title = prompt_to_title[prompt] | |
| try: | |
| # Firebase | |
| append_result(title, model, output) | |
| # Local disk | |
| save_dir = "./outputs" | |
| os.makedirs(save_dir, exist_ok=True) | |
| filename = f"{title}-{safe_key(model)}.jsonl" | |
| filepath = os.path.join(save_dir, filename) | |
| async with aiofiles.open(filepath, mode="a") as f: | |
| await f.write(json.dumps(output) + "\n") | |
| except Exception as e: | |
| print(f"Error saving output for {title} - {model}: {e}") | |
| if progress_cb: | |
| await progress_cb() | |
| else: | |
| if error_cb: | |
| await error_cb() | |
| # Files are kept in OpenAI storage for future reuse | |
| print(f"β Completed all queries. {len(file_path_to_id)} files remain in storage for future use.") |