Spaces:
Build error
Build error
| import importlib | |
| import requests | |
| class SkillRegistry: | |
| def __init__(self): | |
| self.skills = {} | |
| def load_from_md(self, path="skills.md"): | |
| with open(path, "r") as f: | |
| content = f.read() | |
| blocks = content.split("---") | |
| for block in blocks: | |
| lines = [line.strip() for line in block.split("\n") if line.strip()] | |
| if not lines or not lines[0].startswith("## Skill:"): | |
| continue | |
| name = lines[0].replace("## Skill:", "").strip() | |
| skill_data = {} | |
| for line in lines[1:]: | |
| if ":" in line: | |
| key, value = line.split(":", 1) | |
| skill_data[key.strip()] = value.strip() | |
| self.skills[name] = skill_data | |
| def invoke(self, name, payload=None): | |
| skill = self.skills.get(name) | |
| if not skill: | |
| return {"error": "Skill not found"} | |
| if skill["type"] == "huggingface_space": | |
| return self._invoke_space(skill, payload) | |
| if skill["type"] == "local_module": | |
| return self._invoke_local(skill, payload) | |
| def _invoke_space(self, skill, payload): | |
| url = skill["endpoint"].replace("huggingface.co", "hf.space") | |
| full_url = url + skill.get("api_route", "") | |
| response = requests.post(full_url, json=payload) | |
| return response.json() | |
| def _invoke_local(self, skill, payload): | |
| module = importlib.import_module(skill["module"]) | |
| func = getattr(module, skill["function"]) | |
| if payload is None: | |
| return func() | |
| return func(payload) |