Muhammad Waqas commited on
Commit
c9dba17
·
1 Parent(s): b3c71e2

Added: Generate image to video

Browse files
Files changed (1) hide show
  1. app.py +46 -40
app.py CHANGED
@@ -30,6 +30,44 @@ ws_address = os.getenv("WS_ADDRESS")
30
  # Generate a unique client ID
31
  client_id = str(uuid.uuid4())
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  def get_image(filename, subfolder, image_type, token):
34
  url_values = {'filename': filename, 'subfolder': subfolder, 'type': image_type}
35
  url = f"{server_address}/view?{urllib.parse.urlencode(url_values)}"
@@ -73,6 +111,10 @@ def get_images(ws, workflow, token):
73
  def home():
74
  return render_template('home.html')
75
 
 
 
 
 
76
  # Generate image route
77
  @app.route('/generate_image', methods=['POST'])
78
  def generate_image():
@@ -108,10 +150,6 @@ def generate_image():
108
  # seednum = random.randint(1, 9999999999999)
109
  # workflow["3"]["inputs"]["seed"] = seednum
110
 
111
- #######################
112
- # For model Flux1.dev #
113
- #######################
114
-
115
  # Generate a random 15-digit seed as an integer
116
  seednum = random.randint(100000000000000, 999999999999999)
117
  workflow["31"]["inputs"]["seed"] = seednum
@@ -145,41 +183,6 @@ def get_image_file(filename):
145
  return send_file(filename, mimetype='image/png')
146
 
147
 
148
- def allowed_file(filename):
149
- """Check if the uploaded file has an allowed extension."""
150
- return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
151
-
152
- def save_base64_image(b64_string):
153
- """Decode a base64 string and save it as an image in the static folder."""
154
- try:
155
- # Handle Data URI scheme if present
156
- if ',' in b64_string:
157
- header, encoded = b64_string.split(',', 1)
158
- ext = header.split('/')[1].split(';')[0] if '/' in header else 'png'
159
- else:
160
- encoded = b64_string
161
- ext = 'png'
162
-
163
- # Decode the image data
164
- image_data = base64.b64decode(encoded)
165
-
166
- # Generate a unique path for the image in the static folder
167
- image_path = f"static/{uuid.uuid4()}.{ext}"
168
-
169
- # Ensure directory exists
170
- os.makedirs('static', exist_ok=True)
171
-
172
- # Save the image
173
- with open(image_path, 'wb') as f:
174
- f.write(image_data)
175
-
176
- print(f"Image saved at: {image_path}")
177
-
178
- # Return the URL or path of the saved image
179
- return f"https://gosign-de-comfyui-api.hf.space/{image_path}"
180
-
181
- except Exception as e:
182
- raise ValueError(f"Failed to save image: {e}")
183
 
184
  # Route to serve images
185
  @app.route('/static/<path:filename>')
@@ -259,6 +262,9 @@ def get_video_data(filename, subfolder, token):
259
  print(f"URL Error: {e.reason}")
260
  raise
261
 
 
 
 
262
 
263
  # Route: Image to Video
264
  @app.route('/image_to_video', methods=['POST'])
@@ -304,7 +310,7 @@ def image_to_video():
304
  elif base64_image:
305
  # Save base64 image
306
  try:
307
- image_url = save_base64_image(base64_image)
308
  # return jsonify({'image_url': image_url})
309
 
310
  except Exception as e:
 
30
  # Generate a unique client ID
31
  client_id = str(uuid.uuid4())
32
 
33
+
34
+ def allowed_file(filename):
35
+ """Check if the uploaded file has an allowed extension."""
36
+ return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
37
+
38
+ def save_base64_image(b64_string):
39
+ """Decode a base64 string and save it as an image in the static folder."""
40
+ try:
41
+ # Handle Data URI scheme if present
42
+ if ',' in b64_string:
43
+ header, encoded = b64_string.split(',', 1)
44
+ ext = header.split('/')[1].split(';')[0] if '/' in header else 'png'
45
+ else:
46
+ encoded = b64_string
47
+ ext = 'png'
48
+
49
+ # Decode the image data
50
+ image_data = base64.b64decode(encoded)
51
+
52
+ # Generate a unique path for the image in the static folder
53
+ image_path = f"static/{uuid.uuid4()}.{ext}"
54
+
55
+ # Ensure directory exists
56
+ os.makedirs('static', exist_ok=True)
57
+
58
+ # Save the image
59
+ with open(image_path, 'wb') as f:
60
+ f.write(image_data)
61
+
62
+ print(f"Image saved at: {image_path}")
63
+
64
+ # Return the path and URL of the saved image
65
+ image_url = f"https://gosign-de-comfyui-api.hf.space/{image_path}"
66
+ return image_path, image_url
67
+
68
+ except Exception as e:
69
+ raise ValueError(f"Failed to save image: {e}")
70
+
71
  def get_image(filename, subfolder, image_type, token):
72
  url_values = {'filename': filename, 'subfolder': subfolder, 'type': image_type}
73
  url = f"{server_address}/view?{urllib.parse.urlencode(url_values)}"
 
111
  def home():
112
  return render_template('home.html')
113
 
114
+ ####################################################
115
+ # Generate text to image using FLUX1.DEV Checkpoint#
116
+ ####################################################
117
+
118
  # Generate image route
119
  @app.route('/generate_image', methods=['POST'])
120
  def generate_image():
 
150
  # seednum = random.randint(1, 9999999999999)
151
  # workflow["3"]["inputs"]["seed"] = seednum
152
 
 
 
 
 
153
  # Generate a random 15-digit seed as an integer
154
  seednum = random.randint(100000000000000, 999999999999999)
155
  workflow["31"]["inputs"]["seed"] = seednum
 
183
  return send_file(filename, mimetype='image/png')
184
 
185
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
186
 
187
  # Route to serve images
188
  @app.route('/static/<path:filename>')
 
262
  print(f"URL Error: {e.reason}")
263
  raise
264
 
265
+ ##################################################
266
+ # Generate image to video using CogVideoX-5B-12V #
267
+ ##################################################
268
 
269
  # Route: Image to Video
270
  @app.route('/image_to_video', methods=['POST'])
 
310
  elif base64_image:
311
  # Save base64 image
312
  try:
313
+ image_path, image_url = save_base64_image(base64_image)
314
  # return jsonify({'image_url': image_url})
315
 
316
  except Exception as e: