303 lines
13 KiB
Python
303 lines
13 KiB
Python
import json
|
|||
|
|
import re
|
||
|
|
|
||
|
|
def clean_val(v):
|
||
|
|
if v is None:
|
||
|
|
return ""
|
||
|
|
v = str(v).strip()
|
||
|
|
if v.startswith("`") and v.endswith("`"):
|
||
|
|
v = v[1:-1].strip()
|
||
|
|
if v.startswith("**") and v.endswith("**"):
|
||
|
|
v = v[2:-2].strip()
|
||
|
|
if (v.startswith('"') and v.endswith('"')) or (v.startswith("'") and v.endswith("'")):
|
||
|
|
v = v[1:-1].strip()
|
||
|
|
return v
|
||
|
|
|
||
|
|
def is_req(req_str):
|
||
|
|
req_str = req_str.lower()
|
||
|
|
return "req" in req_str and "opt" not in req_str
|
||
|
|
|
||
|
|
def parse_md():
|
||
|
|
with open("web/docs/FASTAPI_BACKEND_API_DOCUMENTATION.md", "r") as f:
|
||
|
|
md_text = f.read()
|
||
|
|
|
||
|
|
sections = re.split(r"\n(?=###\s+)", md_text)
|
||
|
|
md_items = []
|
||
|
|
for s in sections[1:]:
|
||
|
|
lines = s.strip().split("\n")
|
||
|
|
header = lines[0].replace("###", "").strip()
|
||
|
|
|
||
|
|
url_m = re.search(r"\*\*URL:\*\*\s*`([^`]+)`", s)
|
||
|
|
url = url_m.group(1).strip() if url_m else ""
|
||
|
|
|
||
|
|
subs_m = re.search(r"\*\(Sub-endpoints:\s*([^\)]+)\)\*", s)
|
||
|
|
subs = subs_m.group(1).strip() if subs_m else ""
|
||
|
|
|
||
|
|
meth_m = re.search(r"\*\*Method:\*\*\s*`?([A-Za-z0-9_\/ ]+)`?", s)
|
||
|
|
method = meth_m.group(1).strip() if meth_m else ""
|
||
|
|
|
||
|
|
# parse table
|
||
|
|
table_lines = []
|
||
|
|
in_table = False
|
||
|
|
for l in lines:
|
||
|
|
ls = l.strip()
|
||
|
|
if ls.startswith("|") and ls.endswith("|"):
|
||
|
|
in_table = True
|
||
|
|
table_lines.append(ls)
|
||
|
|
elif in_table:
|
||
|
|
break
|
||
|
|
|
||
|
|
params = []
|
||
|
|
if len(table_lines) >= 3:
|
||
|
|
raw_headers = [c.strip() for c in table_lines[0].strip("|").split("|")]
|
||
|
|
for l in table_lines[2:]:
|
||
|
|
cols = [c.strip() for c in l.strip("|").split("|")]
|
||
|
|
if len(cols) == len(raw_headers):
|
||
|
|
row = dict(zip(raw_headers, cols))
|
||
|
|
field = clean_val(row.get("Field", ""))
|
||
|
|
ty = clean_val(row.get("Type", ""))
|
||
|
|
req_raw = row.get("Required", "")
|
||
|
|
req = is_req(req_raw)
|
||
|
|
df = clean_val(row.get("Default", ""))
|
||
|
|
ds = row.get("Description", "")
|
||
|
|
params.append({
|
||
|
|
"name": field,
|
||
|
|
"type": ty,
|
||
|
|
"required": req,
|
||
|
|
"default": df,
|
||
|
|
"description": ds
|
||
|
|
})
|
||
|
|
|
||
|
|
md_items.append({
|
||
|
|
"header": header,
|
||
|
|
"url": url,
|
||
|
|
"subs": subs,
|
||
|
|
"method": method,
|
||
|
|
"params": params,
|
||
|
|
"raw_text": s
|
||
|
|
})
|
||
|
|
return md_items
|
||
|
|
|
||
|
|
def update_nexus():
|
||
|
|
md_items = parse_md()
|
||
|
|
|
||
|
|
with open("web/docs/data.json", "r") as f:
|
||
|
|
nexus_data = json.load(f)
|
||
|
|
|
||
|
|
# Build mapping from MD URL / header to MD item
|
||
|
|
# We will map each Nexus API to its MD item
|
||
|
|
def find_md(api):
|
||
|
|
a_url = (api.get("full_url") or "").rstrip("/")
|
||
|
|
a_link = (api.get("api_ip_link") or "").rstrip("/")
|
||
|
|
a_path = (api.get("path") or "").rstrip("/")
|
||
|
|
a_key = (api.get("api_key") or "").lower()
|
||
|
|
a_title = (api.get("title") or "").lower()
|
||
|
|
|
||
|
|
for m in md_items:
|
||
|
|
m_url = m["url"].rstrip("/")
|
||
|
|
if m_url and (m_url == a_url or m_url == a_link):
|
||
|
|
return m
|
||
|
|
for se in api.get("endpoints", []):
|
||
|
|
se_url = (se.get("full_url") or "").rstrip("/")
|
||
|
|
for m in md_items:
|
||
|
|
m_url = m["url"].rstrip("/")
|
||
|
|
if m_url and m_url == se_url:
|
||
|
|
return m
|
||
|
|
for m in md_items:
|
||
|
|
m_url = m["url"].rstrip("/")
|
||
|
|
if a_path and m_url.endswith(a_path):
|
||
|
|
return m
|
||
|
|
for m in md_items:
|
||
|
|
h = m["header"].lower()
|
||
|
|
if "feedback" in a_key and "feedback" in h:
|
||
|
|
return m
|
||
|
|
if "reports" in a_key and ("telemetry" in h or "reports" in h):
|
||
|
|
return m
|
||
|
|
if "health" in a_title and "health" in h:
|
||
|
|
return m
|
||
|
|
if "waifu2x" in a_key and "gfpgan" in h:
|
||
|
|
return m
|
||
|
|
if "relight" in a_key and "relighting" in h:
|
||
|
|
return m
|
||
|
|
if "skin_analyzer" in a_path and "skin health" in h:
|
||
|
|
return m
|
||
|
|
if "trendy" in a_key and "trendy" in h:
|
||
|
|
return m
|
||
|
|
if "cartoon_model" in a_key and "3.9" in h:
|
||
|
|
return m
|
||
|
|
if "multi_segmentation" in a_key and "multi-segmentation" in h:
|
||
|
|
return m
|
||
|
|
if "hair_color_changer" in a_path and "1.1" in h:
|
||
|
|
return m
|
||
|
|
if "hair_segmentation" in a_path and "3.13" in h:
|
||
|
|
return m
|
||
|
|
return None
|
||
|
|
|
||
|
|
def map_type_to_nexus(m_ty):
|
||
|
|
t = m_ty.lower()
|
||
|
|
if t in ["uploadfile", "file"]:
|
||
|
|
return "file (binary)"
|
||
|
|
if t in ["string", "str"]:
|
||
|
|
return "string"
|
||
|
|
if t in ["integer", "int"]:
|
||
|
|
return "integer"
|
||
|
|
if t in ["float", "number"]:
|
||
|
|
return "float"
|
||
|
|
if t in ["boolean", "bool"]:
|
||
|
|
return "boolean"
|
||
|
|
if t in ["json", "dict", "object"]:
|
||
|
|
return "json"
|
||
|
|
return m_ty
|
||
|
|
|
||
|
|
updated_count = 0
|
||
|
|
|
||
|
|
for sec in nexus_data["sections"]:
|
||
|
|
sec_title = sec.get("title", "")
|
||
|
|
for api in sec.get("eps", []):
|
||
|
|
matched_md = find_md(api)
|
||
|
|
if not matched_md or not matched_md.get("params"):
|
||
|
|
continue
|
||
|
|
|
||
|
|
md_params = matched_md["params"]
|
||
|
|
md_p_map = {clean_val(p["name"]).lower(): p for p in md_params}
|
||
|
|
|
||
|
|
# Helper to sync a params list
|
||
|
|
def sync_param_list(p_list, is_sub=False):
|
||
|
|
# 1. Update existing params
|
||
|
|
existing_names = set()
|
||
|
|
for p in p_list:
|
||
|
|
pn = clean_val(p.get("n"))
|
||
|
|
existing_names.add(pn.lower())
|
||
|
|
al = clean_val(p.get("al"))
|
||
|
|
|
||
|
|
# Check if matched by primary name or alias
|
||
|
|
m_p = md_p_map.get(pn.lower())
|
||
|
|
if not m_p and al:
|
||
|
|
for a in [x.strip().lower() for x in al.split(",")]:
|
||
|
|
if a in md_p_map:
|
||
|
|
m_p = md_p_map[a]
|
||
|
|
break
|
||
|
|
|
||
|
|
if m_p:
|
||
|
|
# Update Required
|
||
|
|
p["rq"] = m_p["required"]
|
||
|
|
# Update Type if different
|
||
|
|
if map_type_to_nexus(m_p["type"]) != p.get("ty"):
|
||
|
|
# Keep file (binary) for UploadFile
|
||
|
|
if m_p["type"].lower() in ["uploadfile", "file"]:
|
||
|
|
p["ty"] = "file (binary)"
|
||
|
|
elif m_p["type"].lower() in ["int", "integer"]:
|
||
|
|
p["ty"] = "integer"
|
||
|
|
elif m_p["type"].lower() in ["bool", "boolean"]:
|
||
|
|
p["ty"] = "boolean"
|
||
|
|
elif m_p["type"].lower() in ["float"]:
|
||
|
|
p["ty"] = "float"
|
||
|
|
# Update Default if MD has specific non-none default
|
||
|
|
if m_p.get("default") and m_p["default"].lower() not in ["none", "null"]:
|
||
|
|
p["df"] = m_p["default"]
|
||
|
|
elif p.get("df") in ["None", "null", "none"]:
|
||
|
|
p["df"] = "None"
|
||
|
|
# Preserve or enhance alias
|
||
|
|
if m_p["name"] != pn and m_p["name"] not in al:
|
||
|
|
p["al"] = (al + f", {m_p['name']}").strip(", ")
|
||
|
|
|
||
|
|
# 2. Add rich/tuning params from MD that are missing
|
||
|
|
for mp in md_params:
|
||
|
|
mp_name = clean_val(mp["name"])
|
||
|
|
if mp_name.lower() not in existing_names:
|
||
|
|
# Check if it was matched as an alias of an existing param
|
||
|
|
already_aliased = False
|
||
|
|
for p in p_list:
|
||
|
|
al_list = [x.strip().lower() for x in clean_val(p.get("al")).split(",")]
|
||
|
|
if mp_name.lower() in al_list:
|
||
|
|
already_aliased = True
|
||
|
|
break
|
||
|
|
if not already_aliased and mp_name.lower() not in ["*none*", "—", "none", "*additional*"]:
|
||
|
|
# Add missing param
|
||
|
|
new_param = {
|
||
|
|
"n": mp_name,
|
||
|
|
"al": "",
|
||
|
|
"ty": map_type_to_nexus(mp["type"]),
|
||
|
|
"lc": "multipart/form-data" if "multipart" in matched_md.get("raw_text", "").lower() else "body",
|
||
|
|
"rq": mp["required"],
|
||
|
|
"df": mp["default"] if mp["default"] else "None",
|
||
|
|
"ds": mp["description"]
|
||
|
|
}
|
||
|
|
# Check if alias exists in description (e.g., aliases: image0, image)
|
||
|
|
al_match = re.search(r"aliases:\s*([^\.]+)", mp["description"], re.IGNORECASE)
|
||
|
|
if al_match:
|
||
|
|
aliases = [a.strip(" `\"'") for a in al_match.group(1).split(",")]
|
||
|
|
aliases = [a for a in aliases if a != mp_name]
|
||
|
|
new_param["al"] = ", ".join(aliases)
|
||
|
|
|
||
|
|
p_list.append(new_param)
|
||
|
|
|
||
|
|
# Sync top-level params
|
||
|
|
sync_param_list(api.get("params", []))
|
||
|
|
|
||
|
|
# Sync each sub-endpoint
|
||
|
|
for se in api.get("endpoints", []):
|
||
|
|
sync_param_list(se.get("params", []), is_sub=True)
|
||
|
|
|
||
|
|
updated_count += 1
|
||
|
|
|
||
|
|
# Specific fine-grained fixes for critical items:
|
||
|
|
# 1. SDXL endpoints: ensure original_image has alias image0 or image
|
||
|
|
for sec in nexus_data["sections"]:
|
||
|
|
if "SDXL" in sec.get("title", ""):
|
||
|
|
for api in sec.get("eps", []):
|
||
|
|
for p in api.get("params", []):
|
||
|
|
if p.get("n") == "original_image" and "image0" not in p.get("al", ""):
|
||
|
|
p["al"] = (p.get("al", "") + ", image0").strip(", ")
|
||
|
|
if p.get("n") == "prompt" and "userPrompt" not in p.get("al", ""):
|
||
|
|
p["al"] = (p.get("al", "") + ", userPrompt").strip(", ")
|
||
|
|
if p.get("n") == "effect_id" and "effectId" not in p.get("al", ""):
|
||
|
|
p["al"] = (p.get("al", "") + ", effectId").strip(", ")
|
||
|
|
for se in api.get("endpoints", []):
|
||
|
|
for p in se.get("params", []):
|
||
|
|
if p.get("n") == "original_image" and "image0" not in p.get("al", ""):
|
||
|
|
p["al"] = (p.get("al", "") + ", image0").strip(", ")
|
||
|
|
if p.get("n") == "prompt" and "userPrompt" not in p.get("al", ""):
|
||
|
|
p["al"] = (p.get("al", "") + ", userPrompt").strip(", ")
|
||
|
|
if p.get("n") == "effect_id" and "effectId" not in p.get("al", ""):
|
||
|
|
p["al"] = (p.get("al", "") + ", effectId").strip(", ")
|
||
|
|
|
||
|
|
# 2. Vision Models: ensure image param has alias image0
|
||
|
|
for sec in nexus_data["sections"]:
|
||
|
|
if "ML Vision" in sec.get("title", ""):
|
||
|
|
for api in sec.get("eps", []):
|
||
|
|
for p in api.get("params", []):
|
||
|
|
if p.get("n") == "image" and "image0" not in p.get("al", ""):
|
||
|
|
p["al"] = (p.get("al", "") + ", image0").strip(", ")
|
||
|
|
for se in api.get("endpoints", []):
|
||
|
|
for p in se.get("params", []):
|
||
|
|
if p.get("n") == "image" and "image0" not in p.get("al", ""):
|
||
|
|
p["al"] = (p.get("al", "") + ", image0").strip(", ")
|
||
|
|
|
||
|
|
# Save data.json
|
||
|
|
with open("web/docs/data.json", "w") as f:
|
||
|
|
json.dump(nexus_data, f, indent=2)
|
||
|
|
|
||
|
|
print(f"Updated {updated_count} APIs in web/docs/data.json")
|
||
|
|
|
||
|
|
# Inject into index.html and FASTAPI_BACKEND_API_DOCS.html
|
||
|
|
json_str = json.dumps(nexus_data)
|
||
|
|
for html_file in ["web/docs/index.html", "web/docs/FASTAPI_BACKEND_API_DOCS.html"]:
|
||
|
|
with open(html_file, "r") as f:
|
||
|
|
html = f.read()
|
||
|
|
|
||
|
|
# Replace content inside <script id="api-data" type="application/json">...</script>
|
||
|
|
pattern = r'(<script\s+id=[\"\']api-data[\"\']\s+type=[\"\']application/json[\"\']>)(.*?)(</script>)'
|
||
|
|
new_html, num_subs = re.subn(pattern, r'\g<1>' + json_str.replace('\\', '\\\\') + r'\g<3>', html, flags=re.DOTALL)
|
||
|
|
if num_subs == 0:
|
||
|
|
# try type before id
|
||
|
|
pattern2 = r'(<script\s+type=[\"\']application/json[\"\']\s+id=[\"\']api-data[\"\']>)(.*?)(</script>)'
|
||
|
|
new_html, num_subs = re.subn(pattern2, r'\g<1>' + json_str.replace('\\', '\\\\') + r'\g<3>', html, flags=re.DOTALL)
|
||
|
|
|
||
|
|
with open(html_file, "w") as f:
|
||
|
|
f.write(new_html)
|
||
|
|
print(f"Injected updated JSON into {html_file} (replacements: {num_subs})")
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
update_nexus()
|