Spaces:
Sleeping
Sleeping
File size: 4,986 Bytes
a72140d | 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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | import os
import json
import sys
import argparse
from openai import OpenAI
try:
from huggingface_hub import HfApi
except ImportError:
HfApi = None
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Refine a planning config by replacing model or dataset names with concrete Hugging Face ids."
)
# Root config
parser.add_argument(
"--output_dir",
type=str,
required=True,
help="Output directory that contains planning_config.yaml",
)
parser.add_argument(
"--gpt_version",
type=str,
default="gpt-4.1-mini",
help="OpenAI chat model name used for name detection.",
)
return parser.parse_args()
args = parse_args()
client = OpenAI(api_key = os.environ["OPENAI_API_KEY"])
planning_config_path = os.path.join(
args.output_dir, f"planning_config.yaml"
)
if not os.path.exists(planning_config_path):
print(f"β Planning config not found: {planning_config_path}", file=sys.stderr)
sys.exit(1)
# ---------------------------------------------------------
# 1. Load original config and call OpenAI to detect names
# ---------------------------------------------------------
with open(planning_config_path, "r", encoding="utf-8") as f:
config_yaml = f.read()
codes = ""
codes += f"```yaml\n## File name: config.yaml\n{config_yaml}\n```\n\n"
messages = [
{
"role": "system",
"content": (
"You are an expert code assistant. Your task is to identify the model "
"name and dataset names in the given configuration file. Return them "
"as a list of strings in the exact format shown in the example below. "
"Do not include any other text or commentary."
),
},
{
"role": "user",
"content": f"""
### Configuration file
{codes}
---
## Instruction
Detect the model name and dataset names in the configuration file so that they can be downloaded successfully from Hugging Face. Your output must strictly follow the format below.
---
## Format Example
["Llama-3", "TriviaQA"]
---
## Answer
""",
},
]
response = client.chat.completions.create(
model=args.gpt_version,
messages=messages,
)
answer = response.choices[0].message.content.strip()
# print("Raw OpenAI answer:", answer)
# Parse the list of names from the model output
try:
detect_lst = json.loads(answer)
if not isinstance(detect_lst, list):
raise ValueError("Parsed value is not a list.")
except Exception as e:
print(f"β Failed to parse OpenAI answer as JSON list: {e}", file=sys.stderr)
sys.exit(1)
print("Detected names:", detect_lst)
# ---------------------------------------------------------
# 2. Use Hugging Face to refine model / dataset ids
# ---------------------------------------------------------
if HfApi is None:
print(
"β οΈ huggingface_hub is not installed. "
"Install it with `pip install huggingface_hub` to refine ids. "
"Using original names.",
file=sys.stderr,
)
refine_lst = detect_lst
else:
api = HfApi()
refine_lst = []
for name in detect_lst:
try:
models = api.list_models(
search=name,
sort="downloads",
direction=-1,
limit=10,
full=True,
)
lst_models = list(models)
except Exception as e:
print(f"β Error querying Hugging Face for '{name}': {e}", file=sys.stderr)
lst_models = []
if not lst_models:
print(f"Warning: no models found for '{name}'. Keeping original name.")
refine_model_id = name
else:
refine_model_id = lst_models[0].id
refine_lst.append(refine_model_id)
# ---------------------------------------------------------
# 3. Replace names in the config with refined Hugging Face ids
# ---------------------------------------------------------
refined_config_yaml = config_yaml
for name, refine_name in zip(detect_lst, refine_lst):
if name != refine_name:
print(f"{name} --> {refine_name}")
refined_config_yaml = refined_config_yaml.replace(name, refine_name)
print("-" * 30)
print("Original config:")
print(config_yaml)
print("-" * 30)
print("Refined config:")
print(refined_config_yaml)
# ---------------------------------------------------------
# 4. Backup and save the refined config
# ---------------------------------------------------------
filepath = planning_config_path
backup_path = f"{filepath}.bak"
try:
if os.path.exists(filepath):
os.rename(filepath, backup_path)
print(f"π Existing file backed up to: {backup_path}")
with open(filepath, "w", encoding="utf-8") as f:
f.write(refined_config_yaml)
print(f"πΎ {filepath}: File saved.\n")
except Exception as e:
print(f"β Error saving file {filepath}: {e}\n")
sys.exit(1)
|