ageraustine commited on
Commit
456d6e3
1 Parent(s): 1935411

use tempdir

Browse files
Files changed (1) hide show
  1. app.py +26 -25
app.py CHANGED
@@ -109,33 +109,34 @@ def generate_story_with_video(topic):
109
  paragraphs = paragraphs[:3]
110
  while len(paragraphs) < 3:
111
  paragraphs.append("...")
112
-
113
- with tempfile.TemporaryDirectory() as temp_dir:
114
- image_files = []
115
- audio_files = []
116
 
117
- for i, paragraph in enumerate(paragraphs):
118
- # Generate and download image
119
- image_url = generate_image(paragraph)
120
- if image_url:
121
- image_file = os.path.join(temp_dir, f"image_{i}.png")
122
- image_files.append(download_image(image_url, image_file))
123
- else:
124
- image_files.append(None)
125
-
126
- # Generate audio
127
- audio_file = os.path.join(temp_dir, f"audio_{i}.mp3")
128
- audio_files.append(generate_speech(paragraph, audio_file))
129
-
130
- # Create video
131
- output_file = os.path.join(temp_dir, "story_video.mp4")
132
- video_file = create_video(paragraphs, image_files, audio_files, output_file)
133
 
134
- # Read the video file if it was created
135
- video_data = None
136
- if video_file and os.path.exists(video_file):
137
- with open(video_file, "rb") as f:
138
- video_data = f.read()
 
 
 
 
 
 
 
 
139
 
140
  return story, video_data, image_files, audio_files
141
 
 
109
  paragraphs = paragraphs[:3]
110
  while len(paragraphs) < 3:
111
  paragraphs.append("...")
 
 
 
 
112
 
113
+ temp_dir = tempfile.mkdtemp()
114
+
115
+ image_files = []
116
+ audio_files = []
117
+
118
+ for i, paragraph in enumerate(paragraphs):
119
+ # Generate and download image
120
+ image_url = generate_image(paragraph)
121
+ if image_url:
122
+ image_file = os.path.join(temp_dir, f"image_{i}.png")
123
+ image_files.append(download_image(image_url, image_file))
124
+ else:
125
+ image_files.append(None)
 
 
 
126
 
127
+ # Generate audio
128
+ audio_file = os.path.join(temp_dir, f"audio_{i}.mp3")
129
+ audio_files.append(generate_speech(paragraph, audio_file))
130
+
131
+ # Create video
132
+ output_file = os.path.join(temp_dir, "story_video.mp4")
133
+ video_file = create_video(paragraphs, image_files, audio_files, output_file)
134
+
135
+ # Read the video file if it was created
136
+ video_data = None
137
+ if video_file and os.path.exists(video_file):
138
+ with open(video_file, "rb") as f:
139
+ video_data = f.read()
140
 
141
  return story, video_data, image_files, audio_files
142