File size: 2,261 Bytes
ec6eb9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Deploy the 参考clawemail mail-agent as its own HF Docker Space.

Reads all credentials from the local .env (gitignored). Creates the space,
injects secrets (CLAW account + the agent api key), uploads the agent folder
(HF builds the image), and restarts it.

    python scripts/deploy_agent.py
"""
import os
import sys

REPO_ID = "Echocq/clawemail-agent"
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
AGENT_DIR = os.path.join(ROOT, "参考clawemail")
CLAW_EMAIL = "echocq@claw.163.com"

IGNORE = ["node_modules/*", ".state-test*", ".git/*", "*.log"]


def env(name):
    path = os.path.join(ROOT, ".env")
    for line in open(path, encoding="utf-8"):
        line = line.strip()
        if line.startswith(f"{name}=") and "=" in line:
            return line.split("=", 1)[1].strip().strip('"').strip("'")
    return None


def main() -> int:
    token = env("HF_TOKEN")
    claw_key = env("CLAW_API_KEY")
    agent_key = env("CLAWEMAIL_API_KEY")
    if not (token and claw_key and agent_key):
        print("missing HF_TOKEN / CLAW_API_KEY / CLAWEMAIL_API_KEY in .env", file=sys.stderr)
        return 2

    from huggingface_hub import HfApi
    api = HfApi(token=token)
    print(f"auth: {api.whoami().get('name')}")

    api.create_repo(repo_id=REPO_ID, repo_type="space", space_sdk="docker",
                    exist_ok=True, private=False)
    print(f"space ready: {REPO_ID}")

    api.add_space_secret(repo_id=REPO_ID, key="CLAW_EMAIL", value=CLAW_EMAIL)
    api.add_space_secret(repo_id=REPO_ID, key="CLAW_API_KEY", value=claw_key)
    api.add_space_secret(repo_id=REPO_ID, key="CLAWEMAIL_API_KEY", value=agent_key)
    print("secrets set: CLAW_EMAIL, CLAW_API_KEY, CLAWEMAIL_API_KEY")

    commit = api.upload_folder(repo_id=REPO_ID, repo_type="space",
                               folder_path=AGENT_DIR, ignore_patterns=IGNORE,
                               commit_message="deploy clawemail mail-agent (api-key gated remote service)")
    print(f"uploaded: {getattr(commit, 'oid', commit)}")

    api.restart_space(repo_id=REPO_ID)
    print("restart -> BUILDING")
    print(f"address: https://echocq-clawemail-agent.hf.space")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())