Sanshruth commited on
Commit
31573b4
1 Parent(s): 09557c9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -5
app.py CHANGED
@@ -1,4 +1,6 @@
1
  import zipfile
 
 
2
  def unzip_content():
3
  try:
4
  # First try using Python's zipfile
@@ -52,14 +54,18 @@ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
52
 
53
 
54
 
55
- def create_video(images_list, video_name='morphing_video.mp4'):
56
  """Create video from a list of image tensors"""
57
  if not images_list:
58
  print("No images provided.")
59
  return None
60
 
61
- with tempfile.NamedTemporaryFile(suffix='.mp4', delete=False) as temp_video:
62
- video_writer = imageio.get_writer(temp_video.name, fps=10)
 
 
 
 
63
 
64
  for img_tensor in images_list:
65
  # Convert tensor to numpy array
@@ -68,7 +74,13 @@ def create_video(images_list, video_name='morphing_video.mp4'):
68
  video_writer.append_data(img)
69
 
70
  video_writer.close()
71
- return temp_video.name
 
 
 
 
 
 
72
 
73
  def save_from_tensors(tensor):
74
  """Process tensor and return the processed version"""
@@ -226,11 +238,33 @@ def generate_art(include_text, exclude_text, extras_text, num_iterations):
226
  except Exception as e:
227
  raise e
228
 
 
229
  def gradio_interface(include_text, exclude_text, extras_text, num_iterations):
 
230
  try:
231
  video_path = generate_art(include_text, exclude_text, extras_text, int(num_iterations))
232
- return video_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
233
  except Exception as e:
 
 
234
  return f"An error occurred: {str(e)}"
235
 
236
  # Try to unzip content at startup
 
1
  import zipfile
2
+ import time
3
+ import threading
4
  def unzip_content():
5
  try:
6
  # First try using Python's zipfile
 
54
 
55
 
56
 
57
+ def create_video(images_list):
58
  """Create video from a list of image tensors"""
59
  if not images_list:
60
  print("No images provided.")
61
  return None
62
 
63
+ # Create a unique filename in the current directory
64
+ video_path = os.path.join(os.getcwd(), f"output_{int(time.time())}.mp4")
65
+
66
+ try:
67
+ video_writer = imageio.get_writer(video_path, fps=10, codec='libx264', quality=7,
68
+ output_params=['-movflags', 'faststart'])
69
 
70
  for img_tensor in images_list:
71
  # Convert tensor to numpy array
 
74
  video_writer.append_data(img)
75
 
76
  video_writer.close()
77
+ return video_path
78
+
79
+ except Exception as e:
80
+ if os.path.exists(video_path):
81
+ os.remove(video_path)
82
+ raise e
83
+
84
 
85
  def save_from_tensors(tensor):
86
  """Process tensor and return the processed version"""
 
238
  except Exception as e:
239
  raise e
240
 
241
+
242
  def gradio_interface(include_text, exclude_text, extras_text, num_iterations):
243
+ video_path = None
244
  try:
245
  video_path = generate_art(include_text, exclude_text, extras_text, int(num_iterations))
246
+
247
+ if not os.path.exists(video_path):
248
+ return "Video generation failed"
249
+
250
+ # Create a copy of the video path before scheduling deletion
251
+ response_path = video_path
252
+
253
+ # Schedule the video file for deletion after a delay
254
+ def cleanup():
255
+ try:
256
+ if os.path.exists(video_path):
257
+ os.remove(video_path)
258
+ except:
259
+ pass
260
+
261
+ threading.Timer(10.0, cleanup).start()
262
+
263
+ return response_path
264
+
265
  except Exception as e:
266
+ if video_path and os.path.exists(video_path):
267
+ os.remove(video_path)
268
  return f"An error occurred: {str(e)}"
269
 
270
  # Try to unzip content at startup