Spaces:
Sleeping
Sleeping
File size: 3,275 Bytes
4312ddd cebf70d 0930e60 82d3540 ba6430d f03486f 4312ddd 93a03fa 099723b 6336c73 099723b 6336c73 4312ddd 099723b 6336c73 099723b 6336c73 0930e60 6336c73 4312ddd 0930e60 6336c73 0930e60 6336c73 0930e60 f03486f 0930e60 6336c73 0930e60 099723b edc314f 93a03fa 6a097d1 6336c73 099723b 6336c73 099723b f03486f 6336c73 099723b 6336c73 63f3598 4312ddd 63f3598 4312ddd 80b1665 4312ddd 80b1665 4312ddd 63f3598 099723b f03486f 4312ddd 099723b f03486f cebf70d f03486f cebf70d f03486f cebf70d f03486f cebf70d f03486f | 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 | # backend/fixed_output.py
import os
import warnings
import json
BASE = os.path.dirname(os.path.abspath(__file__))
def parse_fixed_md(content: str):
lines = [line.strip() for line in content.split("\n") if line.strip()]
result = {
"type": None,
"trigger": None,
"steps": [],
"options": [],
"text": None
}
# -------------------------
# TYPE
# -------------------------
if lines[0].startswith("TYPE:"):
result["type"] = lines[0].split(":", 1)[1].strip()
lines = lines[1:]
# -------------------------
# TRIGGER
# -------------------------
if lines and lines[0].startswith("TRIGGER:"):
result["trigger"] = [
t.strip() for t in lines[0].split(":", 1)[1].split(",")
]
lines = lines[1:]
skill_type = result["type"]
# -------------------------
# multi-step-options(多步驟)
# -------------------------
if skill_type == "multi-step-options":
steps = []
current_step = None
i = 0
while i < len(lines):
line = lines[i]
if line == "[STEP]":
if current_step:
steps.append(current_step)
i += 1
label = lines[i] if i < len(lines) else ""
current_step = {"label": label, "options": []}
elif line.startswith("-") and current_step:
option = line.lstrip("-").strip()
current_step["options"].append(option)
i += 1
if current_step:
steps.append(current_step)
result["steps"] = steps
warnings.warn(json.dumps(result, ensure_ascii=False, indent=2))
return result
# -------------------------
# multi-options(單步驟多選)
# -------------------------
if skill_type == "multi-options":
result["options"] = [
line.lstrip("-").strip()
for line in lines if line.startswith("-")
]
return result
# -------------------------
# fixed-sequence(固定句子)
# -------------------------
if skill_type == "fixed-sequence":
steps = []
for line in lines:
if line.startswith("[STEP]"):
label = line.replace("[STEP]", "").strip()
steps.append({
"label": label,
"options": [label]
})
continue
if line.startswith("-"):
text = line.lstrip("-").strip()
steps.append({
"label": text,
"options": [text]
})
continue
result["steps"] = steps
return result
# -------------------------
# fallback
# -------------------------
result["raw"] = content
return result
def run_fixed_output(field_name: str):
path = os.path.join(BASE, "skills", f"{field_name}_fixed", f"{field_name}.md")
if not os.path.exists(path):
return f"[Error] 找不到固定輸出:{path}"
with open(path, "r", encoding="utf-8") as f:
content = f.read().strip()
return parse_fixed_md(content) |