Muhammad Waqas commited on
Commit
75efe1b
Β·
1 Parent(s): 2e40bb4

Added: Generate image to video

Browse files
app.py CHANGED
@@ -9,7 +9,7 @@ import websocket
9
  import requests
10
  import uuid
11
  from dotenv import load_dotenv
12
- from flask import Flask, request, jsonify, render_template, send_file
13
  from PIL import Image
14
  from werkzeug.utils import secure_filename
15
 
@@ -28,9 +28,6 @@ ws_address = os.getenv("WS_ADDRESS")
28
  # Generate a unique client ID
29
  client_id = str(uuid.uuid4())
30
 
31
-
32
-
33
-
34
  def get_image(filename, subfolder, image_type, token):
35
  url_values = {'filename': filename, 'subfolder': subfolder, 'type': image_type}
36
  url = f"{server_address}/view?{urllib.parse.urlencode(url_values)}"
@@ -160,7 +157,7 @@ def allowed_file(filename):
160
  return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
161
 
162
  def save_base64_image(b64_string):
163
- """Decode a base64 string and save it as an image."""
164
  try:
165
  # Handle Data URI scheme if present
166
  if ',' in b64_string:
@@ -173,22 +170,29 @@ def save_base64_image(b64_string):
173
  # Decode the image data
174
  image_data = base64.b64decode(encoded)
175
 
176
- # Generate a unique path for the image
177
- image_path = f"/tmp/{uuid.uuid4()}.{ext}"
178
 
179
  # Ensure directory exists
180
- os.makedirs('/tmp/', exist_ok=True)
181
 
182
  # Save the image
183
  with open(image_path, 'wb') as f:
184
  f.write(image_data)
185
 
186
  print(f"Image saved at: {image_path}")
187
- return (f"https://gosign-de-comfyui-api.hf.space{image_path}")
 
 
188
 
189
  except Exception as e:
190
  raise ValueError(f"Failed to save image: {e}")
191
 
 
 
 
 
 
192
  def make_request(url, data=None, headers=None):
193
  req = urllib.request.Request(url, data=data, headers=headers)
194
  try:
@@ -234,24 +238,6 @@ def get_video_data(filename, subfolder, token):
234
  print(e.read())
235
  raise
236
 
237
- # Helper: Upload Image to Hugging Face
238
- def upload_image_to_hf(image_path, token):
239
- with open(image_path, 'rb') as img:
240
- headers = {
241
- "Authorization": f"Bearer {token}",
242
- "Content-Type": "image/jpeg" # Change as per your image type
243
- }
244
- response = requests.post(
245
- "https://gosign-de-image-to-video.hf.space",
246
- headers=headers,
247
- files={'file': img}
248
- )
249
-
250
- if response.status_code != 200:
251
- raise ValueError(f"Failed to upload image: {response.text}")
252
-
253
- return response.json().get('url')
254
-
255
  # Route: Image to Video
256
  @app.route('/image_to_video', methods=['POST'])
257
  def image_to_video():
@@ -286,9 +272,6 @@ def image_to_video():
286
  try:
287
  image_path = save_base64_image(base64_image)
288
  # return jsonify({'image_path': image_path})
289
- # image_path = (upload_image_to_hf(image_path, token))
290
-
291
- print(f"Image saved at HF: {image_path}")
292
 
293
  except Exception as e:
294
  return jsonify({'error': f'Invalid base64 image data: {str(e)}'}), 400
@@ -303,7 +286,7 @@ def image_to_video():
303
 
304
  # Modify workflow with inputs
305
  workflow["30"]["inputs"]["prompt"] = text_prompt
306
- workflow["36"]["inputs"]["image"] = image_path
307
  workflow["31"]["inputs"]["prompt"] = "Low quality, watermark, strange motion"
308
  workflow["57"]["inputs"]["seed"] = random.randint(100000000000000, 999999999999999)
309
 
 
9
  import requests
10
  import uuid
11
  from dotenv import load_dotenv
12
+ from flask import Flask, request, jsonify, render_template, send_file, send_from_directory
13
  from PIL import Image
14
  from werkzeug.utils import secure_filename
