BennyKok commited on
Commit
206741c
·
verified ·
1 Parent(s): a9d4dc6

Upload folder using huggingface_hub

Browse files
Files changed (6) hide show
  1. .github/workflows/update_space.yml +28 -0
  2. .gitignore +165 -0
  3. README.md +55 -7
  4. assets/cover.png +0 -0
  5. main.py +127 -0
  6. requirements.txt +3 -0
.github/workflows/update_space.yml ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: Run Python script
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+
8
+ jobs:
9
+ build:
10
+ runs-on: ubuntu-latest
11
+
12
+ steps:
13
+ - name: Checkout
14
+ uses: actions/checkout@v2
15
+
16
+ - name: Set up Python
17
+ uses: actions/setup-python@v2
18
+ with:
19
+ python-version: '3.9'
20
+
21
+ - name: Install Gradio
22
+ run: python -m pip install gradio
23
+
24
+ - name: Log in to Hugging Face
25
+ run: python -c 'import huggingface_hub; huggingface_hub.login(token="${{ secrets.hf_token }}")'
26
+
27
+ - name: Deploy to Spaces
28
+ run: gradio deploy
.gitignore ADDED
@@ -0,0 +1,165 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ venv
7
+ flagged
8
+
9
+ # C extensions
10
+ *.so
11
+
12
+ # Distribution / packaging
13
+ .Python
14
+ build/
15
+ develop-eggs/
16
+ dist/
17
+ downloads/
18
+ eggs/
19
+ .eggs/
20
+ lib/
21
+ lib64/
22
+ parts/
23
+ sdist/
24
+ var/
25
+ wheels/
26
+ share/python-wheels/
27
+ *.egg-info/
28
+ .installed.cfg
29
+ *.egg
30
+ MANIFEST
31
+
32
+ # PyInstaller
33
+ # Usually these files are written by a python script from a template
34
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
35
+ *.manifest
36
+ *.spec
37
+
38
+ # Installer logs
39
+ pip-log.txt
40
+ pip-delete-this-directory.txt
41
+
42
+ # Unit test / coverage reports
43
+ htmlcov/
44
+ .tox/
45
+ .nox/
46
+ .coverage
47
+ .coverage.*
48
+ .cache
49
+ nosetests.xml
50
+ coverage.xml
51
+ *.cover
52
+ *.py,cover
53
+ .hypothesis/
54
+ .pytest_cache/
55
+ cover/
56
+
57
+ # Translations
58
+ *.mo
59
+ *.pot
60
+
61
+ # Django stuff:
62
+ *.log
63
+ local_settings.py
64
+ db.sqlite3
65
+ db.sqlite3-journal
66
+
67
+ # Flask stuff:
68
+ instance/
69
+ .webassets-cache
70
+
71
+ # Scrapy stuff:
72
+ .scrapy
73
+
74
+ # Sphinx documentation
75
+ docs/_build/
76
+
77
+ # PyBuilder
78
+ .pybuilder/
79
+ target/
80
+
81
+ # Jupyter Notebook
82
+ .ipynb_checkpoints
83
+
84
+ # IPython
85
+ profile_default/
86
+ ipython_config.py
87
+
88
+ # pyenv
89
+ # For a library or package, you might want to ignore these files since the code is
90
+ # intended to run in multiple environments; otherwise, check them in:
91
+ # .python-version
92
+
93
+ # pipenv
94
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
95
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
96
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
97
+ # install all needed dependencies.
98
+ #Pipfile.lock
99
+
100
+ # poetry
101
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
102
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
103
+ # commonly ignored for libraries.
104
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
105
+ #poetry.lock
106
+
107
+ # pdm
108
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
109
+ #pdm.lock
110
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
111
+ # in version control.
112
+ # https://pdm.fming.dev/latest/usage/project/#working-with-version-control
113
+ .pdm.toml
114
+ .pdm-python
115
+ .pdm-build/
116
+
117
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
118
+ __pypackages__/
119
+
120
+ # Celery stuff
121
+ celerybeat-schedule
122
+ celerybeat.pid
123
+
124
+ # SageMath parsed files
125
+ *.sage.py
126
+
127
+ # Environments
128
+ .env
129
+ .venv
130
+ env/
131
+ venv/
132
+ ENV/
133
+ env.bak/
134
+ venv.bak/
135
+
136
+ # Spyder project settings
137
+ .spyderproject
138
+ .spyproject
139
+
140
+ # Rope project settings
141
+ .ropeproject
142
+
143
+ # mkdocs documentation
144
+ /site
145
+
146
+ # mypy
147
+ .mypy_cache/
148
+ .dmypy.json
149
+ dmypy.json
150
+
151
+ # Pyre type checker
152
+ .pyre/
153
+
154
+ # pytype static type analyzer
155
+ .pytype/
156
+
157
+ # Cython debug symbols
158
+ cython_debug/
159
+
160
+ # PyCharm
161
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
162
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
163
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
164
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
165
+ #.idea/
README.md CHANGED
@@ -1,12 +1,60 @@
1
  ---
