File size: 3,064 Bytes
86d4cbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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


from .nodes.InputString import FramerComfyInputStringNode
from .nodes.InputNumber import FramerComfyInputNumberNode
from .nodes.InputImage import FramerComfyInputImageNode
from .nodes.InputFloat import FramerComfyFloatInputNode
from .nodes.InputBoolean import FramerComfyBooleanInputNode
from .nodes.OutputImage import FramerComfySaveImageNode

NODE_CLASS_MAPPINGS = {
    "FramerComfyInputStringNode": FramerComfyInputStringNode,
    "FramerComfyInputNumberNode": FramerComfyInputNumberNode,
    "FramerComfyInputImageNode": FramerComfyInputImageNode,
    "FramerComfyFloatInputNode": FramerComfyFloatInputNode,
    "FramerComfyBooleanInputNode": FramerComfyBooleanInputNode,
    "FramerComfySaveImageNode": FramerComfySaveImageNode
}

NODE_DISPLAY_NAME_MAPPINGS = {
    "FramerComfyInputStringNode": "FramerComfy Input String", 
    "FramerComfyInputNumberNode": "FramerComfy Input Number", 
    "FramerComfyInputImageNode": "FramerComfy Input Image", 
    "FramerComfyFloatInputNode": "FramerComfy Input Float",
    "FramerComfyBooleanInputNode": "FramerComfy Input Boolean",
    "FramerComfySaveImageNode": "FramerComfy Save Image"
}

import sys
import os
import random
import traceback
from aiohttp import web
from io import StringIO
from .hf import deploy_workflow
from .generate_reqs import generate_requirements
ext_dir = os.path.dirname(__file__)
sys.path.append(ext_dir)

try:
    import black
except ImportError:
    print("Unable to import requirements for ComfyUI-SaveAsScript.")
    print("Installing...")

    import importlib

    spec = importlib.util.spec_from_file_location(
        "impact_install", os.path.join(os.path.dirname(__file__), "install.py")
    )
    impact_install = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(impact_install)

    print("Successfully installed. Hopefully, at least.")

# Prevent reimporting of custom nodes
os.environ["RUNNING_IN_COMFYUI"] = "TRUE"

from comfyui_to_python import ComfyUItoPython

sys.path.append(os.path.dirname(os.path.dirname(ext_dir)))

import server

WEB_DIRECTORY = "js"


@server.PromptServer.instance.routes.post("/saveasscript")
async def save_as_script(request):
    try:
        data = await request.json()
        name = data["name"]
        workflow = data["workflow"]
        workflow_models = data["workflow_models"]
        hf_access_token = data["hf_access_token"]

        sio = StringIO()
        app_code_generator = ComfyUItoPython(workflow=workflow, output_file=sio, workflow_models=workflow_models)
        app_code = app_code_generator.execute()

        name = f'FramerComfy_{name}_{str(random.randint(1000000000, 9999999999))}'

        requirements_file = generate_requirements()

        print("Deploying to Hugging Face...")
        # Deploy to Hugging Face
        deploy_workflow(name, app_code, requirements_file, hf_access_token)

        sio.seek(0)
        data = sio.read()

        return web.Response(text=data, status=200)
    except Exception as e:
        traceback.print_exc()
        return web.Response(text=str(e), status=500)