15
 
 
28
  # Generate a unique client ID
29
  client_id = str(uuid.uuid4())
30
 
 
 
 
31
  def get_image(filename, subfolder, image_type, token):
32
  url_values = {'filename': filename, 'subfolder': subfolder, 'type': image_type}
33
  url = f"{server_address}/view?{urllib.parse.urlencode(url_values)}"
 
157
  return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
158
 
159
  def save_base64_image(b64_string):
160
+ """Decode a base64 string and save it as an image in the static folder."""
161
  try:
162
  # Handle Data URI scheme if present
163
  if ',' in b64_string:
 
170
  # Decode the image data
171
  image_data = base64.b64decode(encoded)
172
 
173
+ # Generate a unique path for the image in the static folder
174
+ image_path = f"static/{uuid.uuid4()}.{ext}"
175
 
176
  # Ensure directory exists
177
+ os.makedirs('static', exist_ok=True)
178
 
179
  # Save the image
180
  with open(image_path, 'wb') as f:
181
  f.write(image_data)
182
 
183
  print(f"Image saved at: {image_path}")
184
+
185
+ # Return the URL or path of the saved image
186
+ return f"https://gosign-de-comfyui-api.hf.space/{image_path}"
187
 
188
  except Exception as e:
189
  raise ValueError(f"Failed to save image: {e}")
190
 
191
+ # Route to serve images
192
+ @app.route('/static/<path:filename>')
193
+ def serve_static(filename):
194
+ return send_from_directory('static', filename)
195
+
196
  def make_request(url, data=None, headers=None):
197
  req = urllib.request.Request(url, data=data, headers=headers)
198
  try:
 
238
  print(e.read())
239
  raise
240
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
241
  # Route: Image to Video
242
  @app.route('/image_to_video', methods=['POST'])
243
  def image_to_video():
 
272
  try:
273
  image_path = save_base64_image(base64_image)
274
  # return jsonify({'image_path': image_path})
 
 
 
275
 
276
  except Exception as e:
277
  return jsonify({'error': f'Invalid base64 image data: {str(e)}'}), 400
 
286
 
287
  # Modify workflow with inputs
288
  workflow["30"]["inputs"]["prompt"] = text_prompt
289
+ # workflow["73"]["inputs"]["url"] = image_path
290
  workflow["31"]["inputs"]["prompt"] = "Low quality, watermark, strange motion"
291
  workflow["57"]["inputs"]["seed"] = random.randint(100000000000000, 999999999999999)
292
 
static/0e65b3a0-cc05-4f5c-8b20-d5854b2fd6d7.jpeg ADDED
static/127a05b0-0395-45a5-a0c0-ad8ffad070b5.jpeg ADDED
static/71a7077f-cd9c-4d9c-8da8-4975f8853bc5.jpeg ADDED
static/eefb530f-7841-4050-9b04-c51444e1a6eb.jpeg ADDED
workflows/cogvideox_image_to_video_workflow_api.json CHANGED
@@ -24,7 +24,7 @@
24
  },
