codelion commited on
Commit
ebfbf2f
·
verified ·
1 Parent(s): 631cc50

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -26
app.py CHANGED
@@ -249,18 +249,23 @@ def generate_item(user_input, ideas, generate_video=False, max_retries=3):
249
  operation = client.operations.get(operation)
250
  if operation.error:
251
  raise ValueError(f"Video generation failed: {operation.error.message}")
252
- if operation.response and len(operation.response.generated_videos) > 0:
253
- video = operation.response.generated_videos[0]
254
- video_data = client.files.download(file=video.video)
255
- video_bytes = video_data if isinstance(video_data, bytes) else BytesIO(video_data).getvalue()
256
- video_base64 = base64.b64encode(video_bytes).decode()
257
- yield {"stage": "finalizing", "message": random.choice(PROGRESS_STAGES["finalizing"]), "progress": 90}
258
- return {
259
- 'text': text,
260
- 'image_base64': img_str,
261
- 'video_base64': video_base64,
262
- 'ideas': ideas
263
- }
 
 
 
 
 
264
  except Exception as e:
265
  print(f"Error generating video: {e}")
266
  yield {"stage": "generating_video", "message": random.choice(PROGRESS_STAGES["generating_video"]), "progress": 80}
@@ -278,18 +283,25 @@ def generate_item(user_input, ideas, generate_video=False, max_retries=3):
278
  while not operation.done:
279
  time.sleep(20)
280
  operation = client.operations.get(operation)
281
- if operation.response and len(operation.response.generated_videos) > 0:
282
- video = operation.response.generated_videos[0]
283
- video_data = client.files.download(file=video.video)
284
- video_bytes = video_data if isinstance(video_data, bytes) else BytesIO(video_data).getvalue()
285
- video_base64 = base64.b64encode(video_bytes).decode()
286
- yield {"stage": "finalizing", "message": random.choice(PROGRESS_STAGES["finalizing"]), "progress": 90}
287
- return {
288
- 'text': text,
289
- 'image_base64': img_str,
290
- 'video_base64': video_base64,
291
- 'ideas': ideas
292
- }
 
 
 
 
 
 
 
293
  except Exception as e:
294
  print(f"Error generating text-to-video: {e}")
295
  if total_attempts == max_total_attempts:
@@ -525,7 +537,7 @@ def generate_share_links(image_base64, video_base64, caption):
525
  font-size: 14px;
526
  font-weight: bold;
527
  transition: background-color 0.3s;
528
- " onmouseover="this стиле.backgroundColor='#00d9d1'" onmouseout="this.style.backgroundColor='#00f2ea'">Share on TikTok</a>
529
  <a href="https://www.instagram.com/?caption={caption}" target="_blank" style="
530
  background-color: #e1306c;
531
  color: white;
@@ -809,7 +821,7 @@ with gr.Blocks(
809
  label="Generate Video (may take longer)",
810
  value=False
811
  )
812
- magic_button = gr.Button("✨ Generate Next Item", elem_classes="gr-button")
813
 
814
  feed_html = gr.HTML()
815
  share_html = gr.HTML(label="Share this item:")
 
249
  operation = client.operations.get(operation)
250
  if operation.error:
251
  raise ValueError(f"Video generation failed: {operation.error.message}")
252
+ if operation.response and hasattr(operation.response, 'generated_videos') and operation.response.generated_videos:
253
+ if len(operation.response.generated_videos) > 0:
254
+ video = operation.response.generated_videos[0]
255
+ if video and hasattr(video, 'video'):
256
+ video_data = client.files.download(file=video.video)
257
+ video_bytes = video_data if isinstance(video_data, bytes) else BytesIO(video_data).getvalue()
258
+ video_base64 = base64.b64encode(video_bytes).decode()
259
+ yield {"stage": "finalizing", "message": random.choice(PROGRESS_STAGES["finalizing"]), "progress": 90}
260
+ return {
261
+ 'text': text,
262
+ 'image_base64': img_str,
263
+ 'video_base64': video_base64,
264
+ 'ideas': ideas
265
+ }
266
+ raise ValueError("No valid video generated")
267
+ else:
268
+ raise ValueError("Video generation operation failed: No generated_videos in response")
269
  except Exception as e:
270
  print(f"Error generating video: {e}")
271
  yield {"stage": "generating_video", "message": random.choice(PROGRESS_STAGES["generating_video"]), "progress": 80}
 
283
  while not operation.done:
284
  time.sleep(20)
285
  operation = client.operations.get(operation)
286
+ if operation.error:
287
+ raise ValueError(f"Video generation failed: {operation.error.message}")
288
+ if operation.response and hasattr(operation.response, 'generated_videos') and operation.response.generated_videos:
289
+ if len(operation.response.generated_videos) > 0:
290
+ video = operation.response.generated_videos[0]
291
+ if video and hasattr(video, 'video'):
292
+ video_data = client.files.download(file=video.video)
293
+ video_bytes = video_data if isinstance(video_data, bytes) else BytesIO(video_data).getvalue()
294
+ video_base64 = base64.b64encode(video_bytes).decode()
295
+ yield {"stage": "finalizing", "message": random.choice(PROGRESS_STAGES["finalizing"]), "progress": 90}
296
+ return {
297
+ 'text': text,
298
+ 'image_base64': img_str,
299
+ 'video_base64': video_base64,
300
+ 'ideas': ideas
301
+ }
302
+ raise ValueError("No valid video generated")
303
+ else:
304
+ raise ValueError("Video generation operation failed: No generated_videos in response")
305
  except Exception as e:
306
  print(f"Error generating text-to-video: {e}")
307
  if total_attempts == max_total_attempts:
 
537
  font-size: 14px;
538
  font-weight: bold;
539
  transition: background-color 0.3s;
540
+ " onmouseover="this.style.backgroundColor='#00d9d1'" onmouseout="this.style.backgroundColor='#00f2ea'">Share on TikTok</a>
541
  <a href="https://www.instagram.com/?caption={caption}" target="_blank" style="
542
  background-color: #e1306c;
543
  color: white;
 
821
  label="Generate Video (may take longer)",
822
  value=False
823
  )
824
+ magic_button = gr.Button("✨ Create ", elem_classes="gr-button")
825
 
826
  feed_html = gr.HTML()
827
  share_html = gr.HTML(label="Share this item:")