ageraustine commited on
Commit
1935411
1 Parent(s): 29bd487

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -38
app.py CHANGED
@@ -57,32 +57,51 @@ def generate_speech(text, filename):
57
  return None
58
 
59
  def download_image(url, filename):
60
- response = requests.get(url)
61
- with open(filename, 'wb') as f:
62
- f.write(response.content)
 
 
 
 
 
63
 
64
  def create_video(paragraphs, image_files, audio_files, output_file):
65
  clips = []
66
  for paragraph, image_file, audio_file in zip(paragraphs, image_files, audio_files):
67
- audio_clip = AudioFileClip(audio_file)
68
- duration = audio_clip.duration
69
-
70
- image_clip = ImageClip(image_file).set_duration(duration)
71
-
72
- text_clip = TextClip(paragraph, fontsize=30, color='white', bg_color='black',
73
- size=(1024, 200), method='caption').set_position(('center', 'bottom')).set_duration(duration)
74
-
75
- composite_clip = CompositeVideoClip([image_clip, text_clip])
76
- composite_clip = composite_clip.set_audio(audio_clip)
77
- clips.append(composite_clip)
 
 
 
 
 
 
 
78
 
79
- final_clip = concatenate_videoclips(clips)
80
- final_clip.write_videofile(output_file, fps=24)
 
 
 
81
 
82
  def generate_story_with_video(topic):
83
- # Generate the story
84
- story = story_chain.run(topic)
85
-
 
 
 
 
86
  # Split the story into paragraphs
87
  paragraphs = re.split(r'\n\n', story.strip())
88
 
@@ -98,43 +117,68 @@ def generate_story_with_video(topic):
98
  for i, paragraph in enumerate(paragraphs):
99
  # Generate and download image
100
  image_url = generate_image(paragraph)
101
- image_file = os.path.join(temp_dir, f"image_{i}.png")
102
- download_image(image_url, image_file)
103
- image_files.append(image_file)
 
 
104
 
105
  # Generate audio
106
  audio_file = os.path.join(temp_dir, f"audio_{i}.mp3")
107
- generate_speech(paragraph, audio_file)
108
- audio_files.append(audio_file)
109
 
110
  # Create video
111
  output_file = os.path.join(temp_dir, "story_video.mp4")
112
- create_video(paragraphs, image_files, audio_files, output_file)
113
 
114
- # Read the video file
115
- with open(output_file, "rb") as f:
116
- video_data = f.read()
 
 
117
 
118
- return story, video_data
119
 
120
  # Create the Gradio interface
121
  def gradio_interface(topic):
122
- try:
123
- story, video_data = generate_story_with_video(topic)
124
- return story, video_data
125
- except Exception as e:
126
- print(f"Error in gradio_interface: {e}")
127
- return "An error occurred while generating the story and video.", None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
 
129
  iface = gr.Interface(
130
  fn=gradio_interface,
131
  inputs=gr.Textbox(lines=2, placeholder="Enter a topic for the story..."),
132
  outputs=[
133
  gr.Textbox(label="Generated Story"),
134
- gr.Video(label="Story Video")
 
 
 
 
 
 
135
  ],
136
- title="Story Generator with Video",
137
- description="Enter a topic, and the AI will generate a short story with a video combining images, captions, and narration."
138
  )
139
 
140
  # Launch the Gradio app
 
57
  return None
58
 
59
  def download_image(url, filename):
60
+ try:
61
+ response = requests.get(url)
62
+ with open(filename, 'wb') as f:
63
+ f.write(response.content)
64
+ return filename
65
+ except Exception as e:
66
+ print(f"Error downloading image: {e}")
67
+ return None
68
 
69
  def create_video(paragraphs, image_files, audio_files, output_file):
70
  clips = []
71
  for paragraph, image_file, audio_file in zip(paragraphs, image_files, audio_files):
72
+ try:
73
+ audio_clip = AudioFileClip(audio_file) if audio_file else None
74
+ duration = audio_clip.duration if audio_clip else 5 # Default duration if no audio
75
+
76
+ if image_file:
77
+ image_clip = ImageClip(image_file).set_duration(duration)
78
+ else:
79
+ image_clip = ColorClip(size=(1024, 1024), color=(0,0,0)).set_duration(duration)
80
+
81
+ text_clip = TextClip(paragraph, fontsize=30, color='white', bg_color='black',
82
+ size=(1024, 200), method='caption').set_position(('center', 'bottom')).set_duration(duration)
83
+
84
+ composite_clip = CompositeVideoClip([image_clip, text_clip])
85
+ if audio_clip:
86
+ composite_clip = composite_clip.set_audio(audio_clip)
87
+ clips.append(composite_clip)
88
+ except Exception as e:
89
+ print(f"Error creating clip: {e}")
90
 
91
+ if clips:
92
+ final_clip = concatenate_videoclips(clips)
93
+ final_clip.write_videofile(output_file, fps=24)
94
+ return output_file
95
+ return None
96
 
97
  def generate_story_with_video(topic):
98
+ try:
99
+ # Generate the story
100
+ story = story_chain.run(topic)
101
+ except Exception as e:
102
+ print(f"Error generating story: {e}")
103
+ story = "Failed to generate story."
104
+
105
  # Split the story into paragraphs
106
  paragraphs = re.split(r'\n\n', story.strip())
107
 
 
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
 
142
  # Create the Gradio interface
143
  def gradio_interface(topic):
144
+ story, video_data, image_files, audio_files = generate_story_with_video(topic)
145
+
146
+ outputs = [story]
147
+
148
+ if video_data:
149
+ outputs.append(video_data)
150
+ else:
151
+ outputs.append(None)
152
+ outputs.append("Failed to generate video.")
153
+
154
+ for i in range(3):
155
+ if i < len(image_files) and image_files[i]:
156
+ outputs.append(image_files[i])
157
+ else:
158
+ outputs.append(None)
159
+
160
+ if i < len(audio_files) and audio_files[i]:
161
+ outputs.append(audio_files[i])
162
+ else:
163
+ outputs.append(None)
164
+
165
+ return outputs
166
 
167
  iface = gr.Interface(
168
  fn=gradio_interface,
169
  inputs=gr.Textbox(lines=2, placeholder="Enter a topic for the story..."),
170
  outputs=[
171
  gr.Textbox(label="Generated Story"),
172
+ gr.Video(label="Story Video"),
173
+ gr.Image(label="Image 1", type="filepath"),
174
+ gr.Audio(label="Audio 1", type="filepath"),
175
+ gr.Image(label="Image 2", type="filepath"),
176
+ gr.Audio(label="Audio 2", type="filepath"),
177
+ gr.Image(label="Image 3", type="filepath"),
178
+ gr.Audio(label="Audio 3", type="filepath"),
179
  ],
180
+ title="Story Generator with Video, Images, and Audio",
181
+ description="Enter a topic, and the AI will generate a short story with a video, images, and audio narration. If any part fails, you'll still see the successful parts."
182
  )
183
 
184
  # Launch the Gradio app