25
  "30": {
26
  "inputs": {
27
- "prompt": "Darth Vader is a vampire and blood is dripping from his steel fangs. The camera is slowly rotating around him. The background is filled with smoke and a strong light.",
28
  "strength": 1,
29
  "force_offload": true,
30
  "clip": [
@@ -52,16 +52,6 @@
52
  "title": "CogVideo TextEncode"
53
  }
54
  },
55
- "36": {
56
- "inputs": {
57
- "image": "https://huggingface.co/spaces/gosign-de/comfyui-api/raw/main/images/Webimage-1-720x480.jpg",
58
- "upload": "image"
59
- },
60
- "class_type": "LoadImage",
61
- "_meta": {
62
- "title": "Load Image"
63
- }
64
- },
65
  "37": {
66
  "inputs": {
67
  "width": 720,
@@ -71,7 +61,7 @@
71
  "divisible_by": 16,
72
  "crop": "disabled",
73
  "image": [
74
- "36",
75
  0
76
  ]
77
  },
@@ -82,7 +72,7 @@
82
  },
83
  "44": {
84
  "inputs": {
85
- "frame_rate": 24,
86
  "loop_count": 0,
87
  "filename_prefix": "CogVideoX-I2V",
88
  "format": "video/h264-mp4",
@@ -128,11 +118,11 @@
128
  "height": 480,
129
  "width": 720,
130
  "num_frames": 49,
131
- "steps": 50,
132
  "cfg": 6,
133
  "seed": 65334758276105,
134
  "scheduler": "DPM++",
135
- "denoise_strength": 1.0,
136
  "pipeline": [
137
  "1",
138
  0
@@ -172,5 +162,27 @@
172
  "_meta": {
173
  "title": "CogVideo ImageEncode"
174
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
175
  }
176
  }
 
24
  },
25
  "30": {
26
  "inputs": {
27
+ "prompt": "people are swimming in the water.",
28
  "strength": 1,
29
  "force_offload": true,
30
  "clip": [
 
52
  "title": "CogVideo TextEncode"
53
  }
54
  },
 
 
 
 
 
 
 
 
 
 
55
  "37": {
56
  "inputs": {
57
  "width": 720,
 
61
  "divisible_by": 16,
62
  "crop": "disabled",
63
  "image": [
64
+ "73",
65
  0
66
  ]
67
  },
 
72
  },
73
  "44": {
74
  "inputs": {
75
+ "frame_rate": 12,
76
  "loop_count": 0,
77
  "filename_prefix": "CogVideoX-I2V",
78
  "format": "video/h264-mp4",
 
118
  "height": 480,
119
  "width": 720,
120
  "num_frames": 49,
121
+ "steps": 10,
122
  "cfg": 6,
123
  "seed": 65334758276105,
124
  "scheduler": "DPM++",
125
+ "denoise_strength": 1,
126
  "pipeline": [
127
  "1",
128
  0
 
162
  "_meta": {
163
  "title": "CogVideo ImageEncode"
164
  }
165
+ },
166
+ "73": {
167
+ "inputs": {
168
+ "url": "https://huggingface.co/spaces/gosign-de/comfyui-api/resolve/main/images/iStock_000014226797_Small.jpg",
169
+ "cache": true
170
+ },
171
+ "class_type": "LoadImageByUrl //Browser",
172
+ "_meta": {
173
+ "title": "Load Image By URL"
174
+ }
175
+ },
176
+ "75": {
177
+ "inputs": {
178
+ "images": [
179
+ "73",
180
+ 0
181
+ ]
182
+ },
183
+ "class_type": "PreviewImage",
184
+ "_meta": {
185
+ "title": "Preview Image"
186
+ }
187
  }
188
  }
workflows/cogvideox_image_to_video_workflow_api_old.json ADDED
@@ -0,0 +1,176 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "1": {
3
+ "inputs": {
4
+ "model": "THUDM/CogVideoX-5b-I2V",
5
+ "precision": "bf16",
6
+ "fp8_transformer": "disabled",
7
+ "compile": "disabled",
8
+ "enable_sequential_cpu_offload": false
9
+ },
10
+ "class_type": "DownloadAndLoadCogVideoModel",
11
+ "_meta": {
12
+ "title": "(Down)load CogVideo Model"
13
+ }
14
+ },
15
+ "20": {
16
+ "inputs": {
17
+ "clip_name": "t5\\google_t5-v1_1-xxl_encoderonly-fp8_e4m3fn.safetensors",
18
+ "type": "sd3"
19
+ },
20
+ "class_type": "CLIPLoader",
21
+ "_meta": {
22
+ "title": "Load CLIP"
23
+ }
24
+ },
25
+ "30": {
26
+ "inputs": {
27
+ "prompt": "Darth Vader is a vampire and blood is dripping from his steel fangs. The camera is slowly rotating around him. The background is filled with smoke and a strong light.",
28
+ "strength": 1,
29
+ "force_offload": true,
30
+ "clip": [
31
+ "20",
32
+ 0
33
+ ]
34
+ },
35
+ "class_type": "CogVideoTextEncode",
36
+ "_meta": {
37
+ "title": "CogVideo TextEncode"
38
+ }
39
+ },
40
+ "31": {
41
+ "inputs": {
42
+ "prompt": "The video is not of a high quality, it has a low resolution. Watermark present in each frame. Strange motion trajectory. ",
43
+ "strength": 1,
44
+ "force_offload": true,
45
+ "clip": [
46
+ "20",
47
+ 0
48
+ ]
49
+ },
50
+ "class_type": "CogVideoTextEncode",
51
+ "_meta": {
52
+ "title": "CogVideo TextEncode"
53
+ }
54
+ },
55
+ "36": {
56
+ "inputs": {
57
+ "image": "https://images.unsplash.com/photo-1506748686214-e9df14d4d9d0",
58
+ "upload": "image"
59
+ },
60
+ "class_type": "LoadImage",
61
+ "_meta": {
62
+ "title": "Load Image"
63
+ }
64
+ },
65
+ "37": {
66
+ "inputs": {
67
+ "width": 720,
68
+ "height": 480,
69
+ "upscale_method": "lanczos",
70
+ "keep_proportion": false,
71
+ "divisible_by": 16,
72
+ "crop": "disabled",
73
+ "image": [
74
+ "36",
75
+ 0
76
+ ]
77
+ },
78
+ "class_type": "ImageResizeKJ",
79
+ "_meta": {
80
+ "title": "Resize Image"
81
+ }
82
+ },
83
+ "44": {
84
+ "inputs": {
85
+ "frame_rate": 24,
86
+ "loop_count": 0,
87
+ "filename_prefix": "CogVideoX-I2V",
88
+ "format": "video/h264-mp4",
89
+ "pix_fmt": "yuv420p",
90
+ "crf": 19,
91
+ "save_metadata": true,
92
+ "pingpong": false,
93
+ "save_output": true,
94
+ "images": [
95
+ "56",
96
+ 0
97
+ ]
98
+ },
99
+ "class_type": "VHS_VideoCombine",
100
+ "_meta": {
101
+ "title": "Video Combine πŸŽ₯πŸ…₯πŸ…—πŸ…’"
102
+ }
103
+ },
104
+ "56": {
105
+ "inputs": {
106
+ "enable_vae_tiling": false,
107
+ "tile_sample_min_height": 96,
108
+ "tile_sample_min_width": 96,
109
+ "tile_overlap_factor_height": 0.083,
110
+ "tile_overlap_factor_width": 0.083,
111
+ "auto_tile_size": true,
112
+ "pipeline": [
113
+ "57",
114
+ 0
115
+ ],
116
+ "samples": [
117
+ "57",
118
+ 1
119
+ ]
120
+ },
121
+ "class_type": "CogVideoDecode",
122
+ "_meta": {
123
+ "title": "CogVideo Decode"
124
+ }
125
+ },
126
+ "57": {
127
+ "inputs": {
128
+ "height": 480,
129
+ "width": 720,
130
+ "num_frames": 49,
131
+ "steps": 50,
132
+ "cfg": 6,
133
+ "seed": 65334758276105,
134
+ "scheduler": "DPM++",
135
+ "denoise_strength": 1.0,
136
+ "pipeline": [
137
+ "1",
138
+ 0
139
+ ],
140
+ "positive": [
141
+ "30",
142
+ 0
143
+ ],
144
+ "negative": [
145
+ "31",
146
+ 0
147
+ ],
148
+ "image_cond_latents": [
149
+ "58",
150
+ 0
151
+ ]
152
+ },
153
+ "class_type": "CogVideoSampler",
154
+ "_meta": {
155
+ "title": "CogVideo Sampler"
156
+ }
157
+ },
158
+ "58": {
159
+ "inputs": {
160
+ "chunk_size": 16,
161
+ "enable_tiling": true,
162
+ "pipeline": [
163
+ "1",
164
+ 0
165
+ ],
166
+ "image": [
167
+ "37",
168
+ 0
169
+ ]
170
+ },
171
+ "class_type": "CogVideoImageEncode",
172
+ "_meta": {
173
+ "title": "CogVideo ImageEncode"
174
+ }
175
+ }
176
+ }