2
- title: Comfyui Deploy Gradio
3
- emoji:
4
- colorFrom: gray
5
- colorTo: green
6
  sdk: gradio
7
  sdk_version: 4.41.0
8
- app_file: app.py
9
- pinned: false
10
  ---
 
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: comfyui-deploy-gradio
3
+ app_file: main.py
 
 
4
  sdk: gradio
5
  sdk_version: 4.41.0
 
 
6
  ---
7
+ # ComfyDeploy Gradio Interface
8
 
9
+ This project provides a Gradio interface for interacting with ComfyDeploy, allowing users to dynamically generate UI components based on deployment input definitions and submit jobs to ComfyDeploy.
10
+
11
+ ![ComfyDeploy Gradio Interface](assets/cover.png)
12
+
13
+ ## Features
14
+
15
+ - Dynamic UI generation based on ComfyDeploy input definitions
16
+ - Support for various input types (text, image, number, checkbox, etc.)
17
+ - Asynchronous job submission to ComfyDeploy
18
+ - Real-time progress tracking
19
+ - Display of output images and text
20
+
21
+ ## Installation
22
+
23
+ 1. Clone this repository
24
+ 2. Install the required dependencies:
25
+
26
+ ```bash
27
+ pip install comfydeploy gradio pillow requests python-dotenv
28
+ ```
29
+
30
+ 3. Create a `.env` file in the project root with the following content:
31
+
32
+ ```
33
+ API_KEY=your_comfydeploy_api_key
34
+ DEPLOYMENT_ID=your_deployment_id
35
+ ```
36
+
37
+ ## Usage
38
+
39
+ Run the main script:
40
+
41
+ ```bash
42
+ python main.py
43
+ ```
44
+
45
+ This will launch a Gradio interface in your default web browser. The interface will dynamically generate input components based on your ComfyDeploy deployment configuration. You can then input values and submit jobs to ComfyDeploy.
46
+
47
+ ## How it works
48
+
49
+ 1. The script fetches input definitions from ComfyDeploy.
50
+ 2. It dynamically generates Gradio components based on these definitions.
51
+ 3. When the user submits inputs, it sends a job to ComfyDeploy.
52
+ 4. The script then polls for job completion and displays the results (images and text).
53
+
54
+ ## Contributing
55
+
56
+ Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
57
+
58
+ ## License
59
+
60
+ [MIT](https://choosealicense.com/licenses/mit/)
assets/cover.png ADDED
main.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from comfydeploy import ComfyDeploy
2
+ import asyncio
3
+ import os
4
+ import gradio as gr
5
+ from PIL import Image
6
+ import requests
7
+ import dotenv
8
+ from io import BytesIO
9
+ # from gradio_imageslider import ImageSlider
10
+
11
+ dotenv.load_dotenv()
12
+
13
+ client = ComfyDeploy(bearer_auth=os.environ['API_KEY'])
14
+ deployment_id = os.environ['DEPLOYMENT_ID']
15
+
16
+ def get_gradio_component(class_type):
17
+ component_map = {
18
+ 'ComfyUIDeployExternalText': gr.Textbox,
19
+ 'ComfyUIDeployExternalImage': gr.Image,
20
+ 'ComfyUIDeployExternalImageAlpha': gr.Image,
21
+ 'ComfyUIDeployExternalNumber': gr.Number,
22
+ 'ComfyUIDeployExternalNumberInt': gr.Number,
23
+ 'ComfyUIDeployExternalLora': gr.Textbox,
24
+ 'ComfyUIDeployExternalCheckpoint': gr.Textbox,
25
+ 'ComfyDeployWebscoketImageInput': gr.Image,
26
+ 'ComfyUIDeployExternalImageBatch': gr.File,
27
+ 'ComfyUIDeployExternalVideo': gr.Video,
28
+ 'ComfyUIDeployExternalBoolean': gr.Checkbox,
29
+ 'ComfyUIDeployExternalNumberSlider': gr.Slider,
30
+ }
31
+ return component_map.get(class_type, gr.Textbox) # Default to Textbox if not found
32
+
33
+ # Function to update inputs
34
+ def get_inputs():
35
+ res = client.deployment.get_input_definition(id=deployment_id)
36
+ input_definitions = res.response_bodies
37
+ gradio_inputs = []
38
+ for input_def in input_definitions:
39
+ component_class = get_gradio_component(input_def.class_type)
40
+
41
+ kwargs = {
42
+ "label": input_def.input_id,
43
+ "value": input_def.default_value
44
+ }
45
+
46
+ print(kwargs)
47
+
48
+ if input_def.class_type == 'ComfyUIDeployExternalNumberSlider':
49
+ kwargs.update({
50
+ "minimum": input_def.min_value,
51
+ "maximum": input_def.max_value
52
+ })
53
+ elif input_def.class_type in ['ComfyUIDeployExternalImage', 'ComfyUIDeployExternalImageAlpha', 'ComfyDeployWebscoketImageInput']:
54
+ kwargs["type"] = "filepath"
55
+ elif input_def.class_type == 'ComfyUIDeployExternalImageBatch':
56
+ kwargs["file_count"] = "multiple"
57
+ elif input_def.class_type == 'ComfyUIDeployExternalNumberInt':
58
+ kwargs["precision"] = 0
59
+
60
+ # print(kwargs)
61
+
62
+ gradio_inputs.append(component_class(**kwargs))
63
+
64
+ return gradio_inputs, input_definitions
65
+
66
+ with gr.Blocks() as demo:
67
+ with gr.Row():
68
+ with gr.Column(scale=1):
69
+ @gr.render()
70
+ def update_inputs():
71
+ inputs, input_definitions = get_inputs()
72
+ submit_button = gr.Button("Submit")
73
+
74
+ async def main(*args):
75
+ inputs = {input_def.input_id: arg for input_def, arg in zip(input_definitions, args)}
76
+
77
+ for key, value in inputs.items():
78
+ if isinstance(value, list) and all(isinstance(url, str) for url in value):
79
+ inputs[key] = [requests.get(url).content for url in value]
80
+ elif isinstance(value, str) and value.startswith('http'):
81
+ inputs[key] = requests.get(value).content
82
+
83
+ res = await client.run.create_async(
84
+ request={
85
+ "deployment_id": deployment_id,
86
+ "inputs": inputs
87
+ })
88
+
89
+ images = []
90
+ text = ""
91
+ outputs = [
92
+ images,
93
+ text
94
+ ]
95
+ while True:
96
+ if res.object is not None:
97
+ res2 = await client.run.get_async(run_id=res.object.run_id)
98
+ print("checking ", res2.object.progress, res2.object.live_status)
99
+
100
+ if res2.object is not None and res2.object.status == "success":
101
+ # print(res2)
102
+ for output in res2.object.outputs:
103
+ print(output.data.images)
104
+ if output.data.images:
105
+ urls = [image.url for image in output.data.images]
106
+ for url in urls:
107
+ response = requests.get(url)
108
+ img = Image.open(BytesIO(response.content))
109
+ outputs[0].append(img)
110
+ elif output.data.text:
111
+ print(output.data.text)
112
+ outputs[1] += "\n\n" + "\n".join(output.data.text)
113
+ break
114
+ await asyncio.sleep(2)
115
+
116
+ return outputs
117
+
118
+ submit_button.click(fn=main, inputs=inputs, outputs=output_components)
119
+
120
+ with gr.Column(scale=1):
121
+ output_components = [
122
+ gr.Gallery(),
123
+ gr.Textbox(label="Text Output")
124
+ ]
125
+
126
+ if __name__ == "__main__":
127
+ demo.launch(share=True)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ comfydeploy==0.3.4
2
+ gradio==4.41.0
3
+ gradio_imageslider